def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device): """ Downloads image depending on the \c data parameter. @cmview_admin_cm @parameter{description,string} @parameter{name,string} @parameter{path,string} HTTP or FTP path to image to download @parameter{type,image_types} type of image, automatically set, type is in the URL requested @response{None} """ # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception(caller_id, 'Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user = User.get(caller_id) image = SystemImage.create(name=name, description=description, user=user, platform=platform, disk_controller=disk_controller, network_device=network_device, video_device=video_device) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def add(caller_id, address, username, transport, driver, suffix, cpu, memory, disk): """ Adds new Node to this Cluster. Node must be a machine preconfigured to be CC1 node. @cmview_admin_cm @param_post{username,string} Node's operating system username for transport. Should be @val{cc1}. @param_post{address,string} added Node IP adress or domain name @param_post{transport,string} @val{unix}, @val{ssh}, @val{tls} or other available transport name for hypervisor @param_post{suffix,string} optional suffix for transport (i.e. /system for KVM) @param_post{driver,string} hypervisior name (XEN, KVM or other, KVM is recommended) @param_post{cpu,int} @param_post{memory,int} @param_post{disk,int} @note Not used (but might be used one day) """ try: node_tools.add(address, username, transport, driver, suffix, cpu, memory, disk) except Exception, e: log.error(caller_id, 'Cannot add node: %s' % str(e)) raise CMException(str(e))
def download(caller_id, description, name, path, disk_dev, disk_controller): """ Downloads specified StorateImage from remote path. @cmview_admin_cm @param_post{description,string} @param_post{name,string} how to name newly downloaded storage image @param_post{path,string} HTTP or FTP path to download StorageImage. @param_post{disk_dev} @param_post{disk_controller} """ # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception('Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user = User.get(caller_id) image = StorageImage.create(name=name, description=description, user=user, disk_dev=disk_dev, disk_controller=disk_controller) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def update(self): """ Update rrd file, if exists. Otherwise create new rrd """ if not self.vm: raise Exception('No VM specified') try: filesize = os.path.getsize(self.filepath) except Exception: filesize = 0 if (filesize == 0): self.create() else: # appropriate updating ret = rrdtool.update( "%s" % (self.filepath), 'N:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d' % ( int(self.vm['cpu_count']), int(self.vm['cpu_time']) / 100000000 / 10.0 / self.vm['cpu_count'], int(self.vm['rd_req']), int(self.vm['rd_bytes']), int(self.vm['wr_req']), int(self.vm['wr_bytes']), int(self.vm['rx_bytes']), int(self.vm['rx_packets']), int(self.vm['tx_bytes']), int(self.vm['tx_packets']), )) if ret: log.error(0, 'update error: %s' % (rrdtool.error()))
def create(caller_id, name, description, filesystem, size, disk_controller): """ Method creates new Image. It's only used by disk_volume type (only url view with create) @cmview_user @parameter{name,string} @parameter{description,string} @parameter{filesystem,int} id of the filesystem. Supported filesystems are listed in settings @parameter{size,int} size of the Image to create [MB] @parameter{disk_controller} @response{dict} Image's dictionary """ if size < 1: raise CMException('image_invalid_size') user = User.get(caller_id) user.check_storage(size) image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name, size=size) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def assign(self, lease): if lease.vm == None: raise CMException('lease_not_attached') self.lease = lease self.save() log.debug( 0, "Attaching ip with comand: %s" % str([ 'ssh', '-i', '/var/lib/cc1/.ssh/id_rsa', '%s@%s' % (lease.vm.node.username, lease.vm.node.address), 'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address) ])) p = subprocess.Popen([ 'ssh', '-i', '/var/lib/cc1/.ssh/id_rsa', '%s@%s' % (lease.vm.node.username, lease.vm.node.address), 'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address) ], stdout=subprocess.PIPE) p.wait() log.debug(self.user.id, p.stdout.read()) if p.returncode != 0: log.error(self.user.id, "SSH error: %d" % p.returncode) raise CMException('public_ip_failed')
def cpu_load(self): res = ['60', '300', '900'] try: return RrdHandler().cpu_load('vm-%d-%d' % (self.id, self.user_id), res) except CMException, e: log.error(self.user_id, 'cpu_load: %s' % str(e)) return dict([(x, '') for x in res])
def hello(vm_ip, **args): """ First function which must be called by VMs ctx module. It registers VM with status 'running ctx', also serves a special role when creating farms (tracking head, and worker nodes) @parameter{vm_ip,string} @parameter{args} """ vm = VM.get_by_ip(vm_ip) log.debug(vm.user_id, "Hello from vm %d ip: %s" % (vm.id, vm_ip)) vm.ctx_api_version = args.get('version', None) vm.state = vm_states['running ctx'] if vm.ssh_username and vm.ssh_key: Command.execute('add_ssh_key', vm.user_id, vm.id, user=vm.ssh_username, ssh_key=vm.ssh_key) if vm.is_head(): Command.register_head(vm) elif vm.is_farm(): Command.register_node(vm) try: vm.save(update_fields=['state', 'ctx_api_version']) except Exception, e: log.error( vm.user_id, "Cannot update database for vm %d: %s" % (vm.id, e.message)) return response('ctx_error', "Cannot update database: %s" % e.message)
def update(self): """ Update rrd file, if exists. Otherwise create new rrd """ if not self.vm: raise Exception('No VM specified') try: filesize = os.path.getsize(self.filepath) except Exception: filesize = 0 if(filesize == 0): self.create() else: # appropriate updating ret = rrdtool.update("%s" % (self.filepath), 'N:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d' % (int(self.vm['cpu_count']), int(self.vm['cpu_time']) / 100000000 / 10.0 / self.vm['cpu_count'], int(self.vm['rd_req']), int(self.vm['rd_bytes']), int(self.vm['wr_req']), int(self.vm['wr_bytes']), int(self.vm['rx_bytes']), int(self.vm['rx_packets']), int(self.vm['tx_bytes']), int(self.vm['tx_packets']), )) if ret: log.error(0, 'update error: %s' % (rrdtool.error()))
def assign(self, lease): if lease.vm == None: raise CMException('lease_not_attached') self.lease = lease self.save() log.debug(0, "Attaching ip with comand: %s" % str(['ssh', '-i', '/var/lib/cc1/.ssh/id_rsa', '%s@%s' % (lease.vm.node.username, lease.vm.node.address), 'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address)])) p = subprocess.Popen(['ssh', '-i', '/var/lib/cc1/.ssh/id_rsa', '%s@%s' % (lease.vm.node.username, lease.vm.node.address), 'sudo /usr/sbin/cc1_node_public_ip attach %d %s %s' % (lease.vm.id, lease.vm_address, self.address)], stdout=subprocess.PIPE) p.wait() log.debug(self.user.id, p.stdout.read()) if p.returncode != 0: log.error(self.user.id, "SSH error: %d" % p.returncode) raise CMException('public_ip_failed')
def create(caller_id, name, description, filesystem, size, disk_controller): """ Creates new StorageImage. @cmview_user @param_post{name,string} @param_post{description,string} @param_post{filesystem,int} id of the filesystem. Supported filesystems are common.hardware.disk_filesystems @param_post{size,int} size of the SystemImage to create [MB] @param_post{disk_controller} @response{dict} StorageImage.dict property of newly created StorageImage """ if size < 1: raise CMException('image_invalid_size') user = User.get(caller_id) user.check_storage(size) image = StorageImage.create(user=user, disk_controller=disk_controller, description=description, name=name, size=size) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def download(caller_id, name, description, path, disk_controller): """ Downloads specified IsoImage and saves it with specified name and description. @cmview_user @param_post{name,string} @param_post{description,string} @param_post{path,string} HTTP or FTP path to IsoImage to download @param_post{disk_controller} """ user = User.get(caller_id) if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]): path = 'http://' + path.strip() # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception('Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user.check_storage(size / (1024 * 1024)) image = IsoImage.create(user=user, description=description, name=name, disk_controller=disk_controller, disk_dev=1) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def create(self): if not self.vm: raise Exception('No VM specified') rarg = [ "%s" % (self.filepath), "--step", "%d" % settings.PERIOD, "DS:cpu_count:GAUGE:%d:0:100000" % (settings.PERIOD * 2), "DS:cpu_time:COUNTER:%d:0:100000" % (settings.PERIOD * 2), "DS:rd_req:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:rd_bytes:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:wr_req:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:wr_bytes:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:rx_bytes:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:rx_packets:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:tx_bytes:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), "DS:tx_packets:COUNTER:%d:0:100000000" % (settings.PERIOD * 2), ] for s in settings.STATS: rarg.append("RRA:AVERAGE:0.5:%d:%d" % (s[0], s[1])) try: ret = rrdtool.create(rarg) # all data = 3,1MB if ret: log.error(0, 'update error: %s' % (rrdtool.error())) except Exception, e: log.exception(0, e)
def hello(vm_ip, **args): """ First function which must be called by VMs ctx module. It registers VM with status 'running ctx', also serves a special role when creating farms (tracking head, and worker nodes) @parameter{vm_ip,string} @parameter{args} """ vm = VM.get_by_ip(vm_ip) log.debug(vm.user_id, "Hello from vm %d ip: %s" % (vm.id, vm_ip)) vm.ctx_api_version = args.get('version', None) vm.state = vm_states['running ctx'] if vm.ssh_username and vm.ssh_key: Command.execute('add_ssh_key', vm.user_id, vm.id, user=vm.ssh_username, ssh_key=vm.ssh_key) if vm.is_head(): Command.register_head(vm) elif vm.is_farm(): Command.register_node(vm) try: vm.save(update_fields=['state', 'ctx_api_version']) except Exception, e: log.error(vm.user_id, "Cannot update database for vm %d: %s" % (vm.id, e.message)) return response('ctx_error', "Cannot update database: %s" % e.message)
def execute(command_list): try: log.debug(0, "Execute command: %s" % str(command_list)) log_file = file('/var/log/cc1/cm_thread.log', 'a') r = subprocess.call(command_list, stdout=log_file, stderr=log_file) log_file.close() except Exception, e: log.error(0, "Execute command %s failed: %s" % (str(command_list), e))
def exec_cmd(self, args): p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) retr_std = p.stdout.read() ret = p.wait() if ret: retr_err = str(p.stderr.read()) log.error(self.image.user.id, retr_err) log.error(self.image.user.id, retr_std) return retr_err
def run(self): while self.running: try: one = self.rb.get() if not one['address'] in [i.name for i in threading.enumerate()]: t = MonitorThread(one) t.start() except Exception, e: log.error(0, 'Monitoring error %s: %s' % (one['address'], e)) time.sleep(self.frequency)
def read_node(self): used_cpu = 0 used_memory = 0 try: self.c = libvirt.openReadOnly(self.addr) total_cpu = self.c.getInfo()[2] total_memory = self.c.getInfo()[1] except Exception, e: log.error(0, 'libvirt getting info: %s' % (e)) return None
def install(caller_id, node_id, distribution): """ @parameter{node_id,int} node id @parameter{distribution,string} distribution name, e.g. debian """ try: node_tools.install(node_id, distribution) except Exception, e: log.error(caller_id, 'Cannot install node: %s' % str(e)) raise CMException(str(e))
def disk_controller_name(self): """ Method filters DISK_CONTROLLERS list to find controller name by the disk_controller id with is assigned to this Image. @returns{string} name of this Image's disk controller, if such a controller exists """ try: return disk_controllers_reversed[self.disk_controller] except Exception: log.error(self.user.id, 'Cannot find disk controller')
def network_device_name(self): """ Method filters NETWORK_DEVICES list to find network device name by the network_device id with is assigned to this Image. @returns{string} name of this Image's network device, if such a network device exists. """ try: return network_devices_reversed[self.network_device] except Exception: log.error(self.user.id, 'Cannot find network device')
def execute_with_output(command_list): try: log.debug(0, "Execute command (with output): %s" % str(command_list)) p = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE) r = p.communicate() except Exception, e: log.error( 0, "Execute command (with output) %s failed: %s" % (str(command_list), e))
def configure(caller_id, node_id, interfaces): """ @parameter{node_id,int} node id @parameter{interfaces,string list} list of interfaces, which node should use to communicate with other nodes and cm. """ try: node_tools.configure(node_id, interfaces) except Exception, e: log.error(caller_id, 'Cannot configure node: %s' % str(e)) raise CMException(str(e))
def video_device_name(self): """ Method filters VIDEO_DEVICES list to find video device name by the video_device id with is assigned to this Image. @returns{string} name of this Image's the video device, if such a video device exists. otherwise 'vga' """ try: return video_devices_reversed[self.video_device] except Exception: log.error(self.user.id, 'Cannot find video device') return 'vga'
def video_device_name(self): """ Method filters VIDEO_DEVICES list to find video device name by the video_device id that is assigned to this Image. @returns{string} name of this Image's the video device, if such a video device exists. otherwise 'vga' """ try: return video_devices_reversed[self.video_device] except Exception: log.error(self.user.id, 'Cannot find video device') return 'vga'
def run(self): while self.running: try: one = self.rb.get() if not one['address'] in [ i.name for i in threading.enumerate() ]: t = MonitorThread(one) t.start() except Exception, e: log.error(0, 'Monitoring error %s: %s' % (one['address'], e)) time.sleep(self.frequency)
def install(caller_id, node_id, distribution): """ @cmview_admin_cm @param_post{node_id,int} id of the Node where cc1-node should be deployed @param_post{distribution,string} OS distribution name, e.g. Debian @note Not used (but might be used one day) """ try: node_tools.install(node_id, distribution) except Exception, e: log.error(caller_id, 'Cannot install node: %s' % str(e)) raise CMException(str(e))
def configure(caller_id, node_id, interfaces): """ @cmview_admin_cm @param_post{node_id,int} node id @param_post{interfaces,string list} list of interfaces, which node should use to communicate with other nodes and cm. @note Not used (but might be used one day) """ try: node_tools.configure(node_id, interfaces) except Exception, e: log.error(caller_id, 'Cannot configure node: %s' % str(e)) raise CMException(str(e))
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device): """ Downloads image depending on the \c data parameter. @cmview_user @parameter{path,string} HTTP or FTP path to image to download @parameter{name,string} @parameter{description,string} @parameter{type,image_types} type of image, automatically set, type is in the URL requested @response{None} @raises{image_not_found,CMException} @raises{image_create,CMException} """ user = User.get(caller_id) if not any([path.startswith("http://"), path.startswith("https://"), path.startswith("ftp://")]): path = "http://" + path.strip() # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception(caller_id, "Cannot find image") raise CMException("image_not_found") except KeyError: log.exception(caller_id, "Cannot calculate size") raise CMException("image_calculate_size") user = User.get(caller_id) user.check_storage(size / (1024 * 1024)) image = SystemImage.create( name=name, description=description, user=user, platform=platform, disk_controller=disk_controller, network_device=network_device, video_device=video_device, ) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException("image_create")
def check(caller_id, node_id_list): """ Tries to restart cc1-node service on each specified Node @cmview_admin_cm @param_post{node_id_list,list(int)} @note Not used (but might be used one day) """ try: for node_id in node_id_list: node_tools.check(node_id) except Exception, e: log.error(caller_id, 'Cannot check node: %s' % str(e)) raise CMException(str(e))
def get_list(self): """ list vm stats with start and end times """ f = os.listdir(settings.PATH_TO_RRD) rrds = {} for rrd in f: try: t = [] t.append(rrdtool.first(settings.PATH_TO_RRD + rrd)) t.append(rrdtool.last(settings.PATH_TO_RRD + rrd)) rrds.update({os.path.splitext(rrd)[0]: t}) except Exception, e: log.error(0, 'stat_error %s %s' % (rrd, e))
def copy(caller_id, src_image_id, dest_user_id): """ Copy selected StorageImage to user's StorageImages @cmview_admin_cm @param_post{src_image_id,int} @param_post{dest_user_id,int} """ src_image = StorageImage.admin_get(src_image_id) dest_user = User.get(dest_user_id) dest_image = StorageImage.create(name=src_image.name, description=src_image.description, user=dest_user, disk_controller=src_image.disk_controller, size=src_image.size) try: dest_image.save() except Exception, e: log.error(caller_id, "Unable to commit: %s" % str(e)) raise CMException('image_create')
class DownloadImage(threading.Thread): image = None url = None size = 0 def __init__(self, image, url, size): threading.Thread.__init__(self) self.image = image self.url = url self.size = size def run(self): try: if self.url.startswith('/'): src_image = open(self.url, 'r') else: src_image = urllib2.urlopen(self.url) except Exception, e: log.exception(self.image.user.id, "Cannot open url %s: %s" % (self.url, str(e))) self.image.state = image_states['failed'] return if os.path.exists(self.image.path): self.image.state = image_states['failed'] self.image.save(update_fields=['state']) log.error(self.image.user.id, "Destination image %d for user %d exists! Aborting download" % (self.image.id, self.image.user.id)) return try: dirpath = os.path.dirname(self.image.path) if not os.path.exists(dirpath): os.mkdir(dirpath) dest_image = open(self.image.path, 'w') downloaded_size = 0 md5sum = hashlib.md5() while downloaded_size < self.size: buff = src_image.read(1024 * 1024) md5sum.update(buff) downloaded_size += len(buff) dest_image.write(buff) progress = int(downloaded_size * 100 / self.size) if progress != self.image.progress: self.image.progress = progress self.image.save(update_fields=['progress']) dest_image.close() log.info(self.image.user.id, 'md5 hash of image %d is %s' % (self.image.id, md5sum.hexdigest())) self.image.state = image_states['ok'] self.image.size = downloaded_size / (1024 * 1024) self.image.save(update_fields=['progress', 'state', 'size']) message.info(self.image.user.id, 'image_downloaded', {'name': self.image.name, 'md5sum': md5sum.hexdigest()}) except Exception, e: log.exception(self.image.user.id, "Failed to download image: %s" % str(e)) self.image.state = image_states['failed'] self.image.save(update_fields=['state'])
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device): """ Downloads image depending on the \c data parameter. @cmview_user @parameter{path,string} HTTP or FTP path to image to download @parameter{name,string} @parameter{description,string} @parameter{type,image_types} type of image, automatically set, type is in the URL requested @response{None} @raises{image_not_found,CMException} @raises{image_create,CMException} """ user = User.get(caller_id) if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]): path = 'http://' + path.strip() # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception(caller_id, 'Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user = User.get(caller_id) user.check_storage(size / (1024 * 1024)) image = SystemImage.create(name=name, description=description, user=user, platform=platform, disk_controller=disk_controller, network_device=network_device, video_device=video_device) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def copy(caller_id, src_image_id, dest_user_id): """ Copy selected image to user's images @cmview_admin_cm @param_post{src_image_id,int} @param_post{dest_user_id,int} """ src_image = SystemImage.admin_get(src_image_id) dest_user = User.get(dest_user_id) dest_image = SystemImage.create(name=src_image.name, description=src_image.description, user=dest_user, platform=src_image.platform, disk_controller=src_image.disk_controller, network_device=src_image.network_device, video_device=src_image.video_device) try: dest_image.save() except Exception, e: log.error(caller_id, "Unable to commit: %s" % str(e)) raise CMException('image_create')
def download(caller_id, name, description, path, disk_controller): """ Downloads specified IsoImage and saves it with specified name and description. @cmview_user @param_post{name,string} @param_post{description,string} @param_post{path,string} HTTP or FTP path to IsoImage to download @param_post{disk_controller} """ user = User.get(caller_id) if not any([ path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://') ]): path = 'http://' + path.strip() # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception('Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user.check_storage(size / (1024 * 1024)) image = IsoImage.create(user=user, description=description, name=name, disk_controller=disk_controller, disk_dev=1) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def copy(caller_id, src_image_id, dest_user_id): """ Copy selected image to user's images @cmview_admin_cm @parameter{src_id,int} @parameter{dest_id,int} @parameter{img_type} """ src_image = IsoImage.admin_get(src_image_id) dest_user = User.get(dest_user_id) dest_image = IsoImage.create(name=src_image.name, description=src_image.description, user=dest_user, disk_controller=src_image.disk_controller, disk_dev=src_image.disk_dev) try: dest_image.save() except Exception, e: log.error(caller_id, "Unable to commit: %s" % str(e)) raise CMException('image_create')
def add(caller_id, address, username, transport, driver, suffix, cpu, memory, disk): """ Method adds new Node to the Cluster. Node must be machine configured as CC1 node. @cmview_admin_cm @parameter{username,string} cc1 user account on node. Should be cc1 @parameter{address,string} node ip adress or domain name @parameter{transport,string} unix, ssh, tls or other available transport name for kvm @parameter{driver,string} XEN, KVM or other hypervisior name @parameter{cpu,int} @parameter{memory,int} @parameter{disk,int} @parameter{suffix,string} optional suffix for transport (i.e. /system for KVM) """ try: node_tools.add(address, username, transport, driver, suffix, cpu, memory, disk) except Exception, e: log.error(caller_id, 'Cannot add node: %s' % str(e)) raise CMException(str(e))
def download(caller_id, description, name, path, disk_controller, network_device, platform, video_device): """ Downloads specified SystemImage. @cmview_admin_cm @param_post{description,string} @param_post{name,string} @param_post{path,string} HTTP or FTP path to image to download @param_post{disk_controller} @param_post{network_device} @param_post{platform} @param_post{video_device} """ # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception(caller_id, 'Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user = User.get(caller_id) image = SystemImage.create(name=name, description=description, user=user, platform=platform, disk_controller=disk_controller, network_device=network_device, video_device=video_device) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def run(self): if os.path.exists(self.image.path): self.image.state = image_states['failed'] self.image.save(update_fields=['state']) log.error(self.image.user.id, "Destination image %d for user %d exists! Aborting creation" % (self.image.id, self.image.user.id)) return self.image.progress = 0 if self.format() == 'failed': self.image.state = image_states['failed'] self.image.save(update_fields=['state']) else: self.image.progress = 100 self.image.state = image_states['ok'] self.image.save(update_fields=['state', 'progress']) log.debug(self.image.user.id, 'stage [6/6] cleaning..') try: os.remove('%s' % os.path.join('/var/lib/cc1/images-tmp/', os.path.split(self.image.path)[1])) except Exception, e: log.error(self.image.user.id, 'error remove file: %s' % str(e))
def erase(vm): """ Remove all after-effects of the failed vm and free the resources. """ vm.save_vm = 0 vm.set_state('erasing') try: vm.save() transaction.commit() except: log.error(vm.user.id, 'Cannot set save=0') conn = libvirt.open(vm.node.conn_string) try: domain = conn.lookupByID(vm.libvirt_id) domain.destroy() except Exception, e: log.error( vm.user.id, "Cannot find libvirt domain (by ID): %d (%s)" % (vm.libvirt_id, str(e))) try: domain = conn.lookupByName("vm-%d-%d" % (vm.id, vm.user.id)) domain.destroy() except Exception, e: log.error( vm.user.id, "Cannot find libvirt domain (by name): %d (%s)" % (vm.libvirt_id, str(e)))
def download(caller_id, name, description, path, disk_controller): """ Downloads image depending on the \c data parameter. @cmview_user @parameter{name,string} @parameter{description,string} @parameter{path,string} HTTP or FTP path to image to download @parameter{disk_controller} @response{None} """ user = User.get(caller_id) if path.startswith('/'): size = os.path.getsize(path.strip()) else: if not any([path.startswith('http://'), path.startswith('https://'), path.startswith('ftp://')]): path = 'http://' + path.strip() # size value is taken try: connection = urllib.urlopen(path) size = int(connection.info()["Content-Length"]) except IOError: log.exception(caller_id, 'Cannot find image') raise CMException('image_not_found') except KeyError: log.exception(caller_id, 'Cannot calculate size') raise CMException('image_calculate_size') user.check_storage(size / (1024 * 1024)) image = StorageImage.create(name=name, description=description, user=user, disk_controller=disk_controller) try: image.save() except Exception, e: log.error(caller_id, "Unable to save image to DB: %s" % str(e)) raise CMException('image_create')
def get_list(remote_ip): """ @cmview_ci @param_post{remote_ip,string} """ try: node = Node.objects.get(address=remote_ip) except: log.error(0, 'Cannot find node: %s' % remote_ip) raise CMException('node_not_found') vms = node.vm_set.filter(state__in=[vm_states['running'], vm_states['running ctx'], vm_states['init']]).all() public_leases = [] for vm in vms: for lease in vm.lease_set.all(): if lease.publicip_set.count() != 0: d = {} d['vm_id'] = vm.id d['private_lease'] = lease.vm_address d['public_lease'] = lease.publicip_set.all()[0].address public_leases.append(d) return public_leases
def get_list(remote_ip): """ @cmview_ci @param_post{remote_ip,string} """ try: node = Node.objects.get(address=remote_ip) except: log.error(0, 'Cannot find node: %s' % remote_ip) raise CMException('node_not_found') vms = node.vm_set.filter(state__in=[ vm_states['running'], vm_states['running ctx'], vm_states['init'] ]).all() public_leases = [] for vm in vms: for lease in vm.lease_set.all(): if lease.publicip_set.count() != 0: d = {} d['vm_id'] = vm.id d['private_lease'] = lease.vm_address d['public_lease'] = lease.publicip_set.all()[0].address public_leases.append(d) return public_leases
'memory': info[2], 'rd_req': hdd_stat[0], 'rd_bytes': hdd_stat[1], 'wr_req': hdd_stat[2], 'wr_bytes': hdd_stat[3], 'rx_bytes': net_stat[0], 'rx_packets': net_stat[1], 'tx_bytes': net_stat[4], 'tx_packets': net_stat[5] }) except Exception, e: log.exception( 0, 'libvirt lookup (%s id=%d): %s' % (hostname, domain_id, str(e))) return None dom = None g = self.c.close() if g != 0: log.error(0, 'libvirt close error %s' % (str(g))) self.lv_data = [used_cpu, used_memory, total_cpu, total_memory, vms] return self.lv_data def kill(self): log.info(0, 'killing MonitorThread...') try: sys.exit() except Exception: log.info(0, 'monitorThread error...') log.info(0, 'MonitorThread killed')