def get_working_cloud_name(self): ''' get the name of a cloud to work on, if CLOUD not given, will pick the selected or default cloud ''' cloudname = None cloudobj = CloudManage() mongo = cm_mongo() if self.arguments['--cloud']: cloud = cloudobj.get_clouds( self.username, getone=True, cloudname=self.arguments['--cloud']) if cloud is None: Console.error( "could not find cloud '{0}'".format(self.arguments['--cloud'])) return False else: cloudname = self.arguments['--cloud'] else: cloudname = cloudobj.get_selected_cloud(self.username) if cloudname not in mongo.active_clouds(self.username): Console.warning( "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(cloudname)) return False else: return cloudname
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 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 delete_vm(username, cloudname, server_id_list=None, preview=False, refresh=False): ''' delete vms of a cloud of a user, this function provides several ways to find and delete vms :param server_id_list:: the list of VMs(id) to delete :param preview:: True if the user wants to preview and confirm before start to delete TODO: what if fail, how to acknowledge it range search: now if prefix not given, all vms whose index are in the range will be deleted, regardless of its prefix it looks like even though delete a vm and return {msg: seccess}, sometimes refresh after 5 sec, it might be still there ''' # 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'])
def start_vm(username, cloudname, count=1, flavorname=None, flavorid=None, imagename=None, imageid=None, groupname=None, servername=None): ''' create a vm of a cloud of a user will check flavor, image existence if provided user can specify groupname which will be written in metadata, servername which will replace prefix+index as the vm name it's better to check cloud active status before use this function :param username: string :param cloudname: string :param count: number of vms to start :return False if error TODO: what if fail, how to acknowledge it; no return now as using celery input key missing security group ''' # Changed scope of this import - hyungro lee 12/01/2014 from cloudmesh.experiment.group_usage import add_vm_to_group_while_creating mongo = cm_mongo() userobj = cm_user() cloudobj = CloudManage() mongo.activate(cm_user_id=username, names=[cloudname]) userinfo = userobj.info(username) key = None vm_image_id = None vm_flavor_id = None error = '' # ------------------------- # refresh server flavor or image to_refresh = ["servers"] if flavorname is not None or flavorid is not None: to_refresh.append("flavors") if imagename is not None or imageid is not None: to_refresh.append("images") if to_refresh != []: mongo.refresh(username, names=[cloudname], types=to_refresh) # ------------------------- # get exist VM names list, to prevent names duplicate serverdata = mongo.servers( clouds=[cloudname], cm_user_id=username)[cloudname] servers_names_list = [] for k,v in serverdata.iteritems(): servers_names_list.append(v['name']) # ------------------------- # flavor handler if flavorname is not None or flavorid is not None: flavordata = mongo.flavors( clouds=[cloudname], cm_user_id=username)[cloudname] same_name_count = 0 for k, v in flavordata.iteritems(): if flavorname is not None: if flavorname == v['name']: vm_flavor_id = k same_name_count = same_name_count + 1 else: if flavorid == k: vm_flavor_id = k break if vm_flavor_id is None: error = error + "The flavor you provide doesn't exist. " if same_name_count > 1: error = error + "There are more than one flavor with the name you provide" \ "please use flavorid instead or select one by command cloud" \ "set flavor [CLOUD]. " else: try: vm_flavor_id = userinfo["defaults"]["flavors"][cloudname] except: pass if vm_flavor_id in [None, 'none']: error = error + \ "Please specify a default flavor(command: cloud set flavor [CLOUD]). " # ------------------------- # image handler if imagename is not None or imageid is not None: imagedata = mongo.images( clouds=[cloudname], cm_user_id=username)[cloudname] same_name_count = 0 for k, v in imagedata.iteritems(): if imagename is not None: if imagename == v['name']: vm_image_id = k same_name_count = same_name_count + 1 else: if imageid == k: vm_image_id = k break if vm_image_id is None: error = error + "The image you provide doesn't exist. " if same_name_count > 1: error = error + "There are more than one image with the name you provide" \ "please use imageid instead or select one by command cloud" \ "set image [CLOUD]. " else: try: vm_image_id = userinfo["defaults"]["images"][cloudname] except: pass if vm_image_id in [None, 'none']: error = error + \ "Please specify a default image(command: cloud set flavor [CLOUD]). " # ------------------------- # key handler if "key" in userinfo["defaults"]: key = userinfo["defaults"]["key"] elif len(userinfo["keys"]["keylist"].keys()) > 0: key = userinfo["keys"]["keylist"].keys()[0] if key: keycontent = userinfo["keys"]["keylist"][key] if keycontent.startswith('key '): keycontent = keycontent[4:] cm_keys_mongo(username).check_register_key(username, cloudname, key, keycontent) keynamenew = _keyname_sanitation(username, key) else: error = error + \ "No sshkey found. Please Upload one" # ------------------------- if error != '': Console.error(error) return False # ------------------------- metadata = {'cm_owner': username} if groupname: metadata['cm_group'] = groupname tmpnamefl = cloudobj.get_flavors( cloudname=cloudname, getone=True, id=vm_flavor_id)['name'] tmpnameim = cloudobj.get_images( cloudname=cloudname, getone=True, id=vm_image_id)['name'] while count > 0: userinfo = userobj.info(username) if servername: prefix = '' index = '' givenvmname = servername tmpnameser = servername else: prefix = userinfo["defaults"]["prefix"] index = userinfo["defaults"]["index"] givenvmname = None tmpnameser = prefix + '_' + str(index) # ------------------------ # do not allow server name duplicate if tmpnameser in servers_names_list: Console.error("vm name '{0}' exists, please use other names or delete it first".format( tmpnameser)) if not servername: userobj.set_default_attribute(username, "index", int(index) + 1) count = count - 1 continue # ------------------------ # vm start procedure banner("Starting vm->{0} on cloud->{1} using image->{2}, flavor->{3}, key->{4}" .format(tmpnameser, cloudname, tmpnameim, tmpnamefl, keynamenew)) useQueue = False if useQueue: result = mongo.vm_create_queue(cloudname, prefix, index, vm_flavor_id, vm_image_id, keynamenew, meta=metadata, cm_user_id=username, givenvmname=givenvmname) print("job status:", result.state) else: result = mongo.vm_create(cloudname, prefix, index, vm_flavor_id, vm_image_id, keynamenew, meta=metadata, cm_user_id=username, givenvmname=givenvmname) if "server" in result and "adminPass" in result["server"]: result["server"]["adminPass"] = "******" pprint(result) # ------------------------ # add it to the group in database if groupname provided if groupname: try: add_vm_to_group_while_creating(username, groupname, tmpnameser) except Exception, err: Console.error(str(err)) return # ------------------------ # increase index if it is used if not servername: userobj.set_default_attribute(username, "index", int(index) + 1) # ------------------------ servers_names_list.append(tmpnameser) count = count - 1
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 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'], ] } 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 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 do_cluster(self, args, arguments): """ Usage: cluster start CLUSTER_NAME cluster list cluster login CLUSTER_NAME cluster stop CLUSTER_NAME cluster create --count=<count> --group=<group> [--ln=<LoginName>] [--cloud=<CloudName>] [--image=<imgName>|--imageid=<imgId>] [--flavor=<flavorName>|--flavorid=<flavorId>] [--force] Description: Cluster Management cluster create --count=<count> --group=<group> --ln=<LoginName> [options...] <count> specify amount of VMs in the cluster <group> specify a group name of the cluster, make sure it's unique Start a cluster of VMs, and each of them can log into all others. CAUTION: you sould do some default setting before using this command: 1. select cloud to work on, e.g. cloud select india 2. activate the cloud, e.g. cloud on india 3. set the default key to start VMs, e.g. key default [NAME] 4. set the start name of VMs, which is prefix and index, e.g. label --prefix=test --id=1 5. set image of VMs, e.g. default image 6. set flavor of VMs, e.g. default flavor Also, please make sure the group name of the cluster is unique Options: --ln=<LoginName> give a login name for the VMs, e.g. ubuntu --cloud=<CloudName> give a cloud to work on --flavor=<flavorName> give the name of the flavor --flavorid=<flavorId> give the id of the flavor --image=<imgName> give the name of the image --imageid=<imgId> give the id of the image --force if a group exists and there are VMs in it, the program will ask user to proceed or not, use this flag to respond yes as default(if there are VMs in the group before creating this cluster, the program will include the exist VMs into the cluster) """ #pprint(arguments) self.cm_config = cm_config() self.cm_mongo = cm_mongo() self.user = cm_user() # ----------------------------- # TODO:: # add VMs to cluster # ----------------------------- if arguments['start'] and arguments['CLUSTER_NAME']: '''Starts a cluster''' # Initialize default variables. e.g. userid, default cloud and # default keypair userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) userinfo = self.user.info(userid) if "key" in userinfo["defaults"]: key = userinfo["defaults"]["key"] elif len(userinfo["keys"]["keylist"].keys()) > 0: key = userinfo["keys"]["keylist"].keys()[0] if key: keycontent = userinfo["keys"]["keylist"][key] if keycontent.startswith('key '): keycontent = keycontent[4:] cm_keys_mongo(userid).check_register_key(userid, def_cloud, key, keycontent) keynamenew = _keyname_sanitation(userid, key) else: Console.warning("No sshkey found. Please Upload one") return clustername = arguments['CLUSTER_NAME'] s_name = "cluster-{0}-{1}-{2}".format(userid, clustername, get_rand_string()) # TEMP FOR HADOOP CLUSTER if clustername != "hadoop": Console.warning('hadoop is only available cluster') return # 1. keypair for the communication between master and worker nodes privatekey, publickey = generate_keypair() t_url = \ "https://raw.githubusercontent.com/cloudmesh/cloudmesh/dev1.3/heat-templates/ubuntu-14.04/hadoop-cluster/hadoop-cluster.yaml" param = {'KeyName': keynamenew, 'PublicKeyString': publickey, 'PrivateKeyString': privatekey} log.debug(def_cloud, userid, s_name, t_url, param, publickey, privatekey) res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid, servername=s_name, template_url=t_url, parameters=param) log.debug(res) if 'error' in res: print (res['error']['message']) return res elif arguments['list']: userid = self.cm_config.username() self.cm_mongo.activate(userid) self.cm_mongo.refresh(cm_user_id=userid, types=[self._id]) stacks = self.cm_mongo.stacks(cm_user_id=userid) launchers = self.filter_launcher( stacks, {"search": "contain", "key": "stack_name", "value": "launcher"} ) log.debug(launchers) d = {} for k0, v0 in launchers.iteritems(): for k1, v1 in launchers[k0].iteritems(): d[v1['id']] = v1 columns = ['stack_name', 'description', 'stack_status', 'creation_time', 'cm_cloud'] if arguments['--column'] and arguments['--column'] != "all": columns = [x.strip() for x in arguments['--column'].split(',')] if arguments['--format']: if arguments['--format'] not in ['table', 'json', 'csv']: Console.error("please select printing format among table, json and csv") return else: p_format = arguments['--format'] else: p_format = None shell_commands_dict_output(d, print_format=p_format, firstheader="launcher_id", header=columns # vertical_table=True ) elif arguments['login']: Console.error("Not implemented") return elif arguments['stop'] and arguments['CLUSTER_NAME']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) c_id = arguments['CLUSTER_NAME'] self.cm_mongo.activate(userid) res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid, server=c_id) log.debug(res) return res elif arguments['create']: try: config = cm_config() except: Console.error("Failed to load the cloudmesh yaml file") return username = config['cloudmesh']['profile']['username'] cloudname = arguments['--cloud'] or CloudManage().get_selected_cloud(username) temp_dir_name = ".temp_cluster_create_" + username + "_0" while os.path.isdir("./{0}".format(temp_dir_name)): temp_dir_name = temp_dir_name[:-1] + str(int(temp_dir_name[-1]) + 1) dir_name = temp_dir_name #NumOfVM = None GroupName = None vm_login_name = "ubuntu" temp_key_name = "sshkey_temp" _key = "-i ./{0}/{1}".format(dir_name, temp_key_name) StrictHostKeyChecking = "-o StrictHostKeyChecking=no" res = None to_print = [] ''' try: NumOfVM = abs(int(argument['--count'])) except: Console.error("<count> must be an integer") return ''' if arguments['--group'] == '': Console.error("<group> cannot be empty") return else: GroupName = arguments['--group'] if arguments['--ln']: if arguments['--ln'] == '': Console.error("<LoginName> cannot be empty") return else: vm_login_name = arguments['--ln'] if not arguments['--force']: # Moved the import inside of this function # If this import goes up to the top, monodb connection will be # estabilished. Due to that reason, this import stays here # Hyungro Lee 12/01/2014 from cloudmesh.experiment.group import GroupManagement GroupManage = GroupManagement(username) groups_list = GroupManage.get_groups_names_list() if GroupName in groups_list: vms_in_group_list = GroupManage.list_items_of_group(GroupName, _type="VM")["VM"] if len(vms_in_group_list) != 0: if yn_choice("The group you provide exists and it has VMs in it, " + \ "do you want to proceed? (if you choose yes, these exist " +\ "VMs will be included in the cluster, this could also " +\ "rewrite the key on the exist VMs)", default='n', tries=3): pass else: return # start VMs print ("starting VMs...") arguments_temp = arguments arguments_temp['start'] = True arguments_temp['--name'] = None vmclass = VMcommand(arguments_temp) res = vmclass.call_procedure() if res == False: return def string_to_dict(s): h = s.find("{") t = s.rfind("}") return json.loads(s[h:t+1]) def check_public_ip_existence(d): temp = d['addresses']['private'] for item in temp: if item["OS-EXT-IPS:type"] == "floating": return True return False def get_ip(d, kind="floating"): # kind is either floating or fixed temp = d['addresses']['private'] for item in temp: if item["OS-EXT-IPS:type"] == kind: return item['addr']#.encode('ascii') return "FAIL: doesn't exist" # check all VMs are active command_refresh = "vm list --refresh --group={0} --format=json".format(GroupName) def _help0(d): for k, v in d.iteritems(): if v['status'] != 'ACTIVE': return False return True proceed = False repeat_index = 1 while proceed != True: if repeat_index > 10: Console.warning("Please check the network") return print ("checking({0})...".format(repeat_index)) time.sleep(5) res = str(cm(command_refresh)) res = string_to_dict(res) if _help0(res): proceed = True else: repeat_index = repeat_index + 1 continue # assign ip to all VMs print ("assigning public ips...") for k, v in res.iteritems(): if not check_public_ip_existence(v): cm("vm ip assign --id={0}".format(k.encode('ascii'))) def _help(d): for k, v in d.iteritems(): if check_public_ip_existence(v) != True: return False return True # make sure all VMs have been assigned a public ip proceed = False repeat_index = 1 while proceed != True: if repeat_index > 10: Console.warning("Please check the network") return print ("checking({0})...".format(repeat_index)) time.sleep(5) res = str(cm(command_refresh)) res = string_to_dict(res) if _help(res): proceed = True else: repeat_index = repeat_index + 1 continue # ------------------------- # key handler userinfo = cm_user().info(username) key = None if "key" in userinfo["defaults"]: key = userinfo["defaults"]["key"] elif len(userinfo["keys"]["keylist"].keys()) > 0: key = userinfo["keys"]["keylist"].keys()[0] Console.warning("default key is not set, trying to use a key in the database...") if key: keycontent = userinfo["keys"]["keylist"][key] if keycontent.startswith('key '): keycontent = keycontent[4:] cm_keys_mongo(username).check_register_key(username, cloudname, key, keycontent) else: Console.error("No sshkey found. Please Upload one") return # ------------------------- # generate ssh keys for VMs and prepare two files: authorized_keys and hosts print ("generating ssh keys...") os.popen("mkdir {0}".format(dir_name)) fa = open("./{0}/authorized_keys_temp".format(dir_name), "w") fh = open("./{0}/hosts_temp".format(dir_name), "w") fk = open("./{0}/{1}".format(dir_name, temp_key_name), "w") fk.write(keycontent) fk.close() os.popen("chmod 644 ./{0}/{1}".format(dir_name, temp_key_name)) for k, v in res.iteritems(): address_floating = get_ip(v) address_fixed = get_ip(v, kind="fixed") vm_name = v['name']#.encode('ascii') to_print.append("{0} {1}, {2}".format(vm_name, address_floating, address_fixed)) fh.write(address_floating + " " + vm_name + "\n" + address_fixed + " " + vm_name + "-i\n") os.popen("ssh {2} {3} {0}@{1} \"ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa\""\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking)) temp = os.popen("ssh {2} {3} {0}@{1} \"cat ~/.ssh/id_rsa.pub\""\ .format(vm_login_name, address_floating, _key, StrictHostKeyChecking)).read() fa.write(temp) fa.close() fh.close() # copy the files to VMs print ("copying the files...") os.popen("mkdir ./{0}/oops".format(dir_name)) for k, v in res.iteritems(): address_floating = get_ip(v) os.popen("scp {2} {3} {0}@{1}:~/.ssh/authorized_keys ./{4}/"\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name)) os.popen("cat ./{0}/authorized_keys_temp >> ./{0}/authorized_keys"\ .format(dir_name)) os.popen("scp {2} {3} ./{4}/authorized_keys {0}@{1}:~"\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name)) os.popen("ssh {2} {3} {0}@{1} \"sudo mv authorized_keys ~/.ssh/\""\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking)) os.popen("rm ./{0}/authorized_keys".format(dir_name)) os.popen("cp ./{0}/hosts_temp ./{0}/oops/".format(dir_name)) os.popen("mv ./{0}/oops/hosts_temp ./{0}/oops/hosts".format(dir_name)) fh0 = open("./{0}/oops/hosts".format(dir_name), "a") os.popen("scp {2} {3} {0}@{1}:/etc/hosts ./{4}/"\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name)) with open("./{0}/hosts".format(dir_name)) as f0: content = f0.readlines() for item in content: fh0.write(item + "\n") fh0.close() os.popen("scp {2} {3} ./{4}/oops/hosts {0}@{1}:~"\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name)) os.popen("ssh {2} {3} {0}@{1} \"sudo mv hosts /etc/\""\ .format(vm_login_name,address_floating, _key, StrictHostKeyChecking)) os.popen("rm ./{0}/oops/hosts".format(dir_name)) print ("finishing...") os.popen("rm -rf {0}".format(dir_name)) print ("DONE.") print ("cluster group: ", GroupName) for item in to_print: print (item) print ("(host name for private ips will have -i at the end of VM name, e.g. testVM -> testVM-i)")
def do_nova(self, args, arguments): """ Usage: nova set CLOUD nova info [CLOUD] nova help nova ARGUMENTS A simple wrapper for the openstack nova command Arguments: ARGUMENTS The arguments passed to nova help Prints the nova manual set reads the information from the current cloud and updates the environment variables if the cloud is an openstack cloud info the environment values for OS Options: -v verbose mode """ # log.info(arguments) cloud = arguments['CLOUD'] if not self.config: self.config = cm_config() self.cm_user_id = self.config.username() if not self.mongodb: self.mongodb = CloudManage() if arguments["help"]: os.system("nova help") return elif arguments["info"]: # # prints the current os env variables for nova # d = {} # Get default cloud from mongodb self.cloud = self.mongodb.get_default_cloud_for_nova(self.cm_user_id) self.rcfiles = cm_rc.get_rcfiles() self.set_os_environ(self.cloud) for attribute in ['OS_USER_ID', 'OS_USERNAME', 'OS_TENANT_NAME', 'OS_AUTH_URL', 'OS_CACERT', 'OS_PASSWORD', 'OS_REGION']: try: d[attribute] = os.environ[attribute] # d[attribute] = self.rcfiles[cloud or self.cloud][attribute] except: log.warning(sys.exc_info()) d[attribute] = None print(row_table(d, order=None, labels=["Variable", "Value"])) return elif arguments["set"]: if cloud: self.cloud = cloud self.rcfiles = cm_rc.get_rcfiles() self.set_os_environ(self.cloud) # Store to mongodb self.mongodb.update_default_cloud_for_nova(self.cm_user_id, self.cloud) msg = "{0} is set".format(self.cloud) log.info(msg) print(msg) else: print("CLOUD is required") # # TODO: implemet # # cloud = get current default # if cloud type is openstack: # credentials = get credentials # set the credentials in the current os system env variables # else: os.system("nova {0}".format(arguments["ARGUMENTS"])) return
class cm_shell_nova: """opt_example class""" def activate_cm_shell_nova(self): self.config = None self.cm_user_id = None self.mongodb = None self.register_command_topic('cloud','nova') pass def set_os_environ(self, cloudname): '''Set os environment variables on a given cloudname''' try: novars = self.rcfiles[cloudname] for k, v in novars.iteritems(): os.environ[k] = v # TEMP CODE FOR CACERT if cloudname == "india" or cloudname == "india_openstack_havana": os.environ['OS_CACERT'] = \ "{0}/.cloudmesh/india-havana-cacert.pem".format(os.environ['HOME']) elif cloudname == "icehouse": os.environ['OS_CACERT'] = \ "{0}/.cloudmesh/clouds/icehouse/cacert.pem".format(os.environ['HOME']) except: log.warning(sys.exc_info()) @command def do_nova(self, args, arguments): """ Usage: nova set CLOUD nova info [CLOUD] nova help nova ARGUMENTS A simple wrapper for the openstack nova command Arguments: ARGUMENTS The arguments passed to nova help Prints the nova manual set reads the information from the current cloud and updates the environment variables if the cloud is an openstack cloud info the environment values for OS Options: -v verbose mode """ # log.info(arguments) cloud = arguments['CLOUD'] if not self.config: self.config = cm_config() self.cm_user_id = self.config.username() if not self.mongodb: self.mongodb = CloudManage() if arguments["help"]: os.system("nova help") return elif arguments["info"]: # # prints the current os env variables for nova # d = {} # Get default cloud from mongodb self.cloud = self.mongodb.get_default_cloud_for_nova(self.cm_user_id) self.rcfiles = cm_rc.get_rcfiles() self.set_os_environ(self.cloud) for attribute in ['OS_USER_ID', 'OS_USERNAME', 'OS_TENANT_NAME', 'OS_AUTH_URL', 'OS_CACERT', 'OS_PASSWORD', 'OS_REGION']: try: d[attribute] = os.environ[attribute] # d[attribute] = self.rcfiles[cloud or self.cloud][attribute] except: log.warning(sys.exc_info()) d[attribute] = None print(row_table(d, order=None, labels=["Variable", "Value"])) return elif arguments["set"]: if cloud: self.cloud = cloud self.rcfiles = cm_rc.get_rcfiles() self.set_os_environ(self.cloud) # Store to mongodb self.mongodb.update_default_cloud_for_nova(self.cm_user_id, self.cloud) msg = "{0} is set".format(self.cloud) log.info(msg) print(msg) else: print("CLOUD is required") # # TODO: implemet # # cloud = get current default # if cloud type is openstack: # credentials = get credentials # set the credentials in the current os system env variables # else: os.system("nova {0}".format(arguments["ARGUMENTS"])) return