-
Notifications
You must be signed in to change notification settings - Fork 2
/
rs-pv2hvm.py
executable file
·384 lines (345 loc) · 15.1 KB
/
rs-pv2hvm.py
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
# rc-pv2hvm, a script that converts Rackspace Cloud Servers from PV to HVM mode
# version: 0.0.6a
# Copyright 2018 Brian King
# License: Apache
import argparse
import base64
from collections import defaultdict
import datetime
from getpass import getpass
import json
import keyring
import os
import plac
import re
import requests
import sys
import time
import uuid
def getset_keyring_credentials(username=None, password=None):
"""Method to retrieve credentials from keyring."""
username = keyring.get_password("raxcloud", "username")
if username is None:
if sys.version_info.major < 3:
username = raw_input("Enter Rackspace Username: ")
keyring.set_password("raxcloud", 'username', username)
print ("Username value saved in keychain as raxcloud username.")
elif creds == "username":
username = input("Enter Rackspace Username: ")
keyring.set_password("raxcloud", 'username', username)
print ("Username value saved in keychain as raxcloud username.")
else:
print ("Authenticating to Rackspace cloud as %s" % username)
password = keyring.get_password("raxcloud", "password")
if password is None:
password = getpass("Enter Rackspace API key:")
keyring.set_password("raxcloud", 'password' , password)
print ("API key value saved in keychain as raxcloud password.")
return username, password
def wipe_keyring_credentials(username, password):
"""Wipe credentials from keyring."""
try:
keyring.delete_password('raxcloud', 'username')
keyring.delete_password('raxcloud', 'password')
except:
pass
return True
# Request to authenticate using password
def get_auth_token(username,password):
#setting up api call
url = "https://identity.api.rackspacecloud.com/v2.0/tokens"
headers = {'Content-type': 'application/json'}
payload = {'auth':{'passwordCredentials':{'username': username,'password': password}}}
payload2 = {'auth':{'RAX-KSKEY:apiKeyCredentials':{'username': username,'apiKey': password}}}
#authenticating against the identity
try:
r = requests.post(url, headers=headers, json=payload)
except requests.ConnectionError as e:
print("Connection Error: Check your interwebs!")
sys.exit()
if r.status_code != 200:
r = requests.post(url, headers=headers, json=payload2)
if r.status_code != 200:
print ("Error! API responds with %d" % r.status_code)
print("Rerun the script and you will be prompted to re-enter username/password.")
wipe_keyring_credentials(username, password)
sys.exit()
else:
print("Authentication was successful!")
elif r.status_code == 200:
print("Authentication was successful!")
#loads json reponse into data as a dictionary.
data = r.json()
#assign token and account variables with info from json response.
auth_token = data["access"]["token"]["id"]
return auth_token
def find_endpoints(auth_token):
#init Cloud Servers endpoints as an empty list
glance_endpoints=[]
cs_endpoints=[]
#setting up api call
url = ("https://identity.api.rackspacecloud.com/v2.0/tokens/%s/endpoints" % auth_token)
headers = {'content-type': 'application/json', 'Accept': 'application/json',
'X-Auth-Token': auth_token}
raw_service_catalog = requests.get(url, headers=headers)
the_service_catalog = raw_service_catalog.json()
endpoints = the_service_catalog["endpoints"]
for service in range(len(endpoints)):
if "cloudImages" == endpoints[service]["name"]:
glance_endpoints.append(endpoints[service]["publicURL"])
if "cloudServersOpenStack" == endpoints[service]["name"]:
cs_endpoints.append(endpoints[service]["publicURL"])
return glance_endpoints, cs_endpoints, headers
def find_glance_image_and_cs_endpoint(auth_token, headers, cs_endpoints, glance_endpoints, glance_image):
print ("Determining which region your cloud server image is in...")
for endpoint in range(len(glance_endpoints)):
potential_url = ( "%s/images/%s" % (glance_endpoints[endpoint], glance_image) )
potential_image = requests.get(url=potential_url, headers=headers)
if potential_image.status_code == 200:
glance_object = potential_image
region = potential_url.split('//')[1].split('.')[0]
print ("Found image %s in %s region" % (glance_image, region))
break
for endpoint in cs_endpoints:
if region in endpoint:
cs_endpoint = endpoint
return glance_object, cs_endpoint, region
#if we make it this far, the glance image UUID is invalid
print ("Error! Rackspace Cloud Server Image UUID %s was not found." % (glance_image) )
sys.exit()
def check_glance_image(auth_token, headers, glance_image, glance_object):
# sanity checks
print ("Verifying image status...")
glance_image_type = (glance_object.json()["image_type"])
# will not try to convert base image
if glance_image_type != "snapshot":
print ("Error! I won't convert a base image.")
sys.exit()
glance_status = (glance_object.json()["status"])
if glance_status != "active":
print ("Error! Wrong status for glance image %s. Expected 'active' ,\
but found '%s'" % (glance_image, glance_status))
sys.exit()
# vm_mode is not always set. Without it, servers will build in PV mode.
if "vm_mode" not in glance_object.json():
glance_object.json()["vm_mode"] = ""
else:
image_vm_mode = glance_object.json()["vm_mode"]
if image_vm_mode == "hvm":
print(
"Error! Image vm_mode already set to HVM, conversion not needed."
)
sys.exit()
# Confirm the OS is supported
image_os = (glance_object.json()["org.openstack__1__os_distro"])
return image_os
image_version = (glance_object.json()["org.openstack__1__os_version"])
supported_os =('centos', 'debian', 'redhat', 'ubuntu')
supported = False
for os in supported_os:
if os in image_os:
#Check RHEL/CentOS version. We don't do this with Deb/Ubuntu because
# release upgrades make this value unreliable
if image_os == 'com.redhat' or 'org.centos':
if "6." in image_version:
supported = True
else:
supported = False
supported = True
if supported == False:
print("Error! Image built from unsupported OS! Exiting.")
sys.exit()
def determine_cs_flavor(auth_token, headers, glance_image, glance_object):
# for speed's sake, we will build General Purpose servers if possible
min_disk = (glance_object.json()["min_disk"])
if min_disk <= 160:
disk_multiplier = 20
flavor_type = "general"
if (min_disk % disk_multiplier) != 0:
print ("Error! min_disk value should be a multiple of 20.")
sys.exit()
flavor_memory = str(min_disk / disk_multiplier)
flavor = flavor_type + "1-" + flavor_memory
return flavor
elif min_disk == 320:
flavor = 6
return flavor
elif min_disk == 620:
flavor = 7
return flavor
elif min_disk == 1200:
flavor = 8
return flavor
print ("Error! Could not determine flavor to use.")
sys.exit()
def build_server(auth_token, headers, cs_endpoint, glance_image, glance_object, image_os, flavor):
cs_endpoint = ("%s/servers" % (cs_endpoint))
image_name=(glance_object.json()["name"])
rand_postpend = str(uuid.uuid4())
cs_name = image_name + "-pv2hvm-" + rand_postpend[0:7]
#The script to inject at boot time. For Debian, we use cloud-init.
if image_os == "org.debian":
dl_script ='''#!/bin/bash
wget -qO /tmp/hvm.sh http://e942b029c256ec323134-d1408b968928561823109cb66c47ebcd.r37.cf5.rackcdn.com/hvm.sh
bash /tmp/hvm.sh
'''
user_data = base64.b64encode(dl_script)
payload = (
{ "server": {"name": cs_name,
"imageRef": glance_image, "flavorRef": flavor,
"config_drive": True,
"user_data": user_data }}
)
elif "centos" in image_os or "redhat" in image_os or "com.ubuntu" == image_os:
#The script to inject at boot time.
#Cloud-init is unreliable or missing on Ubuntu 12 and RHEL6, so we use
#Server personality and upstart job
path = "/etc/init/pv-convert.conf"
dl_script = '''description "Download and execute PV to HVM conversion script"
author "Brian King"
start on runlevel S or stopping rc RUNLEVEL=[234]
task
script
/usr/bin/wget -qO /tmp/hvm.sh http://e942b029c256ec323134-d1408b968928561823109cb66c47ebcd.r37.cf5.rackcdn.com/hvm.sh
exec /bin/bash /tmp/hvm.sh
end script
pre-start script
printf "%s\n" "[$(date)] PV conversion upstart job is starting " >> /tmp/pv2hvm.log
end script
'''
personality = base64.b64encode(dl_script)
payload = (
{ "server": {"name": cs_name,
"imageRef": glance_image, "flavorRef": flavor,
"personality": [{"path": path,
"contents": personality}]}}
)
cs_object = requests.post(url=cs_endpoint, headers=headers, json=payload)
cs_url = cs_object.json()["server"]["links"][0]["href"]
print("Sent build request for server %s" % (cs_name))
return cs_name, cs_object, cs_url
def poll_cs_status(auth_token, headers, cs_name, cs_obj, cs_url, desired_status="ACTIVE"):
period = "."
counter = 1
cs_status = "INIT"
while cs_status != desired_status:
try:
cs_status_obj = requests.get(url=cs_url, headers=headers)
except requests.ConnectionError as e:
print("Can't connect to API, trying again....")
cs_status = cs_status_obj.json()["server"]["status"]
print ("Server %s is in %s status%s" % (cs_name, cs_status, (period * counter)))
counter = counter + 1
time.sleep(15)
def reboot_server(auth_token, headers, cs_name, cs_url):
print ("Rebooting server %s to activate the script" % (cs_name))
cs_url = ("%s/action" % (cs_url))
payload = (
{ "reboot": {
"type" : "HARD"
}
}
)
rb_request = requests.post(url=cs_url, headers=headers,json=payload)
rb_request.raise_for_status()
def create_cs_image(auth_token, headers, cs_name, cs_object, cs_url):
#sleep so the script can convert the servers
print ("Sleeping for 2 minutes so the script can finish.")
for step in xrange(120,0,-1):
time.sleep(1)
sys.stdout.write(str(step)+' ')
sys.stdout.flush()
print ("Creating image of server %s" % (cs_name))
cs_url = ("%s/action" % (cs_object.json()["server"]["links"][0]["href"]))
rand_postpend = str(uuid.uuid4())
image_creator = "rs-pv2hvm"
image_name = cs_name
payload = (
{ "createImage" : {
"name" : image_name,
"metadata": {
"created_by": image_creator
}
}
}
)
image_create = requests.post(url=cs_url, headers=headers, json=payload)
image_create.raise_for_status()
if image_create.ok:
image_url = image_create.headers["Location"]
return image_name, image_url
#if we made it this far, somehow we never got the image URL.
if not image_url:
print ("Error! Did not receive image URL from Cloud Servers API. Exiting." )
sys.exit()
def poll_image_status(auth_token, headers, image_name, image_url):
image_info = requests.get(url=image_url, headers=headers)
image_status = image_info.json()["image"]["status"]
image_id = image_info.json()["image"]["name"]
period = "."
counter = 1
image_status = "INIT"
while image_status != "ACTIVE":
try:
image_status_object = requests.get(url=image_url, headers=headers)
except requests.ConnectionError as e:
print("Can't connect to API, trying again...")
if image_status_object.status_code == 200:
image_status = image_status_object.json()["image"]["status"]
print ("Image %s is in %s status%s" % (image_name, image_status, (period * counter)))
counter = counter + 1
time.sleep(30)
def set_image_metadata(auth_token, headers, image_name, image_url):
metadata_url = ("%s/metadata" % image_url)
payload = {
"metadata": {
"vm_mode": "hvm"
}
}
image_vm_mode = requests.post(url=metadata_url, headers=headers, json=payload)
image_vm_mode.raise_for_status()
if image_vm_mode.ok:
print ("I set image metadata on %s" % image_name)
def rebuild_server(auth_token, headers, cs_name, cs_object, cs_url, image_url):
image_id=image_url.split('//')[1].split('/')[4]
print ("Rebuilding %s with new HVM-mode image" % (cs_name))
cs_url = ("%s/action" % (cs_url))
payload = (
{ "rebuild": {
"imageRef" : image_id
}
}
)
rebuild_request = requests.post(url=cs_url, headers=headers,json=payload)
rebuild_request.raise_for_status()
rebuild_object = rebuild_request.json()
cs_rebuilt_rootpw = rebuild_object["server"]["adminPass"]
ipv4 = rebuild_object["server"]["accessIPv4"]
ipv6 = rebuild_object["server"]["accessIPv6"]
print ("Server is building. New root password will be %s .\n" % (cs_rebuilt_rootpw))
return ipv4, ipv6
#begin main function
@plac.annotations(
glance_image=plac.Annotation("UUID of Cloud Server image")
)
def main(glance_image):
username,password = getset_keyring_credentials()
auth_token = get_auth_token(username, password)
glance_endpoints, cs_endpoints, headers = find_endpoints(auth_token)
glance_object, cs_endpoint, region = find_glance_image_and_cs_endpoint(auth_token, headers, cs_endpoints, glance_endpoints, glance_image)
image_os = check_glance_image(auth_token, headers, glance_image, glance_object)
flavor = determine_cs_flavor(auth_token, headers, glance_image, glance_object)
cs_name, cs_object, cs_url = build_server(auth_token, headers, cs_endpoint, glance_image, glance_object, image_os, flavor)
poll_cs_status(auth_token, headers, cs_name, cs_object, cs_url, desired_status="ACTIVE")
#FIXME: Don't reboot Debian as it's not necessary
reboot_server(auth_token, headers, cs_name, cs_url)
image_name, image_url = create_cs_image(auth_token, headers, cs_name, cs_object, cs_url)
poll_image_status(auth_token, headers, image_name, image_url)
set_image_metadata(auth_token, headers, image_name, image_url)
ipv4, ipv6 = rebuild_server(auth_token, headers, cs_name, cs_object, cs_url, image_url)
poll_cs_status(auth_token, headers, cs_name, cs_object, cs_url, desired_status="ACTIVE")
print ("Server %s is done rebuilding. Please login now and verify that it built correctly. Public IPs are: %s and %s" % ( cs_name, ipv4, ipv6) )
if __name__ == '__main__':
import plac
plac.call(main)