def edit(caller_id, system_image_id, name, description, disk_controller, video_device, network_device, platform): """ Updates attributes of the specified SystemImage. @cmview_user @param_post{system_image_id,int} id of the SystemImage to edit @param_post{name,string} SystemImage new name (optional) @param_post{description,string} SystemImage new description (optional) @param_post{disk_controller} SystemImage new controller (optional) @param_post{video_device} SystemImage new video device (optional) @param_post{network_device} SystemImage new network device (optional) @param_post{platform} (optional) """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states["ok"]: raise CMException("image_edit") image.name = name image.description = description image.disk_controller = disk_controller image.video_device = video_device image.network_device = network_device image.platform = platform try: image.save() except: raise CMException("image_edit")
def edit(caller_id, system_image_id, name, description, disk_controller, video_device, network_device, platform): """ Updates attributes of the specified SystemImage. @cmview_user @param_post{system_image_id,int} id of the SystemImage to edit @param_post{name,string} SystemImage new name (optional) @param_post{description,string} SystemImage new description (optional) @param_post{disk_controller} SystemImage new controller (optional) @param_post{video_device} SystemImage new video device (optional) @param_post{network_device} SystemImage new network device (optional) @param_post{platform} (optional) """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states['ok']: raise CMException('image_edit') image.name = name image.description = description image.disk_controller = disk_controller image.video_device = video_device image.network_device = network_device image.platform = platform try: image.save() except: raise CMException('image_edit')
def edit(caller_id, system_image_id, name, description, disk_controller, video_device, network_device, platform): """ Sets Image's new attributes. Those should be get by src.cm.manager.image.get_by_id(). @cmview_user @parameter{system_image_id,int} id of the Image to edit @parameter{name,string} new Image name @parameter{description,string} new Image description @parameter{disk_controller} new Image controller optional @parameter{video_device} new video device optional @parameter{network_device} new network device optional @parameter{platform} optional """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states['ok']: raise CMException('image_edit') image.name = name image.description = description image.disk_controller = disk_controller image.video_device = video_device image.network_device = network_device image.platform = platform try: image.save() except: raise CMException('image_edit')
def get_by_id(caller_id, system_image_id, groups): """ @cmview_user @param_post{groups,list(int)} list of Groups ids, required for @val{group} access @param_post{system_image_id,int} id of the requested Image @response{dict} SystemImage.dict property of the requested SystemImage """ return SystemImage.get(caller_id, system_image_id, groups).dict
def get_by_id(caller_id, system_image_id, groups): """ @cmview_user @parameter{groups,list(int)} list of Group ids necessary when access is group, for system and cd images @parameter{system_image_id,int} id of the Image to get @response{dict} extended information about specified Image """ return SystemImage.get(caller_id, system_image_id, groups).dict
def convert_to_storage_image(caller_id, system_image_id): """ Changes type of the given Image. @cmview_user @param_post{system_image_id,int} ID of an Image to change type of """ image = SystemImage.get(caller_id, system_image_id) try: image.recast("cm.storageimage") image.save() except Exception: log.exception(caller_id, "convert_to_storage_image") raise CMException("image_change_type")
def convert_to_storage_image(caller_id, system_image_id): """ Changes type of the given Image. @cmview_user @param_post{system_image_id,int} ID of an Image to change type of """ image = SystemImage.get(caller_id, system_image_id) try: image.recast('cm.storageimage') image.save() except Exception: log.exception(caller_id, "convert_to_storage_image") raise CMException('image_change_type')
def delete(caller_id, system_image_id): """ Deletes specified SystemImage @cmview_user @param_post{system_image_ids,list(int)} id of the SystemImage to delete """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states["ok"]: raise CMException("image_delete") try: subprocess.call(["rm", image.path]) except Exception, e: raise CMException("image_delete")
def convert_to_storage_image(caller_id, system_image_id): """ Changes type of the given Image. @cmview_user @parameter{system_image_id,int} ID of an Image to change type of @response{None} """ image = SystemImage.get(caller_id, system_image_id) try: image.recast('cm.storageimage') image.save() except Exception: log.exception(caller_id, "convert_to_storage_image") raise CMException('image_change_type')
def delete(caller_id, system_image_id): """ Deletes specified SystemImage @cmview_user @param_post{system_image_ids,list(int)} id of the SystemImage to delete """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states['ok']: raise CMException('image_delete') try: subprocess.call(['rm', image.path]) except Exception, e: raise CMException('image_delete')
def delete(caller_id, system_image_id): """ Deletes given Image @cmview_user @parameter{system_image_id} id of the Image to delete """ image = SystemImage.get(caller_id, system_image_id) if image.state != image_states['ok']: raise CMException('image_delete') try: subprocess.call(['rm', image.path]) except Exception, e: raise CMException('image_delete')
def set_private(caller_id, system_image_id, leader_groups): """ Marks SystemImage as @val{private}. The caller needs to be: - either the <b>owner of the SystemImage</b> - or the <b>leader of the group</b> to which SystemImage belongs @cmview_user @param_post{system_image_id,int} id of the Image to set private @param_post{leader_groups,list(int)} ids of the group where the caller is leader, requierd if Image's access type is group """ image = SystemImage.get(caller_id, system_image_id, leader_groups) image.access = image_access["private"] # delete the existing group association try: image.save() image.systemimagegroup_set.all().delete() except: log.exception(caller_id, "image_set_private") raise CMException("image_set_private")
def set_private(caller_id, system_image_id, leader_groups): """ Marks SystemImage as @val{private}. The caller needs to be: - either the <b>owner of the SystemImage</b> - or the <b>leader of the group</b> to which SystemImage belongs @cmview_user @param_post{system_image_id,int} id of the Image to set private @param_post{leader_groups,list(int)} ids of the group where the caller is leader, requierd if Image's access type is group """ image = SystemImage.get(caller_id, system_image_id, leader_groups) image.access = image_access['private'] # delete the existing group association try: image.save() image.systemimagegroup_set.all().delete() except: log.exception(caller_id, 'image_set_private') raise CMException('image_set_private')
def set_private(caller_id, system_image_id, leader_groups): """ Sets Image as private, only for system and cd image To succeed it requires caller to be: - either the <b>owner of the image</b> - or the <b>leader of the group</b> to which Image belongs @cmview_user @parameter{system_image_id,int} id of the Image to set private @parameter{leader_groups,list(int)} ids of the group where the caller is leader, requierd if Image's access type is group """ image = SystemImage.get(caller_id, system_image_id, leader_groups) image.access = image_access['private'] # delete the existing group association try: image.save() image.systemimagegroup_set.all().delete() except: log.exception(caller_id, 'image_set_private') raise CMException('image_set_private')
def set_group(caller_id, system_image_id, group_id): """ Method sets specified Image access type as group (belonging to specified Group) (only for sys and cd images - from private or public to group) @cmview_user @param_post{group_id,int} id of the Group Image should belong to @param_post{system_image_id,int} @response{None} """ image = SystemImage.get(caller_id, system_image_id) image.access = image_access["group"] # create new group-image object ig = SystemImageGroup() ig.image = image ig.group_id = group_id try: ig.save() image.save() except: raise CMException("image_set_group")
def set_group(caller_id, system_image_id, group_id): """ Method sets specified Image access type as group (belonging to specified Group) (only for sys and cd images - from private or public to group) @cmview_user @param_post{group_id,int} id of the Group Image should belong to @param_post{system_image_id,int} @response{None} """ image = SystemImage.get(caller_id, system_image_id) image.access = image_access['group'] # create new group-image object ig = SystemImageGroup() ig.image = image ig.group_id = group_id try: ig.save() image.save() except: raise CMException('image_set_group')
def create(user, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, ssh_key=None, ssh_username=None, count=1, farm=None, head_template_id=None, node_id=False, lease_id=None, user_data=None): from cm.models.storage_image import StorageImage from cm.utils.threads.vm import VMThread template = Template.get(template_id) image = SystemImage.get(user.id, image_id, groups) if image.state != image_states['ok']: raise CMException('image_unavailable') if farm: head_template = Template.get(head_template_id) wn_template = template user.check_quota([(head_template, 1), (wn_template, count)]) count += 1 else: user.check_quota([(template, count)]) vms = [] reservation_id = None for i in range(count): # create VM instance log.debug(user.id, "Looking for node") node = Node.get_free_node(head_template, image, node_id) if farm and i == 0 else Node.get_free_node(template, image, node_id) log.info(user.id, 'Selected node: %d' % node.id) vm = VM() vm.libvirt_id = -1 if farm: if i == 0: vm.name = '%s-head' % name vm.description = 'Farm head' vm.template = head_template else: vm.name = '%s-wn%d' % (name, i) vm.description = 'Worker Node' vm.template = wn_template else: vm.template = template vm.description = description if count > 1: vm.name = '%s_%d' % (name, i + 1) else: vm.name = name vm.user = user vm.state = vm_states['init'] vm.start_time = datetime.now() vm.system_image = image vm.node = node vm.save_vm = True if farm: vm.farm = farm # Find first free vnc port used_ports = VM.objects.exclude(state__in=[vm_states['closed'], vm_states['erased']]).values_list('vnc_port', flat=True) for new_vnc_port in xrange(VNC_PORTS['START'], VNC_PORTS['END'] + 1): if new_vnc_port not in used_ports and new_vnc_port not in VNC_PORTS['EXCLUDE']: break else: raise CMException('vm_vnc_not_found') log.debug(user.id, "Found vnc port: %d" % new_vnc_port) vm.vnc_port = new_vnc_port # Find first free novnc port used_ports = VM.objects.exclude(state__in=[vm_states['closed'], vm_states['erased']]).values_list('novnc_port', flat=True) for new_novnc_port in xrange(NOVNC_PORTS['START'], NOVNC_PORTS['END'] + 1): if new_novnc_port not in used_ports and new_novnc_port not in NOVNC_PORTS['EXCLUDE']: break else: raise CMException('vm_novnc_not_found') log.debug(user.id, "Found novnc port: %d" % new_novnc_port) vm.novnc_port = new_novnc_port if vnc: vm.attach_vnc() vm.vnc_passwd = password_gen(13, chars=['letters', 'digits'], extra_chars='!@#$%^&*()') vm.ssh_key = ssh_key vm.ssh_username = ssh_username vm.user_data = user_data vm.save() if not reservation_id: reservation_id = vm.id vm.reservation_id = reservation_id vm.save() if farm and i == 0: farm.head = vm vms.append(vm) log.debug(user.id, "Attaching disks") disk_devs = [] if i == 0 and disk_list: for disk_id in disk_list: log.debug(user.id, 'Attaching disks to first VM') disk = StorageImage.get(user.id, disk_id) if disk.vm != None: raise CMException('image_attached') while disk.disk_dev in disk_devs: disk.disk_dev += 1 disk_devs.append(disk.disk_dev) disk.vm = vm disk.save() log.debug(user.id, "Attaching CD") if i == 0 and iso_list: for iso_id in iso_list: log.debug(user.id, 'Attaching iso to first VM') # cd image have not be attached to any other vm iso = IsoImage.get(user.id, iso_id) iso.check_attached() vm.iso_image = iso vm.save() for i, vm in enumerate(vms): if lease_id != None: lease = Lease.objects.get(id=lease_id) if lease.user_network.user != user: raise CMException('lease_permission') if lease.vm != None: raise CMException('lease_attached') lease.vm = vm log.debug(user.id, "Attached ip: %s" % lease.address) else: lease = AvailableNetwork.get_lease(user) lease.vm = vm lease.save() log.debug(user.id, "Attached ip: %s" % lease.address) if i == 0 and public_ip_id > 0: log.debug(user.id, "Attaching PublicIP") try: publicip = PublicIP.objects.filter(user=user).get(id=public_ip_id) publicip.assign(lease) publicip.save() except Exception, e: log.exception(user.id, str(e)) raise CMException("lease_not_found")
def create(user, name, description, image_id, template_id, public_ip_id, iso_list, disk_list, vnc, groups, ssh_key=None, ssh_username=None, count=1, farm=None, head_template_id=None, node_id=False, lease_id=None, user_data=None): from cm.models.storage_image import StorageImage from cm.utils.threads.vm import VMThread template = Template.get(template_id) image = SystemImage.get(user.id, image_id, groups) if image.state != image_states['ok']: raise CMException('image_unavailable') if farm: head_template = Template.get(head_template_id) wn_template = template user.check_quota([(head_template, 1), (wn_template, count)]) count += 1 else: user.check_quota([(template, count)]) vms = [] reservation_id = None for i in range(count): # create VM instance log.debug(user.id, "Looking for node") node = Node.get_free_node( head_template, image, node_id) if farm and i == 0 else Node.get_free_node( template, image, node_id) log.info(user.id, 'Selected node: %d' % node.id) vm = VM() vm.libvirt_id = -1 if farm: if i == 0: vm.name = '%s-head' % name vm.description = 'Farm head' vm.template = head_template else: vm.name = '%s-wn%d' % (name, i) vm.description = 'Worker Node' vm.template = wn_template else: vm.template = template vm.description = description if count > 1: vm.name = '%s_%d' % (name, i + 1) else: vm.name = name vm.user = user vm.state = vm_states['init'] vm.start_time = datetime.now() vm.system_image = image vm.node = node vm.save_vm = True if farm: vm.farm = farm # Find first free vnc port used_ports = VM.objects.exclude( state__in=[vm_states['closed'], vm_states['erased'] ]).values_list('vnc_port', flat=True) for new_vnc_port in xrange(VNC_PORTS['START'], VNC_PORTS['END'] + 1): if new_vnc_port not in used_ports and new_vnc_port not in VNC_PORTS[ 'EXCLUDE']: break else: raise CMException('vm_vnc_not_found') log.debug(user.id, "Found vnc port: %d" % new_vnc_port) vm.vnc_port = new_vnc_port # Find first free novnc port used_ports = VM.objects.exclude( state__in=[vm_states['closed'], vm_states['erased'] ]).values_list('novnc_port', flat=True) for new_novnc_port in xrange(NOVNC_PORTS['START'], NOVNC_PORTS['END'] + 1): if new_novnc_port not in used_ports and new_novnc_port not in NOVNC_PORTS[ 'EXCLUDE']: break else: raise CMException('vm_novnc_not_found') log.debug(user.id, "Found novnc port: %d" % new_novnc_port) vm.novnc_port = new_novnc_port if vnc: vm.attach_vnc() vm.vnc_passwd = password_gen(13, chars=['letters', 'digits'], extra_chars='!@#$%^&*()') vm.ssh_key = ssh_key vm.ssh_username = ssh_username vm.user_data = user_data vm.save() if not reservation_id: reservation_id = vm.id vm.reservation_id = reservation_id vm.save() if farm and i == 0: farm.head = vm vms.append(vm) log.debug(user.id, "Attaching disks") disk_devs = [] if i == 0 and disk_list: for disk_id in disk_list: log.debug(user.id, 'Attaching disks to first VM') disk = StorageImage.get(user.id, disk_id) if disk.vm != None: raise CMException('image_attached') while disk.disk_dev in disk_devs: disk.disk_dev += 1 disk_devs.append(disk.disk_dev) disk.vm = vm disk.save() log.debug(user.id, "Attaching CD") if i == 0 and iso_list: for iso_id in iso_list: log.debug(user.id, 'Attaching iso to first VM') # cd image have not be attached to any other vm iso = IsoImage.get(user.id, iso_id) iso.check_attached() vm.iso_image = iso vm.save() for i, vm in enumerate(vms): if lease_id != None: lease = Lease.objects.get(id=lease_id) if lease.user_network.user != user: raise CMException('lease_permission') if lease.vm != None: raise CMException('lease_attached') lease.vm = vm log.debug(user.id, "Attached ip: %s" % lease.address) else: lease = AvailableNetwork.get_lease(user) lease.vm = vm lease.save() log.debug(user.id, "Attached ip: %s" % lease.address) if i == 0 and public_ip_id > 0: log.debug(user.id, "Attaching PublicIP") try: publicip = PublicIP.objects.filter(user=user).get( id=public_ip_id) publicip.assign(lease) publicip.save() except Exception, e: log.exception(user.id, str(e)) raise CMException("lease_not_found")