[lxc-devel] [pylxd/master] fix stateless migration

johnybx on Github lxc-bot at linuxcontainers.org
Thu Mar 19 16:15:32 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 357 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200319/da640621/attachment.bin>
-------------- next part --------------
From 5916ab42019876ccf2fdf28da24a9bb70a354034 Mon Sep 17 00:00:00 2001
From: "jan.brechtl" <johnybx at users.noreply.github.com>
Date: Thu, 19 Mar 2020 17:09:44 +0100
Subject: [PATCH] fix stateless migration

Signed-off-by: jan.brechtl <johnybx at users.noreply.github.com>
---
 pylxd/models/container.py            | 20 +++++++++++---------
 pylxd/tests/mock_lxd.py              |  4 +++-
 pylxd/tests/models/test_container.py |  3 +--
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/pylxd/models/container.py b/pylxd/models/container.py
index 47a72ded..d0fa7d1f 100644
--- a/pylxd/models/container.py
+++ b/pylxd/models/container.py
@@ -516,20 +516,22 @@ def migrate(self, new_client, live=False, wait=False):
         if self.status_code == 103:
             try:
                 res = new_client.containers.create(
-                    self.generate_migration_data(live), wait=wait)
+                    self.generate_migration_data(live, new_client), wait=wait)
             except LXDAPIException as e:
                 if e.response.status_code == 103:
-                    self.delete()
+                    self.stop(wait=wait)
+                    self.delete(wait=wait)
                     return new_client.containers.get(self.name)
                 else:
                     raise e
         else:
             res = new_client.containers.create(
-                self.generate_migration_data(live), wait=wait)
-        self.delete()
+                self.generate_migration_data(live, new_client), wait=wait)
+        self.stop(wait=wait)
+        self.delete(wait=wait)
         return res
 
-    def generate_migration_data(self, live=False):
+    def generate_migration_data(self, live=False, new_client=None):
         """Generate the migration data.
 
         This method can be used to handle migrations where the client
@@ -538,20 +540,20 @@ def generate_migration_data(self, live=False):
 
         :param live: Whether to include "live": "true" in the migration
         :type live: bool
+        :param live: Destination client - used to obtain correct certificate
+        :type live: :class:`pylxd.client.Client`
         :raises: LXDAPIException if the request to migrate fails
         :returns: dictionary of migration data suitable to send to an new
             client to complete a migration.
         :rtype: Dict[str, ANY]
         """
         self.sync()  # Make sure the object isn't stale
-        _json = {'migration': True}
-        if live:
-            _json['live'] = True
+        _json = {'migration': True, 'live': live}
         response = self.api.post(json=_json)
         operation = self.client.operations.get(response.json()['operation'])
         operation_url = self.client.api.operations[operation.id]._api_endpoint
         secrets = response.json()['metadata']['metadata']
-        cert = self.client.host_info['environment']['certificate']
+        cert = new_client.host_info['environment']['certificate']
 
         return {
             'name': self.name,
diff --git a/pylxd/tests/mock_lxd.py b/pylxd/tests/mock_lxd.py
index 5d44550e..be190dfc 100644
--- a/pylxd/tests/mock_lxd.py
+++ b/pylxd/tests/mock_lxd.py
@@ -145,7 +145,9 @@ def snapshot_DELETE(request, context):
         'text': json.dumps({
             'type': 'sync',
             'metadata': {'auth': 'trusted',
-                         'environment': {},
+                         'environment': {
+                            'certificate': 'another-pem-cert',
+                         },
                          'api_extensions': []
                          }}),
         'method': 'GET',
diff --git a/pylxd/tests/models/test_container.py b/pylxd/tests/models/test_container.py
index 34f6829f..be1cf501 100644
--- a/pylxd/tests/models/test_container.py
+++ b/pylxd/tests/models/test_container.py
@@ -340,7 +340,7 @@ def generate_exception(*args, **kwargs):
 
         self.assertEqual('an-container', an_migrated_container.name)
         self.assertEqual(client2, an_migrated_container.client)
-        generate_migration_data.assert_called_once_with(True)
+        generate_migration_data.assert_called_once_with(True, client2)
 
     def test_migrate_started(self):
         """A container is migrated."""
@@ -362,7 +362,6 @@ def test_migrate_stopped(self):
         client2 = Client(endpoint='http://pylxd2.test')
         an_container = models.Container.get(self.client, name='an-container')
         an_container.status_code = 102
-
         an_migrated_container = an_container.migrate(client2)
 
         self.assertEqual('an-container', an_migrated_container.name)


More information about the lxc-devel mailing list