Commit 99c65048 by Czémán Arnold

ceph: add user parameter to save() and restore() function and some rework

parent 2d1af23c
...@@ -23,18 +23,12 @@ class CephConfig: ...@@ -23,18 +23,12 @@ class CephConfig:
def __init__(self, user=None, config_path=None, keyring_path=None): def __init__(self, user=None, config_path=None, keyring_path=None):
self.user = user self.user = user or "admin"
self.config_path = config_path self.config_path = (
self.keyring_path = keyring_path config_path or os.getenv("CEPH_CONFIG", "/etc/ceph/ceph.conf"))
default_keyring = "/etc/ceph/ceph.client.%s.keyring" % self.user
if user is None: self.keyring_path = (
self.user = "admin" keyring_path or os.getenv("CEPH_KEYRING", default_keyring))
if config_path is None:
self.config_path = os.getenv("CEPH_CONFIG",
"/etc/ceph/ceph.conf")
if keyring_path is None:
default_keyring = "/etc/ceph/ceph.client.%s.keyring" % self.user
self.keyring_path = os.getenv("CEPH_KEYRING", default_keyring)
def cmd_args(self): def cmd_args(self):
return ["--keyring", self.keyring_path, return ["--keyring", self.keyring_path,
...@@ -44,14 +38,10 @@ class CephConfig: ...@@ -44,14 +38,10 @@ class CephConfig:
class CephConnection: class CephConnection:
def __init__(self, pool_name, conf=None): def __init__(self, pool_name, conf=None, **kwargs):
self.pool_name = pool_name self.pool_name = pool_name
self.conf = conf self.conf = conf or CephConfig(**kwargs)
if conf is None:
self.conf = CephConfig()
self.cluster = None self.cluster = None
self.ioctx = None self.ioctx = None
...@@ -90,10 +80,10 @@ def map_rbd(conf, ceph_path, local_path): ...@@ -90,10 +80,10 @@ def map_rbd(conf, ceph_path, local_path):
sudo("/bin/rbd", "map", ceph_path, *conf.cmd_args()) sudo("/bin/rbd", "map", ceph_path, *conf.cmd_args())
def get_secret_key(conf, user): def get_secret_key(conf):
return subprocess.check_output((["/bin/ceph", return subprocess.check_output(
"auth", "print-key", "client.%s" % user] (["/bin/ceph", "auth", "print-key", "client.%s" % conf.user] +
+ conf.cmd_args())) conf.cmd_args()))
def parse_endpoint(mon): def parse_endpoint(mon):
...@@ -102,9 +92,8 @@ def parse_endpoint(mon): ...@@ -102,9 +92,8 @@ def parse_endpoint(mon):
def _get_endpoints(conf): def _get_endpoints(conf):
output = subprocess.check_output((["/bin/ceph", output = subprocess.check_output(
"mon", "dump", "--format=json"] (["/bin/ceph", "mon", "dump", "--format=json"] + conf.cmd_args()))
+ conf.cmd_args()))
mon_data = json.loads(output) mon_data = json.loads(output)
mons = mon_data["mons"] mons = mon_data["mons"]
return map(parse_endpoint, mons) return map(parse_endpoint, mons)
...@@ -115,14 +104,14 @@ def get_endpoints(user): ...@@ -115,14 +104,14 @@ def get_endpoints(user):
return _get_endpoints(conf) return _get_endpoints(conf)
def save(domain, poolname, diskname): def save(domain, poolname, diskname, user):
diskname = str(diskname) diskname = str(diskname)
poolname = str(poolname) poolname = str(poolname)
ceph_path = os.path.join(poolname, diskname) ceph_path = os.path.join(poolname, diskname)
local_path = os.path.join("/dev/rbd", ceph_path) local_path = os.path.join("/dev/rbd", ceph_path)
disk_size = DUMP_SIZE_LIMIT disk_size = DUMP_SIZE_LIMIT
with CephConnection(poolname) as conn: with CephConnection(poolname, user=user) as conn:
rbd_inst = rbd.RBD() rbd_inst = rbd.RBD()
try: try:
rbd_inst.create(conn.ioctx, diskname, disk_size) rbd_inst.create(conn.ioctx, diskname, disk_size)
...@@ -139,16 +128,16 @@ def save(domain, poolname, diskname): ...@@ -139,16 +128,16 @@ def save(domain, poolname, diskname):
unmap_rbd(conn.conf, local_path) unmap_rbd(conn.conf, local_path)
def restore(connection, poolname, diskname): def restore(libvirt_conn, poolname, diskname, user):
diskname = str(diskname) diskname = str(diskname)
poolname = str(poolname) poolname = str(poolname)
ceph_path = os.path.join(poolname, diskname) ceph_path = os.path.join(poolname, diskname)
local_path = os.path.join("/dev/rbd", ceph_path) local_path = os.path.join("/dev/rbd", ceph_path)
config = CephConfig(user=user)
map_rbd(connection.conf, ceph_path, local_path) map_rbd(config, ceph_path, local_path)
connection.restore(local_path) libvirt_conn.restore(local_path)
unmap_rbd(connection.conf, local_path) unmap_rbd(config, local_path)
with CephConnection(poolname) as conn: with CephConnection(poolname, conf=config) as conn:
rbd_inst = rbd.RBD() rbd_inst = rbd.RBD()
rbd_inst.remove(conn.ioctx, diskname) rbd_inst.remove(conn.ioctx, diskname)
...@@ -185,8 +174,8 @@ def find_secret(user): ...@@ -185,8 +174,8 @@ def find_secret(user):
@req_connection @req_connection
@wrap_libvirtError @wrap_libvirtError
def create_secret(user): def create_secret(user):
conf = CephConfig() conf = CephConfig(user=user)
secretkey = get_secret_key(conf, user) secretkey = get_secret_key(conf)
xml = generate_secret_xml(user) xml = generate_secret_xml(user)
conn = Connection.get() conn = Connection.get()
......
...@@ -227,11 +227,11 @@ def suspend(name): ...@@ -227,11 +227,11 @@ def suspend(name):
@celery.task @celery.task
@req_connection @req_connection
@wrap_libvirtError @wrap_libvirtError
def save(name, data_store_type, dir, filename): def save(name, data_store_type, dir, filename, ceph_user=None):
""" Stop virtual machine and save its memory to path. """ """ Stop virtual machine and save its memory to path. """
domain = lookupByName(name) domain = lookupByName(name)
if data_store_type == "ceph_block": if data_store_type == "ceph_block":
ceph.save(domain, dir, filename) ceph.save(domain, dir, filename, ceph_user)
else: else:
path = os.path.join(dir, filename) path = os.path.join(dir, filename)
domain.save(path) domain.save(path)
...@@ -240,7 +240,7 @@ def save(name, data_store_type, dir, filename): ...@@ -240,7 +240,7 @@ def save(name, data_store_type, dir, filename):
@celery.task @celery.task
@req_connection @req_connection
@wrap_libvirtError @wrap_libvirtError
def restore(name, data_store_type, dir, filename): def restore(name, data_store_type, dir, filename, ceph_user=None):
""" Restore a saved virtual machine. """ Restore a saved virtual machine.
Restores the virtual machine from the memory image Restores the virtual machine from the memory image
...@@ -249,7 +249,7 @@ def restore(name, data_store_type, dir, filename): ...@@ -249,7 +249,7 @@ def restore(name, data_store_type, dir, filename):
""" """
if data_store_type == "ceph_block": if data_store_type == "ceph_block":
ceph.restore(Connection.get(), dir, filename) ceph.restore(Connection.get(), dir, filename, ceph_user)
else: else:
path = os.path.join(dir, filename) path = os.path.join(dir, filename)
Connection.get().restore(path) Connection.get().restore(path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment