def generate_from_dict(self, d, out_file): cloudmesh_yaml = path_expand(self.filename) t = cm_template(cloudmesh_yaml) result = t.replace(kind="dict", values=d) 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)
def generate_from_dict(self, d, out_file): cloudmesh_yaml = path_expand(self.filename) t = cm_template(cloudmesh_yaml) result = t.replace(kind="dict", values=d) 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)
def install(): """installs mongo in ~/ENV/bin. Make sure your path is set correctly""" if sys.platform == "darwin": os_version = "osx" elif sys.platform in ["linux", "linux2"]: os_version = "linux" else: print "ERROR: Wrong opertaing system: Found", sys.platform sys.exit() ENV = os.environ['VIRTUAL_ENV'] + "/bin" if not ENV.endswith("ENV/bin"): print "WARNING: You are using a non standrad development firtualenv location" print " The standard location is", path_expand("~/ENV/bin") print " You use", ENV if not yn_choice("Would you like to proceed", default="n"): sys.exit() else: print "SUCCESS: You use the standard virtualenv setup" print " The standard location is", path_expand("~/ENV/bin") mongo_version = "mongodb-{0}-x86_64-2.4.6".format(os_version) mongo_tar = "{0}.tgz".format(mongo_version) # for some reason with does not work # with cd('/tmp'): if os.path.isfile("/tmp/{0}".format(mongo_tar)): print "WRANING: mongo tar file already downloaded" print " using", "/tmp/{0}".format(mongo_tar) else: if sys.platform == "darwin": local("cd /tmp; curl -O http://fastdl.mongodb.org/{1}/{0}.tgz".format(mongo_version, os_version)) else: local("cd /tmp; wget http://fastdl.mongodb.org/{1}/{0}.tgz".format(mongo_version, os_version)) local("cd /tmp; tar -xvf {0}.tgz".format(mongo_version)) local("cd /tmp; cp {0}/bin/* {1}".format(mongo_version, ENV)) where = local("which mongo", capture=True) if where.startswith(ENV): print "SUCCESS. mongo commands are now installed in", ENV else: print "ERROR: mongo is not in the path" print " it should be in", ENV print " we found it in", where """
def install(): """installs mongo in ~/ENV/bin. Make sure your path is set correctly""" if sys.platform == "darwin": os_version = "osx" elif sys.platform in ["linux", "linux2"]: os_version = "linux" else: print "ERROR: Wrong opertaing system: Found", sys.platform sys.exit() ENV = os.environ['VIRTUAL_ENV'] + "/bin" if not ENV.endswith("ENV/bin"): print "WARNING: You are using a non standrad development firtualenv location" print " The standard location is", path_expand("~/ENV/bin") print " You use", ENV if not yn_choice("Would you like to proceed", default="n"): sys.exit() else: print "SUCCESS: You use the standard virtualenv setup" print " The standard location is", path_expand("~/ENV/bin") mongo_version = "mongodb-{0}-x86_64-2.4.6".format(os_version) mongo_tar = "{0}.tgz".format(mongo_version) # for some reason with does not work # with cd('/tmp'): if os.path.isfile("/tmp/{0}".format(mongo_tar)): print "WRANING: mongo tar file already downloaded" print " using", "/tmp/{0}".format(mongo_tar) else: if sys.platform == "darwin": local("cd /tmp; curl -O http://fastdl.mongodb.org/{1}/{0}.tgz". format(mongo_version, os_version)) else: local("cd /tmp; wget http://fastdl.mongodb.org/{1}/{0}.tgz".format( mongo_version, os_version)) local("cd /tmp; tar -xvf {0}.tgz".format(mongo_version)) local("cd /tmp; cp {0}/bin/* {1}".format(mongo_version, ENV)) where = local("which mongo", capture=True) if where.startswith(ENV): print "SUCCESS. mongo commands are now installed in", ENV else: print "ERROR: mongo is not in the path" print " it should be in", ENV print " we found it in", where """
def start(auth=True): ''' start the mongod service in the location as specified in ~/.futuregrid/cloudmesh_server.yaml ''' banner("Starting mongod") config = cm_config_server().get("cloudmesh.server.mongo") path = path_expand(config["path"]) port = config["port"] if not os.path.exists(path): print "Creating mongodb directory in", path local("mkdir -p {0}".format(path)) with settings(warn_only=True): with hide('output', 'running', 'warnings'): lines = local("ps -ax |grep '[m]ongod.*port {0}'".format(port), capture=True).split("\n") if lines != ['']: pid = lines[0].split(" ")[0] print "NO ACTION: mongo already running in pid {0} for port {1}".format(pid, port) else: print "ACTION: Starting mongod" with_auth = "" if isyes(auth): with_auth = "--auth" local("mongod {2} --bind_ip 127.0.0.1 --fork --dbpath {0} --logpath {0}/mongodb.log --port {1}".format(path, port, with_auth))
def start(auth=True): ''' start the mongod service in the location as specified in ~/.futuregrid/cloudmesh_server.yaml ''' banner("Starting mongod") config = cm_config_server().get("cloudmesh.server.mongo") path = path_expand(config["path"]) port = config["port"] if not os.path.exists(path): print "Creating mongodb directory in", path local("mkdir -p {0}".format(path)) with settings(warn_only=True): with hide('output', 'running', 'warnings'): lines = local("ps -ax |grep '[m]ongod.*port {0}'".format(port), capture=True).split("\n") if lines != ['']: pid = lines[0].split(" ")[0] print "NO ACTION: mongo already running in pid {0} for port {1}".format( pid, port) else: print "ACTION: Starting mongod" with_auth = "" if isyes(auth): with_auth = "--auth" local( "mongod {2} --bind_ip 127.0.0.1 --fork --dbpath {0} --logpath {0}/mongodb.log --port {1}" .format(path, port, with_auth))
def get_credential (self, cloud=None, expand=False): if expand: d = self.get("cloudmesh.clouds.{0}.credentials".format(cloud)) for key in d: d[key] = path_expand(str(d[key])) return d else: return self.cloud(key)['credentials']
def cm_image_command(arguments): """ cm-image admin on HOSTS cm-image admin off HOSTS """ path = path_expand(definitions[0]) if arguments["version"]: print cloudmesh.__version__ elif arguments["info"]: banner("info") banner("System", c='-') print "Kind: ", arguments['--kind'] print "Path: ", path print "Version:", cloudmesh.__version__ banner("List of templates", c='-') system_name = None for definition in definitions: try: path = path_expand(definition) if os.path.exists(path): os.system("cd '%s' ; veewee vbox list" % path) else: print "WARNING: path", path, "does not exist" except KeyError, key: print 'WARNING: no environment variable called', key, 'found' print print "To build one, please use one of the" print print " cm-image build OS" print print "Next you need to register the image" print print " cm-image register OS" print print "where OS is one of the labels listed above." print
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 test_expand(self): HEADING() result = self.config.get('cloudmesh.clouds.sierra_openstack_grizzly') dir = result['credentials']['OS_CACERT'] print dir assert dir.startswith("~") dir = path_expand(dir) print dir assert not dir.startswith("~")
def userkeys(self, attribute=None, expand=True): if attribute is None: return self['cloudmesh']['keys'] else: if attribute == 'default': name = self['cloudmesh']['keys']['default'] value = self['cloudmesh']['keys']['keylist'][name] else: value = self['cloudmesh']['keys']['keylist'][attribute] if expand: value = path_expand(value) return value
class Test_cloudmesh: filename = path_expand("$HOME/.futuregrid/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 test_teefaa(self): HEADING() host = "i066" image = "ubuntu1304v2btsync" Provisioner = ProvisionerTeefaa p = Provisioner() (success, result) = p.provision(host, image) print result print success
def test02_names(self): HEADING() print self.keys.names() names = [] lines = grep("ssh-", path_expand(self.filename)) for line in lines: (name, rest) = line.strip().split(":") if name not in self.keys.names(): print "Key", name, "not found" assert false return else: names.append(name) print "keys found", names assert len(names) == len(self.keys.names())
def wipe(): """wipes out all traces from mongo""" kill() config = cm_config_server().get("cloudmesh.server.mongo") path = path_expand(config["path"]) banner("{0}".format(path)) local("mkdir -p {0}".format(path)) result = str(ls(path)) banner(path, "-") print result print 70 * "-" if result != "": if yn_choice("deleting the directory", default="n"): local("rm -rf {0}".format(path)) local("mkdir -p {0}".format(path)) banner("{0}".format(path)) local("ls {0}".format(path))
def boot(auth=True): # kill mongo kill() time.sleep(1) # wipe mongo wipe() time.sleep(1) # start mongo without auth start(auth=False) time.sleep(1) if isyes(auth): # create users admin() time.sleep(1) # restart with auth kill() time.sleep(10) start(auth=auth) time.sleep(1) config = cm_config_server().get("cloudmesh.server.mongo") path = path_expand(config["path"]) banner(path) print ls(path) banner("PROCESS") with settings(warn_only=True): local("ps -ax | fgrep mongo")
def write(self, filename=None, format="dict"): """write the dict""" if filename is not None: location = path_expand(filename) else: location = self.location # with open('data.yml', 'w') as outfile: # outfile.write( yaml.dump(data, default_flow_style=True) ) f = os.open(location, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, stat.S_IRUSR | stat.S_IWUSR) if format == "json": os.write(f, self.json()) elif format in ['yml', 'yaml']: d = dict(self) os.write(f, yaml.dump(d, default_flow_style=False)) elif format == "print": os.write(f, custom_print(self, 4)) else: os.write(f, self.dump()) os.close(f)
def __init__(self, filename): self.filename = path_expand(filename) self.content = open(self.filename, 'r').read()
def __init__(self, filename): self.filename = path_expand(filename) self.content = open(self.filename, "r").read()
def _generate_from_dict(self, d): cloudmesh_yaml = path_expand(self.filename) try: result = self.replace(kind="dict", values=d) except Exception, e: print "EEEE", e
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: 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 content = open(filename_template, 'r').read()
def metric(): """puts an example of a log file into the mongodb logfile""" log_file = path_expand("~/.futuregrid/metric/sierra-sample.log")
def _set_filename(self, filename): self['filename'] = filename self['location'] = path_expand(self["filename"])
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_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 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