38 |
38 |
import os
|
39 |
39 |
import sys
|
40 |
40 |
|
|
41 |
from base64 import b64encode
|
41 |
42 |
from collections import defaultdict
|
|
43 |
from grp import getgrgid
|
42 |
44 |
from optparse import OptionParser
|
|
45 |
from pwd import getpwuid
|
43 |
46 |
|
44 |
47 |
from client import Client, ClientError
|
45 |
48 |
|
... | ... | |
239 |
242 |
|
240 |
243 |
def add_options(self, parser):
|
241 |
244 |
parser.add_option('-f', dest='flavor', metavar='FLAVOR_ID', default=1,
|
242 |
|
help='use flavor FLAVOR_ID')
|
|
245 |
help='use flavor FLAVOR_ID')
|
243 |
246 |
parser.add_option('-i', dest='image', metavar='IMAGE_ID', default=1,
|
244 |
|
help='use image IMAGE_ID')
|
245 |
|
parser.add_option('--personality', dest='personality', action='append',
|
246 |
|
metavar='PATH,SERVERPATH',
|
247 |
|
help='add a personality file')
|
|
247 |
help='use image IMAGE_ID')
|
|
248 |
parser.add_option('--personality',
|
|
249 |
dest='personalities',
|
|
250 |
action='append',
|
|
251 |
default=[],
|
|
252 |
metavar='PATH[,SERVER PATH[,OWNER[,GROUP,[MODE]]]]',
|
|
253 |
help='add a personality file')
|
248 |
254 |
|
249 |
255 |
def main(self, name):
|
250 |
256 |
flavor_id = int(self.flavor)
|
251 |
257 |
image_id = int(self.image)
|
252 |
|
personality = []
|
253 |
|
if self.personality:
|
254 |
|
for p in self.personality:
|
255 |
|
lpath, sep, rpath = p.partition(',')
|
256 |
|
if not lpath or not rpath:
|
257 |
|
log.error("Invalid personality argument '%s'", p)
|
258 |
|
return
|
259 |
|
if not os.path.exists(lpath):
|
260 |
|
log.error("File %s does not exist", lpath)
|
261 |
|
return
|
262 |
|
with open(lpath) as f:
|
263 |
|
personality.append((rpath, f.read()))
|
|
258 |
personalities = []
|
|
259 |
for personality in self.personalities:
|
|
260 |
p = personality.split(',')
|
|
261 |
p.extend([None] * (5 - len(p))) # Fill missing fields with None
|
|
262 |
|
|
263 |
path = p[0]
|
|
264 |
|
|
265 |
if not path:
|
|
266 |
log.error("Invalid personality argument '%s'", p)
|
|
267 |
return
|
|
268 |
if not os.path.exists(path):
|
|
269 |
log.error("File %s does not exist", path)
|
|
270 |
return
|
|
271 |
|
|
272 |
with open(path) as f:
|
|
273 |
contents = b64encode(f.read())
|
|
274 |
|
|
275 |
st = os.stat(path)
|
|
276 |
personalities.append({
|
|
277 |
'path': p[1] or os.path.abspath(path),
|
|
278 |
'owner': p[2] or getpwuid(st.st_uid).pw_name,
|
|
279 |
'group': p[3] or getgrgid(st.st_gid).gr_name,
|
|
280 |
'mode': int(p[4]) if p[4] else 0x7777 & st.st_mode,
|
|
281 |
'contents': contents})
|
264 |
282 |
|
265 |
283 |
reply = self.client.create_server(name, flavor_id, image_id,
|
266 |
|
personality)
|
|
284 |
personalities)
|
267 |
285 |
print_dict(reply)
|
268 |
286 |
|
269 |
287 |
|