def _print_dict(self, d, format):
     if format in ["table"]:
         print column_table(d)
     elif format in ["list"]:
         print d["Clouds"]
     elif format in ["dict"]:
         print yaml.safe_dump(d, indent=4,default_flow_style=False)
     elif format in ["json"]:
         print json.dumps(d, indent=4)
 def _print_dict(self, d, format):
     if format in ["table"]:
         print column_table(d)
     elif format in ["list"]:
         print d["Clouds"]
     elif format in ["dict"]:
         print yaml.safe_dump(d, indent=4, default_flow_style=False)
     elif format in ["json"]:
         print json.dumps(d, indent=4)
Example #3
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