Sfoglia il codice sorgente

Merge pull request #124 from Dany9966/normal-migration-replicator

Adapt replicator client to source resource deployment changes
Nashwan Azhari 6 anni fa
parent
commit
5fa3c6a63c
3 ha cambiato i file con 61 aggiunte e 12 eliminazioni
  1. 7 0
      coriolis/exception.py
  2. 24 12
      coriolis/providers/replicator.py
  3. 30 0
      coriolis/utils.py

+ 7 - 0
coriolis/exception.py

@@ -351,3 +351,10 @@ if six.PY2:
         pass
 else:
     ConnectionRefusedError = six.moves.builtins.ConnectionRefusedError
+
+
+class UnrecognizedWorkerInitSystem(CoriolisException):
+    message = _(
+        "Could not determine init system for temporary worker VM. The image "
+        "used for the worker VM must use systemd as an init system for "
+        "Coriolis to be able to use it for data Replication.")

+ 24 - 12
coriolis/providers/replicator.py

@@ -440,6 +440,25 @@ class Replicator(object):
             else:
                 time.sleep(5)
 
+    def start(self):
+        utils.start_service(self._ssh, REPLICATOR_SVC_NAME)
+
+    def stop(self):
+        utils.stop_service(self._ssh, REPLICATOR_SVC_NAME)
+
+    def restart(self):
+        utils.restart_service(self._ssh, REPLICATOR_SVC_NAME)
+
+    def update_state(self, state, restart=False):
+        state_file = tempfile.mkstemp()[1]
+        with open(state_file, 'w') as fp:
+            json.dump(state, fp)
+
+        self._copy_file(self._ssh, state_file, REPLICATOR_STATE)
+        if restart:
+            self.restart()
+            self._cli._test_connection()
+
     @utils.retry_on_error()
     def _get_ssh_client(self, args):
         """
@@ -510,20 +529,13 @@ class Replicator(object):
 
     @utils.retry_on_error()
     def _copy_file(self, ssh, localPath, remotePath):
-        tmp = os.path.join("/tmp", str(uuid.uuid4()))
+        tmp = tempfile.mktemp(dir="/tmp")
 
         sftp = paramiko.SFTPClient.from_transport(ssh.get_transport())
-        try:
-            # Check if the remote file already exists
-            sftp.stat(remotePath)
-        except IOError as ex:
-            if ex.errno != errno.ENOENT:
-                raise
-            sftp.put(localPath, tmp)
-            utils.exec_ssh_cmd(
-                ssh, "sudo mv %s %s" % (tmp, remotePath), get_pty=True)
-        finally:
-            sftp.close()
+        sftp.put(localPath, tmp)
+        utils.exec_ssh_cmd(
+            ssh, "sudo mv %s %s" % (tmp, remotePath), get_pty=True)
+        sftp.close()
 
     @utils.retry_on_error()
     def _copy_replicator_cmd(self, ssh):

+ 30 - 0
coriolis/utils.py

@@ -724,6 +724,36 @@ def create_service(ssh, cmdline, svcname, run_as=None, start=True):
             "could not determine init system")
 
 
+@retry_on_error()
+def restart_service(ssh, svcname):
+    if test_ssh_path(ssh, "/lib/systemd/system"):
+        exec_ssh_cmd(ssh, "sudo systemctl restart %s" % svcname, get_pty=True)
+    elif test_ssh_path(ssh, "/etc/init"):
+        exec_ssh_cmd(ssh, "restart %s" % svcname)
+    else:
+        raise exception.UnrecognizedWorkerInitSystem()
+
+
+@retry_on_error()
+def start_service(ssh, svcname):
+    if test_ssh_path(ssh, "/lib/systemd/system"):
+        exec_ssh_cmd(ssh, "sudo systemctl start %s" % svcname, get_pty=True)
+    elif test_ssh_path(ssh, "/etc/init"):
+        exec_ssh_cmd(ssh, "start %s" % svcname)
+    else:
+        raise exception.UnrecognizedWorkerInitSystem()
+
+
+@retry_on_error()
+def stop_service(ssh, svcname):
+    if test_ssh_path(ssh, "/lib/systemd/system"):
+        exec_ssh_cmd(ssh, "sudo systemctl stop %s" % svcname, get_pty=True)
+    elif test_ssh_path(ssh, "/etc/init"):
+        exec_ssh_cmd(ssh, "stop %s" % svcname)
+    else:
+        raise exception.UnrecognizedWorkerInitSystem()
+
+
 class Grub2ConfigEditor(object):
     """This class edits GRUB2 configs, normally found in
     /etc/default/grub. This class tries to preserve commented