def wsgi(action="start"): pidfile = config_file("/uwsgi/cloudmesh_uwsgi.pid") logfile = config_file("/uwsgi/cloudmesh_uwsgi.log") command = False user_pidfile = os.path.expanduser(pidfile) user_logfile = os.path.expanduser(logfile) if action == "restart": wsgi("stop") action = "start" if action == "start": command = "uwsgi -s /tmp/cloudmesh.sock -M -p 2 -t 10 \ --daemonize={0} \ --pidfile={1} \ --chown-socket=cloudmesh:www-data \ --chdir=cloudmesh_web \ --module=server \ --callable=app".format(user_logfile, user_pidfile) elif (action == "stop" or action == "kill"): command = "kill -INT `cat {0}`".format(user_pidfile) elif (action == "reload"): command = "kill -HUP `cat {0}`".format(user_pidfile) if (command): local(command)
def setup_inventory(self): banner("Read Dicts") self.sample_user = ConfigDict(filename=config_file("/me.yaml")) self.portalname = self.sample_user.get("portalname") print("PORTALNAME", self.portalname) print("SAMPLE USER", self.sample_user) banner("create user from template, duplicates cm init generate me") t = cm_template(config_file("/etc/cloudmesh.yaml")) pprint(set(t.variables())) self.config = t.replace(kind="dict", values=self.sample_user) print(type(self.config)) print(self.config) # # BUG? # self.idp = cm_userLDAP() self.idp.connect("fg-ldap", "ldap") self.idp.refresh() ldap_info = self.idp.get(self.portalname) print(ldap_info) print(type(self.config)) self.config['cloudmesh']['projects'] = ldap_info['projects'] self.config['cloudmesh']['keys'] = ldap_info['keys'] try: self.config['cloudmesh']['projects']['deafult'] = ldap_info[ 'projects']['active'][0] except: print("ERROR: you have no projects")
def configure(): """configure india environment for cloudmesh rapid deployment""" # running on server mode with external port listening filename = config_file('/cloudmesh_server.yaml') print "modify ->", filename yaml_file_replace(filename='/cloudmesh_server.yaml', replacements={ 'browser: True': 'browser: False', 'host: 127.0.0.1': 'host: 0.0.0.0' }) # port 5000 needs to be open # ec2secgroup_openport('india', 5000) # now managed via nova before vm is started. # new way to replace an attribute in yaml filename = config_file("/cloudmesh.yaml") print "modify ->", filename replacements = { "cloudmesh.clouds.india.cm_service_url_type": "internalURL", } yaml_attribute_replace(filename, replacements, indent_by=4) print "Configuration changes have been made successfully"
class Test_cloudmesh: filename = config_file("/cloudmesh.yaml") def setup(self): pass def tearDown(self): pass def test_policy(self): HEADING() pass def test_simulator(self): HEADING() # hosts = ["1", "2", "3", "a1", "a2", "a3", # "b1", "b2", "b3", "c1", "c2", "c3", ] # image = "a" hosts = ["i066"] image = "ubuntu1304v2btsync" Provisioner = ProvisionerSimulator (success, result) = (None, None) p = Provisioner() (success, result) = p.provision(hosts, image) print(result) print(success) pass
def new_cloudmesh_yaml(): """ Generate yaml files from the templates in etc directory if yaml files exist, this function won't perform. - check existance - create ~/.futuregrid - copy templates from etc/ to $HOME/.futuregrid """ dir = config_file("") # Make sure we do not clobber existing non-empty yaml files. # (installation may create empty yaml files to avoid errors) if os.path.exists(dir): for f in glob.glob(dir + "/*.yaml"): if (os.path.getsize(f) > 0): print "ERROR: the (nonempty) yaml file '{0}' already exists.".format(f) print " The 'new' command will not overwrite files." sys.exit(1) else: os.makedirs(dir, stat.S_IRWXU) filename_tmp = dir + '/cloudmesh-new.yaml' cloudmesh_out = dir + '/cloudmesh.yaml' filename_bak = cloudmesh_out cloudmesh_template = "etc/cloudmesh.yaml" filename_values = dir + "/me.yaml" # copy the yaml files def cp_urw(file_from, file_to): print "copy {0} -> {1}".format(file_from, file_to) shutil.copy(file_from, file_to) os.chmod(file_to, stat.S_IRWXU) # Copy yaml files from etc directoy to the destination for file_from in glob.glob("etc/*.yaml"): file_to = dir + "/" + file_from.replace("etc/", "") cp_urw(file_from, file_to) # Copy me-none.yaml to me.yaml which is filled with TBD cp_urw(dir + "/me-none.yaml", dir + "/me.yaml") # me_values = "etc/me-none.yaml" # me_template = "etc/me-all.yaml" me_file = dir + "/me.yaml" try: # do simple yaml load result = open(me_file, 'r').read() values = yaml.safe_load(Template(result).substitute(os.environ)) # values = yaml.safe_load(Template(result).substitute(os.environ)) # print json.dumps(values, indent=4) except Exception, e: print "ERROR: There is an error in the yaml file", e sys.exit(1)
def new_cloudmesh_yaml(): """ Generate yaml files from the templates in etc directory if yaml files exist, this function won't perform. - check existance - create ~/.futuregrid - copy templates from etc/ to $HOME/.futuregrid """ dir = config_file("") # Make sure we do not clobber existing non-empty yaml files. # (installation may create empty yaml files to avoid errors) if os.path.exists(dir): for f in glob.glob(dir + "/*.yaml"): if (os.path.getsize(f) > 0): print "ERROR: the (nonempty) yaml file '{0}' already exists.".format( f) print " The 'new' command will not overwrite files." sys.exit(1) else: os.makedirs(dir, stat.S_IRWXU) filename_tmp = dir + '/cloudmesh-new.yaml' cloudmesh_out = dir + '/cloudmesh.yaml' filename_bak = cloudmesh_out cloudmesh_template = "etc/cloudmesh.yaml" filename_values = dir + "/me.yaml" # copy the yaml files def cp_urw(file_from, file_to): print "copy {0} -> {1}".format(file_from, file_to) shutil.copy(file_from, file_to) os.chmod(file_to, stat.S_IRWXU) # Copy yaml files from etc directoy to the destination for file_from in glob.glob("etc/*.yaml"): file_to = dir + "/" + file_from.replace("etc/", "") cp_urw(file_from, file_to) # Copy me-none.yaml to me.yaml which is filled with TBD cp_urw(dir + "/me-none.yaml", dir + "/me.yaml") # me_values = "etc/me-none.yaml" # me_template = "etc/me-all.yaml" me_file = dir + "/me.yaml" try: # do simple yaml load result = open(me_file, 'r').read() values = yaml.safe_load(Template(result).substitute(os.environ)) # values = yaml.safe_load(Template(result).substitute(os.environ)) # print json.dumps(values, indent=4) except Exception, e: print "ERROR: There is an error in the yaml file", e sys.exit(1)
class Test_cloudmesh: username = ConfigDict( filename=config_file("/cloudmesh.yaml")).get("cloudmesh.hpc.username") filename = "etc/cloudmesh.yaml" def setup(self): self.idp = cm_userLDAP() self.idp.connect("fg-ldap", "ldap") self.idp.refresh() def tearDown(self): pass def test_me(self): print("USERNAME", self.username) user = self.idp.find_one({'cm_user_id': self.username}) print(user) def test_list(self): users = self.idp.list() pprint(users) pprint(self.idp.users) def test_auth(self): password = getpass.getpass() if self.idp.authenticate(self.username, password): print("SUCCESS") else: print("FAILED")
def get_ldap_user_from_yaml(): me = ConfigDict(filename=config_file("/me.yaml")) d = {} for element in ["firstname", "lastname", "email", "phone", "address"]: d[element] = me.get("profile.{0}".format(element)) d["cm_user_id"] = me.get("portalname") d["gidNumber"] = 0 d["uidNumber"] = 0 if "gidNumber" in me.keys(): d["gidNumber"] = me.get("gidNumber") if "uidNumber" in me.keys(): d["uidNumber"] = me.get("uidNumber") d["projects"] = me.get("projects") # # copy the keys # d['keys'] = me.get("keys.keylist") return d
def _load_projects(self): if not self.cm_shell_project_loaded: filename = config_file("/cloudmesh.yaml") self.projects = cm_projects(filename) if self.echo: log.info( "Reading project information from -> {0}".format(filename)) self.cm_shell_project_loaded = True
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 __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 display_launch_table(): launcher_config = ConfigDict(filename=config_file("/cloudmesh_launcher.yaml")) launch_recipies = launcher_config.get("cloudmesh.launcher.recipies") columns = launcher_config.get("cloudmesh.launcher.columns") return render_template('mesh/mesh_launch.html', recipies=launch_recipies, columns=columns, )
def test_getitem_server(self): HEADING() filename = config_file("/cloudmesh_server.yaml") config = ConfigDict(filename=filename) print(config) existing = config.get("cloudmesh.server.mongo.db") test1 = existing is not None print("QUERY", existing) print("Port", config.get("cloudmesh.server.mongo.port"))
def _load_keys_from_yaml(self): try: filename = config_file("/cloudmesh.yaml") if self.echo: log.info( "Reading keys information from -> {0}".format(filename)) self.keys = cm_keys_yaml(filename) self.keys_loaded = True except: print("ERROR: could not find the keys in %s" % filename)
def _get_value_from_yaml(filepath, column_keys): dir = config_file("") cm_file = dir + filepath try: result = open(cm_file, 'r').read() values = yaml.safe_load(Template(result).substitute(os.environ)) except Exception, e: print "ERROR: There is an error in the yaml file", e sys.exit(1)
def password(): user_config = cm_config(filename=config_file("/cloudmesh.yaml")) user = user_config.cloud('india')['credentials'] server_config = ConfigDict(filename=config_file("/cloudmesh_server.yaml")) server = server_config.get('cloudmesh.server.keystone.india') print(" ".join([ "keystone", "--os-username", server['OS_USERNAME'], "--os-password", server['OS_PASSWORD'], "--os-tenant-name", server['OS_TENANT_NAME'], "--os-auth-url", server['OS_AUTH_URL'], "user-password-update", "--pass", user['OS_PASSWORD'], user['OS_USERNAME'] ])) keystone("--os-username", server['OS_USERNAME'], "--os-password", server['OS_PASSWORD'], "--os-tenant-name", server['OS_TENANT_NAME'], "--os-auth-url", server['OS_AUTH_URL'], "user-password-update", "--pass", user['OS_PASSWORD'], user['OS_USERNAME'])
def __init__(self): config = ConfigDict(filename=config_file("/cloudmesh_server.yaml")) port = config['cloudmesh']['server']['mongo']['port'] get_mongo_db("manage", DBConnFactory.TYPE_MONGOENGINE) #db = connect('manage', port=port) self.users = User.objects() # thism may be wrong meta = {"db_alias": "default"}
def _load_keys_from_yaml(self): try: filename = config_file("/cloudmesh.yaml") if self.echo: log.info("Reading keys information from -> {0}" .format(filename)) self.keys = cm_keys_yaml(filename) self.keys_loaded = True except: print("ERROR: could not find the keys in %s" % filename)
def check(username=None): """clean the dirs""" if username is None: # bug for some reason the get method does not work # useranme = ConfigDict(filename=config_file("/cloudmesh.yaml")).get("cloudmesh.hpc.username") username = ConfigDict(filename=config_file( "/cloudmesh.yaml"))["cloudmesh"]["hpc"]["username"] print "Username: "******"india"]: local("ssh %s@%s.futuregrid.org hostname -a" % (username, host))
class cm_config_flavor(ConfigDict): """ reads the information contained in the file cloudmesh_flavor.yaml """ filename = config_file("/cloudmesh_flavor.yaml") def __init__(self, filename=None): if filename is None: filename = self.filename ConfigDict.__init__(self, filename=filename, kind="flavor")
def metric_index(): metric = "metric-summary" term = "last_3_months" config = ConfigDict(filename=config_file( "/cloudmesh_server.yaml"))["cloudmesh"]["server"]["metric"] address = "{0}:{1}/{2}/{3}".format(config["host"], config["port"], metric, term) r = requests.get(address) return render_template('/metric/index.html', data=r.text)
class cm_config_launcher(ConfigDict): """ reads the information contained in the file ~/.futuregrid/cloudmesh_launcher.yaml """ filename = config_file("/cloudmesh_launcher.yaml") def __init__(self, filename=None): if filename is None: filename = self.filename ConfigDict.__init__(self, filename=filename, kind="launcher")
def metric_index(): metric = "metric-summary" term = "last_3_months" config = ConfigDict(filename=config_file( "/cloudmesh_server.yaml"))["cloudmesh"]["server"]["metric"] address = "{0}:{1}/{2}/{3}".format( config["host"], config["port"], metric, term) r = requests.get(address) return render_template('/metric/index.html', data=r.text)
def view(link=""): """run the browser""" from cloudmesh.config.ConfigDict import ConfigDict server_config = ConfigDict(filename=config_file("/cloudmesh_server.yaml")) host = server_config.get("cloudmesh.server.webui.host") port = server_config.get("cloudmesh.server.webui.port") url_link = "http://{0}:{1}/{2}".format(host, port, link) local("%s %s" % (web_browser, url_link))
def LOGGER(filename): """creates a logger with the given name. You can use it as follows:: log = cloudmesh.util.LOGGER(__file__) log.error("this is an error") log.info("this is an info") log.warning("this is a warning") """ pwd = os.getcwd() name = filename.replace(pwd, "$PWD") try: (first, name) = name.split("site-packages") name = "... site" + name except: pass loglevel = logging.CRITICAL try: level = grep("loglevel:", config_file("/cloudmesh_server.yaml")).strip().split(":")[1].strip().lower() if level == "debug": loglevel = logging.DEBUG elif level == "info": loglevel = logging.INFO elif level == "warning": loglevel = logging.WARNING else: level = logging.CRITICAL except: # print "LOGLEVEL NOT FOUND" loglevel = logging.DEBUG log = logging.getLogger(name) log.setLevel(loglevel) formatter = logging.Formatter( 'CM {0:>50}:%(lineno)s: %(levelname)6s - %(message)s'.format(name)) #formatter = logging.Formatter( # 'CM {0:>50}: %(levelname)6s - %(module)s:%(lineno)s %funcName)s: %(message)s'.format(name)) handler = logging.StreamHandler() handler = logging.StreamHandler() handler.setFormatter(formatter) log.addHandler(handler) return log
def yaml_file_replace(filename='/cloudmesh_xxx.yaml', replacements={}): filename = config_file(filename) with open(filename, 'r') as f: content = f.read() for _old, _new in replacements.iteritems(): content = content.replace(_old, _new) outfile = open(filename, 'w') outfile.write(content) outfile.close()
class Test_cloudmesh: # filename = None # filename = "credentials-example-keys.yaml" # filename = config_file("/cloudmesh-new.yaml") filename = config_file("/cloudmesh.yaml") def setup(self): self.projects = cm_projects(self.filename) def tearDown(self): pass def test00_wrong_file(self): HEADING() try: self.projects = cm_projects("wrong file") except: pass def test01_print(self): HEADING() print(self.projects) pass def test02_dump(self): HEADING() print(self.projects.dump()) pass def test03_active(self): HEADING() print(self.projects.names("active")) pass def test04_default(self): HEADING() print(self.projects.names("default")) pass def test05_default(self): HEADING() print(self.projects.names("completed")) pass def test06_wrong_status(self): HEADING() try: print(self.projects.names("wrong")) except Exception, e: print(e) pass
def activate_project(self, project): """ this routine is wrong and has been copied from a deprecated code""" self.credentials = credentials_rc("eucalyptus") self.credentials.location = config_file("/india/eucalyptus/") + \ project + "/eucarc" self.credentials.type('eucalyptus') self.access_key = self.credentials._get_rc_variable("accesskey") self.secret_key = self.credentials._get_rc_variable("secretkey") print(self.access_key) print(self.secret_key)
def _set_value_to_yaml(filepath, data): dir = config_file("") cm_file = dir + filepath # make a backup bak = _make_a_backup(cm_file) print "[%s] backup made" % bak # Write yaml with open(cm_file, 'w') as outfile: outfile.write(yaml.dump(data, default_flow_style=False)) print "[%s] updated" % cm_file
def launch_servers(): launcher_config = ConfigDict(filename=config_file("/cloudmesh_launcher.yaml")) celery_config = ConfigDict(filename=config_file("/cloudmesh_celery.yaml")) launch_recipies = launcher_config.get("cloudmesh.launcher.recipies") server = request.form.get("server") name_index = request.form.get("name_index") return_dict = dict(launch_recipies[server][int(name_index)]) return_dict["user"] = "******" # default user.... change to read from session return_dict["server"] = server parameters = launch_recipies[server][int(name_index)]["parameters"] # load the needed set of parameters for parameter in parameters: parameters[parameter] = request.form.get("parameter_{0}".format(parameter)) return_dict["parameters"] = parameters return_dict["name"] = launch_recipies[server][int(name_index)]["name"] queue = celery_config = celery_config.get("cloudmesh.workers.launcher.queue") task_launch.apply_async(queue=queue, args=[return_dict]) return "Task has been submitted to the queue.... <br/><br/>Data sent was:<br/>" + str(return_dict)
def display_launch_table(): """ To satisfy Pylint. Will update with proper comments """ if request.method == 'POST': print "HHHHHH", request.form.keys() for key in request.form.keys(): print key, ":", request.form[key] else: print "HEY JUDE" launcher_config = ConfigDict( filename=config_file("/cloudmesh_launcher.yaml")) launch_recipies = launcher_config.get("cloudmesh.launcher.recipies") return render_template('mesh/mesh_launch.html', recipies=launch_recipies)
def LOGGER(filename): """creates a logger with the given name. You can use it as follows:: log = cloudmesh.util.LOGGER(__file__) log.error("this is an error") log.info("this is an info") log.warning("this is a warning") """ pwd = os.getcwd() name = filename.replace(pwd, "$PWD") try: (first, name) = name.split("site-packages") name = "... site" + name except: pass loglevel = logging.CRITICAL try: level = grep("loglevel:", config_file( "/cloudmesh_server.yaml")).strip().split(":")[1].strip().lower() if level.upper() == "DEBUG": loglevel = logging.DEBUG elif level.upper() == "INFO": loglevel = logging.INFO elif level.upper() == "WARNING": loglevel = logging.WARNING elif level.upper() == "ERROR": loglevel = logging.ERROR else: level = logging.CRITICAL except: # print "LOGLEVEL NOT FOUND" loglevel = logging.DEBUG log = logging.getLogger(name) log.setLevel(loglevel) formatter = logging.Formatter( 'CM {0:>50}:%(lineno)s: %(levelname)6s - %(message)s'.format(name)) # formatter = logging.Formatter( # 'CM {0:>50}: %(levelname)6s - %(module)s:%(lineno)s %funcName)s: %(message)s'.format(name)) handler = logging.StreamHandler() handler = logging.StreamHandler() handler.setFormatter(formatter) log.addHandler(handler) return log
def get_rcfiles(fpath=None): dir = config_file("") read_values = ["OS_TENANT_NAME", "OS_USERNAME", "OS_PASSWORD", "OS_AUTH_URL"] #, "OS_CACERT"] rcfile_path = dir + "/clouds/" new_values = {} for filepath in glob.glob(rcfile_path + "/*/*rc"): filename = os.path.basename(filepath) cloud_name = os.path.basename( os.path.normpath(filepath.replace(filename, ""))) new_values[cloud_name] = get_variables(filepath, read_values) return new_values
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 shell_command_open_web(arguments): """ Usage: web [--fg|--cm] [LINK] Arguments: LINK the link on the localhost cm server is opened. Options: -v verbose mode --fg opens a link on the FG portal --cm opens a link on the CM portal Description: Opens a web page with the specified link """ link = arguments["LINK"] if link is None or link == "/": link = "" web_browser = "firefox" if sys.platform == 'darwin': web_browser = "open" if arguments["--fg"]: location = "https://portal.futuregrid.org" elif arguments["--cm"]: location = "https://cloudmesh.futuregrid.org" else: try: filename = config_file("/cloudmesh_server.yaml") server_config = ConfigDict(filename=filename) host = server_config.get("cloudmesh.server.webui.host") port = server_config.get("cloudmesh.server.webui.port") location = "http://{0}:{1}".format(host, port) except Exception, e: print("ERROR: some error reading from the config file") print(e) return
def metric_project(project_id): config = ConfigDict(filename=config_file( "/cloudmesh_server.yaml"))["cloudmesh"]["server"]["metric"] base_url = "project-summary" address = "{0}:{1}/{2}/{3}".format( config["host"], config["port"], base_url, project_id) try: r = requests.get(address) pprint(r.json()) data = dict(yaml.load(r.text)) data = data["message"] return render_template('/metric/project.html', data=data) except Exception, e: print e error = str(e) return render_template('error.html', error=error)
def display_mongo_qstat_refresh(host=None): celery_config = ConfigDict(filename=config_file("/cloudmesh_celery.yaml")) log.info ("qstat refresh request {0}".format(host)) # timeout = 15; config = cm_config() user = config["cloudmesh"]["hpc"]["username"] if host is None: hosts = ["india.futuregrid.org", "lima.futuregrid.org", "sierra.futuregrid.org", "hotel.futuregrid.org", "alamo.futuregrid.org"] elif host in ['bravo.futuregrid.org', 'echo.futuregrid.org', 'delta.futuregrid.org']: hosts = ['india.futuregrid.org'] else: hosts = [host] error = "" try: pbs = pbs_mongo() # queue = celery_config = celery_config.get("cloudmesh.workers.qstat.queue") # res = tasks.refresh_qstat.apply_async(queue=queue, priority=0, args=[hosts]) for host in hosts: pbs.activate(host, user) pbs.refresh_qstat(host) # error = res.get(timeout=timeout) except Exception, e: print traceback.format_exc() error = "{0}".format(e) log.error(error) return render_template('error.html', error=error, type="Some error in qstat", msg="")
def display_mongo_qstat_refresh(host=None): celery_config = ConfigDict(filename=config_file("/cloudmesh_celery.yaml")) log.info("qstat refresh request {0}".format(host)) # timeout = 15; config = cm_config() user = config["cloudmesh"]["hpc"]["username"] if host is None: hosts = ["india.futuregrid.org"] elif host in ['bravo.futuregrid.org', 'echo.futuregrid.org', 'delta.futuregrid.org']: hosts = ['india.futuregrid.org'] else: hosts = [host] error = "" try: pbs = pbs_mongo() # queue = celery_config = celery_config.get("cloudmesh.workers.qstat.queue") # res = tasks.refresh_qstat.apply_async(queue=queue, priority=0, args=[hosts]) for host in hosts: pbs.activate(host, user) log.info("refresh qstat: {0} {1}".format(host, user)) pbs.refresh_qstat(host) # error = res.get(timeout=timeout) except Exception, e: print traceback.format_exc() error = "{0}".format(e) log.error(error) category = "qstat-{0}".format(host) flash(error, category=str(category))
def generate_yaml(self, id, basename): ''' Generates the content for a yaml file based on the passed parameters. :param id: The username for which we want to create the yaml file :type id: String :param basename: The base name of the yaml file in the etc directory. Allowed values are 'me' and 'cloudmesh' :type basename: String ''' """id = username""" """basename = me, cloudmesh""" log.info("generate {1} yaml {0}".format(id, basename)) result = self.info(id) result['password'] = self.get_credentials(id) etc_filename = config_file("/etc/{0}.yaml".format(basename)) t = cm_template(etc_filename) out = t.replace(kind='dict', values=result) return out
except Exception, e: print "ERROR: There is an error in the yaml file", e for cloud in values['clouds']: values['clouds'][cloud]['default'] = {} values['clouds'][cloud]['default']['image'] = None values['clouds'][cloud]['default']['flavor'] = None banner("%s -> %s" % (filename_values, filename_template)) env = Environment(undefined=IgnoreUndefined) template = env.from_string(content) result = template.render(values) print result if arguments["generate"]: filename_tmp = config_file('/cloudmesh-new.yaml') filename_out = config_file('/cloudmesh.yaml') filename_bak = backup_name(filename_out) filename_template = config_file("/etc/cloudmesh.yaml") if arguments["generate"] and (arguments["me"]): filename_values = config_file("/me.yaml") elif arguments["generate"] and (arguments["yaml"]): filename_values = config_file("/me.yaml") elif (arguments["generate"] and arguments["none"]): filename_values = config_file("/etc/me-none.yaml") elif arguments["FILENAME"] is not None: filename_values = path_expand(arguments["FILENAME"]) # print me_filename # print etc_filename try:
from oauth2client import tools # Parser for command-line arguments. parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, parents=[tools.argparser]) # CLIENT_SECRETS is name of a file containing the OAuth 2.0 information for this # application, including client_id and client_secret. You can see the Client ID # and Client secret on the APIs page in the Cloud Console: # <https://cloud.google.com/console#/project/855417837151/apiui> #CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json') CLIENT_SECRETS = config_file('/client_secrets.json') # Set up a Flow object to be used for authentication. # Add one or more of the following scopes. PLEASE ONLY ADD THE SCOPES YOU # NEED. For more information on using scopes please see # <https://developers.google.com/+/best-practices>. FLOW = client.flow_from_clientsecrets(CLIENT_SECRETS, scope=[ 'https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly', ], message=tools.message_if_missing(CLIENT_SECRETS)) def main(argv):
def init_shell_command(arguments): """ Usage: init [--force] generate yaml init [--force] generate me init [--force] generate none init [--force] generate FILENAME init list [KIND] [--json] init list clouds [--file=FILENAME] [--json] init inspect --file=FILENAME init fill --file=FILENAME [VALUES] Initializes cloudmesh from a yaml file Arguments: generate generates a yaml file yaml specifies if a yaml file is used for generation the file is located at ~/.futuregrid/me.yaml me same as yaml none specifies if a yaml file is used for generation the file is located at ~/.futuregrid/etc/none.yaml FILENAME The filename to be generated or from which to read information. VALUES yaml file with the velues to be sed in the FILENAME KIND The kind of the yaml file. Options: --force force mode does not ask. This may be dangerous as it overwrites the ~/.futuregrid/cloudmesh.yaml file --file=FILENAME The file --json make the output format json -v verbose mode Description: init list [KIND] [--json] list the versions and types of the yaml files in the ~/.futuregrid and ~/.futuregrid/etc directories. init list clouds [--file=FILENAME] Lists the available clouds in the configuration yaml file. init inspect --file=FILENAME print the variables in the yaml template """ if arguments["inspect"]: filename = arguments['--file'] if filename is None: filename = config_file('/cloudmesh.yaml') content = open(filename, 'r').read() t = cm_template(filename) sorted_vars = sorted(set(t.variables())) print "\n".join(sorted_vars) # banner("PARSER") # env = Environment() # ast = env.parse(content) # for v in meta.find_undeclared_variables(ast): # print v if arguments["list"] and not arguments["clouds"]: dirs = [path_expand(config_dir + '/*.yaml'), path_expand(config_dir + '/etc/*.yaml')] file_list = [] for dir in dirs: file_list.extend(glob.glob(dir)) vector = {} vector['kind'] = [] vector['yaml_version'] = [] vector['meta'] = [] vector['filename'] = [] for filename in file_list: values = {'kind': "-", 'yaml_version': "-", 'meta': "-"} head_of_file = find_meta (filename) values = {'kind': "-", 'yaml_version': "-", 'meta': "-"} for line in head_of_file: if ":" in line: (attribute, value) = line.strip().split(":") if attribute in ["kind","yaml_version"]: values[attribute] = value.strip() if attribute in ["meta"]: values[attribute] = "+" if arguments["KIND"] is None or values['kind'] == arguments['KIND']: for attribute in values.keys(): vector[attribute].append(values[attribute]) vector['filename'].append(filename) vector['Kind'] = vector.pop('kind') vector['Version'] = vector.pop('yaml_version') vector['Meta'] = vector.pop('meta') vector['Filename'] = vector.pop('filename') banner("Configuration Directory: {0}".format(config_dir), c="-") print column_table(vector) #print filename, values["kind"], values["version"] if arguments["list"] and arguments["clouds"]: filename = arguments['--file'] if filename is None: filename = config_file('/cloudmesh.yaml') config = cm_config(filename) data = {} data['Clouds'] = config.cloudnames() data['Labels'] = [] data['Type'] = [] data['Version'] = [] for cloud_key in data['Clouds']: data['Labels'].append(config.cloud(cloud_key)['cm_label']) data['Type'].append(config.cloud(cloud_key)['cm_type']) data['Version'].append(config.cloud(cloud_key)['cm_type_version']) if arguments["--json"]: print json.dumps(data, sort_keys=True, indent=4) else: print column_table(data, ['Labels','Clouds','Type','Version']) if arguments["fill"]: filename_template = arguments['--file'] if filename_template is None: filename_template = config_file('/etc/cloudmesh.yaml') filename_template = path_expand(filename_template) filename_values = arguments['VALUES'] if filename_values is None: filename_values = config_file('/me.yaml') content = open(filename_template, 'r').read() t = cm_template(filename_template) sorted_vars = sorted(set(t.variables())) try: values = ConfigDict(filename=filename_values) except Exception, e: print "ERROR: There is an error in the yaml file", e for cloud in values['clouds']: values['clouds'][cloud]['default'] = {} values['clouds'][cloud]['default']['image'] = None values['clouds'][cloud]['default']['flavor'] = None banner("%s -> %s" % (filename_values, filename_template)) env = Environment(undefined=IgnoreUndefined) template = env.from_string(content) result = template.render(values) print result
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 generate(self, me_file, out_file): cloudmesh_yaml = path_expand(self.filename) user_config = ConfigDict(filename=me_file) t = cm_template(cloudmesh_yaml) result = t.replace(kind="dict", values=user_config) location = path_expand(out_file) yaml_file = open(location, "w+") print >> yaml_file, yaml.dump(result, default_flow_style=False) yaml_file.close() log.info("Written new yaml file in " + location) if __name__ == "__main__": cloudmesh_yaml = config_file("/etc/cloudmesh.yaml") user_config = ConfigDict(filename=config_file("/me.yaml")) t = cm_template(cloudmesh_yaml) banner("VARIABLES") s = set(t.variables()) print ("\n".join(s)) banner("GREP") s = t.grep() print ("\n".join(s)) # banner("YAML FILE") # result = t.replace(kind="dict", values=user_config) # print yaml.dump(result, default_flow_style=False) # location = config_file('/cloudmesh-new.yaml')
#! /usr/bin/env python # # fix-for-india-vm -f # # changes the ~/.cloudmesh/cloudmesh_server.yaml import sys from cloudmesh.config.cm_config import cm_config from cloudmesh_install import config_file filename = config_file("/cloudmesh_server.yaml") with open(filename, 'r') as f: content = f.read() replacements = {'browser: True': 'browser: False', 'host: 127.0.0.1': 'host: 0.0.0.0'} for _old, _new in replacements.iteritems(): content = content.replace(_old, _new) if sys.argv[1] == "-f": outfile = open(filename, 'w') outfile.write(content) outfile.close() elif sys.argv[1] == "-v": print content
def display_rack_map(): #### # # Flag of debug, True means generate fake data with random generator # False means fetch the real data from server #### flag_debug = False # class name means the specific class to generate map for different service type # method name means the specific method to fetch real data of different service type, # the methods are defined in class FetchClusterInfo service_options = { "temperature": { "class": HeatClusterMap, "method": "fetch_temperature_ipmi", }, "service": { "class": ServiceClusterMap, "method": "fetch_service_type", }, } # rack denote the rack user selected # service denote the service user selected on the specific rack rack = request.form['select_rack'] service = request.form['select_service'] # double check to make sure rack can provide the specific service rack_form = RackForm() if rack not in rack_form.all_services_dict[service]["clusters"]: log.error("Someone try to hack the service [service: '{0}' on rack: '{1}'] provided by Rack Diagram. Just ignore it.".format( service, rack)) return redirect("/inventory/rack") # get location of configuration file, input diag, output image dir_base = config_file("") server_config = cm_config_server() relative_dir_diag = server_config.get("cloudmesh.server.rack.input") relative_dir_image = server_config.get( "cloudmesh.server.rack.diagrams.{0}".format(service)) # log.debug("relative dir image, {0}".format(relative_dir_image)) flask_dir = "static" # guess absolute path of cloudmesh_web rack_py_dir = pwd().strip().split("/") cloudmesh_web_dir = rack_py_dir # [:-1] # log.debug("cloudmesh_web dir, {0}".format(cloudmesh_web_dir)) list_image_dir = [flask_dir] + relative_dir_image.strip().split("/") abs_dir_image = "/".join(cloudmesh_web_dir + list_image_dir) abs_dir_diag = dir_base + "/" + relative_dir_diag # dynamic generate image map_class = service_options[service]["class"]( rack, dir_base, abs_dir_diag, abs_dir_image) # get cluster server data dict_data = None if flag_debug: dict_data = map_class.genRandomValues() else: # fetch the real data .... # TODO cloudmesh.hpc.proxyserver # should we add a field in cloudmesh.yaml for the proxy server to run # pbsnodes ??? config = cm_config() user = config.get("cloudmesh.hpc.username") myfetch = FetchClusterInfo(user, "india.futuregrid.org") flag_filter = None if rack == "all" else rack # If user want to customize the action, user can set optional param here # by calling map_class.set_optional_param(value) # optional param aparam = map_class.get_optional_param() dict_data = getattr(myfetch, service_options[service]["method"])( flag_filter, aparam) # update data map_class.update(dict_data) # plot map map_class.plot() # get image names filename_image = map_class.getImageFilename() filename_legend = map_class.getLegendFilename() image_size = map_class.getImageSize() legend_size = map_class.getImageLegendSize() # log.debug("legend size is: {0}".format(legend_size)) abs_web_path_image = "/".join([""] + list_image_dir + [filename_image]) abs_web_path_legend = "/".join([""] + list_image_dir + [filename_legend]) img_flag = "?" + str(time.time()) return render_template("mesh/rack/rack.html", flag_home=False, rack=rack, imageWidth=image_size["width"], imageHeight=image_size["height"], legendWidth=legend_size["width"], legendHeight=legend_size["height"], service=service, imageFilename=abs_web_path_image + img_flag, legendFilename=abs_web_path_legend + img_flag )
def get_fg_username_password_from_rcfiles(): """ rc_dir_location = {} # (hostname, loaction of dir on remote host, location of dir on localhost) rc_dir_location["india_openstack_havana"] = ("india.futuregird.org", "?", "?") rc_dir_location["sierra_openstack_grizzly"] = ("sierra.futuregrid.org", "?", "?") for label in rc_dir_location (host,dir) = rc_rdilocation(label) get rc file form the host and dir and copy to install_dir me_dict = read current me.yaml for label in rc_dir_location (host,dir) = rc_rdilocation(label) if label does not exist make it and also add the credentials for it, fill out initially with TBD if openstack: put values from local dir into dict elif eucalyptus: put values from local dir into dict return me dict """ class Readrcfile(object): """ Read novarc, eucarc and store variables with configparser reference: http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 """ def __init__(self, fp): self.fp = fp self.head = '[rcfile]\n' def readline(self): if self.head: try: return self.head finally: self.head = None else: return self.fp.readline().replace("export ", "") def get_variables(fpath): section_title = "rcfile" read_values = ["OS_TENANT_NAME", "OS_PASSWORD"] # case-sensitive result = {} cp = SafeConfigParser() try: cp.readfp(Readrcfile(open(fpath))) # cp.items(section_title) for read_value in read_values: tmp = cp.get(section_title, read_value) if tmp.startswith("$"): tmp = cp.get(section_title, tmp[1:]) # without $ sign result[read_value] = tmp return result except: print "ERROR: Failed to read rc files. Please check you have valid \ rcfiles in %s." % fpath print sys.exc_info() sys.exit(1) # ####################################################### # MISSING PART IS HERE # WE HAVE TO FILL THE me.yaml FILE WITH A REAL VALUE HERE # ####################################################### # me-all.yaml is correct one instead me-none.yaml # to fill variables with template # ----------------------------------------------- dir = config_file("") me_file = dir + "/me.yaml" try: result = open(me_file, 'r').read() values = yaml.safe_load(Template(result).substitute(os.environ)) except Exception, e: print "ERROR: There is an error in the yaml file", e sys.exit(1)
import sys import random from hostlist import expand_hostlist from datetime import datetime from mongoengine import * from pprint import pprint from cloudmesh_common.logger import LOGGER from cloudmesh_common.util import check_file_for_tabs from cloudmesh_common.util import path_expand from cloudmesh.util.config import read_yaml_config from cloudmesh_install import config_file log = LOGGER(__file__) inventory_config_filename = config_file("/cloudmesh-db.yaml") FABRIC_TYPES = ["cluster", "server", "service", "iamge"] """The types a fabric server can have""" PROVISIONING_CHOICES = ['openstack', 'eucalyptus', 'hpc'] """the provisioning choices for a fabric server""" SERVICE_CHOICES = ('ganglia', 'nagios') """the service choices for a service""" SERVER_CHOICES = ('dynamic', 'static')
def __init__(self): """read config""" BaremetalProvisinerABC.__init__(self) self.filename = config_file("/cloudmesh_server.yaml") self.teefaa_config = read_yaml_config (self.filename, check=True)
config = ConfigDict({"a": "1", "b": {"c": 3}}, prefix="cloudmesh.server", filename="./etc/cloudmesh_server.yaml") print("PPRINT") print(70 * "=") pprint(config) print("PRINT") print(70 * "=") print(config.pprint()) print(config.json()) print(70 * "=") print("A =", config["a"]) config.write(config_file("/d.yaml"), format="dict") config.write(config_file("/j.yaml"), format="json") config.write(config_file("/y.yaml"), format="yaml") # this does not work # config.write(config_file("/print.yaml"), format="print") print("mongo.path GET =", config.get("cloudmesh.server.mongo.path")) print("mongo.path ATTIRBUTE =", config.attribute("mongo.path")) print("get A =", config.get("a")) print("wrong mongo.path ATTRIBUTE =", config.attribute("mongo.path.wrong")) print("wrong mongo.path GET =", config.get("cloudmesh.server.mongo.path.wrong")) # print config["dummy"]