-
Notifications
You must be signed in to change notification settings - Fork 16
/
migrate-cloud
241 lines (212 loc) · 11 KB
/
migrate-cloud
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
import sys
from mist.api.clouds.models import Cloud
from mist.api.images.models import CloudImage
from mist.api.machines.models import Machine, KeyMachineAssociation
from mist.api.networks.models import Network
from mist.api.tag.models import Tag
def get_resource_by_id(cloud, resource, resource_type):
if resource_type.__name__ == 'Machine':
new_resource = Machine.objects.get(cloud=cloud,
machine_id=resource.machine_id,
missing_since=None)
elif resource_type.__name__ == 'Network':
new_resource = Network.objects.get(cloud=cloud,
network_id=resource.network_id,
missing_since=None)
return new_resource
def migrate_images(old_cloud, new_cloud):
failed = migrated = 0
old_images = CloudImage.objects.filter(cloud=old_cloud,
missing_since=None)
print("*** Starting migrating {} Images***".format(old_images.count()))
for image in old_images:
try:
new_image = CloudImage.objects.get(cloud=new_cloud,
external_id=image.external_id,
missing_since=None)
new_image.starred = image.starred
try:
new_image.save()
print("Successfully migrated image {}".format(image.name))
migrated += 1
except Exception:
print("*** Could not migrate image {} with mist id \
{} ***".format(image.name, image.id))
except CloudImage.DoesNotExist:
print("*** WARNING: Image {} with mist id {} was not found on \
new cloud. Could not migrate ***".format(image.name,
image.id))
failed += 1
print("==============================================================")
print("Successfully migrated {} images".format(migrated))
print("==============================================================")
if failed:
print("Failed to migrate {} images".format(failed))
print("==============================================================")
def migrate_ownership(old_cloud, new_cloud):
for resource_type in (Machine, Network):
failed = migrated = 0
old_resources = resource_type.objects.filter(cloud=old_cloud,
missing_since=None)
print("*** Starting migrating ownership of {} \
{}s***".format(old_resources.count(),
resource_type.__name__))
for resource in old_resources:
try:
new_resource = get_resource_by_id(new_cloud, resource,
resource_type)
new_resource.owned_by = resource.owned_by
# also migrate `created_by` field
new_resource.created_by = resource.created_by
try:
new_resource.save()
print("Successfully migrated ownership of {} \
{}".format(resource_type.__name__,
resource.name))
migrated += 1
except Exception:
print("*** Could not migrate ownership of {} {} with \
mist id {} ***".format(resource_type.__name__,
resource.name,
resource.id))
except resource_type.DoesNotExist:
print("*** WARNING: {} {} with mist id {} was not found on new\
cloud. Could not migrate ownership \
***".format(resource_type.__name__, resource.name,
resource.id))
failed += 1
print("==========================================================")
print("Successfully migrated {} {}s".format(migrated,
resource_type.__name__))
print("==========================================================")
if failed:
print("Failed to migrate {} {}s".format(failed,
resource_type.__name__))
print("==========================================================")
def migrate_tags(old_cloud, new_cloud):
for resource_type in (Machine, Network):
failed = migrated = 0
old_resources = resource_type.objects.filter(cloud=old_cloud,
missing_since=None)
print("*** Starting migrating tags of {} {}s***".format(old_resources.
count(),
resource_type.
__name__))
for resource in old_resources:
tags = Tag.objects(resource_id=resource.id,
resource_type=resource_type.__name__.lower())
if tags:
for tag in tags:
new_tag = Tag()
new_tag.key = tag.key
new_tag.value = tag.value
new_tag.owner = tag.owner
new_tag.resource_type = tag.resource_type
try:
new_resource = get_resource_by_id(new_cloud, resource,
resource_type)
new_tag.resource_id = new_resource.id
try:
new_tag.save()
print("Successfully migrated tags of {} \
{}".format(resource_type.__name__,
resource.name))
migrated += 1
except Exception:
print("*** Could not migrate tags of {} {} with \
mist id {} ***".format(resource_type.
__name__,
resource.name,
resource.id))
except resource_type.DoesNotExist:
print("*** WARNING: {} {} with mist id {} was not found\
on new cloud. Could not migrate tags\
***".format(resource_type.__name__,
resource.name,
resource.id))
failed += 1
else:
print("Tags were not found for {} \
{}".format(resource_type.__name__, resource.name))
migrated += 1
print("==========================================================")
print("Successfully migrated {} {}s".format(migrated,
resource_type.__name__))
print("==========================================================")
if failed:
print("Failed to migrate {} {}s".format(failed,
resource_type.__name__))
print("=========================================================")
def migrate_key_associations(old_cloud, new_cloud):
failed = migrated = 0
old_machines = Machine.objects.filter(cloud=old_cloud,
missing_since=None)
print("*** Starting migrating key associations of {} Machines \
***".format(old_machines.count()))
for ma in old_machines:
key_associations = KeyMachineAssociation.objects(machine=ma)
if key_associations:
for key_assoc in key_associations:
new_key_assoc = KeyMachineAssociation()
new_key_assoc.key = key_assoc.key
new_key_assoc.last_used = key_assoc.last_used
new_key_assoc.ssh_user = key_assoc.ssh_user
new_key_assoc.sudo = key_assoc.sudo
new_key_assoc.port = key_assoc.port
try:
new_resource = get_resource_by_id(new_cloud, ma,
Machine)
new_key_assoc.machine = new_resource
try:
new_key_assoc.save()
print("Successfully migrated key associations of \
Machine {}".format(ma.name))
migrated += 1
except Exception:
print("*** Could not migrate key associations of \
Machine {} with mist id {} ***".format(ma.name,
ma.id))
except Machine.DoesNotExist:
print("*** WARNING: Machine{} with mist id {} was \
not found on new cloud. Could not migrate \
tags ***".format(ma.name,
ma.id))
failed += 1
else:
print("Key associations were not found for Machine \
{}".format(ma.name))
migrated += 1
print("==============================================================")
print("Successfully migrated {} Machines".format(migrated))
print("==============================================================")
if failed:
print("Failed to migrate {} Machines".format(failed))
print("==============================================================")
def migrate_cloud(old_cloud_id, new_cloud_id):
try:
old_cloud = Cloud.objects.get(id=old_cloud_id)
except Cloud.DoesNotExist:
print("Cloud with id {} not found. Exiting...".format(old_cloud_id))
sys.exit(1)
try:
new_cloud = Cloud.objects.get(id=new_cloud_id)
except Cloud.DoesNotExist:
print("Cloud with id {} not found. Exiting...".format(new_cloud_id))
print("==============================================================")
print("=== Will migrate {} cloud to {} cloud ===".format(old_cloud.name,
new_cloud.name))
print("==============================================================")
migrate_ownership(old_cloud, new_cloud)
migrate_tags(old_cloud, new_cloud)
migrate_images(old_cloud, new_cloud)
migrate_key_associations(old_cloud, new_cloud)
return
if __name__ == '__main__':
try:
old_cloud_id = sys.argv[1]
new_cloud_id = sys.argv[2]
except IndexError:
print("Old cloud id and new cloud id are required. Exiting...")
sys.exit(1)
migrate_cloud(old_cloud_id, new_cloud_id)