Fixed pep8 errors
Unit tests passed
This commit is contained in:
parent
93d6050078
commit
2ecd9de1a0
@ -438,12 +438,15 @@ class VMHelper(HelperBase):
|
||||
return vdis
|
||||
else:
|
||||
return None
|
||||
@classmethod
|
||||
def lookup_kernel_ramdisk(cls,session,vm):
|
||||
vm_rec = session.get_xenapi().VM.get_record(vm)
|
||||
return (vm_rec['PV_kernel'],vm_rec['PV_ramdisk'])
|
||||
|
||||
|
||||
@classmethod
|
||||
def lookup_kernel_ramdisk(cls, session, vm):
|
||||
vm_rec = session.get_xenapi().VM.get_record(vm)
|
||||
if 'PV_kernel' in vm_rec and 'PV_ramdisk' in vm_rec:
|
||||
return (vm_rec['PV_kernel'], vm_rec['PV_ramdisk'])
|
||||
else:
|
||||
return (None, None)
|
||||
|
||||
@classmethod
|
||||
def compile_info(cls, record):
|
||||
"""Fill record with VM status information"""
|
||||
|
@ -73,8 +73,8 @@ def key_init(self, arg_dict):
|
||||
@jsonify
|
||||
def password(self, arg_dict):
|
||||
"""Writes a request to xenstore that tells the agent to set
|
||||
the root password for the given VM. The password should be
|
||||
encrypted using the shared secret key that was returned by a
|
||||
the root password for the given VM. The password should be
|
||||
encrypted using the shared secret key that was returned by a
|
||||
previous call to key_init. The encrypted password value should
|
||||
be passed as the value for the 'enc_pass' key in arg_dict.
|
||||
"""
|
||||
@ -108,7 +108,8 @@ def _wait_for_agent(self, request_id, arg_dict):
|
||||
# First, delete the request record
|
||||
arg_dict["path"] = "data/host/%s" % request_id
|
||||
xenstore.delete_record(self, arg_dict)
|
||||
raise TimeoutError("TIMEOUT: No response from agent within %s seconds." %
|
||||
raise TimeoutError(
|
||||
"TIMEOUT: No response from agent within %s seconds." %
|
||||
AGENT_TIMEOUT)
|
||||
ret = xenstore.read_record(self, arg_dict)
|
||||
# Note: the response for None with be a string that includes
|
||||
|
@ -43,42 +43,47 @@ CHUNK_SIZE = 8192
|
||||
KERNEL_DIR = '/boot/guest'
|
||||
FILE_SR_PATH = '/var/run/sr-mount'
|
||||
|
||||
def remove_kernel_ramdisk(session,args):
|
||||
|
||||
def remove_kernel_ramdisk(session, args):
|
||||
"""Removes kernel and/or ramdisk from dom0's file system"""
|
||||
kernel_file=exists(args,'kernel-file')
|
||||
ramdisk_file=exists(args,'ramdisk-file')
|
||||
kernel_file = exists(args, 'kernel-file')
|
||||
ramdisk_file = exists(args, 'ramdisk-file')
|
||||
if kernel_file:
|
||||
os.remove(kernel_file)
|
||||
if ramdisk_file:
|
||||
os.remove(ramdisk_file)
|
||||
return "ok"
|
||||
|
||||
def copy_kernel_vdi(session,args):
|
||||
|
||||
def copy_kernel_vdi(session, args):
|
||||
vdi = exists(args, 'vdi-ref')
|
||||
size = exists(args,'image-size')
|
||||
size = exists(args, 'image-size')
|
||||
#Use the uuid as a filename
|
||||
vdi_uuid=session.xenapi.VDI.get_uuid(vdi)
|
||||
copy_args={'vdi_uuid':vdi_uuid,'vdi_size':int(size)}
|
||||
filename=with_vdi_in_dom0(session, vdi, False,
|
||||
vdi_uuid = session.xenapi.VDI.get_uuid(vdi)
|
||||
copy_args = {'vdi_uuid': vdi_uuid, 'vdi_size': int(size)}
|
||||
filename = with_vdi_in_dom0(session, vdi, False,
|
||||
lambda dev:
|
||||
_copy_kernel_vdi('/dev/%s' % dev,copy_args))
|
||||
_copy_kernel_vdi('/dev/%s' % dev, copy_args))
|
||||
return filename
|
||||
|
||||
def _copy_kernel_vdi(dest,copy_args):
|
||||
vdi_uuid=copy_args['vdi_uuid']
|
||||
vdi_size=copy_args['vdi_size']
|
||||
logging.debug("copying kernel/ramdisk file from %s to /boot/guest/%s",dest,vdi_uuid)
|
||||
filename=KERNEL_DIR + '/' + vdi_uuid
|
||||
|
||||
def _copy_kernel_vdi(dest, copy_args):
|
||||
vdi_uuid = copy_args['vdi_uuid']
|
||||
vdi_size = copy_args['vdi_size']
|
||||
logging.debug("copying kernel/ramdisk file from %s to /boot/guest/%s",
|
||||
dest, vdi_uuid)
|
||||
filename = KERNEL_DIR + '/' + vdi_uuid
|
||||
#read data from /dev/ and write into a file on /boot/guest
|
||||
of=open(filename,'wb')
|
||||
f=open(dest,'rb')
|
||||
of = open(filename, 'wb')
|
||||
f = open(dest, 'rb')
|
||||
#copy only vdi_size bytes
|
||||
data=f.read(vdi_size)
|
||||
data = f.read(vdi_size)
|
||||
of.write(data)
|
||||
f.close()
|
||||
of.close()
|
||||
logging.debug("Done. Filename: %s",filename)
|
||||
return filename
|
||||
of.close()
|
||||
logging.debug("Done. Filename: %s", filename)
|
||||
return filename
|
||||
|
||||
|
||||
def put_vdis(session, args):
|
||||
params = pickle.loads(exists(args, 'params'))
|
||||
@ -86,22 +91,23 @@ def put_vdis(session, args):
|
||||
image_id = params["image_id"]
|
||||
glance_host = params["glance_host"]
|
||||
glance_port = params["glance_port"]
|
||||
|
||||
|
||||
sr_path = get_sr_path(session)
|
||||
#FIXME(sirp): writing to a temp file until Glance supports chunked-PUTs
|
||||
tmp_file = "%s.tar.gz" % os.path.join('/tmp', str(image_id))
|
||||
tmp_file = "%s.tar.gz" % os.path.join('/tmp', str(image_id))
|
||||
tar_cmd = ['tar', '-zcf', tmp_file, '--directory=%s' % sr_path]
|
||||
paths = [ "%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids ]
|
||||
paths = ["%s.vhd" % vdi_uuid for vdi_uuid in vdi_uuids]
|
||||
tar_cmd.extend(paths)
|
||||
logging.debug("Bundling image with cmd: %s", tar_cmd)
|
||||
subprocess.call(tar_cmd)
|
||||
logging.debug("Writing to test file %s", tmp_file)
|
||||
logging.debug("Writing to test file %s", tmp_file)
|
||||
put_bundle_in_glance(tmp_file, image_id, glance_host, glance_port)
|
||||
return "" # FIXME(sirp): return anything useful here?
|
||||
# FIXME(sirp): return anything useful here?
|
||||
return ""
|
||||
|
||||
|
||||
def put_bundle_in_glance(tmp_file, image_id, glance_host, glance_port):
|
||||
size = os.path.getsize(tmp_file)
|
||||
size = os.path.getsize(tmp_file)
|
||||
basename = os.path.basename(tmp_file)
|
||||
|
||||
bundle = open(tmp_file, 'r')
|
||||
@ -122,7 +128,7 @@ def put_bundle_in_glance(tmp_file, image_id, glance_host, glance_port):
|
||||
for header, value in headers.iteritems():
|
||||
conn.putheader(header, value)
|
||||
conn.endheaders()
|
||||
|
||||
|
||||
chunk = bundle.read(CHUNK_SIZE)
|
||||
while chunk:
|
||||
conn.send(chunk)
|
||||
@ -135,6 +141,7 @@ def put_bundle_in_glance(tmp_file, image_id, glance_host, glance_port):
|
||||
finally:
|
||||
bundle.close()
|
||||
|
||||
|
||||
def get_sr_path(session):
|
||||
sr_ref = find_sr(session)
|
||||
|
||||
@ -165,6 +172,6 @@ def find_sr(session):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
XenAPIPlugin.dispatch({'put_vdis': put_vdis,
|
||||
XenAPIPlugin.dispatch({'put_vdis': put_vdis,
|
||||
'copy_kernel_vdi': copy_kernel_vdi,
|
||||
'remove_kernel_ramdisk': remove_kernel_ramdisk})
|
||||
|
@ -43,34 +43,37 @@ SECTOR_SIZE = 512
|
||||
MBR_SIZE_SECTORS = 63
|
||||
MBR_SIZE_BYTES = MBR_SIZE_SECTORS * SECTOR_SIZE
|
||||
|
||||
def is_vdi_pv(session,args):
|
||||
|
||||
def is_vdi_pv(session, args):
|
||||
logging.debug("Checking wheter VDI has PV kernel")
|
||||
vdi = exists(args, 'vdi-ref')
|
||||
pv=with_vdi_in_dom0(session, vdi, False,
|
||||
pv = with_vdi_in_dom0(session, vdi, False,
|
||||
lambda dev: _is_vdi_pv('/dev/%s' % dev))
|
||||
if pv:
|
||||
return 'true'
|
||||
else:
|
||||
return 'false'
|
||||
|
||||
|
||||
def _is_vdi_pv(dest):
|
||||
logging.debug("Running pygrub against %s",dest)
|
||||
output=os.popen('pygrub -qn %s' % dest)
|
||||
pv=False
|
||||
logging.debug("Running pygrub against %s", dest)
|
||||
output = os.popen('pygrub -qn %s' % dest)
|
||||
pv = False
|
||||
for line in output.readlines():
|
||||
#try to find kernel string
|
||||
m=re.search('(?<=kernel:)/.*(?:>)',line)
|
||||
m = re.search('(?<=kernel:)/.*(?:>)', line)
|
||||
if m:
|
||||
if m.group(0).find('xen')!=-1:
|
||||
pv=True
|
||||
logging.debug("PV:%d",pv)
|
||||
return pv
|
||||
|
||||
if m.group(0).find('xen') != -1:
|
||||
pv = True
|
||||
logging.debug("PV:%d", pv)
|
||||
return pv
|
||||
|
||||
|
||||
def get_vdi(session, args):
|
||||
src_url = exists(args, 'src_url')
|
||||
username = exists(args, 'username')
|
||||
password = exists(args, 'password')
|
||||
raw_image=validate_bool(args, 'raw', 'false')
|
||||
raw_image = validate_bool(args, 'raw', 'false')
|
||||
add_partition = validate_bool(args, 'add_partition', 'false')
|
||||
(proto, netloc, url_path, _, _, _) = urlparse.urlparse(src_url)
|
||||
sr = find_sr(session)
|
||||
@ -88,16 +91,17 @@ def get_vdi(session, args):
|
||||
vdi = create_vdi(session, sr, src_url, vdi_size, False)
|
||||
with_vdi_in_dom0(session, vdi, False,
|
||||
lambda dev: get_vdi_(proto, netloc, url_path,
|
||||
username, password, add_partition,raw_image,
|
||||
username, password,
|
||||
add_partition, raw_image,
|
||||
virtual_size, '/dev/%s' % dev))
|
||||
return session.xenapi.VDI.get_uuid(vdi)
|
||||
|
||||
|
||||
def get_vdi_(proto, netloc, url_path, username, password, add_partition,raw_image,
|
||||
virtual_size, dest):
|
||||
def get_vdi_(proto, netloc, url_path, username, password,
|
||||
add_partition, raw_image, virtual_size, dest):
|
||||
|
||||
#Salvatore: vdi should not be partitioned for raw images
|
||||
if (add_partition and not raw_image):
|
||||
#vdi should not be partitioned for raw images
|
||||
if add_partition and not raw_image:
|
||||
write_partition(virtual_size, dest)
|
||||
|
||||
offset = (add_partition and not raw_image and MBR_SIZE_BYTES) or 0
|
||||
@ -144,7 +148,7 @@ def get_kernel(session, args):
|
||||
password = exists(args, 'password')
|
||||
|
||||
(proto, netloc, url_path, _, _, _) = urlparse.urlparse(src_url)
|
||||
|
||||
|
||||
dest = os.path.join(KERNEL_DIR, url_path[1:])
|
||||
|
||||
# Paranoid check against people using ../ to do rude things.
|
||||
@ -154,8 +158,8 @@ def get_kernel(session, args):
|
||||
dirname = os.path.dirname(dest)
|
||||
try:
|
||||
os.makedirs(dirname)
|
||||
except os.error, e:
|
||||
if e.errno != errno.EEXIST:
|
||||
except os.error, e:
|
||||
if e.errno != errno.EEXIST:
|
||||
raise
|
||||
if not os.path.isdir(dirname):
|
||||
raise Exception('Cannot make directory %s', dirname)
|
||||
@ -248,5 +252,5 @@ def download_all(response, length, dest_file, offset):
|
||||
|
||||
if __name__ == '__main__':
|
||||
XenAPIPlugin.dispatch({'get_vdi': get_vdi,
|
||||
'get_kernel': get_kernel,
|
||||
'get_kernel': get_kernel,
|
||||
'is_vdi_pv': is_vdi_pv})
|
||||
|
Loading…
x
Reference in New Issue
Block a user