def shell_command_user(arguments): """ :: Usage: user list user info [ID] user id Administrative command to lists the users from LDAP Arguments: list list the users ID list the user with the given ID Options: -v verbose mode """ user = cm_user() if (arguments["info"]): id = arguments["ID"] if id is None: config = cm_config() id = config.username() banner("User Information in Mongo for user: {0}".format(id)) user = cm_user() result = user.info(id) pprint(result) elif (arguments["list"]): user = cm_user() list_of_users = user.list_users() pprint(list_of_users) print() print("=========================") num = len(list_of_users) print(str(num) + " users listed") elif arguments['id']: config = cm_config() print(config.username()) else: print("WRONG PARAMETERS") return
def do_limits(self, args, arguments): """ Usage: limits [CLOUD] limits help | -h Current usage data with limits on a selected project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_limits.__doc__) else: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) usage_with_limits = self.cm_mongo.usage_with_limits(def_cloud, userid) print(row_table(usage_with_limits, order=None, labels=["Limits", "(Used/Max)"])) return usage_with_limits
def chkActivation(self, userId): ret = False userinfo = cm_user().info(userId) if "activeclouds" in userinfo["defaults"] and\ len(userinfo["defaults"]["activeclouds"]) > 0: ret = True return ret
def do_quota(self, args, arguments): """ Usage: quota [CLOUD] quota help | -h quota limit on a current project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print(self.do_quota.__doc__) else: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) quota = self.cm_mongo.quota(def_cloud, userid) print(row_table(quota, order=None, labels=["Variable", "Value"])) return quota
def activate_userinfo(self, cm_user_id): # Set userinfo from cloudmesh.user.cm_user import cm_user self.cm_user = cm_user() self.userinfo = self.cm_user.info(cm_user_id) self.userid = cm_user_id
def do_quota(self, args, arguments): """ Usage: quota [CLOUD] quota help | -h quota limit on a current project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_quota.__doc__) else: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) quota = self.cm_mongo.quota(def_cloud, userid) print(row_table(quota, order=None, labels=["Variable", "Value"])) return quota
def test_list(self): user = cm_user() list_of_users = user.list_users() pprint(list_of_users) print() print("=========================") num = len(list_of_users) print(str(num) + " users listed")
def __init__(self): self.filename = config_file("/cloudmesh.yaml") self.config = cm_config(filename=self.filename) self.cm_user_id = self.config.get("cloudmesh.hpc.username") self.clouds = self.config.get("cloudmesh.clouds") self.user_obj = cm_user() self.profile = self.config.profile() self.mongo = cm_mongo()
def test_gregor(self): banner("ME") id = ConfigDict(filename=config_file("/me.yaml")).get("portalname") user = cm_user() result = user.info(id) pprint(result) pass
def _start_cm_user(self): if not self.started_cm_user: try: self.user_obj = cm_user() except: Console.error("There is a problem with " "cm_user object initialization") return self.started_cm_user = True
def delete_vm_confirm(): time_now = datetime.now().strftime("%Y-%m-%d %H:%M") # filter() config = cm_config() c = cm_mongo() c.activate(cm_user_id=g.user.id) userdata = g.user username = userdata.id user_obj = cm_user() user = user_obj.info(username) clouds = c.servers(cm_user_id=username) images = c.images(cm_user_id=username) flavors = c.flavors(cm_user_id=username) os_attributes = [ "name", "status", "addresses", "flavor", "id", "image", "user_id", "metadata", "key_name", "created", ] cloud_filters = None filtered_clouds = clouds cloud = request.form["cloud"] select = request.form.getlist("selection_" + cloud) if select != None and cloud != None: session["delete_selection"] = (cloud, select) if "delete_selection" in session: print "writing selection to session" # print filtered_clouds selected_cloud_data = {} selected_cloud_data[cloud] = get_selected_clouds( filtered_clouds[session["delete_selection"][0]], session["delete_selection"][1] ) return render_template( "mesh/cloud/delete_vms.html", address_string=address_string, attributes=os_attributes, updated=time_now, clouds=selected_cloud_data, config=config, user=user, images=images, flavors=flavors, filters=cloud_filters, ) else: return render_template("error.html", type="Deleting VMs", error="No VMs to delete. ")
def setup(self): self.t = cm_template(path_expand(self.filename)) self.user = cm_user() try: self.setup_inventory() except: print("=" * 40) print("setup_inventory() failed. ldap test will not be performed") print("=" * 40)
def do_limits(self, args, arguments): """ :: Usage: limits [CLOUD] [--format=json] limits help | -h Current usage data with limits on a selected project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_limits.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return usage_with_limits = self.cm_mongo.usage_with_limits(cloudid, userid) if arguments["--format"] is None: print(row_table(usage_with_limits, order=None, labels=[ "Limits", "(Used/Max)" ])) elif 'json' in arguments["--format"]: print(json.dumps(usage_with_limits, indent=4)) else: Console.error('Quota is not supported.') return usage_with_limits
def mongo(): register() filename = "~/.futuregrid/cloudmesh.yaml" banner("reding data from {0}".format(filename)) config = cm_config(filename=filename) profile = config.profile() element = { "firstname" : profile["firstname"], "lastname" : profile["lastname"], "uidNumber" : profile["uid"], "phone" : profile["phone"], "gidNumber" : profile["gid"], "address" : profile["address"][0], "cm_user_id" : config.get("cloudmesh.hpc.username"), "email" : profile["email"], "activeclouds" : config.get("cloudmesh.active") } projects = {} active = config.get("cloudmesh.projects.active") if active != ['None']: projects["active"] = active completed = config.get("cloudmesh.projects.completed") if completed != ['None']: projects["completed"] = completed if projects != {}: element["projects"] = projects # get keys and clean the key titles (replace '.' with '_' due to mongo restriction) keys = config.get("cloudmesh.keys.keylist") for keytitle in keys.keys(): if "." in keytitle: keycontent = keys[keytitle] newkeytitle = keytitle.replace(".", "_") del keys[keytitle] keys[newkeytitle] = keycontent element['keys'] = keys pprint (element) # hpc username as key username = element["cm_user_id"] # populate the local userinfo into the same mongo as though it were from LDAP. userstore = cm_userLDAP() userstore.updates(username, element) user_obj = cm_user() user_obj.init_defaults(username)
def do_limits(self, args, arguments): """ :: Usage: limits [CLOUD] [--format=json] limits help | -h Current usage data with limits on a selected project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print(self.do_limits.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return usage_with_limits = self.cm_mongo.usage_with_limits( cloudid, userid) if arguments["--format"] is None: print( row_table(usage_with_limits, order=None, labels=["Limits", "(Used/Max)"])) elif 'json' in arguments["--format"]: print(json.dumps(usage_with_limits, indent=4)) else: Console.error('Quota is not supported.') return usage_with_limits
def register(): config = cm_config() cm_user_id = config.get("cloudmesh.hpc.username") clouds = config.get("cloudmesh.clouds") user_obj = cm_user() for cloudname in config.cloudnames(): user_obj.set_credential(cm_user_id, cloudname, clouds[cloudname]['credentials'])
def shell_command_storm(arguments): """ :: Usage: storm list storm ID storm register ID [--kind=KIND] [ARGUMENTS...] Arguments: list list the available high level services to be provisioned. ID list the user with the given ID ARGUMENTS The name of the arguments that need to be passed Options: --kind=KIND the kind of the storm. It can be chef, puppet, or other frameworks. At this time we will focus on chef [default: chef]. -v verbose mode Description: Command to invoce a provisioning of high level services such as provided with chef, puppet, or other high level DevOps Tools. If needed the machines can be provisioned prior to a storm with rain. Together this forms a rain storm. """ user = cm_user() if (arguments["list"]): print("not implemented") print("list") elif not (arguments["list"] or arguments["register"]): id = arguments["ID"] print("ID", id) elif (arguments["register"]): id = arguments["ID"] kind = arguments['--kind'] print(arguments) else: print("WRONG PARAMETERS") return
def mongo_flavors(): if not with_active_clouds(): error = "No Active Clouds set!" msg = "Please <a href='/mesh/register/clouds'>Register and Activate</a> a Cloud First" return render_template('error.html', type="Refreshing Clouds", error=error, msg=msg) time_now = datetime.now().strftime("%Y-%m-%d %H:%M") # filter() config = cm_config() # getting user info userdata = g.user username = userdata.id user_obj = cm_user() user = user_obj.info(username) c = cm_mongo() c.activate(cm_user_id=username) # c.refresh(types=["flavors"]) clouds = c.flavors(cm_user_id=username) os_attributes = [ 'id', 'name', 'vcpus', 'ram', 'disk', 'cm_refresh', ] # fake entry # user['defaults']['pagestatus'] = {'sierra': {"open": "false", "flavor": "3"}} # commented by HC on Nov. 8, 2013 # The following check is repetive because these attributes are added # by 'cm_user.init_defaults(username)' # this method will be called when page is loaded """ if 'pagestatus' not in user['defaults']: user['defaults']['pagestatus'] = init_user_pagestatus([cloud_name for cloud_name in clouds]) """ # ONLY for debug, if the Accordion does not work, please uncomment it # log.debug("mesh_flavors, before render, user defaults: {0}".format(user['defaults'])) return render_template('mesh/cloud/mesh_flavors.html', address_string=address_string, attributes=os_attributes, updated=time_now, clouds=clouds, user=user, config=config)
def start(self, cloud, cm_user_id): """Launch a new VM instance with a default setting for flavor, image and key name""" # Hyungro Lee - Sep 4th, 2014 from cloudmesh.user.cm_user import cm_user userinfo = cm_user().info(cm_user_id) prefix = userinfo['defaults']['prefix'] index = userinfo['defaults']['index'] flavor = userinfo['defaults']['flavors'][cloud] # or flavor = "2" small image = userinfo['defaults']['images'][cloud] # or image = '02cf1545-dd83-493a-986e-583d53ee3728' # ubuntu-14.04 key = "%s_%s" % (cm_user_id, userinfo['defaults']['key']) meta = {'cm_owner': cm_user_id} result = self.vm_create(cloud, prefix, index, flavor, image, key, meta, cm_user_id) # increase index after the completion of vm_create() cm_user().set_default_attribute(cm_user_id, "index", int(index) + 1) return result
def delete_defaults(): filename = "~/.futuregrid/cloudmesh.yaml" banner("reding data from {0}".format(filename)) config = cm_config(filename=filename) username = config.get("cloudmesh.hpc.username") print username user = cm_user() user.set_defaults(username, {}) # user.set_default_attribute(username, 'images', {}) info(username)
def delete_defaults(): filename = config_file("/cloudmesh.yaml") banner("reading data from {0}".format(filename)) config = cm_config(filename=filename) username = config.get("cloudmesh.hpc.username") print username user = cm_user() user.set_defaults(username, {}) # user.set_default_attribute(username, 'images', {}) user.info(username)
def login(): error = None form = LoginForm() if request.method == 'POST' and form.validate_on_submit(): form.error = None try: # idp = cm_userLDAP () # idp.connect("fg-ldap", "ldap") # user = idp.find_one({'cm_user_id': form.username.data}) # print "MONGO USER" # banner("LDAPUSER") # pprint (user) banner("CM_USER") user_obj = cm_user() user = user_obj.info(form.username.data) except Exception, e: print traceback.format_exc() error = "LDAP server not reachable" error += str(e) return render_template('error.html', form=form, type="Can not reach LDAP", msg="") if user is None: form.error = 'Login Invalid' elif user['cm_user_id'] != form.username.data: form.error = 'Login Invalid' elif user_obj.authenticate(form.username.data, form.password.data): print "LOGIN USER" g.user = load_user(form.username.data) ret = login_user(g.user) identity_changed.send(current_app._get_current_object(), identity=Identity(g.user.id)) return redirect(request.args.get('next') or '/') else: form.error = 'Login Invalid'
def delete_vm_confirm(): time_now = datetime.now().strftime("%Y-%m-%d %H:%M") # filter() config = cm_config() c = cm_mongo() c.activate(cm_user_id=g.user.id) userdata = g.user username = userdata.id user_obj = cm_user() user = user_obj.info(username) clouds = c.servers(cm_user_id=username) images = c.images(cm_user_id=username) flavors = c.flavors(cm_user_id=username) os_attributes = [ 'name', 'status', 'addresses', 'flavor', 'id', 'image', 'user_id', 'metadata', 'key_name', 'created' ] cloud_filters = None filtered_clouds = clouds cloud = request.form["cloud"] select = request.form.getlist("selection_" + cloud) if select is not None and cloud is not None: session["delete_selection"] = (cloud, select) if "delete_selection" in session: print "writing selection to session" # print filtered_clouds selected_cloud_data = {} selected_cloud_data[cloud] = get_selected_clouds( filtered_clouds[session["delete_selection"][0]], session["delete_selection"][1]) return render_template('mesh/cloud/delete_vms.html', address_string=address_string, attributes=os_attributes, updated=time_now, clouds=selected_cloud_data, config=config, user=user, images=images, flavors=flavors, filters=cloud_filters) else: return render_template('error.html', type="Deleting VMs", error="No VMs to delete. ")
def do_usage(self, args, arguments): """ Usage: usage [CLOUD] [--start=START] [--end=END] usage help | -h Usage data on a current project (tenant) Arguments: CLOUD Cloud name to see the usage START start date of usage (YYYY-MM-DD) END end date of usage (YYYY-MM-DD) help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print(self.do_usage.__doc__) else: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) usage = self.cm_mongo.usage(def_cloud, userid) # server usages need to be supressed. # e.g. {u'hours': 24.00000006388889, u'uptime': 1960234, # u'started_at': u'2014-10-07T23:03:57.000000', u'ended_at': None, # u'name': u'hrlee-server-2zuvke4wujud', u'tenant_id': # u'3e6eaf1d913a48f694a7bc0fbb027507', u'instance_id': # u'2c9d24e0-7453-4f83-84b7-f8c0254a574f', u'state': # u'active', u'memory_mb': 2048, u'vcpus': 1, u'flavor': # u'm1.small', u'local_gb': 20} try: usage['server_usages'] = str(len( usage['server_usages'])) + " vms" except: pass print(row_table(usage, order=None, labels=["Variable", "Value"])) return usage
def do_quota(self, args, arguments): """ :: Usage: quota [CLOUD] [--format=json] quota help | -h quota limit on a current project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print(self.do_quota.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return quota = self.cm_mongo.quota(cloudid, userid) if arguments["--format"] is None: print( row_table(quota, order=None, labels=["Variable", "Value"])) elif 'json' in arguments["--format"]: print(json.dumps(quota, indent=4)) else: Console.error('Quota is not supported.') return quota
def do_quota(self, args, arguments): """ :: Usage: quota [CLOUD] [--format=json] quota help | -h quota limit on a current project (tenant) Arguments: CLOUD Cloud name to see the usage help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_quota.__doc__) else: userid = self.cm_config.username() self.cm_mongo.activate(userid) cloudid = arguments["CLOUD"] if cloudid is None: cloudid = self.get_cloud_name(userid) # if an id is still not found print error if cloudid is None: Console.error('Please set a default cloud.') return quota = self.cm_mongo.quota(cloudid, userid) if arguments["--format"] is None: print( row_table(quota, order=None, labels=["Variable", "Value"])) elif 'json' in arguments["--format"]: print(json.dumps(quota, indent=4)) else: Console.error('Quota is not supported.') return quota
def do_usage(self, args, arguments): """ Usage: usage [CLOUD] [--start=START] [--end=END] usage help | -h Usage data on a current project (tenant) Arguments: CLOUD Cloud name to see the usage START start date of usage (YYYY-MM-DD) END end date of usage (YYYY-MM-DD) help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() if arguments["help"] or arguments["-h"]: print (self.do_usage.__doc__) else: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) usage = self.cm_mongo.usage(def_cloud, userid) # server usages need to be supressed. # e.g. {u'hours': 24.00000006388889, u'uptime': 1960234, # u'started_at': u'2014-10-07T23:03:57.000000', u'ended_at': None, # u'name': u'hrlee-server-2zuvke4wujud', u'tenant_id': # u'3e6eaf1d913a48f694a7bc0fbb027507', u'instance_id': # u'2c9d24e0-7453-4f83-84b7-f8c0254a574f', u'state': # u'active', u'memory_mb': 2048, u'vcpus': 1, u'flavor': # u'm1.small', u'local_gb': 20} try: usage['server_usages'] = str(len(usage['server_usages'])) + " vms" except: pass print(row_table(usage, order=None, labels=["Variable", "Value"])) return usage
def mongo_save_pagestatus(): username = g.user.id user_obj = cm_user() user = user_obj.info(username) cmongo = cm_mongo() user_cloud_list = cmongo.active_clouds(username) # commented/modified by HC on Nov. 8, 2013 # The following check is repetive because these attributes are added # by 'cm_user.init_defaults(username)' # this method will be called when page is loaded """ previous_page_status = {} if 'pagestatus' in user['defaults']: previous_page_status = user['defaults']['pagestatus'] for user_cloud in user_cloud_list: if user_cloud not in previous_page_status: previous_page_status[user_cloud] = "false" user['defaults']['pagestatus'] = previous_page_status """ # ONLY for debug, if the Accordion does not work, please uncomment it # log.debug("request.json page status: {0}".format(request.json)) current_page_status = request.json status_options = { "open": "pagestatus", "flavor": "flavors", "image": "images", } for cloud_name in current_page_status: if cloud_name in user_cloud_list: for item in current_page_status[cloud_name]: user["defaults"][status_options[item]][ cloud_name] = current_page_status[cloud_name][item] # ONLY for debug, if the Accordion does not work, please uncomment it # log.debug("mesh_save_status, before save, user defaults: {0}".format(user['defaults'])) user_obj.set_defaults(username, user['defaults']) # ONLY for debug, if the Accordion does not work, please uncomment it # BEGIN debug # user_obj = cm_user() # user = user_obj.info(username) # log.debug("mesh_save_status, after save, user defaults: {0}".format(user['defaults'])) # END debug return "ok"
def get_command_list_refresh_default_setting(username): ''' value to define the default behaviour of command list, if True, then refresh before list as default ''' try: user_obj = cm_user() except: Console.error("There is a problem with " "cm_user object initialization") return False defaults_data = user_obj.info(username)['defaults'] if "shell_command_list_refresh_default_setting" not in defaults_data or\ defaults_data["shell_command_list_refresh_default_setting"] in [None, 'none']: defaults_data["shell_command_list_refresh_default_setting"] = True user_obj.set_defaults(username, defaults_data) return defaults_data["shell_command_list_refresh_default_setting"] else: return defaults_data["shell_command_list_refresh_default_setting"]
def get_default_print_format(username): ''' the funtion will try to find the default printing form from db_defaults if db_defaults has no item 'shell_print_format', the function will set 'shell_print_format' = table ''' format_type = None user_obj = cm_user() userdata = user_obj.info(username) try: format_type = userdata['defaults']['shell_print_format'] except: pass if format_type in [None, 'none']: userdata['defaults']['shell_print_format'] = "table" user_obj.set_defaults(username, userdata['defaults']) userdata = user_obj.info(username) format_type = userdata['defaults']['shell_print_format'] return format_type
def get_command_list_refresh_default_setting(username): ''' value to define the default behaviour of command list, if True, then refresh before list as default ''' try: user_obj = cm_user() except: Console.error("There is a problem with " "cm_user object initialization") return False defaults_data = user_obj.info(username)['defaults'] if "shell_command_list_refresh_default_setting" not in defaults_data or \ defaults_data["shell_command_list_refresh_default_setting"] in [None, 'none']: defaults_data["shell_command_list_refresh_default_setting"] = True user_obj.set_defaults(username, defaults_data) return defaults_data["shell_command_list_refresh_default_setting"] else: return defaults_data["shell_command_list_refresh_default_setting"]
def managekeys(): idp = cm_user() user_mongo = idp.info(g.user.id) # else: # user_mongo = get_ldap_user_from_yaml() print 70 * "-" pprint(user_mongo) if 'defaults' not in user_mongo: user_mongo['defaults'] = {} msg = '' """ keys: default: name 1 keylist: name 1: $HOME/.ssh/id_rsa.pub # replaced with the key name 2: $HOME/.ssh/id_rsa2.pub # replaced with the key bla: key ssh-rsa AAAAB3.....zzzz keyname """ if request.method == 'POST' and request.form.has_key('keyname'): keyname = request.form['keyname'] fileorstring = request.form['keyorpath'] # # TODO: keys not defined # if keys.defined(keyname): msg = "Key name already exists. Please delete the key '{0}' before proceeding.".format( keyname) else: try: keys.set(keyname, fileorstring, expand=True) msg = 'Key %s added successfully' % keyname keys.write() except Exception, e: keys.delete(keyname) msg = e
def do_cluster(self, args, arguments): """ :: Usage: cluster list [--format=FORMAT] cluster create <name> [--count=<count>] [--ln=<LoginName>] [--cloud=<CloudName>] [--image=<imgName>|--imageid=<imgId>] [--flavor=<flavorName>|--flavorid=<flavorId>] [--force] cluster show <name> [--format=FORMAT] [--column=COLUMN] [--detail] cluster remove <name> [--grouponly] Description: Cluster Management cluster list list the clusters cluster create <name> --count=<count> --ln=<LoginName> [options...] 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, it is better to choose a unused group name cluster show <name> show the detailed information about the cluster VMs cluster remove <name> [--grouponly] remove the cluster and its VMs, if you want to remove the cluster(group name) without removing the VMs, use --grouponly flag Arguments: <name> cluster name or group name Options: --count=<count> give the number of VMs to add into the cluster --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) --grouponly remove the group only without removing the VMs, otherwise cluster remove command will remove all the VMs of this cluster FORMAT output format: table, json, csv COLUMN customize what information to display, for example: --column=status,addresses prints the columns status and addresses --detail for table print format, a brief version is used as default, use this flag to print detailed table """ #pprint(arguments) # defer the import of Clusters, as this further imports classes from cloudmesh.experiment.group, # which connects to mongo automatically due to the using of mongoengine. from cloudmesh.shell.clusters import Clusters # cannot move up at the beginning. clusterobj = Clusters() userobj = cm_user() config = cm_config() username = config.username() if arguments['list']: try: clusters_list = clusterobj.list_clusters() except Exception, err: Console.error(str(err)) return if arguments['--format']: if arguments['--format'] not in ALLOWED_PRINT_FORMAT: Console.error("wrong print format: {0}. (allowed print format: {1})".format(format_type, ", ".join(ALLOWED_PRINT_FORMAT))) return else: p_format = arguments['--format'] else: p_format = None shell_commands_dict_output(username, clusters_list, print_format=p_format, firstheader="cluster", header=[["num of nodes", "num_of_nodes"], ["num of active nodes", "num_of_active_nodes"]])
def profile(): config = cm_config() userdata = g.user username = userdata.id user_obj = cm_user() user = user_obj.info(username) if 'defaults' not in user: user['defaults'] = {} user.set_defaults(username, {}) if request.method == 'POST': # print "REQUEST" # pprint(request.__dict__) # print "OOOOOO", request.form if 'field-project' in request.form: user['defaults']['project'] = request.form['field-project'] if 'field-securitygroup' in request.form: user['defaults']['securitygroup'] = request.form[ 'field-securitygroup'] if 'field-index' in request.form: user['defaults']['index'] = request.form['field-index'] if 'field-prefix' in request.form: user['defaults']['prefix'] = request.form['field-prefix'] if 'field-default-cloud' in request.form: user['defaults']['cloud'] = request.form['field-default-cloud'] if 'field-key' in request.form: user['defaults']['key'] = request.form['field-key'] user['defaults']['activeclouds'] = [] regclouds = user['defaults']['registered_clouds'] for cloudname in regclouds: form_key = 'field-cloud-activated-{0}'.format(cloudname) if form_key in request.form: print "ACTIVE IN FORM", cloudname user['defaults']['activeclouds'].append(cloudname) ''' user['profile']['firstname'] = request.form['field-firstname'] user['profile']['lastname'] = request.form['field-lastname'] user['profile']['phone'] = request.form['field-phone'] user['profile']['email'] = request.form['field-email'] ''' print "setting the values" user_obj.set_defaults(username, user['defaults']) # print request.form["field-cloud-activated-" + value] # print "setting the cloud values" # print config # config.write() # print "WRITING DONE" user = user_obj.info(username) # print "UD", user["defaults"]["activeclouds"] time_now = datetime.now().strftime("%Y-%m-%d %H:%M") return render_template('user/profile.html', updated=time_now, # just to populate security groups configuration=config['cloudmesh'], user=user, userdata=userdata )
def cm_info(self): if self._cm_info is None: cmu = cm_user() self._cm_info = cmu.info(self.id) cmu.init_defaults(self.id) return self._cm_info
def _load_data(self): # Get profile and project data from LDAP cmu = cm_user.cm_user() self._data = cmu.info(self.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 ~/.futuregrid directory [default: sierra.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 ~/.futuregrid/novarc """ default_path = '.futuregrid/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'] != 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'] var['file'] = ".futuregrid/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 >> new_yaml_file, yaml.dump(user_yaml, default_flow_style=False) 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("/.futuregrid/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 cm_info(self): if self._cm_info is None: cmu = cm_user() self._cm_info = cmu.info(self.id) return self._cm_info
def initialize_user(self): self.set_credentials() element = { "firstname": self.profile["firstname"], "lastname": self.profile["lastname"], "uidNumber": self.profile["uid"], "phone": self.profile["phone"], "gidNumber": self.profile["gid"], "address": self.profile["address"][0], "cm_user_id": self.config.get("cloudmesh.hpc.username"), "email": self.profile["email"], "activeclouds": self.config.get("cloudmesh.active") } projects = {} active = self.config.get("cloudmesh.projects.active") if active != ['None']: projects["active"] = active completed = self.config.get("cloudmesh.projects.completed") if completed != ['None']: projects["completed"] = completed if projects != {}: element["projects"] = projects # get keys and clean the key titles (replace '.' with '_' due # to mongo restriction) keys = self.config.get("cloudmesh.keys.keylist") for keytitle in keys.keys(): keycontent = keys[keytitle] if keytype(keycontent) == "file": keycontent = get_key_from_file(keycontent) if keycontent: keycontent = keycontent.strip() keys[keytitle] = keycontent else: print( "The specified key file does not exist and thus ingored!" ) print("You can run ssh-keygen to generate one key pair") del keys[keytitle] break if "." in keytitle: newkeytitle = keytitle.replace(".", "_") del keys[keytitle] keys[newkeytitle] = keycontent element['keys'] = keys pprint(element) # hpc username as key username = element["cm_user_id"] # populate the local userinfo into the same mongo as though it # were from LDAP. userstore = cm_userLDAP() userstore.updates(username, element) self.user_obj = cm_user() self.user_obj.init_defaults(username) # # info disabled due to NameError: global name 'info' is not # defined info(username) # ------------------------------------------------------------------------------ # added by Mark X. on Aug.25 2014 add clouds information to # mongo when initialize user iformation in mongo self.mongo.db_clouds.remove({ 'cm_kind': 'cloud', 'cm_user_id': username }) cloudsdict = self.config.get("cloudmesh", "clouds") for key in cloudsdict: Database.import_cloud_to_mongo(cloudsdict[key], key, username) print("cloud '{0}' added.".format(key))
def initialize_user(self): self.set_credentials() element = { "firstname": self.profile["firstname"], "lastname": self.profile["lastname"], "uidNumber": self.profile["uid"], "phone": self.profile["phone"], "gidNumber": self.profile["gid"], "address": self.profile["address"][0], "cm_user_id": self.config.get("cloudmesh.hpc.username"), "email": self.profile["email"], "activeclouds": self.config.get("cloudmesh.active") } projects = {} active = self.config.get("cloudmesh.projects.active") if active != ['None']: projects["active"] = active completed = self.config.get("cloudmesh.projects.completed") if completed != ['None']: projects["completed"] = completed if projects != {}: element["projects"] = projects # get keys and clean the key titles (replace '.' with '_' due # to mongo restriction) keys = self.config.get("cloudmesh.keys.keylist") for keytitle in keys.keys(): keycontent = keys[keytitle] if keytype(keycontent) == "file": keycontent = get_key_from_file(keycontent) if keycontent: keycontent = keycontent.strip() keys[keytitle] = keycontent else: print("The specified key file does not exist and thus ingored!") print("You can run ssh-keygen to generate one key pair") del keys[keytitle] break if "." in keytitle: newkeytitle = keytitle.replace(".", "_") del keys[keytitle] keys[newkeytitle] = keycontent element['keys'] = keys pprint(element) # hpc username as key username = element["cm_user_id"] # populate the local userinfo into the same mongo as though it # were from LDAP. userstore = cm_userLDAP() userstore.updates(username, element) self.user_obj = cm_user() self.user_obj.init_defaults(username) # # info disabled due to NameError: global name 'info' is not # defined info(username) # ------------------------------------------------------------------------------ # added by Mark X. on Aug.25 2014 add clouds information to # mongo when initialize user iformation in mongo self.mongo.db_clouds.remove({ 'cm_kind': 'cloud', 'cm_user_id': username }) cloudsdict = self.config.get("cloudmesh", "clouds") for key in cloudsdict: Database.import_cloud_to_mongo(cloudsdict[key], key, username) print("cloud '{0}' added.".format(key))
def do_register(self, args, arguments): ''' Usage: register [options] NAME Arguments: NAME Name of the cloud to be registered Options: -a --act Activate the cloud to be registered -d --deact Deactivate the cloud ''' config = cm_config() cm_user_id = config.username() user_obj = cm_user() user = user_obj.info(cm_user_id) cloudtypes = {} error = {} registered = {} cloudname = arguments['NAME'] # check all the registered clouds. for cloud in user['defaults']['registered_clouds']: registered[cloud] = True if (cloud == cloudname and not (arguments['--act'] or arguments['--deact'])): print("Cloud {0} is already registered.".format(cloud)) return for cloud in config.get("cloudmesh.clouds"): cloudtypes[cloud] = \ config['cloudmesh']['clouds'][cloud]['cm_type'] # get credentials from cm_user -- files. credentials = user_obj.get_credentials(cm_user_id) # find if credentials for cloudname are present. if cloudname in credentials: if 'credential' in credentials[cloudname]: # Credentials specified credential = credentials[cloudname]['credential'] else: # Credentials not present in files credentials[cloudname] = None else: print('Please specify the right cloud name.') error[cloudname] = '' if cloudtypes[cloudname] == "openstack": d = {} # credentials not present in files. if credentials[cloudname] is None: print('This will set credentials.') d = { 'OS_USERNAME': cm_user_id, 'OS_PASSWORD': '', 'OS_TENANT_NAME': '' } d['OS_PASSWORD'] = getpass.getpass("Please enter password: "******"Please specify the tenant: ") d['CM_CLOUD_TYPE'] = cloudtypes[cloudname] user_obj.set_credential(cm_user_id, cloudname, d) # elif cloudtypes[cloudname] == "ec2": error[cloudname] = '' d = {"CM_CLOUD_TYPE": cloudtypes[cloudname]} if credentials[cloudname] is None: d = { 'EC2_URL': credential['EC2_URL'], 'EC2_ACCESS_KEY': credential['EC2_ACCESS_KEY'], 'EC2_SECRET_KEY': credential['EC2_SECRET_KEY'] } user_obj.set_credential(cm_user_id, cloudname, d) mongoClass = cm_mongo() cloud = mongoClass.get_cloud(cm_user_id=cm_user_id, cloud_name=cloudname, force=True) if cloud: registered[cloudname] = True if cloudname not in user['defaults']['registered_clouds']: user['defaults']['registered_clouds'].append(cloudname) user_obj.set_defaults(cm_user_id, user['defaults']) else: registered[cloudname] = False print("The cloud could not be registered.") if registered[cloudname] is True: if arguments['--act']: try: if cloudname not in user['defaults']['activeclouds']: (user['defaults']['activeclouds']).append(cloudname) user_obj.set_defaults(cm_user_id, user['defaults']) print("Cloud {0} set to active".format(cloudname)) except: # create_dict(user, "defaults", "activeclouds") log.info("ERROR user defaults activecloud does not exist") if arguments['--deact']: try: if cloudname in user['defaults']['activeclouds']: active = user['defaults']['activeclouds'] active.remove(cloudname) user['defaults']['activeclouds'] = active user_obj.set_defaults(cm_user_id, user['defaults']) print("Cloud {0} deactived".format(cloudname)) except: # create_dict(user, "defaults", "activeclouds") log.info("ERROR user defaults activecloud does not exist") return
def shell_command_experiment_group(arguments): """ Usage: group list [--format=FORMAT] group create NAME group remove NAME group add item NAME TYPE VALUE group remove item NAME TYPE VALUE group show NAME [TYPE] [--format=FORMAT] Arguments: NAME name of the group TYPE type of the item in the group, e.g. vm VALUE value of item to add, e.g. vm name Options: -v verbose mode --format=FORMAT output format: table, json, csv Description: group list lists the groups group create creates a new group group remove removes a group group add item addes an item of a type to a group group remove item removes an item of a type from a group group show lists items of a group Examples: group add item sample vm samplevm add vm named samplevm to group sample group show sample vm --format=json list all VMs of group sample in json format Example: group create experiment_1 vm start last = vm label group add experiment_1 vm last group create experiment_2 vm start last = vm info label # prints the vm label /prefix + number ipno = vm info ip # prints the ip of the last vm ipno = vm info ip gvonlasz_1 # get ip of vm with label gvonlasz_1 group add expermiment_2 ip ipno groups are just tuples i can have multiple Kinds in the tuple mongoengine class groupObject def add (... name, kind, attribute ...) def printer ( ... kind, printfunction, name...) def getter ( .... kind, name) def getter ( .... kind, name ...) if kind == "vm": vm = get vm from mongo return vm elif kind = "image" iamge = get image from mongo return iamge .... def vmPrinter ( .... vm ...) print vm.ip print vm.name .... def imagePrinter ( .... image ...) print image.size print image.name .... g = groupObject() g.printer("vm", cmPrinter) g.printer("image", imagePrinter) """ # Changed the scope of this import. from cloudmesh.experiment.group import GroupManagement from cloudmesh.experiment.group_usage import add_item_to_group name = arguments["NAME"] type = arguments["TYPE"] value = arguments["VALUE"] config = cm_config() username = config.username() # print username user = cm_user() GroupManage = GroupManagement(username) ''' if arguments["info"]: print("Default experiment group:", user.get_defaults(username)["group"]) ''' if arguments["list"]: try: res = GroupManage.get_groups_names_list() except Exception, err: Console.error(str(err)) return d = {} d["groups"] = res if arguments['--format']: p_format = arguments['--format'] else: p_format = None shell_commands_dict_output(username, d, print_format=p_format, table_format="key_list", indexed=True)
def do_launcher(self, args, arguments): """ :: Usage: launcher start MENU launcher stop STACK_NAME launcher list launcher show STACK_NAME launcher menu [--column=COLUMN] [--format=FORMAT] launcher import [FILEPATH] [--force] launcher export FILEPATH launcher help | -h An orchestration tool with Chef Cookbooks Arguments: MENU Name of a cookbook STACK_NAME Name of a launcher FILEPATH Filepath COLUMN column name to display FORMAT display format (json, table) help Prints this message Options: -v verbose mode """ log.info(arguments) self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() self._id = "t_stacks" if arguments["help"] or arguments["-h"]: print(self.do_launcher.__doc__) elif arguments['show'] and arguments['STACK_NAME']: print("NOT IMPLEMENTED") return elif arguments['menu']: userid = self.cm_config.username() launchers = self.cm_mongo.launcher_get(userid) if launchers.count() == 0: Console.warning( "no launcher in database, please import launcher first" "(launcher import [FILEPATH] [--force])") return else: d = {} for launcher in launchers: d[launcher['cm_launcher']] = launcher if "_id" in d[launcher['cm_launcher']]: del d[launcher['cm_launcher']]['_id'] columns = None if arguments['--column']: if arguments['--column'] != "all": columns = [ x.strip() for x in arguments['--column'].split(',') ] else: columns = ['name', 'description'] 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(userid, d, print_format=p_format, firstheader="launcher", header=columns # vertical_table=True ) 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(userid, d, print_format=p_format, firstheader="launcher_id", header=columns # vertical_table=True ) elif arguments['start'] and arguments['MENU']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) self.cm_mongo.activate(userid) config_launcher = cm_config_launcher() userinfo = self.cm_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") cookbook = arguments['MENU'] s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook, get_rand_string()) dummy = "123456789" # doing nothing. just for test try: t_url = \ config_launcher['cloudmesh']['launcher']['default']['template'] except: # If key is missing (KeyError), new cloudmesh_launcher.yaml # needs to be copied to ~/.cloudmesh t_url = \ "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml" param = { 'KeyName': keynamenew, 'Cookbook': cookbook, 'dummy': dummy } # test for openmpi, hadoop if cookbook[:6] == "hadoop" or cookbook[:7] == "openmpi": privatekey, publickey = generate_keypair() try: t_url = \ config_launcher['cloudmesh']['launcher']['recipes'][cookbook]['template'] except: # If key is missing (KeyError), new cloudmesh_launcher.yaml # needs to be copied to ~/.cloudmesh t_url = \ ("https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/ubuntu-14.04/" + str(cookbook) + "-cluster/" + str(cookbook) + "-cluster.yaml") param = { 'KeyName': keynamenew, 'PublicKeyString': publickey, 'PrivateKeyString': privatekey } if cookbook[:9] == "hadoop2.7": param["UserName"] = userid log.debug(def_cloud, userid, s_name, t_url, param) 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['stop'] and arguments['STACK_NAME']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) s_id = arguments['STACK_NAME'] self.cm_mongo.activate(userid) res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid, server=s_id) log.debug(res) return res elif arguments['import']: filepath = config_file("/cloudmesh_launcher.yaml") if arguments['FILEPATH']: filepath = arguments['FILEPATH'] try: filename = path_expand(filepath) fileconfig = ConfigDict(filename=filename) except Exception, err: Console.error( "error while loading '{0}', please check".format(filepath)) print(traceback.format_exc()) print(sys.exc_info()[0]) return try: recipes_dict = fileconfig.get("cloudmesh", "launcher", "recipies") except: Console.error("error while loading recipies from the file") # print recipes_dict userid = self.cm_config.username() launcher_names = [] launchers = self.cm_mongo.launcher_get(userid) for launcher in launchers: launcher_names.append(launcher['cm_launcher'].encode("ascii")) for key in recipes_dict: if key in launcher_names: if arguments['--force']: self.cm_mongo.launcher_remove(userid, key) self.cm_mongo.launcher_import(recipes_dict[key], key, userid) print("launcher '{0}' overwritten.".format(key)) else: print("ERROR: launcher '{0}' exists, " "please remove it first, or use " "'--force' when adding".format(key)) else: self.cm_mongo.launcher_import(recipes_dict[key], key, userid) print("launcher '{0}' added.".format(key))
def do_stack(self, args, arguments): """ Usage: stack start NAME [--template=TEMPLATE] [--param=PARAM] stack stop NAME stack show NAME stack list [--refresh] [--column=COLUMN] [--format=FORMAT] stack help | -h An orchestration tool (OpenStack Heat) Arguments: NAME stack name help Prints this message Options: -v verbose mode """ self.cm_mongo = cm_mongo() self.cm_config = cm_config() self.cm_user = cm_user() log.info(arguments) if arguments["help"] or arguments["-h"]: print(self.do_stack.__doc__) elif arguments['show'] and arguments['NAME']: print ("NOT IMPLEMENTED") return elif arguments['start'] and arguments['NAME']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) t_url = arguments['--template'] param = arguments['--param'] s_name = arguments['NAME'] self.cm_mongo.activate(userid) res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid, servername=s_name, template_url=t_url, parameters=param) print(res) return res elif arguments['stop'] and arguments['NAME']: userid = self.cm_config.username() def_cloud = self.get_cloud_name(userid) s_name = arguments['NAME'] self.cm_mongo.activate(userid) res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid, server=s_name) return res elif arguments['list']: userid = self.cm_config.username() if arguments['--refresh']: self.cm_mongo.activate(userid) self.cm_mongo.refresh(cm_user_id=userid, types=[self._id]) d = self.cm_mongo.stacks(cm_user_id=userid) columns = None if arguments['--column']: if arguments['--column'] != "all": columns = [x.strip() for x in arguments['--column'].split(',')] else: columns = ['stack_name','description', 'stack_status', 'creation_time', 'cm_cloud'] 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 for k, v in d.iteritems(): for k0, v0 in v.iteritems(): if '_id' in v0: del v0['_id'] shell_commands_dict_output(userid, v, print_format=p_format, firstheader="stack_id", header=columns)