Example #1
0
 def __init__(self, filename=None):
     """initializes based on cm_config and returns pointer to the projects dict."""
     # Check if the file exists
     if filename == None:
         self.config = cm_config()
     else:
         self.filename = self._path_expand(filename)
         try:
             with open(self.filename): pass
         except IOError:
             print 'ERROR: cm_projects, file "%s" does not exist' % self.filename
             sys.exit()
         self.config = cm_config(self.filename)
Example #2
0
    def config(self):
        """
        reads the cloudmesh yaml file that defines which clouds build
        the cloudmesh
        """
        #print "CONFIG"

        self.configuration = cm_config()

        #pp.pprint (configuration)


        active_clouds = self.configuration.active()
        #print active_clouds
        
        for cloud_name in active_clouds:
            try:
                credential = self.configuration.get(cloud_name)
                cloud_type = self.configuration.get()['clouds'][cloud_name]['cm_type']

                if cloud_type in ['openstack','eucalyptus','azure']:
                    self.clouds[cloud_name] = {'name': cloud_name,
                                               'cm_type': cloud_type,
                                               'credential': credential}
                #if cloud_type in ['openstack']:
                #    self.clouds[cloud_name] = {'states': "HALLO"}

            except: #ignore
                pass
                #print "ERROR: could not initialize cloud %s" % cloud_name
                #sys.exit(1)

        return
Example #3
0
 def config(self):
     configuration = cm_config()
     for name in configuration.keys():
         credential = configuration.get(name)
         type = credential['cm_type']
         self.clouds[name] = {'cm_type': type, 'credential' : credential}
         self.update(name, type)
         
     return
Example #4
0
 def config(self):
     """
     reads the cloudmesh yaml file that defines which clouds build
     the cloudmesh
     """
     configuration = cm_config()
     all_keys = configuration.keys()
     all_keys = all_keys[2:]
     for name in all_keys:
         if str(name) != 'username' and str(name) != 'default':
             credential = configuration.get(name)
             print name
             type = credential['cm_type']
             self.clouds[name] = {'cm_type': type, 'credential': credential}
         # self.update(name, type)
     return
Example #5
0
def main():
    default_path = '.futuregrid/novarc'
    arguments = docopt(__doc__, version='0.8')

    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 "%s: Configuration file '%s' not found" % ("CM ERROR", file)
            sys.exit(1)
        except (yaml.scanner.ScannerError, yaml.parser.ParserError) as yamlerror:
            print "%s: YAML error: %s, in configuration file '%s'" % ("CM ERROR", yamlerror, file)
            sys.exit(1)
        except:
            print "Unexpected error:", sys.exc_info()[0]
            sys.exit(1)

        name = arguments['NAME']
        
        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'] == None:
                var['user'] = getpass.getuser()

            from_location = "%(user)s@%(host)s:%(file)s" % var
            to_location = os.path.expanduser("~/%(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)


        if arguments['projects'] and arguments['list']:

            projects = config.data['cloudmesh']['projects']
            print yaml.dump(projects, default_flow_style=False, indent=4)
            sys.exit(0)

        if arguments['projects'] and arguments['?']:

            projects = config.data['cloudmesh']['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

            sys.exit(0)

        if arguments['init'] or name == 'init':
            output = arguments['--out']
            username = arguments['--user'] or os.getenv('USER')
            config.userdata_handler = ldap_user
            config.cloudcreds_handler = openstack_grizzly_cloud
            ########### for testing #############################################################
            # config.cloudcreds_handler._client = mock_keystone.Client
            # config.cloudcreds_handler._client.mockusername = username
            # config.cloudcreds_handler._client.mocktenants = config.data['cloudmesh']['active']
            #####################################################################################
            config.initialize(username)
            try:
                config.write(output)
            except OSError as oserr:
                if oserr.errno == 17:
                    print "'%s' exists, please rename or remove it and try again." % oserr.filename
            sys.exit(0)

        if arguments['list'] or name == 'list':
            for name in config.keys():
                if 'cm_type' in config.data['cloudmesh']['clouds'][name]:
                    print name, "(%s)" % config.data['cloudmesh']['clouds'][name]['cm_type']
            sys.exit(0)

        if arguments['dump'] or name =='dump':
            format = arguments['--format']
            if format == 'yaml':
                print yaml.dump(config, default_flow_style=False, indent=4)
            elif format == 'dict' or format ==None:
                print config
            sys.exit(0)

        if name == '?':
            if file == 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.keys():
                    if 'cm_type' in config.data['cloudmesh']['clouds'][name]:
                        print counter, "-" "%20s" % name, "(%s)" % config.data['cloudmesh']['clouds'][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']

        if name != 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.keys():
                    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)

            if arguments["-"]:
                print result
            else:
                if output == None:
                    arguments['--out'] = "%s/%s" % (home, default_path)
                    output = arguments['--out']
                out = False
                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)
Example #6
0
 def setup(self):
     if self.filename == None:
         self.config = cm_config()
     else:
         self.config = cm_config(self.filename)