Example #1
0
    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")
Example #2
0
    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")
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
    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
Example #6
0
    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
Example #7
0
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