def stop(): """stop the rabbit mq server""" if PRODUCTION: print "Run '/etc/init.d/rabbitmq-server stop' to stop server" while not yn_choice("Is rabbitmq stopped?", 'n'): print "Please stop rabbitmq-server." else: local("{rabbitmqctl} stop".format(**rabbit_env))
def start(detached=None): """start the rabbit mq server""" if PRODUCTION: print "Run '/etc/init.d/rabbitmq-server start' to start server" while not yn_choice("Is rabbitmq running?", 'n'): print "Please start rabbitmq-server." else: PROGRESS.next() print if detached is None: rabbit_env['detached'] = "-detached" # log.info (rabbit_env) local("{rabbitmq_server} {detached}".format(**rabbit_env))
def wipe(): """wipes out all traces from mongo""" kill() config = cm_config_server().get("cloudmesh.server.mongo") path = path_expand(config["path"]) banner("{0}".format(path)) local("mkdir -p {0}".format(path)) result = str(Shell.ls(path)) banner(path, "-") print result print 70 * "-" if result != "": if yn_choice("deleting the directory", default="n"): local("rm -rf {0}".format(path)) local("mkdir -p {0}".format(path)) banner("{0}".format(path)) local("ls {0}".format(path))
def do_admin(self, args, arguments): """ :: Usage: admin password reset admin password Options: Description: admin password reset Reset portal password """ pprint(arguments) if arguments['password'] and arguments['reset']: Console.ok('password reset ...') self.password = getpass("Password:"******"~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["password"] = self.password config.write(filename=filename, output="yaml") Console.ok("Resetting password. ok.") elif arguments['password']: if yn_choice("Would you like to print the password?"): filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) try: self.password = config["cmd3"]["properties"]["password"] except Exception: Console.error("could not find the password. Please set it.") return Console.msg("Password: {:}".format(self.password))
def do_admin(self, args, arguments): """ :: Usage: admin password reset admin password Options: Description: admin password reset Reset portal password """ pprint(arguments) if arguments["password"] and arguments["reset"]: Console.ok("password reset ...") self.password = getpass("Password:"******"~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) config["cmd3"]["properties"]["password"] = self.password config.write(filename=filename, output="yaml") Console.ok("Resetting password. ok.") elif arguments["password"]: if yn_choice("Would you like to print the password?"): filename = path_expand("~/.cloudmesh/cmd3.yaml") config = ConfigDict(filename=filename) try: self.password = config["cmd3"]["properties"]["password"] except Exception: Console.error("could not find the password. Please set it.") return Console.msg("Password: {:}".format(self.password))
def install_command(args): """ Usage: install -h | --help install --version install cloudmesh [--force] install delete_yaml install system install query install new [--force] install vagrant install enable admin [--username=<username>] """ # This is a debuging message # print "IIIII<" + args + ">" arguments = docopt(install_command.__doc__, args) # print arguments if arguments["cloudmesh"]: force = arguments['--force'] deploy(force=force) elif arguments["new"]: force = arguments["--force"] new_cloudmesh_yaml(force) elif arguments["delete_yaml"]: answer = yn_choice( "THIS COMMAND IS REAL DANGEROUS AND WILL DELETE ALL YOUR YAML FILE. Proceed", default='y') if answer: # TODO: cp is not imported, defined print("You fool we just deleted your yaml files") cp("etc/*.yaml", config_file_prefix()) else: print "puuh you interrupted" pass elif arguments["system"]: banner("Installing Ubuntu System Requirements") if is_ubuntu(): ubuntu() elif is_osx(): osx() elif is_centos(): centos() elif arguments["query"]: import platform print "System: ", platform.system() # print "Uname: ", platform.uname() print "Machine: ", platform.machine() print "Processor: ", platform.processor() print "Platform: ", platform.platform() print "Python: ", platform.python_version() print "Virtualenv:", hasattr(sys, 'real_prefix') elif arguments["vagrant"]: vagrant() elif arguments["enable"] and arguments["admin"]: enable_admin_page(arguments['--username'])
def cm_manage(): """Usage: cm-manage config projects list cm-manage config projects cm-manage config [-f FILE] [-o OUT] [-p PROJECT] cloud NAME [-] cm-manage config dump [--format=(yaml|dict)] cm-manage config init [-o OUT] [-u USER] cm-manage config list cm-manage config password NAME cm-manage config show passwords cm-manage config fetch [-u USER] [-r HOST] cm-manage --version cm-manage --help Arguments: NAME name of the cloud Options: -h --help show this help message and exit -v --version show version and exit -f NAME --file=NAME the Name of the cloud to be specified, if ? a selection is presented -o OUT --out=OUT writes the result in the specifide file -p PROJECT --project=PROJECT selects a project (e.g. for eucalyptus which has project-specific environments) -u USER --user=USER the user (login) name -r HOST --remote=HOST the host machine on which the yaml file is located in the CONFIG directory [default: india.futuregrid.org] -d --debug debug - this option is a - at the end of the command. If data is written to a file it is also put out to stdout Description: Command to generate rc files from our cloudmesh configuration files. This program generates form a YAML file containing the login information for a cloud an rc file that can be used to later source it. Example: we assume the yaml file has an entry india-openstack:: cm-manage config -o novarc india-openstack source novarc This will create a novarc file and than you can source it:: cm-manage config ? - Presents a selction of cloud choices and writes the choice into a file called CONFIG/novarc """ default_path = config_file_prefix + '/novarc' arguments = docopt(cm_manage.__doc__) DEBUG("arguments", arguments) home = os.environ['HOME'] # DEBUG("home", home) # # This secion deals with handeling "cm config" related commands ###################################################################### is_config = arguments['config'] is not None if is_config: # DEBUG('Arguments', arguments) file = arguments['--file'] try: config = cm_config(file) # DEBUG("config", config) except IOError: print("{0}: Configuration file '{1}' not found".format("CM ERROR", file)) sys.exit(1) except (yaml.scanner.ScannerError, yaml.parser.ParserError) as yamlerror: print("{0}: YAML error: {1}, in configuration file '{2}'".format("CM ERROR", yamlerror, file)) sys.exit(1) except: print("Unexpected error:", sys.exc_info()[0]) sys.exit(1) name = arguments['NAME'] # # NOT TESTED # if arguments['fetch'] or name == 'fetch': DEBUG('Arguments', arguments) # get user var = {} var['user'] = arguments['--user'] var['host'] = arguments['--remote'] # # BUG should be # var['file'] = config_file_prefix() + "/cloudmesh.yaml" if var['user'] is None: var['user'] = getpass.getuser() from_location = "%(user)s@%(host)s:%(file)s" % var to_location = config_file("/%(file)s" % var) if os.path.isfile(to_location): print("WARNING: The file %s exists" % to_location) if not yn_choice("Would you like to replace the file", default='y'): sys.exit(1) print(from_location) print(to_location) print("Copy cloudmesh file from %s to %s" % (from_location, to_location)) result = scp(from_location, to_location) print(result) sys.exit(0) # # ok # # if (arguments['projects'] and arguments['list']) : if arguments['projects'] and arguments['list']: projects = config.get('cloudmesh.projects') print(yaml.dump(projects, default_flow_style=False, indent=4)) sys.exit(0) # # OK, needs setting # if arguments['projects']: projects = config.projects('active') print("Please select from the following:") print("0 - Cancel") selected = False choices = [] while not selected: counter = 1 for name in projects: print(counter, "-" "%20s" % name) choices.append(name) counter += 1 print("Please select:") input = int(sys.stdin.readline()) if input == 0: print("Selection terminated") sys.exit(0) selected = (input > 0) and (input < counter) print("Selected: ", input) name = choices[input - 1] print(name) print("ERROR: THIS JUST SELECTS A PROJECT ID BUT DOES NOT SET IT") sys.exit(0) if arguments['init'] or name == 'init': output = arguments['--out'] username = arguments['--user'] or os.getenv('USER') location = path_expand(output) new_yaml_file = open(location, 'w+') user_yaml = cm_user().generate_yaml(username, 'cloudmesh') print(yaml.dump( user_yaml, default_flow_style=False), file=new_yaml_file) new_yaml_file.close() print("Written new yaml file in " + location) sys.exit(0) # # OK # if arguments['list'] or name == 'list': for name in config.cloudnames(): if 'cm_type' in config.cloud(name): print(name, "(%s)" % config.cloud(name)['cm_type']) sys.exit(0) # # NOT TESTED # if arguments['password']: oldpass = getpass("Current password: "******"New password: "******"New password (again): ") # TODO: some kind of password strength checking? if newpass1 == newpass2: config.change_own_password(name, oldpass, newpass1) else: print("New passwords did not match; password not changed.") sys.exit(0) # # OK, but does not display the username # if arguments['show'] or name == 'show' and arguments['passwords']: warning = "Your passwords will appear on the screen. Continue?" if yn_choice(warning, 'n'): me = ConfigDict(filename=config_file("/me.yaml")) banner("PASSWORDS") for name in me['password']: print("{0}: {1}".format(name, me['password'][name])) sys.exit(0) # # OK # if arguments['dump'] or name == 'dump': format = arguments['--format'] if format == 'yaml': d = dict(config) print(yaml.dump(d, default_flow_style=False)) elif format == 'dict' or format is None: print(config) sys.exit(0) # # NOT TESTED # if name in ['?', 'x']: if file is None: arguments['--out'] = "%s/%s" % (home, default_path) print("Please select from the following:") print("0 - Cancel") selected = False choices = [] while not selected: counter = 1 for name in config.cloudnames(): if 'cm_type' in config.cloud(name): print("{0} - {1:<30} ({2})".format(counter, name, config.cloud(name)['cm_type'])) choices.append(name) counter += 1 print("Please select:") input = int(sys.stdin.readline()) if input == 0: print("Selection terminated") sys.exit(0) selected = (input > 0) and (input < counter) print("Selected: ", input) name = choices[input - 1] output = arguments['--out'] # # OK # if name is not None: cloud = config.cloud(name) if not cloud: print("%s: The cloud '%s' is not defined." % ("CM ERROR", name)) print("Try instead:") for keyname in config.cloudnames(): print(" ", keyname) sys.exit(1) if cloud['cm_type'] == 'eucalyptus': if arguments['--project']: project = arguments['--project'] if not project in cloud: print("No such project %s defined in cloud %s." % (project, name)) sys.exit(1) else: project = config.cloud_default( name, 'project') or config.projects('default') if not project in cloud: print("Default project %s not defined in cloud %s." % (project, name)) sys.exit(1) rc_func = lambda name: config.rc_euca(name, project) else: rc_func = config.rc result = rc_func(name) # # OK # if arguments["-"]: print(result) else: if output is None: arguments['--out'] = "%s/%s" % (home, default_path) output = arguments['--out'] out = False if yn_choice("Would you like to review the information", default="y"): banner("WARNING: FIle will be written to " + output) print(result) print(banner("")) try: # First we try to open the file assuming it doesn't exist out = os.open( output, os.O_CREAT | os.O_EXCL | os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR) except OSError as oserr: # If file exists, offer to replace it if oserr.errno == 17: delete = raw_input( "'%s' exists; Overwrite it [N|y]? " % output) if delete.strip().lower() == 'y': out = os.open(output, os.O_TRUNC | os.O_WRONLY) if out: os.write(out, result) os.close(out)
def do_hpc(self, args, arguments): # noinspection PyPep8 """ :: Usage: hpc queue [--job=NAME][--cluster=CLUSTER][--format=FORMAT] hpc info [--cluster=CLUSTER][--format=FORMAT] hpc run list [ID] [--cluster=CLUSTER] hpc run output [ID] [--cluster=CLUSTER] hpc run rm [ID] [--cluster=CLUSTER] hpc run SCRIPT [--queue=QUEUE] [--t=TIME] [--N=nodes] [--name=NAME] [--cluster=CLUSTER][--dir=DIR][--group=GROUP][--format=FORMAT] hpc delete --job=NAME [--cluster=CLUSTER][--group=GROUP] hpc delete all [--cluster=CLUSTER][--group=GROUP][--format=FORMAT] hpc status [--job=name] [--cluster=CLUSTER] [--group=GROUP] hpc test --cluster=CLUSTER [--time=SECONDS] Options: --format=FORMAT the output format [default: table] Examples: Special notes if the group is specified only jobs from that group are considered. Otherwise the default group is used. If the group is set to None, all groups are used. cm hpc queue lists the details of the queues of the hpc cluster cm hpc queue --job=NAME lists the details of the job in the queue of the hpc cluster cm hpc info lists the details of the hpc cluster cm hpc run SCRIPT submits the script to the cluster. The script will be copied prior to execution into the home directory on the remote machine. If a DIR is specified it will be copied into that dir. The name of the script is either specified in the script itself, or if not the default naming scheme of cloudmesh is used using the same index incremented name as in vms fro clouds: cloudmes husername-index cm hpc delete all kills all jobs on the default hpc group cm hpc delete --job=NAME kills a job with a given name or job id cm default cluster=NAME sets the default hpc cluster cm hpc status returns the status of all jobs cm hpc status job=ID returns the status of the named job cm hpc test --cluster=CLUSTER --time=SECONDS submits a simple test job to the named cluster and returns if the job could be successfully executed. This is a blocking call and may take a long time to complete dependent on if the queuing system of that cluster is busy. It will only use one node/core and print the message #CLOUDMESH: Test ok in that is being looked for to identify if the test is successful. If time is used, the job is terminated after the time is elapsed. Examples: cm hpc queue cm hpc queue --job=xxx cm hpc info cm hpc delete --job=6 cm hpc delete all cm hpc status cm hpc status --job=6 cm hpc run uname cm hpc run ~/test.sh --cluster=india """ format = arguments['--format'] cluster = arguments['--cluster'] or Default.get_cluster() arguments["CLUSTER"] = cluster if cluster is None: Console.error("Default cluster doesn't exist") return batch = BatchProvider(cluster) if arguments["queue"]: name = arguments['--job'] result = batch.queue(cluster, format=format, job=name) Console.msg(result) elif arguments["info"]: Console.msg(batch.info(cluster, format)) elif arguments['delete'] and arguments['all']: group = arguments['--group'] or Default.get('group') if group is None: Console.error('set default group using: default group=<value> --cloud=general') return Console.ok(batch.delete(cluster,None,group)) elif arguments["delete"]: job = arguments['--job'] Console.ok(batch.delete(cluster, job)) elif arguments["status"]: name = arguments['--job'] result = batch.queue(cluster, format=format, job=name) Console.msg(result) elif arguments["test"]: time_secs = arguments['--time'] if time_secs: time = '00:00:' + time_secs else: time = '00:00:10' # give a default time of 10 secs print(batch.test(cluster, time)) elif arguments["run"] and arguments["list"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: print ("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments)) result = Experiment.list(cluster, id=arguments["ID"], format="list") if result is not None: print ("\n".join(result)) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) else: print ("# List of experiments on Cluster {CLUSTER}".format(**arguments)) ids = Experiment.list(cluster, id=None, format="list") if ids is not None: print (", ".join([str(i) for i in ids])) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) elif arguments["run"] and arguments["rm"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: force = yn_choice("Would you like to delete experiment {ID} on Cluster {CLUSTER}".format(**arguments)) if force: try: result = Experiment.rm(cluster, id=arguments["ID"]) Console.ok("Experiment {ID} on Cluster {CLUSTER} deleted".format(**arguments)) except: Console.error("Could not delete experiment {ID} on {CLUSTER}".format(**arguments)) else: result = Experiment.list(cluster, id=None, format="list") if result is not None: arguments['experiments'] = ", ".join([str(i) for i in result]) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) return "" force = yn_choice("Would you like to delete the experiments {experiments} on Cluster {CLUSTER}".format( **arguments)) if force: try: result = Experiment.rm(cluster, id=None) Console.ok("Experiments {experiments} on Cluster {CLUSTER} deleted".format(**arguments)) except: Console.error("Could delete the experiments on {CLUSTER}".format(**arguments)) return "" elif arguments["run"] and arguments["output"]: # hpc experiment list [--cluster=CLUSTER] if arguments["ID"]: print ("# List of experiment {ID} on Cluster {CLUSTER}".format(**arguments)) result = Experiment.output(cluster, id=arguments["ID"], format="list") if result is not None: print ("\n".join(result)) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) else: print ("# List of experiments on Cluster {CLUSTER}".format(**arguments)) ids = Experiment.output(cluster, id=None, format="list") if ids is not None: print (", ".join([str(i) for i in ids])) else: Console.error("Could not find experiment {ID} on {CLUSTER}".format(**arguments)) elif arguments["run"]: queue = arguments['--queue'] or Default.get('queue') # if not queue: # Console.error('set default queue using: default queue=<value>') # return group = arguments['--group'] or Default.get('group') if group is None: Console.error('set default group using: default group=<value> --cloud=general') return script = arguments['SCRIPT'] arg_dict = { '-name': arguments['--name'], '-p': queue, '-t': arguments['--t'], '-N': arguments['--N'] } result = batch.run(cluster, group, script, **arg_dict) if isinstance(result, dict): print (attribute_printer(result)) Console.ok("Experiment {count}: Started batch job {job_id} on {cluster}".format(**result)) else: Console.error(result) return ""
# it's safe to import any class definition now at the beginning of file # Fugang 02/06/2015 # # from cloudmesh.experiment.group import GroupManagement # GroupManage = GroupManagement(username) groups_list = GroupManage.get_groups_names_list() vms_in_group_list = {} if GroupName in groups_list: vms_in_group_list = GroupManage.list_items_of_group( GroupName, _type="VM")["VM"] if not arguments['--force'] and 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 if GroupName not in groups_list: GroupManage.create_group(GroupName) GroupManage.add_tag_to_group(GroupName, "cluster") # start VMs print("starting VMs...") arguments_temp = arguments arguments_temp['start'] = True arguments_temp['--name'] = None
for cloud in values['clouds']: values['clouds'][cloud]['default'] = {} values['clouds'][cloud]['default']['image'] = None values['clouds'][cloud]['default']['flavor'] = None content = open(filename_template, 'r').read() env = Environment(undefined=IgnoreUndefined) template = env.from_string(content) result = template.render(values) out_file = open(filename_tmp, 'w+') out_file.write(result) out_file.close() if not arguments["--force"]: if yn_choice("Review the new yaml file", default='n'): print(filename_tmp) os.system('less -E {0}'.format(filename_tmp)) if arguments["--force"]: shutil.copy(filename_out, filename_bak) os.rename(filename_tmp, filename_out) print("# Template: {0}".format(filename_template)) print("# Values : {0}".format(filename_values)) print("# Backup : {0}".format(filename_bak)) print("# Created : {0}".format(filename_out)) elif yn_choice("Move the new yaml file to {0}" .format(filename_out), default='y'): shutil.copy(filename_out, filename_bak) os.rename(filename_tmp, filename_out) print("# Template: {0}".format(filename_template)) print("# Values : {0}".format(filename_values))
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'])
def do_register(self, args, arguments): """ :: Usage: register info register new register clean [--force] register list ssh [--format=FORMAT] register list [--yaml=FILENAME][--info][--format=FORMAT] register cat [--yaml=FILENAME] register edit [--yaml=FILENAME] register export HOST [--password] [--format=FORMAT] register source HOST register merge FILEPATH register form [--yaml=FILENAME] register check [--yaml=FILENAME] register test [--yaml=FILENAME] register json HOST register remote [CLOUD] [--force] register india [--force] register CLOUD CERT [--force] register CLOUD --dir=DIR register env [--provider=PROVIDER] managing the registered clouds in the cloudmesh.yaml file. It looks for it in the current directory, and than in ~/.cloudmesh. If the file with the cloudmesh.yaml name is there it will use it. If neither location has one a new file will be created in ~/.cloudmesh/cloudmesh.yaml. Some defaults will be provided. However you will still need to fill it out with valid entries. Arguments: HOST the host name USER the user name FILEPATH the path of the file CLOUD the cloud name CERT the path of the certificate PROVIDER the provider or type of cloud [Default: openstack] Options: --provider=PROVIDER Provider to be used for cloud. Values are: openstack, azure, ec2. --version=VERSION Version of the openstack cloud. --openrc=OPENRC The location of the openrc file --password Prints the password --force ignore interactive questions and execute the action Description: register info It looks out for the cloudmesh.yaml file in the current directory, and then in ~/.cloudmesh register list [--yaml=FILENAME] [--name] [--info] lists the clouds specified in the cloudmesh.yaml file. If info is specified it also prints the location of the yaml file. register list ssh lists hosts from ~/.ssh/config register cat [--yaml=FILENAME] outputs the cloudmesh.yaml file register edit [--yaml=FILENAME] edits the cloudmesh.yaml file register export HOST [--format=FORMAT] prints the contents of an openrc.sh file based on the information found in the cloudmesh.yaml file. register remote CLOUD [--force] reads the Openstack OPENRC file from a remote host that is described in cloudmesh.yaml file. We assume that the file has already a template for this host. If not it can be created from other examples before you run this command. It uses the OS_OPENRC variable to locate the file and copy it onto your computer. register merge FILENAME Replaces the TBD in cloudmesh.yaml with the contents present in the named file register form [--yaml=FILENAME] interactively fills out the form wherever we find TBD. register check [--yaml=FILENAME] checks the yaml file for completness register test [--yaml=FILENAME] checks the yaml file and executes tests to check if we can use the cloud. TODO: maybe this should be in a test command register json host displays the host details in json format register remote CLOUD registers a remote cloud and copies the openrc file specified in the credentials of the cloudmesh.yaml register CLOUD CERT [--force] Copies the CERT to the ~/.cloudmesh/clouds/host directory and registers that cert in the coudmesh.yaml file. register CLOUD --dir Copies the entire directory from the cloud and puts it in ~/.cloudmesh/clouds/host For india, The directory would be copied to ~/.cloudmesh/clouds/india register env [--provider=PROVIDER] [HOSTNAME] Reads env OS_* variables and registers a new cloud in yaml, interactively. Default PROVIDER is openstack and HOSTNAME is localhost. """ # from pprint import pprint # pprint(arguments) def _get_config_yaml_file(arguments): filename = arguments["--yaml"] or "cloudmesh.yaml" filename = Config.find_file(filename) return filename def exists(filename): return os.path.isfile(filename) def export(host, output): config = ConfigDict("cloudmesh.yaml") credentials = dict( config["cloudmesh"]["clouds"][host]["credentials"]) if not arguments["--password"]: credentials["OS_PASSWORD"] = "******" if output is None: for attribute, value in credentials.iteritems(): print("export {}={}".format(attribute, value)) elif output == "table": print(attribute_printer(credentials)) else: print(dict_printer(credentials, output=output)) # TODO: bug csv does not work return "" if arguments["info"]: filename = _get_config_yaml_file(arguments) if os.path.isfile(filename): Console.ok("File '{}' exists. ok.".format(filename)) else: Console.error("File {} does not exist".format(filename)) return "" elif arguments["new"]: import shutil import cloudmesh_client.etc print(cloudmesh_client.etc.__file__) filename = os.path.join( os.path.dirname(cloudmesh_client.etc.__file__), "cloudmesh.yaml") Console.ok(filename) yamlfile = path_expand("~/.cloudmesh/cloudmesh.yaml") shutil.copyfile(filename, yamlfile) print("copy ") print("From: ", filename) print("To: ", yamlfile) # filename = _get_config_yaml_file(arguments) # if _exists(filename): # Console.ok("File '{}' exists. ok.".format(filename)) # else: # Console.error("File {} does not exist".format(filename)) return "" elif arguments["clean"]: filename = _get_config_yaml_file(arguments) force = arguments["--force"] or False if filename is not None: print(filename, force) if exists(filename): print("Delete cloudmesh.yaml file:", filename) if not force: force = yn_choice("Would you like to delete the " "cloudmesh.yaml file") print(force) if force: os.remove(filename) Console.ok("Deleted the file " + filename + ". ok.") else: Console.ok("Please use Y to delete the file.") pass else: Console.error("File {} does not exist".format(filename)) else: Console.error("No cloudmesh.yaml file found.") return "" elif arguments["cat"]: filename = _get_config_yaml_file(arguments) if exists(filename): with open(filename, 'r') as f: lines = f.read().split("\n") print('\n'.join(lines)) else: Console.error("File {} does not exist".format(filename)) return "" elif arguments["edit"]: filename = _get_config_yaml_file(arguments) if exists(filename): try: data = {"editor": os.environ["EDITOR"], "filename": filename} Console.ok("editing file " + filename) os.system("{editor} {filename}".format(**data)) except: Console.error("No EDITOR variable set in shell.") else: Console.error("File {} does not exist".format(filename)) return "" elif arguments['list'] and arguments['ssh']: output = arguments['--format'] or 'table' hosts = CloudRegister.list_ssh() print(print_list(hosts, output=output)) return "" elif arguments['list']: filename = _get_config_yaml_file(arguments) info = arguments["--info"] or False output = arguments["--format"] or "table" if not filename: Console.error("File {} doesn't exist".format(filename)) else: d = CloudRegister.list(filename, info=info, output=output) print(d) return "" elif arguments['check']: filename = _get_config_yaml_file(arguments) if not filename: Console.error("File {} doesn't exist".format( arguments["--yaml"] or 'cloudmesh.yaml')) else: CloudRegister.check_yaml_for_completeness(filename) return "" elif arguments['merge']: filename = arguments['FILENAME'] CloudRegister.from_file(filename) return elif arguments['test']: filename = _get_config_yaml_file(arguments) CloudRegister.test(filename) return "" elif arguments['form']: filename = _get_config_yaml_file(arguments) if not filename: Console.error("File {} doesn't exist".format( arguments["--yaml"] or 'cloudmesh.yaml')) else: CloudRegister.fill_out_form(filename) return "" elif arguments['source']: host = arguments['HOST'] config = ConfigDict("cloudmesh.yaml") credentials = dict( config["cloudmesh"]["clouds"][host]["credentials"]) # unset variables = list(os.environ) for attribute in variables: if attribute.startswith("OS_"): print("x ", attribute) del os.environ[attribute] # set for attribute, value in credentials.iteritems(): os.putenv(attribute, value) print("+ ", attribute) export(host, "table") return "" elif arguments['export']: output = arguments['--format'] host = arguments['HOST'] variables = list(os.environ) for attribute in variables: if attribute.startswith("OS_"): print("unset ", attribute) del os.environ[attribute] export(host, output) # elif arguments['rc']: # host = arguments['HOST'] # openrc = arguments['FILENAME'] # force = arguments['--force'] or False # # result = CloudRegister.read_rc_file(host, openrc, force) # credentials = dict(result) # # # output password as requested by user # if not arguments["--password"]: # credentials["OS_PASSWORD"] = "******" # print(row_table(credentials, order=None, # labels=["Variable", "Value"])) # return elif arguments['json']: host = arguments['HOST'] result = CloudRegister.get(host) if result: print(json.dumps(result, indent=4)) else: print("Cloud {:} is not described in cloudmesh.yaml".format( host)) return "" elif arguments['remote']: force = arguments['--force'] cloud = arguments['CLOUD'] if cloud is None: clouds = ["kilo"] else: clouds = [cloud] for cloud in clouds: CloudRegister.remote(cloud, force) export(cloud, "table") return "" elif arguments['CLOUD']: if arguments['CERT']: # path to the cacert.pem cloud = arguments['CLOUD'] path = arguments['CERT'] force = False if arguments['--force']: force = True CloudRegister.certificate(cloud, path, force) elif arguments['--dir']: cloud = arguments['CLOUD'] directory = arguments['--dir'] Console.ok(directory) CloudRegister.directory(cloud, directory) elif arguments['env']: try: CloudRegister.from_environ(arguments['--provider']) except Exception, e: import traceback print(traceback.format_exc()) print(e) return ""
# we have modified how the mongonenigne connects and # it's safe to import any class definition now at the beginning of file # Fugang 02/06/2015 # # from cloudmesh.experiment.group import GroupManagement # GroupManage = GroupManagement(username) groups_list = GroupManage.get_groups_names_list() vms_in_group_list = {} if GroupName in groups_list: vms_in_group_list = GroupManage.list_items_of_group(GroupName, _type="VM")["VM"] if not arguments['--force'] and 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 if GroupName not in groups_list: GroupManage.create_group(GroupName) GroupManage.add_tag_to_group(GroupName, "cluster") # start VMs print ("starting VMs...") arguments_temp = arguments arguments_temp['start'] = True arguments_temp['--name'] = None
for cloud in values['clouds']: values['clouds'][cloud]['default'] = {} values['clouds'][cloud]['default']['image'] = None values['clouds'][cloud]['default']['flavor'] = None content = open(filename_template, 'r').read() env = Environment(undefined=IgnoreUndefined) template = env.from_string(content) result = template.render(values) out_file = open(filename_tmp, 'w+') out_file.write(result) out_file.close() if not arguments["--force"]: if yn_choice("Review the new yaml file", default='n'): print(filename_tmp) os.system('less -E {0}'.format(filename_tmp)) if arguments["--force"]: shutil.copy(filename_out, filename_bak) os.rename(filename_tmp, filename_out) print("# Template: {0}".format(filename_template)) print("# Values : {0}".format(filename_values)) print("# Backup : {0}".format(filename_bak)) print("# Created : {0}".format(filename_out)) elif yn_choice("Move the new yaml file to {0}".format(filename_out), default='y'): shutil.copy(filename_out, filename_bak) os.rename(filename_tmp, filename_out) print("# Template: {0}".format(filename_template)) print("# Values : {0}".format(filename_values))