class ListInfo(object): def __init__(self, arguments): self.cloudmanage = CloudManage() try: self.config = cm_config() except: Console.error( "There is a problem with the configuration yaml files") self.username = self.config['cloudmesh']['profile']['username'] self.arguments = arguments # pprint(self.arguments) self.cloudmanage = CloudManage() try: self.config = cm_config() except: Console.error( "There is a problem with the configuration yaml files") self.username = self.config['cloudmesh']['profile']['username'] self.refresh_default_setting = get_command_list_refresh_default_setting( self.username) def _list_flavor(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = [['id', 'id'], ['name', 'name'], ['vcpus', 'vcpus'], ['ram', 'ram'], ['disk', 'disk'], ['refresh time', 'cm_refresh']] if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate(cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh(cm_user_id=self.username, names=clouds, types=['flavors']) # --format p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [ x.strip() for x in self.arguments['--column'].split(',') ] new_itemkeys = [] for item in itemkeys: if item[0] in s_column: new_itemkeys.append(item) itemkeys = new_itemkeys for cloud in clouds: self.cloudmanage.print_cloud_flavors( username=self.username, cloudname=cloud.encode("ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format) else: return def _list_image(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = { "openstack": [ # [ "Metadata", "metadata"], ["name", "name"], ["status", "status"], ["id", "id"], ["type_id", "metadata", "instance_type_id"], ["iname", "metadata", "instance_type_name"], ["location", "metadata", "image_location"], ["state", "metadata", "image_state"], ["updated", "updated"], # [ "minDisk" , "minDisk"], ["memory_mb", "metadata", 'instance_type_memory_mb'], ["fid", "metadata", "instance_type_flavorid"], ["vcpus", "metadata", "instance_type_vcpus"], # [ "user_id" , "metadata", "user_id"], # [ "owner_id" , "metadata", "owner_id"], # [ "gb" , "metadata", "instance_type_root_gb"], # [ "arch", ""] ], "ec2": [ # [ "Metadata", "metadata"], ["state", "extra", "state"], ["name", "name"], ["id", "id"], ["public", "extra", "is_public"], ["ownerid", "extra", "owner_id"], ["imagetype", "extra", "image_type"] ], "azure": [["name", "label"], ["category", "category"], ["id", "id"], ["size", "logical_size_in_gb"], ["os", "os"]], "aws": [["state", "extra", "state"], ["name", "name"], ["id", "id"], ["public", "extra", "ispublic"], ["ownerid", "extra", "ownerid"], ["imagetype", "extra", "imagetype"]] } if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate(cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh(cm_user_id=self.username, names=clouds, types=['images']) p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [ x.strip() for x in self.arguments['--column'].split(',') ] new_itemkeys = {x: [] for x in itemkeys.keys()} for cloud, items in itemkeys.iteritems(): for item in items: if item[0] in s_column: new_itemkeys[cloud].append(item) itemkeys = new_itemkeys for cloud in clouds: self.cloudmanage.print_cloud_images( username=self.username, cloudname=cloud.encode("ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format) else: return def _list_server(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = { "openstack": [['name', 'name'], ['status', 'status'], ['addresses', 'addresses'], ['id', 'id'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id'], ['user_id', 'cm_user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ['cloud', 'cm_cloud']], "ec2": [["name", "id"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'imageId'], ["user_id", 'user_id'], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"]], "aws": [["name", "name"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'image_id'], ["user_id", "user_id"], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"]], "azure": [ ['name', 'name'], ['status', 'status'], ['addresses', 'vip'], ['flavor', 'flavor', 'id'], ['id', 'id'], ['image', 'image', 'id'], ['user_id', 'user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ] } itemkeys_short = { "openstack": [['name', 'name'], ['status', 'status'], ['addresses', 'addresses'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id']], "ec2": [["name", "id"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['image', 'extra', 'imageId']], "aws": [["name", "name"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['image', 'extra', 'image_id']], "azure": [['name', 'name'], ['status', 'status'], ['addresses', 'vip'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id']] } if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate(cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh(cm_user_id=self.username, names=clouds, types=['servers']) p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [ x.strip() for x in self.arguments['--column'].split(',') ] new_itemkeys = {x: [] for x in itemkeys.keys()} for cloud, items in itemkeys.iteritems(): for item in items: if item[0] in s_column: new_itemkeys[cloud].append(item) itemkeys = new_itemkeys else: if not self.arguments['--detail']: itemkeys = itemkeys_short for cloud in clouds: self.cloudmanage.print_cloud_servers( username=self.username, cloudname=cloud.encode("ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format, group=self.arguments['--group']) else: return def _list_project(self): self.cloudmanage._connect_to_mongo() selected_project = None try: selected_project = self.cloudmanage.mongo.db_defaults.find_one( {'cm_user_id': self.username + "OIO"})['project'] except Exception, NoneType: Console.warning("could not find selected project in the database") except Exception, e: Console.error("could not connect to the database") print(e)
class ListInfo(object): def __init__(self, arguments): self.cloudmanage = CloudManage() try: self.config = cm_config() except: Console.error("There is a problem with the configuration yaml files") self.username = self.config['cloudmesh']['profile']['username'] self.arguments = arguments # pprint(self.arguments) self.cloudmanage = CloudManage() try: self.config = cm_config() except: Console.error("There is a problem with the configuration yaml files") self.username = self.config['cloudmesh']['profile']['username'] self.refresh_default_setting = get_command_list_refresh_default_setting(self.username) def _list_flavor(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = [ ['id', 'id'], ['name', 'name'], ['vcpus', 'vcpus'], ['ram', 'ram'], ['disk', 'disk'], ['refresh time', 'cm_refresh'] ] if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate( cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh( cm_user_id=self.username, names=clouds, types=['flavors']) # --format p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [x.strip() for x in self.arguments['--column'].split(',')] new_itemkeys = [] for item in itemkeys: if item[0] in s_column: new_itemkeys.append(item) itemkeys = new_itemkeys for cloud in clouds: self.cloudmanage.print_cloud_flavors(username=self.username, cloudname=cloud.encode( "ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format) else: return def _list_image(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = {"openstack": [ # [ "Metadata", "metadata"], ["name", "name"], ["status", "status"], ["id", "id"], ["type_id", "metadata", "instance_type_id"], ["iname", "metadata", "instance_type_name"], ["location", "metadata", "image_location"], ["state", "metadata", "image_state"], ["updated", "updated"], # [ "minDisk" , "minDisk"], ["memory_mb", "metadata", 'instance_type_memory_mb'], ["fid", "metadata", "instance_type_flavorid"], ["vcpus", "metadata", "instance_type_vcpus"], # [ "user_id" , "metadata", "user_id"], # [ "owner_id" , "metadata", "owner_id"], # [ "gb" , "metadata", "instance_type_root_gb"], # [ "arch", ""] ], "ec2": [ # [ "Metadata", "metadata"], ["state", "extra", "state"], ["name", "name"], ["id", "id"], ["public", "extra", "is_public"], ["ownerid", "extra", "owner_id"], ["imagetype", "extra", "image_type"] ], "azure": [ ["name", "label"], ["category", "category"], ["id", "id"], ["size", "logical_size_in_gb"], ["os", "os"] ], "aws": [ ["state", "extra", "state"], ["name", "name"], ["id", "id"], ["public", "extra", "ispublic"], ["ownerid", "extra", "ownerid"], ["imagetype", "extra", "imagetype"] ] } if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate( cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh( cm_user_id=self.username, names=clouds, types=['images']) p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [x.strip() for x in self.arguments['--column'].split(',')] new_itemkeys = {x: [] for x in itemkeys.keys()} for cloud, items in itemkeys.iteritems(): for item in items: if item[0] in s_column: new_itemkeys[cloud].append(item) itemkeys = new_itemkeys for cloud in clouds: self.cloudmanage.print_cloud_images(username=self.username, cloudname=cloud.encode( "ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format) else: return def _list_server(self): self.cloudmanage._connect_to_mongo() clouds = self.get_working_cloud_name() if clouds: itemkeys = {"openstack": [ ['name', 'name'], ['status', 'status'], ['addresses', 'addresses'], ['id', 'id'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id'], ['user_id', 'cm_user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ['cloud', 'cm_cloud'] ], "ec2": [ ["name", "id"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'imageId'], ["user_id", 'user_id'], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"] ], "aws": [ ["name", "name"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'image_id'], ["user_id", "user_id"], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"] ], "azure": [ ['name', 'name'], ['status', 'status'], ['addresses', 'vip'], ['flavor', 'flavor', 'id'], ['id', 'id'], ['image', 'image', 'id'], ['user_id', 'user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ] } itemkeys_short = {"openstack": [ ['name', 'name'], ['status', 'status'], ['addresses', 'addresses'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id'] ], "ec2": [ ["name", "id"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['image', 'extra', 'imageId'] ], "aws": [ ["name", "name"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['image', 'extra', 'image_id'] ], "azure": [ ['name', 'name'], ['status', 'status'], ['addresses', 'vip'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id'] ] } if self.refresh_default_setting or self.arguments['--refresh']: self.cloudmanage.mongo.activate( cm_user_id=self.username, names=clouds) self.cloudmanage.mongo.refresh( cm_user_id=self.username, names=clouds, types=['servers']) p_format = self.arguments['--format'] # --column # available columns are: id, name, vcpus, ram, disk, refresh time, # and all if self.arguments['--column']: if self.arguments['--column'] != "all": s_column = [x.strip() for x in self.arguments['--column'].split(',')] new_itemkeys = {x: [] for x in itemkeys.keys()} for cloud, items in itemkeys.iteritems(): for item in items: if item[0] in s_column: new_itemkeys[cloud].append(item) itemkeys = new_itemkeys else: if not self.arguments['--detail']: itemkeys = itemkeys_short for cloud in clouds: self.cloudmanage.print_cloud_servers(username=self.username, cloudname=cloud.encode( "ascii"), itemkeys=itemkeys, refresh=False, output=False, print_format=p_format, group=self.arguments['--group']) else: return def _list_project(self): self.cloudmanage._connect_to_mongo() selected_project = None try: selected_project = self.cloudmanage.mongo.db_defaults.find_one( {'cm_user_id': self.username + "OIO"})['project'] except Exception, NoneType: Console.warning("could not find selected project in the database") except Exception, e: Console.error("could not connect to the database") print(e)
def delete_vm(username, cloudname, server_id_list=None, preview=False, refresh=False): ''' delete vms of a cloud for a user this function accepts a list of VM ids and delete them TODO: what if fail to delete, how to acknowledge it; it looks like even though delete a vm and return {msg: seccess}, sometimes refresh after 5 sec, it might be still there :param username: user name :type username: string :param cloudname: cloud name :type cloudname: string :param server_id_list: the list of VM ids to delete :type server_id_list: list :param preview: True if the user wants to preview and confirm before start to delete :type preview: boolean :param refresh: True to refresh the database before processing, the database has to be refreshed before start processing here. Since there are many places that refreshes the database, so if there is a place in the program refreshes shortly before calling this funtion, to avoid redundant work, make this argument False :type refresh: boolean :return: False if error occurs :rtype: boolean ''' # changed the scope of this import # Benefit: other functions are not affected with this import # drawback: hard to see which module is being loaded in this file # Hyungro Lee 12/01/2014 from cloudmesh.experiment.group_usage import remove_vm_from_group_while_deleting try: mongo = cm_mongo() except: Console.error("There is a problem with the mongo server") return False if refresh: mongo.activate(cm_user_id=username, names=[cloudname]) mongo.refresh(cm_user_id=username, names=[cloudname], types=['servers']) serverdata = mongo.servers( clouds=[cloudname], cm_user_id=username)[cloudname] # ------------------------- # preview and confirm confirm_deletion = True if preview: if server_id_list == []: Console.warning("no vm meets the condition") return False else: resserverdata = {} for i in server_id_list: resserverdata[i] = serverdata[i] cloudobj = CloudManage() itemkeys = {"openstack": [ ['name', 'name'], ['status', 'status'], ['addresses', 'addresses'], ['id', 'id'], ['flavor', 'flavor', 'id'], ['image', 'image', 'id'], ['user_id', 'cm_user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ['cloud', 'cm_cloud'] ], "ec2": [ ["name", "id"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'imageId'], ["user_id", 'user_id'], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"] ], "aws": [ ["name", "name"], ["status", "extra", "status"], ["addresses", "public_ips"], ["flavor", "extra", "instance_type"], ['id', 'id'], ['image', 'extra', 'image_id'], ["user_id", "user_id"], ["metadata", "metadata"], ["key_name", "extra", "key_name"], ["created", "extra", "launch_time"] ], "azure": [ ['name', 'name'], ['status', 'status'], ['addresses', 'vip'], ['flavor', 'flavor', 'id'], ['id', 'id'], ['image', 'image', 'id'], ['user_id', 'user_id'], ['metadata', 'metadata'], ['key_name', 'key_name'], ['created', 'created'], ] } cloudobj.print_cloud_servers(username=username, cloudname=cloudname, itemkeys=itemkeys, refresh=False, output=False, serverdata=resserverdata) if yn_choice("confirm to delete these vms?", default='n', tries=3): pass else: confirm_deletion = False # ------------------------- # deleting if confirm_deletion: if server_id_list == []: return watch = time.time() useQueue = False if useQueue: # not functioning cloudmanager = mongo.clouds[username][cloudname]["manager"] cm_type = mongo.get_cloud_info(username, cloudname)['cm_type'] package = "cloudmesh.iaas.%s.queue" % cm_type name = "tasks" imported = getattr(__import__(package, fromlist=[name]), name) queue_name = "%s-%s" % (cm_type, "servers") for i in server_id_list: tempservername = serverdata[i]['name'].encode("ascii") banner( "Deleting vm->{0} on cloud->{1}".format(tempservername, cloudname)) result = imported.vm_delete.apply_async( (cloudname, i, username), queue=queue_name) print("job status:", result.state) try: remove_vm_from_group_while_deleting(username, tempservername) except Exception, err: Console.error(str(err)) return # print result.traceback ######### imported.wait.apply_async( args=None, kwargs={'t': 10}, queue=queue_name) handleip = imported.release_unused_public_ips.apply_async( (cloudname, username), queue=queue_name) handlerefresh = imported.refresh.apply_async(args=None, kwargs={'cm_user_id': username, 'names': [cloudname], 'types': ['servers']}, queue=queue_name) # print handleip.state # print handleip.traceback # print handlerefresh.state # print handlerefresh.traceback if preview: print("to check realtime vm status: list vm --refresh") else: for i in server_id_list: tempservername = serverdata[i]['name'].encode("ascii") banner( "Deleting vm->{0} on cloud->{1}".format(tempservername, cloudname)) result = mongo.vm_delete(cloudname, i, username) pprint(result) try: remove_vm_from_group_while_deleting(username, tempservername) except Exception, err: Console.error(str(err)) return time.sleep(5) mongo.release_unused_public_ips(cloudname, username) mongo.refresh(username, names=[cloudname], types=['servers'])