Browse Source

Sync network groups during cloning environment

for creating of network groups in the seed cluster
such as in the original cluster

Change-Id: I17f95756fa26ef0f7df0e969f9f1ba4331047c8d
Closes-Bug: #1616817
Anastasiya 2 years ago
parent
commit
821f8373ab

+ 8
- 0
cluster_upgrade/objects/adapters.py View File

@@ -121,6 +121,10 @@ class NailgunReleaseAdapter(object):
121 121
     def environment_version(self):
122 122
         return self.release.environment_version
123 123
 
124
+    @property
125
+    def id(self):
126
+        return self.release.id
127
+
124 128
     @property
125 129
     def roles_metadata(self):
126 130
         return self.release.roles_metadata
@@ -136,6 +140,10 @@ class NailgunNetworkManager(object):
136 140
         self.cluster = cluster
137 141
         self.net_manager = net_manager
138 142
 
143
+    def create(self, network_configuration, group_id=None):
144
+        self.net_manager.create_network_group(self.cluster,
145
+                                              network_configuration, group_id)
146
+
139 147
     def update(self, network_configuration):
140 148
         self.net_manager.update(self.cluster, network_configuration)
141 149
 

+ 66
- 0
cluster_upgrade/tests/test_upgrade.py View File

@@ -229,6 +229,72 @@ class TestUpgradeHelperCloneCluster(base_tests.BaseCloneClusterTest):
229 229
         self.assertEqual('image',
230 230
                          attrs['editable']['provision']['method']['value'])
231 231
 
232
+    def check_different_attributes(self, orig_cluster, new_cluster):
233
+        release = new_cluster.release.id
234
+        nodegroups_id_maping = self.helper.get_nodegroups_id_mapping(
235
+            orig_cluster, new_cluster
236
+        )
237
+        orig_ngs = self.serialize_nets(orig_cluster.cluster)['networks']
238
+        seed_ngs = self.serialize_nets(new_cluster.cluster)['networks']
239
+        for seed_ng in seed_ngs:
240
+            for orig_ng in orig_ngs:
241
+                if orig_ng['name'] == seed_ng['name'] \
242
+                        and orig_ng['name'] != "fuelweb_admin":
243
+
244
+                    self.assertEqual(seed_ng['group_id'],
245
+                                     nodegroups_id_maping[orig_ng['group_id']])
246
+
247
+                    if seed_ng.get('release'):
248
+                        self.assertEqual(seed_ng['release'], release)
249
+
250
+    def skip_different_attributes(self, orig_cluster, new_cluster):
251
+        orig_ngs = self.serialize_nets(orig_cluster.cluster)['networks']
252
+        seed_ngs = self.serialize_nets(new_cluster.cluster)['networks']
253
+        keys = ['release', 'id', 'group_id']
254
+        orig_ngs_names = {ng['name']: ng for ng in orig_ngs}
255
+        for seed_ng in seed_ngs:
256
+            if seed_ng['name'] == 'fuelweb_admin':
257
+                continue
258
+            orig_ng = orig_ngs_names.get(seed_ng['name'])
259
+            if not orig_ng:
260
+                continue
261
+            for key in keys:
262
+                orig_ng.pop(key, None)
263
+                seed_ng.pop(key, None)
264
+        return orig_ngs, seed_ngs
265
+
266
+    def test_sync_network_groups(self):
267
+        new_cluster = self.helper.create_cluster_clone(self.src_cluster,
268
+                                                       self.data)
269
+        self.helper.sync_network_groups(self.src_cluster, new_cluster)
270
+        self.check_different_attributes(self.src_cluster, new_cluster)
271
+        orig_ngs, seed_ngs = self.skip_different_attributes(self.src_cluster,
272
+                                                            new_cluster)
273
+        self.assertEqual(orig_ngs, seed_ngs)
274
+
275
+    def test_remove_network_groups(self):
276
+        new_cluster = self.helper.create_cluster_clone(self.src_cluster,
277
+                                                       self.data)
278
+        self.helper.remove_network_groups(new_cluster)
279
+        seed_ngs = self.serialize_nets(new_cluster.cluster)['networks']
280
+        self.assertEqual(len(seed_ngs), 1)
281
+        self.assertEqual(seed_ngs[0]['name'], 'fuelweb_admin')
282
+
283
+    def test_copy_network_groups(self):
284
+        new_cluster = self.helper.create_cluster_clone(self.src_cluster,
285
+                                                       self.data)
286
+        nodegroups_id_maping = self.helper.get_nodegroups_id_mapping(
287
+            self.src_cluster, new_cluster
288
+        )
289
+        release = new_cluster.release.id
290
+        self.helper.remove_network_groups(new_cluster)
291
+        self.helper.copy_network_groups(self.src_cluster, nodegroups_id_maping,
292
+                                        release)
293
+        self.check_different_attributes(self.src_cluster, new_cluster)
294
+        orig_ngs, seed_ngs = self.skip_different_attributes(self.src_cluster,
295
+                                                            new_cluster)
296
+        self.assertEqual(orig_ngs, seed_ngs)
297
+
232 298
     def test_change_env_settings_no_editable_provision(self):
