-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-openstack-lxc.py
More file actions
396 lines (351 loc) · 12 KB
/
python-openstack-lxc.py
File metadata and controls
396 lines (351 loc) · 12 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
from flask import Flask, request, Response
app = Flask(__name__)
try:
from simplejson import json
except:
import json
import psutil
import shlex, subprocess, re # calls to lxc-*
import threading
global base_url
base_url = 'http://localhost:5000/v2'
@app.route('/v2.0/tokens', methods = [ 'POST' ])
def auth():
auth_dict = get_auth()
json_raw = json.dumps(auth_dict)
return Response(json_raw, status=200, mimetype='application/json')
def is_auth_ok(request_obj):
headers = request_obj.headers
if 'X-Auth-Token' in headers:
if headers['X-Auth-Token'] == 'only_this_id':
return True
return False
def get_auth():
d = {}
d['access'] = {}
d['access']['token'] = { 'id': 'only_this_id'}
d['access']['serviceCatalog'] = get_serviceCatalog()
d['access']['user'] = d['access']['token'] # for ruby fog?
return d
def get_serviceCatalog():
url = base_url + '/compute'
d = [{
"endpoints": [
{
# "adminURL": url,
"internalURL": url,
"publicURL": url,
"region": "RegionOne"
}
],
"name": "nova",
"type": "compute"
}]
return d
# --- This ensures authorization and json representation
def gen_response(dict_data):
ok_str = 'AUTH_OK' if is_auth_ok(request) else 'BAD_AUTH'
print('{0}'.format(ok_str))
threads = [t.name for t in threading.enumerate()]
print('threads: {0}'.format(threads))
json_raw = json.dumps(dict_data)
return Response(json_raw, status=200, mimetype='application/json')
# --- This deserializes a json request
def unmarshal(request_obj):
# TODO: ensure json, support xml
json_raw = json.loads(request_obj.data)
return json_raw
#--- Compute - Create
def run(cmd):
try:
print "EXEC: {0}".format(cmd)
out = subprocess.check_output(shlex.split(cmd))
print out
except subprocess.CalledProcessError as e:
print 'WOW "{0}" SAYS "{1}"'.format(cmd, e.output)
def lxc_create(name, template_id):
# cmd = 'lxc-create -n {0} -t {1} -- -r precise'.format(name, template_id)
cmd = 'lxc-create -n {0} -t {1} -- -u /vagrant/userdata.cloud-init -r precise -S /home/vagrant/.ssh/id_rsa.pub'.format(name, template_id)
run(cmd)
def lxc_start(name):
cmd = 'lxc-start -n {0} -d -o /tmp/lxc.log'.format(name)
run(cmd)
def lxc_wait(name, state):
cmd = 'lxc-wait -n {0} -s {1}'.format(name, state)
run(cmd)
def lxc_destroy(name):
cmd = 'lxc-destroy -n {0}'.format(name)
run(cmd)
def lxc_stop(name):
cmd = 'lxc-stop -n {0}'.format(name)
run(cmd)
def start_lxc_compute(compute_request):
"""thread: lxc-create, lxc-start, get_ip"""
import time
time.sleep(2)
info = compute_request['server']
name = info['name']
lxc_create(name, 'ubuntu-cloud')
#lxc_create(name, 'ubuntu')
lxc_wait(name, 'STOPPED')
lxc_start(name)
print "done w/ {0}".format(name)
def start_lxc_thread(compute_request):
existing_nodes = [ n for node_per_state in lxc_list() for n in node_per_state ]
n = compute_request["server"]["name"]
if n in existing_nodes:
print 'AAAAAAAAAAAAAAAHHHHHH {0} exists.'.format(n)
else:
t = threading.Thread(target=start_lxc_compute, name="starting:{0}".format(n), args=(compute_request, ))
t.daemon = True
t.start()
def get_server(name):
new_compute = unmarshal(request)
print(new_compute)
start_lxc_thread(new_compute)
#d = { 'servers': [] }
compute_id = name #"5bbcc3c4-1da2-4437-a48a-66f15b1b13f9"
url = base_url + '/compute/servers/' + compute_id
# self was with version, bookmark w/o version
d = {
"server": {
"adminPass": "ubuntu",
"id": compute_id,
"links": [
{
"href": url,
"rel": "self"
},
{
"href": url,
"rel": "bookmark"
}
]
}
}
return d
@app.route('/v2/compute/servers', methods = [ 'POST' ] )
def compute_servers():
compute_request = unmarshal(request)
if "server" in compute_request and "name" in compute_request["server"]:
name = compute_request["server"]["name"]
resp = get_server(name)
else:
resp = {}
return gen_response(resp)
def delete_compute_server(compute_id):
lxc_stop(compute_id)
lxc_wait(compute_id, 'STOPPED')
lxc_destroy(compute_id)
#--- Compute - Single compute info
@app.route('/v2/compute/servers/<string:compute_id>', methods = [ 'GET', 'DELETE' ])
def get_compute_servers(compute_id):
if request.method == 'DELETE':
print('asking to delete {0}'.format(compute_id))
delete_compute_server(compute_id)
return gen_response({})
else:
print('{0} compute_id'.format(compute_id))
return gen_response(create_compute_server(compute_id))
def get_id_and_links(datadict):
return dict([ (k, datadict[k]) for k in ('id', 'links') if k in datadict ])
def name_to_compute_id(cid):
return cid
def get_ip_from_lease_file(compute_id):
name = name_to_compute_id(compute_id)
filename = "/var/lib/lxc/{0}/rootfs/var/lib/dhcp/dhclient.eth0.leases".format(name)
lease_contents = []
ip = None
try:
with open(filename) as f:
lease_contents = f.readlines()
except IOError as e:
pass
for lease in lease_contents:
match = re.search(r'fixed-address ([^;]+);', lease)
if match:
ip = match.groups()[0]
return ip
def is_cloudinit_done(compute_id):
name = name_to_compute_id(compute_id)
filename = "/var/lib/lxc/{0}/rootfs/var/log/cloud-init-output.log".format(name)
cloudinit_contents = []
completion_time = None
try:
with open(filename) as f:
cloudinit_contents = f.readlines()
except IOError as e:
pass
for cloudinit in cloudinit_contents:
match = re.match(r'cloud-init boot finished at ([^.]+).', cloudinit)
if match:
completion_time = match.groups()[0]
return completion_time
def get_compute_addresses(compute_id):
ip = get_ip_from_lease_file(compute_id)
d = { "private": [], "public": [] }
if ip:
d["private"].append({ "addr": ip, "version": 4 })
d["public"].append({ "addr": ip, "version": 4 })
return d
def create_compute_server(compute_id):
flavor = get_id_and_links(get_flavor())
image = get_id_and_links(get_images()['images'][0])
#compute_id = "893c7791-f1df-4c3d-8383-3caae9656c62"
url = base_url + '/compute/servers/' + compute_id
d = {
"server": {
"accessIPv4": "",
"accessIPv6": "",
"addresses": get_compute_addresses(compute_id),
"created": "2012-08-20T21:11:09Z",
"flavor": flavor,
"hostId": compute_id, #"65201c14a29663e06d0748e561207d998b343e1d164bfa0aafa9c45d",
"id": compute_id,
"image": image,
"links": [
{
"href": url,
"rel": "self"
},
{
"href": url,
"rel": "bookmark"
}
],
"metadata": {},
"name": compute_id,
"progress": 0,
"status": "UNKNOWN",
"tenant_id": "openstack",
"updated": "2012-08-20T21:11:09Z",
"user_id": "fake"
}
}
return d
#--- Compute - Detail
@app.route('/v2/compute/servers/detail')
def compute_servers_detail():
return gen_response(get_servers_detail())
# all in thread list should be pending
# then lxc_list runing should be running
# unless they don't have an ip, then they are also pending
def get_pending_from_threads():
threads = [t.name for t in threading.enumerate()]
return [ t.split(':')[1] for t in threads if "starting:" in t]
def get_servers_detail():
pending_nodes = get_pending_from_threads()
running_lxc_nodes = lxc_list()[0]
running_with_ip = [ n for n in running_lxc_nodes if get_ip_from_lease_file(n) ]
running_with_cloudinit = [ n for n in running_lxc_nodes if is_cloudinit_done(n) ]
# pending nodes are from threads and running w/o ip
running_without_ip = set(running_lxc_nodes).difference(running_with_ip)
print 'running w/o ip: {0}'.format(running_without_ip)
running_without_cloudinit = set(running_lxc_nodes).difference(running_with_cloudinit)
print 'running w/o cloudinit: {0}'.format(running_without_cloudinit)
print 'not complete: {0}'.format(running_without_ip.union(running_without_cloudinit))
pending_nodes = set(pending_nodes).union(running_without_ip.union(running_without_cloudinit))
print "pending: {0}".format(pending_nodes)
all_running = set(running_lxc_nodes).difference(pending_nodes)
print "running: {0}".format(all_running)
print "all: {0}".format(all_running.union(pending_nodes))
servers = []
for n in pending_nodes:
cn = create_compute_server(n)['server']
cn["status"] = "BUILD"
servers.append(cn)
if len(pending_nodes) > 0:
import time
time.sleep(10)
for n in all_running:
cn = create_compute_server(n)['server']
cn["status"] = "ACTIVE"
servers.append(cn)
d = { 'servers': servers }
return d
def lxc_list():
""" returns tuple of started, running node list"""
out = subprocess.check_output('lxc-list')
node_info = re.split(r'(?:RUNNING|FROZEN|STOPPED)\n', out)
types = \
[ n.strip().split('\n') if len(n.strip()) else [] for n in node_info ]
_, r, f, s = \
[ [ n.strip() for n in t ] for t in types ]
return (r, s)
#--- Compute - Flavors
def get_images():
d = {
"images": [
{
"created": "2011-01-01T01:02:03Z",
"id": "70a599e0-31e7-49b7-b260-868f441e862b",
"links": [
{
"href": "http://openstack.example.com/v2/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b",
"rel": "self"
},
{
"href": "http://openstack.example.com/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b",
"rel": "bookmark"
},
{
"href": "http://glance.openstack.example.com/openstack/images/70a599e0-31e7-49b7-b260-868f441e862b",
"rel": "alternate",
"type": "application/vnd.openstack.image"
}
],
"metadata": {
"architecture": "x86_64",
"auto_disk_config": "True",
"kernel_id": "nokernel",
"ramdisk_id": "nokernel"
},
"minDisk": 0,
"minRam": 0,
"name": "fakeimage7",
"progress": 100,
"status": "ACTIVE",
"updated": "2011-01-01T01:02:03Z"
}]}
return d
@app.route('/v2/compute/images/detail')
def images():
return gen_response(get_images())
#--- Compute - Flavors
@app.route('/v2/compute/flavors/detail')
def flavors():
return gen_response(get_flavors())
def get_flavors():
d = { 'flavors' : [ get_flavor() ] }
return d
def get_ram():
return psutil.virtual_memory().available / (1024**2)
def get_vcpus():
return psutil.NUM_CPUS
def get_disk():
return psutil.disk_usage('/').free / (1024**3)
def get_flavor():
flavor_id = 1
url = base_url + '/compute/flavors/' + str(flavor_id)
# bookmark link does not have version in it..
d = {
"disk": get_disk(),
"id": str(flavor_id),
"links": [
{
"href": url,
"rel": "self"
},
{
"href": url,
"rel": "bookmark"
}
],
"name": "default.lxc",
"ram": get_ram(),
"vcpus": get_vcpus()
}
return d
if __name__ == '__main__':
app.debug = True
app.run(host = '0.0.0.0', port = 5000 )