233 299
         new_cluster = self.helper.create_cluster_clone(self.src_cluster,
234 300
                                                        self.data)

+ 60
- 0
cluster_upgrade/upgrade.py View File

@@ -20,6 +20,7 @@ import collections
20 20
 import six
21 21
 
22 22
 from nailgun import consts
23
+from nailgun.db import db
23 24
 from nailgun.extensions.network_manager.objects.serializers import \
24 25
     network_configuration
25 26
 from nailgun import objects
@@ -100,6 +101,7 @@ class UpgradeHelper(object):
100 101
         relations.UpgradeRelationObject.create_relation(orig_cluster.id,
101 102
                                                         new_cluster.id)
102 103
         cls.change_env_settings(orig_cluster, new_cluster)
104
+        cls.sync_network_groups(orig_cluster, new_cluster)
103 105
         return new_cluster
104 106
 
105 107
     @classmethod
@@ -136,6 +138,55 @@ class UpgradeHelper(object):
136 138
         attrs = new_cluster.attributes
137 139
         attrs['editable']['provision']['method']['value'] = 'image'
138 140
 
141
+    @classmethod
142
+    def sync_network_groups(cls, orig_cluster, new_cluster):
143
+        cls.remove_network_groups(new_cluster)
144
+        nodegroups_id_maping = cls.get_nodegroups_id_mapping(orig_cluster,
145
+                                                             new_cluster)
146
+        release = new_cluster.release.id
147
+        cls.copy_network_groups(orig_cluster, nodegroups_id_maping, release)
148
+
149
+    @classmethod
150
+    def remove_network_groups(cls, cluster):
151
+        seed_ng = cluster.get_network_groups()
152
+        for ng in seed_ng:
153
+            if ng.name == 'fuelweb_admin':
154
+                continue
155
+            objects.NetworkGroup.delete(ng.network_group)
156
+
157
+    @classmethod
158
+    def copy_network_groups(cls, orig_cluster, nodegroups_id_maping, release):
159
+        nets_serializer = cls.network_serializers[orig_cluster.net_provider]
160
+        orig_net = nets_serializer.serialize_for_cluster(orig_cluster.cluster)
161
+        for ng in orig_net['networks']:
162
+            if ng['name'] == 'fuelweb_admin':
163
+                continue
164
+            meta = ng['meta']
165
+            metadata = {
166
+                'notation': 'cidr',
167
+                'render_type': None,
168
+                'map_priority': 2,
169
+                'configurable': True,
170
+                'use_gateway': False,
171
+                'name': ng['name'],
172
+                'vlan_start': ng['vlan_start']
173
+            }
174
+            metadata.update(meta)
175
+            if metadata['notation'] == 'ip_ranges':
176
+                metadata['ip_range'] = ng['ip_ranges'][0]
177
+                metadata['cidr'] = ng['cidr']
178
+            data = {
179
+                'name': ng['name'],
180
+                'release': release,
181
+                'vlan_start': ng['vlan_start'],
182
+                'cidr': ng['cidr'],
183
+                'gateway': ng['gateway'],
184
+                'group_id': nodegroups_id_maping[ng['group_id']],
185
+                'meta': metadata
186
+            }
187
+            objects.NetworkGroup.create(data)
188
+        db().commit()
189
+
139 190
     @classmethod
140 191
     def copy_node_groups(cls, orig_cluster, new_cluster):
141 192
         for ng in orig_cluster.node_groups:
@@ -259,3 +310,12 @@ class UpgradeHelper(object):
259 310
         mapping[orig_cluster.get_admin_network_group().id] = \
260 311
             seed_cluster.get_admin_network_group().id
261 312
         return mapping
313
+
314
+    @classmethod
315
+    def get_nodegroups_id_mapping(cls, orig_cluster, seed_cluster):
316
+        orig_ng = orig_cluster.node_groups
317
+        seed_ng = seed_cluster.node_groups
318
+
319
+        seed_ng_dict = dict((ng.name, ng.id) for ng in seed_ng)
320
+        mapping = dict((ng.id, seed_ng_dict[ng.name]) for ng in orig_ng)
321
+        return mapping

Loading…
Cancel
Save