Exemplo n.º 1
0
 def get_working_cloud_name(self):
     '''
     get the name of a cloud to be work on, if CLOUD not given, will pick the
     slected cloud, is --all, will return a list of active clouds
     '''
     self.cloudmanage._connect_to_mongo()
     activeclouds = None
     try:
         activeclouds = self.cloudmanage.mongo.active_clouds(self.username)
     except:
         pass
     if self.arguments['--all']:
         if activeclouds is None:
             print("no active cloud, please activate a cloud by 'cloud on [CLOUD]'")
             return False
         return activeclouds
     else:
         if self.arguments['CLOUD']:
             name = self.arguments['CLOUD']
         else:
             name = self.cloudmanage.get_selected_cloud(self.username)
         if self.cloudmanage.get_clouds(self.username, getone=True, cloudname=name) is None:
             Console.error(
                 "no cloud information of '{0}' in database".format(name))
             return False
         if name not in activeclouds:
             Console.warning(
                 "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(name))
             return False
         return [name]
Exemplo n.º 2
0
 def get_working_cloud_name(self):
     '''
     get the name of a cloud to be work on, if CLOUD not given, will pick the
     slected cloud, is --all, will return a list of active clouds
     '''
     self.cloudmanage._connect_to_mongo()
     activeclouds = None
     try:
         activeclouds = self.cloudmanage.mongo.active_clouds(self.username)
     except:
         pass
     if self.arguments['--all']:
         if activeclouds is None:
             print("no active cloud, please activate a cloud by 'cloud on [CLOUD]'")
             return False
         return activeclouds
     else:
         if self.arguments['CLOUD']:
             name = self.arguments['CLOUD']
         else:
             name = self.cloudmanage.get_selected_cloud(self.username)
         if self.cloudmanage.get_clouds(self.username, getone=True, cloudname=name) is None:
             Console.error(
                 "no cloud information of '{0}' in database".format(name))
             return False
         if name not in activeclouds:
             Console.warning(
                 "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(name))
             return False
         return [name]
Exemplo n.º 3
0
 def get_working_cloud_name(self):
     '''
     get the name of a cloud to work on, if CLOUD not given, will pick the
     selected or default cloud
     '''
     cloudname = None
     cloudobj = CloudManage()
     mongo = cm_mongo()
     if self.arguments['--cloud']:
         cloud = cloudobj.get_clouds(
             self.username, getone=True, cloudname=self.arguments['--cloud'])
         if cloud is None:
             Console.error(
                 "could not find cloud '{0}'".format(self.arguments['--cloud']))
             return False
         else:
             cloudname = self.arguments['--cloud']
     else:
         cloudname = cloudobj.get_selected_cloud(self.username)
     if cloudname not in mongo.active_clouds(self.username):
         Console.warning(
             "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(cloudname))
         return False
     else:
         return cloudname
Exemplo n.º 4
0
 def _default_format(self):
     self._start_cm_user()
     defaults_data = self.user_obj.info(self.username)['defaults']
     if self.arguments['VALUE']:
         allowed_formats = ['table', 'json', 'csv']
         if self.arguments['VALUE'] not in allowed_formats:
             Console.warning("allowed formats are {0}".format(
                 str(allowed_formats)))
             return
         else:
             defaults_data['shell_print_format'] = self.arguments['VALUE']
             self.user_obj.set_defaults(self.username, defaults_data)
             Console.ok("set '{0}' as default print format".format(
                 self.arguments['VALUE']))
     else:
         format = None
         try:
             format = defaults_data['shell_print_format']
         except:
             pass
         if format not in [None, 'none']:
             print(defaults_data['shell_print_format'])
         else:
             defaults_data['shell_print_format'] = "table"
             self.user_obj.set_defaults(self.username, defaults_data)
             defaults_data = self.user_obj.info(self.username)
             print(defaults_data['shell_print_format'])
Exemplo n.º 5
0
 def _default_format(self):
     self._start_cm_user()
     defaults_data = self.user_obj.info(self.username)['defaults']
     if self.arguments['VALUE']:
         allowed_formats = ['table', 'json', 'csv']
         if self.arguments['VALUE'] not in allowed_formats:
             Console.warning("allowed formats are {0}".format(str(allowed_formats)))
             return
         else:
             defaults_data['shell_print_format'] = self.arguments['VALUE']
             self.user_obj.set_defaults(self.username, defaults_data)
             Console.ok("set '{0}' as default print format".format(self.arguments['VALUE']))
     else:
         format = None
         try:
             format = defaults_data['shell_print_format']
         except:
             pass
         if format not in [None, 'none']:
             print(defaults_data['shell_print_format'])
         else:
             defaults_data['shell_print_format'] = "table"
             self.user_obj.set_defaults(self.username, defaults_data)
             defaults_data = self.user_obj.info(self.username)
             print(defaults_data['shell_print_format'])
Exemplo n.º 6
0
 def _list_project(self):
     self.cloudmanage._connect_to_mongo()
     selected_project = None
     try:
         selected_project = self.cloudmanage.mongo.db_defaults.find_one(
             {'cm_user_id': self.username + "OIO"})['project']
     except Exception, NoneType:
         Console.warning("could not find selected project in the database")
Exemplo n.º 7
0
 def _list_project(self):
     self.cloudmanage._connect_to_mongo()
     selected_project = None
     try:
         selected_project = self.cloudmanage.mongo.db_defaults.find_one(
             {'cm_user_id': self.username + "OIO"})['project']
     except Exception, NoneType:
         Console.warning("could not find selected project in the database")
Exemplo n.º 8
0
 def get_working_server_id(self, cloudname):
     """
      returns the id of a VM
      In the command line, the user can provide either VM name or VM id to 
      identify a VM, this function figures out what the users input and check
      the existence of the VM, then returns the VM id or False if check 
      failed
      
      :param cloudname: cloud name
      :type cloudname: string
      :return: VM id, otherwise False
      :rtype: string or boolean
     """
     serverid = None
     mongo = cm_mongo()
     mongo.activate(cm_user_id=self.username, names=[cloudname])
     mongo.refresh(self.username, names=[cloudname], types=['servers'])
     serverdata = mongo.servers(
         clouds=[cloudname], cm_user_id=self.username)[cloudname]
     vmname = self.arguments['--name'] or self.arguments['NAME']
     if vmname:
         ls = []
         for k, v in serverdata.iteritems():
             if vmname == v['name']:
                 ls.append(k)
         if len(ls) > 1:
             Console.warning("There are more than one VM named {0}, please use VM id instead"
                             .format(vmname))
             return False
         elif len(ls) == 0:
             Console.error(
                 "Could not find VM named {0}".format(vmname))
             return False
         else:
             serverid = ls[0]
     elif self.arguments['--id']:
         for k, v in serverdata.iteritems():
             if self.arguments['--id'] == k:
                 serverid = self.arguments['--id']
                 break
         if serverid is None:
             Console.error(
                 "Could not find VM with id {0}".format(self.arguments['--id']))
             return False
     else:
         Console.warning("Please specify a VM name or id")
         return False
     return serverid
Exemplo n.º 9
0
def shell_command_label(arguments):
    # TODO: [--width=WIDTH]
    # WIDTH   The width of the ID in teh label, padded with 0
    """
    Usage:
           label [--prefix=PREFIX] [--id=ID] [--raw]

    Options:

      --prefix=PREFIX    provide the prefix for the label
      --id=ID            provide the start ID which is an integer
      --raw              prints label only

    Description:

        A command to set the prefix and id for creating an automatic
        lable for VMs. Without paremeter it prints the currect label.

    """
    try:
        config = cm_config()
    except:
        Console.error("There is a problem with the configuration yaml files")
        return

    username = config['cloudmesh']['profile']['username']

    # print arguments #######
    if arguments['--id'] or arguments['--prefix']:
        id = None
        if arguments['--id']:
            error = False
            try:
                id = int(arguments['--id'])
            except:
                error = True
            if not error:
                if id < 0:
                    error = True
            if error:
                Console.warning("id must be 0 or a positive integer")
                return
        prefix = None
        if arguments['--prefix']:
            prefix = arguments['--prefix']
        _helper(username, prefix=prefix, idx=id, raw=arguments['--raw'])
    else:
        _helper(username, raw=arguments['--raw'])
Exemplo n.º 10
0
def shell_command_label(arguments):
    """
    ::

      Usage:
             label [--prefix=PREFIX] [--id=ID] [--raw]

      Options:

        --prefix=PREFIX    provide the prefix for the label
        --id=ID            provide the start ID which is an integer
        --raw              prints label only

      Description:

          A command to set a prefix and an id for a name of VM. 
          Without a paremeter, it prints a current label.

    """
    try:
        config = cm_config()
    except:
        Console.error("There is a problem with the configuration yaml files")
        return

    username = config['cloudmesh']['profile']['username']

    # print arguments #######
    if arguments['--id'] or arguments['--prefix']:
        id = None
        if arguments['--id']:
            error = False
            try:
                id = int(arguments['--id'])
            except:
                error = True
            if not error:
                if id < 0:
                    error = True
            if error:
                Console.warning("id must be 0 or a positive integer")
                return
        prefix = None
        if arguments['--prefix']:
            prefix = arguments['--prefix']
        _helper(username, prefix=prefix, idx=id, raw=arguments['--raw'])
    else:
        _helper(username, raw=arguments['--raw'])
Exemplo n.º 11
0
def shell_command_label(arguments):
    """
    ::

      Usage:
             label [--prefix=PREFIX] [--id=ID] [--raw]

      Options:

        --prefix=PREFIX    provide the prefix for the label
        --id=ID            provide the start ID which is an integer
        --raw              prints label only

      Description:

          A command to set a prefix and an id for a name of VM. 
          Without a paremeter, it prints a current label.

    """
    try:
        config = cm_config()
    except:
        Console.error("There is a problem with the configuration yaml files")
        return

    username = config['cloudmesh']['profile']['username']

    # print arguments #######
    if arguments['--id'] or arguments['--prefix']:
        id = None
        if arguments['--id']:
            error = False
            try:
                id = int(arguments['--id'])
            except:
                error = True
            if not error:
                if id < 0:
                    error = True
            if error:
                Console.warning("id must be 0 or a positive integer")
                return
        prefix = None
        if arguments['--prefix']:
            prefix = arguments['--prefix']
        _helper(username, prefix=prefix, idx=id, raw=arguments['--raw'])
    else:
        _helper(username, raw=arguments['--raw'])
Exemplo n.º 12
0
def shell_command_label(arguments):
    # TODO: [--width=WIDTH]
    # WIDTH   The width of the ID in teh label, padded with 0
    """
    Usage:
           label [--prefix=PREFIX] [--id=ID] [--raw]

    Options:

      --prefix=PREFIX    provide the prefix for the label
      --id=ID            provide the start ID which is an integer
      --raw              prints label only

    Description:

        A command to set the prefix and id for creating an automatic
        lable for VMs. Without paremeter it prints the currect label.

    """
    try:
        config = cm_config()
    except:
        Console.error("There is a problem with the configuration yaml files")
        return

    username = config['cloudmesh']['profile']['username']

    # print arguments #######
    if arguments['--id'] or arguments['--prefix']:
        id = None
        if arguments['--id']:
            error = False
            try:
                id = int(arguments['--id'])
            except:
                error = True
            if not error:
                if id < 0:
                    error = True
            if error:
                Console.warning("id must be 0 or a positive integer")
                return
        prefix = None
        if arguments['--prefix']:
            prefix = arguments['--prefix']
        _helper(username, prefix=prefix, idx=id, raw=arguments['--raw'])
    else:
        _helper(username, raw=arguments['--raw'])
Exemplo n.º 13
0
 def _default_cloud(self):
     self._start_cm_user()
     defaults_data = self.user_obj.info(self.username)['defaults']
     if self.arguments['VALUE']:
         value = self.arguments['VALUE']
         if (value in defaults_data['activeclouds'] and
                     value in defaults_data['registered_clouds']):
             defaults_data['cloud'] = value
             self.user_obj.set_defaults(self.username, defaults_data)
             Console.ok("set '{0}' as default cloud".format(value))
         else:
             Console.warning("To set a default cloud, it must be registered and "
                             "active, to register and activate a CLOUD: cloud on [CLOUD]")
     else:
         if "cloud" in defaults_data:
             print(defaults_data['cloud'])
         else:
             print("default cloud not set")
Exemplo n.º 14
0
    def do_color(self, args, arguments):
        """
        ::
        
          Usage:
              color on
              color off
              color

              Turns the shell color printing on or off

          Description:

              color on   switched the color on

              color off  switches the color off

              color      without parameters prints a test to display
                         the various colored mesages. It is intended
                         as a test to see if your terminal supports
                         colors.

        """
        if arguments['on']:
            key = "cloudmesh.shell.color"
            value = True
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = True
            print("color on.")
        elif arguments['off']:
            key = "cloudmesh.shell.color"
            value = False
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = False
            print("color off.")
        else:
            print("Color:", Console.color)
            Console.warning("Warning")
            Console.error("Error")
            Console.info("Info")
            Console.msg("Msg")
            Console.ok("Success")
Exemplo n.º 15
0
    def do_color(self, args, arguments):
        """
        ::
        
          Usage:
              color on
              color off
              color

              Turns the shell color printing on or off

          Description:

              color on   switched the color on

              color off  switches the color off

              color      without parameters prints a test to display
                         the various colored mesages. It is intended
                         as a test to see if your terminal supports
                         colors.

        """
        if arguments['on']:
            key = "cloudmesh.shell.color"
            value = True
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = True
            print ("color on.")
        elif arguments['off']:
            key = "cloudmesh.shell.color"
            value = False
            self.cm_config._update(key, value)
            self.cm_config.write(output="yaml")
            Console.color = False
            print ("color off.")
        else:
            print("Color:", Console.color)
            Console.warning("Warning")
            Console.error("Error")
            Console.info("Info")
            Console.msg("Msg")
            Console.ok("Success")
Exemplo n.º 16
0
 def _vm_login(self):
     """
     login to a VM  
     """
     cloudname = self.get_working_cloud_name()
     if not cloudname:
         return
     address = None
     if self.arguments['--addr']:
         address = self.arguments['--addr']
     else:
         serverid = self.get_working_server_id(cloudname)
         if not serverid:
             return
         mongo = cm_mongo()
         serverdata = mongo.servers(clouds=[cloudname],
                                    cm_user_id=self.username)[cloudname]
         serverdata = serverdata[serverid]['addresses']
         temp_val = serverdata.keys()[0]
         serverdata = serverdata[temp_val]
         for i in serverdata:
             if i['OS-EXT-IPS:type'] == "floating":
                 address = i['addr']
         if address is None:
             Console.warning("Please assign a public ip to the VM first"
                             "(vm ip (--name=<vmname>|--id=<id>))")
             return
     if self.arguments['<command>']:
         commands = ' '.join(self.arguments['<command>'])
         try:
             print(">>>\n")
             print(ssh_execute(self.arguments['--ln'], address,
                               commands, key=self.arguments['--key']))
         except:
             err = sys.exc_info()
             Console.error("Can not execute ssh on {0}:{1}".format(address, err))
     else:
         host = "{0}@{1}".format(self.arguments['--ln'], address)
         option = "-o StrictHostKeyChecking=no "
         if self.arguments['--key']:
             call(['ssh', option, '-i', self.arguments['--key'], host])
         else:
             call(['ssh', option, host])
Exemplo n.º 17
0
 def _default_cloud(self):
     self._start_cm_user()
     defaults_data = self.user_obj.info(self.username)['defaults']
     if self.arguments['VALUE']:
         value = self.arguments['VALUE']
         if (value in defaults_data['activeclouds']
                 and value in defaults_data['registered_clouds']):
             defaults_data['cloud'] = value
             self.user_obj.set_defaults(self.username, defaults_data)
             Console.ok("set '{0}' as default cloud".format(value))
         else:
             Console.warning(
                 "To set a default cloud, it must be registered and "
                 "active, to register and activate a CLOUD: cloud on [CLOUD]"
             )
     else:
         if "cloud" in defaults_data:
             print(defaults_data['cloud'])
         else:
             print("default cloud not set")
Exemplo n.º 18
0
 def get_working_server_id(self, cloudname):
     serverid = None
     mongo = cm_mongo()
     mongo.activate(cm_user_id=self.username, names=[cloudname])
     mongo.refresh(self.username, names=[cloudname], types=['servers'])
     serverdata = mongo.servers(
         clouds=[cloudname], cm_user_id=self.username)[cloudname]
     vmname = self.arguments['--name'] or self.arguments['NAME']
     if vmname:
         ls = []
         for k, v in serverdata.iteritems():
             if vmname == v['name']:
                 ls.append(k)
         if len(ls) > 1:
             Console.warning("There are more than one VM named {0}, please use VM id instead"
                             .format(vmname))
             return False
         elif len(ls) == 0:
             Console.error(
                 "Could not find VM named {0}".format(vmname))
             return False
         else:
             serverid = ls[0]
     elif self.arguments['--id']:
         for k, v in serverdata.iteritems():
             if self.arguments['--id'] == k:
                 serverid = self.arguments['--id']
                 break
         if serverid == None:
             Console.error(
                 "Could not find VM with id {0}".format(self.arguments['--id']))
             return False
     else:
         Console.warning("Please specify a VM name or id")
         return False
     return serverid
Exemplo n.º 19
0
 def _show_ip(self):
     '''
     list the ips of VMs
     '''
     mongo = cm_mongo()
     cloudname = self.get_working_cloud_name()
     if cloudname == False: return
     if get_command_list_refresh_default_setting(self.username) or self.arguments['--refresh']:
         mongo.activate(cm_user_id=self.username, names=[cloudname])
         mongo.refresh(cm_user_id=self.username,
                       names=[cloudname],
                       types=['servers'])
         
     servers_dict = mongo.servers(
                 clouds=[cloudname], cm_user_id=self.username)[cloudname]
                 
     AllCloudVMs = False
     if self.arguments['--cloud'] and\
     self.arguments['NAME'] is None and\
     self.arguments['--id'] is None and\
     self.arguments['--group'] is None and\
     self.arguments['--prefix'] is None and\
     self.arguments['--names'] is None:
         AllCloudVMs = True
                 
     server_id_list = get_vms_look_for(self.username,
                                       cloudname,
                                       servername=self.arguments['NAME'],
                                       serverid=self.arguments['--id'],
                                       groupname=self.arguments['--group'],
                                       prefix=self.arguments['--prefix'],
                                       hostls=self.arguments['--names'],
                                       getAll=AllCloudVMs,
                                       refresh=False)
     if server_id_list == False: return
     if server_id_list == []:
         Console.warning("no vm meets the condition")
         return
     
     res = {}
     for item in server_id_list:
         temp = servers_dict[item]['addresses']['private']
         fixed = ""
         floating = ""
         for item0 in temp:
             if item0['OS-EXT-IPS:type'] == 'fixed':
                 fixed = fixed + item0['addr'] + ", "
             elif item0['OS-EXT-IPS:type'] == 'floating':
                 floating = floating + item0['addr'] + ", "
         if fixed != "": fixed = fixed[:-2]
         if floating != "": floating = floating[:-2]
         temp0 = {}
         temp0['name'] = servers_dict[item]['name']
         temp0['fixed'] = fixed
         temp0['floating'] = floating
         res[item] = temp0
         
     if self.arguments['--format']:
         if self.arguments['--format'] not in ['table', 'json', 'csv']:
             Console.error("please select printing format among table, json and csv")
             return
         else:
             p_format = self.arguments['--format']
     else:
         p_format = None
     
     header = ['name', 'fixed', 'floating']
     
     shell_commands_dict_output(self.username,
                                res,
                                print_format=p_format,
                                firstheader="id",
                                header=header)
Exemplo n.º 20
0
                return
            if arguments['--format']:
                if arguments['--format'] not in ALLOWED_PRINT_FORMAT:
                    Console.error("wrong print format: {0}. (allowed print format: {1})".format(arguments['--format'],
                        ", ".join(ALLOWED_PRINT_FORMAT)))
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None
            
            columns = None
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]
            if columns == []:
                Console.warning("please provide column names for --column")
                return
            
            vmobj = VMs()
            vmobj._helper_vm_cli_printer(vms_dict, 
                                         print_format=p_format,
                                         columns=columns,
                                         detailed=arguments['--detail'])
                
            


        elif arguments['remove']: 
            try:
                clusterobj.delete(arguments['<name>'], grouponly=arguments['--grouponly'])
            except Exception, err:
Exemplo n.º 21
0
    def do_launcher(self, args, arguments):
        """
        ::

          Usage:
                launcher start MENU
                launcher stop STACK_NAME
                launcher list
                launcher show STACK_NAME
                launcher menu [--column=COLUMN] [--format=FORMAT]
                launcher import [FILEPATH] [--force]
                launcher export FILEPATH
                launcher help | -h

            An orchestration tool with Chef Cookbooks

            Arguments:

              MENU           Name of a cookbook
              STACK_NAME     Name of a launcher
              FILEPATH       Filepath
              COLUMN         column name to display
              FORMAT         display format (json, table)
              help           Prints this message

            Options:

               -v       verbose mode

        """
        log.info(arguments)
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()
        self._id = "t_stacks"

        if arguments["help"] or arguments["-h"]:
            print(self.do_launcher.__doc__)
        elif arguments['show'] and arguments['STACK_NAME']:
            print("NOT IMPLEMENTED")
            return
        elif arguments['menu']:
            userid = self.cm_config.username()
            launchers = self.cm_mongo.launcher_get(userid)

            if launchers.count() == 0:
                Console.warning(
                    "no launcher in database, please import launcher first"
                    "(launcher import [FILEPATH] [--force])")
                return
            else:
                d = {}
                for launcher in launchers:
                    d[launcher['cm_launcher']] = launcher
                    if "_id" in d[launcher['cm_launcher']]:
                        del d[launcher['cm_launcher']]['_id']

            columns = None
            if arguments['--column']:
                if arguments['--column'] != "all":
                    columns = [
                        x.strip() for x in arguments['--column'].split(',')
                    ]
            else:
                columns = ['name', 'description']

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format ",
                                  "among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['list']:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)
            self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            stacks = self.cm_mongo.stacks(cm_user_id=userid)
            launchers = self.filter_launcher(stacks, {
                "search": "contain",
                "key": "stack_name",
                "value": "launcher"
            })
            log.debug(launchers)

            d = {}
            for k0, v0 in launchers.iteritems():
                for k1, v1 in launchers[k0].iteritems():
                    d[v1['id']] = v1
            columns = [
                'stack_name', 'description', 'stack_status', 'creation_time',
                'cm_cloud'
            ]
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error(
                        "please select printing format among table, json and csv"
                    )
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher_id",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['start'] and arguments['MENU']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            config_launcher = cm_config_launcher()

            userinfo = self.cm_user.info(userid)
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]

            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith('key '):
                    keycontent = keycontent[4:]
                cm_keys_mongo(userid).check_register_key(
                    userid, def_cloud, key, keycontent)
                keynamenew = _keyname_sanitation(userid, key)
            else:
                Console.warning("No sshkey found. Please Upload one")

            cookbook = arguments['MENU']
            s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook,
                                                   get_rand_string())
            dummy = "123456789"  # doing nothing. just for test
            try:
                t_url = \
                config_launcher['cloudmesh']['launcher']['default']['template']
            except:
                # If key is missing (KeyError), new cloudmesh_launcher.yaml
                # needs to be copied to ~/.cloudmesh
                t_url = \
                "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml"

            param = {
                'KeyName': keynamenew,
                'Cookbook': cookbook,
                'dummy': dummy
            }
            # test for openmpi, hadoop
            if cookbook[:6] == "hadoop" or cookbook[:7] == "openmpi":
                privatekey, publickey = generate_keypair()
                try:
                    t_url = \
                            config_launcher['cloudmesh']['launcher']['recipes'][cookbook]['template']
                except:
                    # If key is missing (KeyError), new cloudmesh_launcher.yaml
                    # needs to be copied to ~/.cloudmesh
                    t_url = \
                            ("https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/ubuntu-14.04/"
                             + str(cookbook) + "-cluster/" + str(cookbook) +
                             "-cluster.yaml")
                param = {
                    'KeyName': keynamenew,
                    'PublicKeyString': publickey,
                    'PrivateKeyString': privatekey
                }
                if cookbook[:9] == "hadoop2.7":
                    param["UserName"] = userid

            log.debug(def_cloud, userid, s_name, t_url, param)
            res = self.cm_mongo.stack_create(cloud=def_cloud,
                                             cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            log.debug(res)
            if 'error' in res:
                print(res['error']['message'])
            return res

        elif arguments['stop'] and arguments['STACK_NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_id = arguments['STACK_NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud,
                                             cm_user_id=userid,
                                             server=s_id)
            log.debug(res)
            return res

        elif arguments['import']:
            filepath = config_file("/cloudmesh_launcher.yaml")

            if arguments['FILEPATH']:
                filepath = arguments['FILEPATH']
            try:
                filename = path_expand(filepath)
                fileconfig = ConfigDict(filename=filename)
            except Exception, err:
                Console.error(
                    "error while loading '{0}', please check".format(filepath))
                print(traceback.format_exc())
                print(sys.exc_info()[0])
                return
            try:
                recipes_dict = fileconfig.get("cloudmesh", "launcher",
                                              "recipies")
            except:
                Console.error("error while loading recipies from the file")

            # print recipes_dict
            userid = self.cm_config.username()
            launcher_names = []
            launchers = self.cm_mongo.launcher_get(userid)
            for launcher in launchers:
                launcher_names.append(launcher['cm_launcher'].encode("ascii"))

            for key in recipes_dict:
                if key in launcher_names:
                    if arguments['--force']:
                        self.cm_mongo.launcher_remove(userid, key)
                        self.cm_mongo.launcher_import(recipes_dict[key], key,
                                                      userid)
                        print("launcher '{0}' overwritten.".format(key))
                    else:
                        print("ERROR: launcher '{0}' exists, "
                              "please remove it first, or use "
                              "'--force' when adding".format(key))
                else:
                    self.cm_mongo.launcher_import(recipes_dict[key], key,
                                                  userid)
                    print("launcher '{0}' added.".format(key))
Exemplo n.º 22
0
    def do_volume(self, args, arguments):
        """
        ::

          Usage:
              volume list
              volume create <size>
                            [--snapshot-id=<snapshot-id>]
                            [--image-id=<image-id>]
                            [--display-name=<display-name>]
                            [--display-description=<display-description>]
                            [--volume-type=<volume-type>]
                            [--availability-zone=<availability-zone>]
              volume delete <volume>
              volume attach <server> <volume> <device>
              volume detach <server> <volume>
              volume show <volume>
              volume snapshot-list
              volume snapshot-create <volume-id>
                                     [--force]
                                     [--display-name=<display-name>]
                                     [--display-description=<display-description>]
              volume snapshot-delete <snapshot>
              volume snapshot-show <snapshot>
              volume help


          volume management

          Arguments:
              <size>            Size of volume in GB
              <volume>          Name or ID of the volume to delete
              <volume-id>       ID of the volume to snapshot
              <server>          Name or ID of server(VM).
              <device>          Name of the device e.g. /dev/vdb. Use "auto" for 
                                autoassign (if supported)
              <snapshot>        Name or ID of the snapshot

          Options:
              --snapshot-id <snapshot-id>
                                      Optional snapshot id to create the volume from.
                                      (Default=None)
              --image-id <image-id>
                                      Optional image id to create the volume from.
                                      (Default=None)
              --display-name <display-name>
                                      Optional volume name. (Default=None)
              --display-description <display-description>
                                      Optional volume description. (Default=None)
              --volume-type <volume-type>
                                      Optional volume type. (Default=None)
              --availability-zone <availability-zone>
                                      Optional Availability Zone for volume. (Default=None)
              --force                 Optional flag to indicate whether to snapshot a volume
                                      even if its attached to an instance. (Default=False)

          Description:
              volume list
                  List all the volumes
              volume create <size> [options...]
                  Add a new volume
              volume delete <volume>
                  Remove a volume   
              volume attach <server> <volume> <device>
                  Attach a volume to a server    
              volume-detach <server> <volume>
                  Detach a volume from a server
              volume show <volume>        
                  Show details about a volume
              volume snapshot-list
                  List all the snapshots
              volume snapshot-create <volume-id> [options...]
                  Add a new snapshot
              volume snapshot-delete <snapshot>
                  Remove a snapshot
              volume-snapshot-show <snapshot>
                  Show details about a snapshot
              volume help 
                  Prints the nova manual

        """
        # log.info(arguments)
        
        keys = os.environ.keys()
        items = ['OS_USERNAME','OS_PASSWORD','OS_TENANT_NAME','OS_AUTH_URL']
        for item in items:
            if item not in keys:
                Console.warning("Please first update the environment variables from the cloud by: "
                                "nova set [CLOUD]")
                return

        if arguments["help"]:
            os.system("nova help")

        elif arguments['list']:
            os.system('nova volume-list')
            
        elif arguments['create']:
            command = "nova volume-create " + arguments['<size>']
            for item in ['--snapshot-id',
                         '--image-id',
                         '--display-name',
                         '--display-description',
                         '--volume-type',
                         '--availability-zone']:
                if arguments[item]:
                    command = command + " {0} {1}".format(item, arguments[item])
            os.system(command)
               
        elif arguments['delete']:
            os.system("nova volume-delete {0}".format(arguments['<volume>']))
            
        elif arguments['attach']:
            os.system("nova volume-attach {0} {1} {2}".format(arguments['<server>'],
                                                              arguments['<volume>'],
                                                              arguments['<device>']))
            
        elif arguments['detach']:
            os.system("nova volume-detach {0} {1}".format(arguments['<server>'],
                                                          arguments['<volume>']))
        
        elif arguments['show']:
            os.system("nova volume-show {0}".format(arguments['<volume>']))
            
        elif arguments['snapshot-create']:
            command = "nova volume-snapshot-create " + arguments['<volume-id>']
            for item in ['--display-name',
                         '--display-description']:
                if arguments[item]:
                    command = command + " {0} {1}".format(item, arguments[item])
            if arguments['--force']:
                command = command + " --force True"
            os.system(command)
        
        elif arguments['snapshot-delete']:
            os.system("nova volume-snapshot-delete {0}".format(arguments['<snapshot>']))
            
        elif arguments['snapshot-list']:
            os.system('nova volume-snapshot-list')
        
        elif arguments['snapshot-show']:
            os.system("nova volume-snapshot-show {0}".format(arguments['<snapshot>']))
            
        else:
            print(arguments)
            return
Exemplo n.º 23
0
    def do_launcher(self, args, arguments):
        """
        ::

          Usage:
                launcher start MENU
                launcher stop STACK_NAME
                launcher list
                launcher show STACK_NAME
                launcher menu [--column=COLUMN] [--format=FORMAT]
                launcher import [FILEPATH] [--force]
                launcher export FILEPATH
                launcher help | -h

            An orchestration tool with Chef Cookbooks

            Arguments:

              MENU           Name of a cookbook
              STACK_NAME     Name of a launcher
              FILEPATH       Filepath
              COLUMN         column name to display
              FORMAT         display format (json, table)
              help           Prints this message

            Options:

               -v       verbose mode

        """
        log.info(arguments)
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()
        self._id = "t_stacks"

        if arguments["help"] or arguments["-h"]:
            print(self.do_launcher.__doc__)
        elif arguments["show"] and arguments["STACK_NAME"]:
            print("NOT IMPLEMENTED")
            return
        elif arguments["menu"]:
            userid = self.cm_config.username()
            launchers = self.cm_mongo.launcher_get(userid)

            if launchers.count() == 0:
                Console.warning(
                    "no launcher in database, please import launcher first" "(launcher import [FILEPATH] [--force])"
                )
                return
            else:
                d = {}
                for launcher in launchers:
                    d[launcher["cm_launcher"]] = launcher
                    if "_id" in d[launcher["cm_launcher"]]:
                        del d[launcher["cm_launcher"]]["_id"]

            columns = None
            if arguments["--column"]:
                if arguments["--column"] != "all":
                    columns = [x.strip() for x in arguments["--column"].split(",")]
            else:
                columns = ["name", "description"]

            if arguments["--format"]:
                if arguments["--format"] not in ["table", "json", "csv"]:
                    Console.error("please select printing format ", "among table, json and csv")
                    return
                else:
                    p_format = arguments["--format"]
            else:
                p_format = None

            shell_commands_dict_output(
                userid,
                d,
                print_format=p_format,
                firstheader="launcher",
                header=columns
                # vertical_table=True
            )

        elif arguments["list"]:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)
            self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            stacks = self.cm_mongo.stacks(cm_user_id=userid)
            launchers = self.filter_launcher(stacks, {"search": "contain", "key": "stack_name", "value": "launcher"})
            log.debug(launchers)

            d = {}
            for k0, v0 in launchers.iteritems():
                for k1, v1 in launchers[k0].iteritems():
                    d[v1["id"]] = v1
            columns = ["stack_name", "description", "stack_status", "creation_time", "cm_cloud"]
            if arguments["--column"] and arguments["--column"] != "all":
                columns = [x.strip() for x in arguments["--column"].split(",")]

            if arguments["--format"]:
                if arguments["--format"] not in ["table", "json", "csv"]:
                    Console.error("please select printing format among table, json and csv")
                    return
                else:
                    p_format = arguments["--format"]
            else:
                p_format = None

            shell_commands_dict_output(
                userid,
                d,
                print_format=p_format,
                firstheader="launcher_id",
                header=columns
                # vertical_table=True
            )

        elif arguments["start"] and arguments["MENU"]:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            config_launcher = cm_config_launcher()

            userinfo = self.cm_user.info(userid)
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]

            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith("key "):
                    keycontent = keycontent[4:]
                cm_keys_mongo(userid).check_register_key(userid, def_cloud, key, keycontent)
                keynamenew = _keyname_sanitation(userid, key)
            else:
                Console.warning("No sshkey found. Please Upload one")

            cookbook = arguments["MENU"]
            s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook, get_rand_string())
            dummy = "123456789"  # doing nothing. just for test
            try:
                t_url = config_launcher["cloudmesh"]["launcher"]["default"]["template"]
            except:
                # If key is missing (KeyError), new cloudmesh_launcher.yaml
                # needs to be copied to ~/.cloudmesh
                t_url = "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml"

            param = {"KeyName": keynamenew, "Cookbook": cookbook, "dummy": dummy}
            # test for openmpi, hadoop
            if cookbook[:6] == "hadoop" or cookbook[:7] == "openmpi":
                privatekey, publickey = generate_keypair()
                try:
                    t_url = config_launcher["cloudmesh"]["launcher"]["recipes"][cookbook]["template"]
                except:
                    # If key is missing (KeyError), new cloudmesh_launcher.yaml
                    # needs to be copied to ~/.cloudmesh
                    t_url = (
                        "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/ubuntu-14.04/"
                        + str(cookbook)
                        + "-cluster/"
                        + str(cookbook)
                        + "-cluster.yaml"
                    )
                param = {"KeyName": keynamenew, "PublicKeyString": publickey, "PrivateKeyString": privatekey}
                if cookbook[:9] == "hadoop2.7":
                    param["UserName"] = userid

            log.debug(def_cloud, userid, s_name, t_url, param)
            res = self.cm_mongo.stack_create(
                cloud=def_cloud, cm_user_id=userid, servername=s_name, template_url=t_url, parameters=param
            )
            log.debug(res)
            if "error" in res:
                print(res["error"]["message"])
            return res

        elif arguments["stop"] and arguments["STACK_NAME"]:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_id = arguments["STACK_NAME"]
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid, server=s_id)
            log.debug(res)
            return res

        elif arguments["import"]:
            filepath = config_file("/cloudmesh_launcher.yaml")

            if arguments["FILEPATH"]:
                filepath = arguments["FILEPATH"]
            try:
                filename = path_expand(filepath)
                fileconfig = ConfigDict(filename=filename)
            except Exception, err:
                Console.error("error while loading '{0}', please check".format(filepath))
                print(traceback.format_exc())
                print(sys.exc_info()[0])
                return
            try:
                recipes_dict = fileconfig.get("cloudmesh", "launcher", "recipies")
            except:
                Console.error("error while loading recipies from the file")

            # print recipes_dict
            userid = self.cm_config.username()
            launcher_names = []
            launchers = self.cm_mongo.launcher_get(userid)
            for launcher in launchers:
                launcher_names.append(launcher["cm_launcher"].encode("ascii"))

            for key in recipes_dict:
                if key in launcher_names:
                    if arguments["--force"]:
                        self.cm_mongo.launcher_remove(userid, key)
                        self.cm_mongo.launcher_import(recipes_dict[key], key, userid)
                        print("launcher '{0}' overwritten.".format(key))
                    else:
                        print(
                            "ERROR: launcher '{0}' exists, "
                            "please remove it first, or use "
                            "'--force' when adding".format(key)
                        )
                else:
                    self.cm_mongo.launcher_import(recipes_dict[key], key, userid)
                    print("launcher '{0}' added.".format(key))
Exemplo n.º 24
0
    def do_volume(self, args, arguments):
        """
        Usage:
            volume list
            volume create <size>
                          [--snapshot-id=<snapshot-id>]
                          [--image-id=<image-id>]
                          [--display-name=<display-name>]
                          [--display-description=<display-description>]
                          [--volume-type=<volume-type>]
                          [--availability-zone=<availability-zone>]
            volume delete <volume>
            volume attach <server> <volume> <device>
            volume detach <server> <volume>
            volume show <volume>
            volume snapshot-list
            volume snapshot-create <volume-id>
                                   [--force]
                                   [--display-name=<display-name>]
                                   [--display-description=<display-description>]
            volume snapshot-delete <snapshot>
            volume snapshot-show <snapshot>
            volume help


        volume management

        Arguments:
            <size>            Size of volume in GB
            <volume>          Name or ID of the volume to delete
            <volume-id>       ID of the volume to snapshot
            <server>          Name or ID of server(VM).
            <device>          Name of the device e.g. /dev/vdb. Use "auto" for 
                              autoassign (if supported)
            <snapshot>        Name or ID of the snapshot

        Options:
            --snapshot-id <snapshot-id>
                                    Optional snapshot id to create the volume from.
                                    (Default=None)
            --image-id <image-id>
                                    Optional image id to create the volume from.
                                    (Default=None)
            --display-name <display-name>
                                    Optional volume name. (Default=None)
            --display-description <display-description>
                                    Optional volume description. (Default=None)
            --volume-type <volume-type>
                                    Optional volume type. (Default=None)
            --availability-zone <availability-zone>
                                    Optional Availability Zone for volume. (Default=None)
            --force                 Optional flag to indicate whether to snapshot a volume
                                    even if its attached to an instance. (Default=False)
                                    
        Description:
            volume list
                List all the volumes
            volume create <size> [options...]
                Add a new volume
            volume delete <volume>
                Remove a volume   
            volume attach <server> <volume> <device>
                Attach a volume to a server    
            volume-detach <server> <volume>
                Detach a volume from a server
            volume show <volume>        
                Show details about a volume
            volume snapshot-list
                List all the snapshots
            volume snapshot-create <volume-id> [options...]
                Add a new snapshot
            volume snapshot-delete <snapshot>
                Remove a snapshot
            volume-snapshot-show <snapshot>
                Show details about a snapshot
            volume help 
                Prints the nova manual

        """
        # log.info(arguments)
        
        keys = os.environ.keys()
        items = ['OS_USERNAME','OS_PASSWORD','OS_TENANT_NAME','OS_AUTH_URL']
        for item in items:
            if item not in keys:
                Console.warning("Please first update the environment variables from the cloud by: "
                                "nova set [CLOUD]")
                return

        if arguments["help"]:
            os.system("nova help")

        elif arguments['list']:
            os.system('nova volume-list')
            
        elif arguments['create']:
            command = "nova volume-create " + arguments['<size>']
            for item in ['--snapshot-id',
                         '--image-id',
                         '--display-name',
                         '--display-description',
                         '--volume-type',
                         '--availability-zone']:
                if arguments[item]:
                    command = command + " {0} {1}".format(item, arguments[item])
            os.system(command)
               
        elif arguments['delete']:
            os.system("nova volume-delete {0}".format(arguments['<volume>']))
            
        elif arguments['attach']:
            os.system("nova volume-attach {0} {1} {2}".format(arguments['<server>'],
                                                              arguments['<volume>'],
                                                              arguments['<device>']))
            
        elif arguments['detach']:
            os.system("nova volume-detach {0} {1}".format(arguments['<server>'],
                                                          arguments['<volume>']))
        
        elif arguments['show']:
            os.system("nova volume-show {0}".format(arguments['<volume>']))
            
        elif arguments['snapshot-create']:
            command = "nova volume-snapshot-create " + arguments['<volume-id>']
            for item in ['--display-name',
                         '--display-description']:
                if arguments[item]:
                    command = command + " {0} {1}".format(item, arguments[item])
            if arguments['--force']:
                command = command + " --force True"
            os.system(command)
        
        elif arguments['snapshot-delete']:
            os.system("nova volume-snapshot-delete {0}".format(arguments['<snapshot>']))
            
        elif arguments['snapshot-list']:
            os.system('nova volume-snapshot-list')
        
        elif arguments['snapshot-show']:
            os.system("nova volume-snapshot-show {0}".format(arguments['<snapshot>']))
            
        else:
            print(arguments)
            return
Exemplo n.º 25
0
    def do_launcher(self, args, arguments):
        """
        Usage:
            launcher start MENU
            launcher stop STACK_NAME
            launcher list
            launcher show STACK_NAME
            launcher menu [--column=COLUMN] [--format=FORMAT]
            launcher import [FILEPATH] [--force]
            launcher export FILEPATH
            launcher help | -h

        An orchestration tool with Chef Cookbooks

        Arguments:

          MENU           Name of a cookbook
          STACK_NAME     Name of a launcher
          FILEPATH       Filepath
          COLUMN         column name to display
          FORMAT         display format (json, table)
          help           Prints this message

        Options:

           -v       verbose mode

        """
        log.info(arguments)
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.user = cm_user()

        if arguments["help"] or arguments["-h"]:
            print (self.do_launcher.__doc__)
        elif arguments['show'] and arguments['STACK_NAME']:
            print ("NOT IMPLEMENTED")
            return
        elif arguments['menu']:
            userid = self.cm_config.username()
            launchers = self.cm_mongo.launcher_get(userid)

            if launchers.count() == 0:
                Console.warning("no launcher in database, please import launcher first"
                                "(launcher import [FILEPATH] [--force])")
                return
            else:
                d = {}
                for launcher in launchers:
                    d[launcher['cm_launcher']] = launcher
                    if "_id" in d[launcher['cm_launcher']]:
                        del d[launcher['cm_launcher']]['_id']

            columns = None
            if arguments['--column']:
                if arguments['--column'] != "all":
                    columns = [x.strip() for x in arguments['--column'].split(',')]
            else:
                columns = ['name', 'description']

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format ",
                                  "among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['list']:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)
            self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            stacks = self.cm_mongo.stacks(cm_user_id=userid)
            launchers = self.filter_launcher(
                stacks,
                {"search": "contain",
                 "key": "stack_name",
                 "value": "launcher"}
                )
            log.debug(launchers)

            d = {}
            for k0, v0 in launchers.iteritems():
                for k1, v1 in launchers[k0].iteritems():
                    d[v1['id']] = v1
            columns = ['stack_name', 'description', 'stack_status',
                       'creation_time', 'cm_cloud']
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher_id",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['start'] and arguments['MENU']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            
            userinfo = self.user.info(userid)
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]
        
            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith('key '):
                    keycontent = keycontent[4:]
                cm_keys_mongo(userid).check_register_key(userid, def_cloud, key, keycontent)
                keynamenew = _keyname_sanitation(userid, key)
            else:
                Console.warning("No sshkey found. Please Upload one")
            
            cookbook = arguments['MENU']
            s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook, get_rand_string())
            dummy = "123456789"  # doing nothing. just for test
            t_url = \
            "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml"
            param = {'KeyName': keynamenew,
                     'Cookbook': cookbook,
                     'dummy': dummy}
            log.debug(def_cloud, userid, s_name, t_url, param)
            res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            log.debug(res)
            if 'error' in res:
                print (res['error']['message'])
            return res

        elif arguments['stop'] and arguments['STACK_NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_id = arguments['STACK_NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud,
                                             cm_user_id=userid,
                                             server=s_id)
            log.debug(res)
            return res

        elif arguments['import']:
            filepath = config_file("/cloudmesh_launcher.yaml")

            if arguments['FILEPATH']:
                filepath = arguments['FILEPATH']
            try:
                filename = path_expand(filepath)
                fileconfig = ConfigDict(filename=filename)
            except Exception, err:
                Console.error(
                    "error while loading '{0}', please check".format(filepath))
                print (traceback.format_exc())
                print (sys.exc_info()[0])
                return
            try:
                recipes_dict = fileconfig.get("cloudmesh", "launcher", "recipies")
            except:
                Console.error("error while loading recipies from the file")

            # print recipes_dict
            userid = self.cm_config.username()
            launcher_names = []
            launchers = self.cm_mongo.launcher_get(userid)
            for launcher in launchers:
                launcher_names.append(launcher['cm_launcher'].encode("ascii"))

            for key in recipes_dict:
                if key in launcher_names:
                    if arguments['--force']:
                        self.cm_mongo.launcher_remove(userid, key)
                        self.cm_mongo.launcher_import(
                            recipes_dict[key], key, userid)
                        print ("launcher '{0}' overwritten.".format(key))
                    else:
                        print ("ERROR: launcher '{0}' exists, "
                               "please remove it first, or use "
                               "'--force' when adding".format(key))
                else:
                    self.cm_mongo.launcher_import(
                        recipes_dict[key], key, userid)
                    print ("launcher '{0}' added.".format(key))
Exemplo n.º 26
0
class cm_shell_launcher:

    """opt_example class"""
    _id = "t_stacks"  # id for stack in cm_mongo

    def activate_cm_shell_launcher(self):
        self.register_command_topic('cloud', 'launcher')
        pass

    def get_cloud_name(self, cm_user_id):
        """Returns a default cloud name if exists
        """
        try:
            return self.cm_user.get_defaults(cm_user_id)['cloud']
        except KeyError:
            log.error('set a default cloud with openstack. "stack" works on'
                      ' openstack platform only')
            return None

    @command
    def do_launcher(self, args, arguments):
        """
        Usage:
            launcher start MENU
            launcher stop STACK_NAME
            launcher list
            launcher show STACK_NAME
            launcher menu [--column=COLUMN] [--format=FORMAT]
            launcher import [FILEPATH] [--force]
            launcher export FILEPATH
            launcher help | -h

        An orchestration tool with Chef Cookbooks

        Arguments:

          MENU           Name of a cookbook
          STACK_NAME     Name of a launcher
          FILEPATH       Filepath
          COLUMN         column name to display
          FORMAT         display format (json, table)
          help           Prints this message

        Options:

           -v       verbose mode

        """
        log.info(arguments)
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.user = cm_user()

        if arguments["help"] or arguments["-h"]:
            print (self.do_launcher.__doc__)
        elif arguments['show'] and arguments['STACK_NAME']:
            print ("NOT IMPLEMENTED")
            return
        elif arguments['menu']:
            userid = self.cm_config.username()
            launchers = self.cm_mongo.launcher_get(userid)

            if launchers.count() == 0:
                Console.warning("no launcher in database, please import launcher first"
                                "(launcher import [FILEPATH] [--force])")
                return
            else:
                d = {}
                for launcher in launchers:
                    d[launcher['cm_launcher']] = launcher
                    if "_id" in d[launcher['cm_launcher']]:
                        del d[launcher['cm_launcher']]['_id']

            columns = None
            if arguments['--column']:
                if arguments['--column'] != "all":
                    columns = [x.strip() for x in arguments['--column'].split(',')]
            else:
                columns = ['name', 'description']

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format ",
                                  "among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['list']:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)
            self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            stacks = self.cm_mongo.stacks(cm_user_id=userid)
            launchers = self.filter_launcher(
                stacks,
                {"search": "contain",
                 "key": "stack_name",
                 "value": "launcher"}
                )
            log.debug(launchers)

            d = {}
            for k0, v0 in launchers.iteritems():
                for k1, v1 in launchers[k0].iteritems():
                    d[v1['id']] = v1
            columns = ['stack_name', 'description', 'stack_status',
                       'creation_time', 'cm_cloud']
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(userid,
                                       d,
                                       print_format=p_format,
                                       firstheader="launcher_id",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['start'] and arguments['MENU']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            
            userinfo = self.user.info(userid)
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]
        
            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith('key '):
                    keycontent = keycontent[4:]
                cm_keys_mongo(userid).check_register_key(userid, def_cloud, key, keycontent)
                keynamenew = _keyname_sanitation(userid, key)
            else:
                Console.warning("No sshkey found. Please Upload one")
            
            cookbook = arguments['MENU']
            s_name = "launcher-{0}-{1}-{2}".format(userid, cookbook, get_rand_string())
            dummy = "123456789"  # doing nothing. just for test
            t_url = \
            "https://raw.githubusercontent.com/cloudmesh/cloudmesh/master/heat-templates/centos6/launcher/launcher.yaml"
            param = {'KeyName': keynamenew,
                     'Cookbook': cookbook,
                     'dummy': dummy}
            log.debug(def_cloud, userid, s_name, t_url, param)
            res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            log.debug(res)
            if 'error' in res:
                print (res['error']['message'])
            return res

        elif arguments['stop'] and arguments['STACK_NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_id = arguments['STACK_NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud,
                                             cm_user_id=userid,
                                             server=s_id)
            log.debug(res)
            return res

        elif arguments['import']:
            filepath = config_file("/cloudmesh_launcher.yaml")

            if arguments['FILEPATH']:
                filepath = arguments['FILEPATH']
            try:
                filename = path_expand(filepath)
                fileconfig = ConfigDict(filename=filename)
            except Exception, err:
                Console.error(
                    "error while loading '{0}', please check".format(filepath))
                print (traceback.format_exc())
                print (sys.exc_info()[0])
                return
            try:
                recipes_dict = fileconfig.get("cloudmesh", "launcher", "recipies")
            except:
                Console.error("error while loading recipies from the file")

            # print recipes_dict
            userid = self.cm_config.username()
            launcher_names = []
            launchers = self.cm_mongo.launcher_get(userid)
            for launcher in launchers:
                launcher_names.append(launcher['cm_launcher'].encode("ascii"))

            for key in recipes_dict:
                if key in launcher_names:
                    if arguments['--force']:
                        self.cm_mongo.launcher_remove(userid, key)
                        self.cm_mongo.launcher_import(
                            recipes_dict[key], key, userid)
                        print ("launcher '{0}' overwritten.".format(key))
                    else:
                        print ("ERROR: launcher '{0}' exists, "
                               "please remove it first, or use "
                               "'--force' when adding".format(key))
                else:
                    self.cm_mongo.launcher_import(
                        recipes_dict[key], key, userid)
                    print ("launcher '{0}' added.".format(key))

        elif arguments['export']:
            userid = self.cm_config.username()
            launchers = self.cm_mongo.launcher_get(userid)

            if launchers.count() == 0:
                Console.warning(
                    "no launcher in database, "
                    "please import launcher first"
                    "(launcher import [FILEPATH] [--force])")
            else:
                d = {}
                for launcher in launchers:
                    key = launcher['cm_launcher']
                    d[key] = launcher
                    if "_id" in d[key]:
                        del d[key]['_id']
                    if "cm_launcher" in d[key]:
                        del d[key]['cm_launcher']
                    if "cm_kind" in d[key]:
                        del d[key]['cm_kind']
                    if "cm_user_id" in d[key]:
                        del d[key]['cm_user_id']

                d = dict_uni_to_ascii(d)

                d = {"meta": {"yaml_version": "2.1",
                              "kind": "launcher"},
                     "cloudmesh": {"launcher": {"recipies": d}}}

                pprint(d)

                print ("exporting to {0}...".format(arguments['FILEPATH']))

                try:
                    filename = path_expand(arguments['FILEPATH'])
                    stream = file(filename, 'w')
                    ordered_dump(d, stream=stream)
                    Console.ok("done")
                except Exception, err:
                    Console.error("failed exporting to {0}"
                                  .format(arguments['FILEPATH']))
                    print (traceback.format_exc())
                    print (sys.exc_info()[0])
Exemplo n.º 27
0
    def do_cluster(self, args, arguments):
        """
        Usage:
            cluster start CLUSTER_NAME
            cluster list
            cluster login CLUSTER_NAME
            cluster stop CLUSTER_NAME
            cluster create --count=<count>
                           --group=<group>
                           [--ln=<LoginName>]
                           [--cloud=<CloudName>]
                           [--image=<imgName>|--imageid=<imgId>]
                           [--flavor=<flavorName>|--flavorid=<flavorId>]
                           [--force]
                                    
        Description:
            Cluster Management
            
            cluster create --count=<count> --group=<group> --ln=<LoginName> [options...]
            <count>            specify amount of VMs in the cluster
            <group>            specify a group name of the cluster, make sure it's unique
                Start a cluster of VMs, and each of them can log into all others.
                CAUTION: you sould do some default setting before using this command:
                1. select cloud to work on, e.g. cloud select india
                2. activate the cloud, e.g. cloud on india
                3. set the default key to start VMs, e.g. key default [NAME]
                4. set the start name of VMs, which is prefix and index, e.g. label --prefix=test --id=1
                5. set image of VMs, e.g. default image
                6. set flavor of VMs, e.g. default flavor
                Also, please make sure the group name of the cluster is unique
                
        Options:
            --ln=<LoginName>           give a login name for the VMs, e.g. ubuntu
            --cloud=<CloudName>        give a cloud to work on
            --flavor=<flavorName>      give the name of the flavor
            --flavorid=<flavorId>      give the id of the flavor
            --image=<imgName>          give the name of the image
            --imageid=<imgId>          give the id of the image
            --force                    if a group exists and there are VMs in it, the program will
                                       ask user to proceed or not, use this flag to respond yes as 
                                       default(if there are VMs in the group before creating this 
                                       cluster, the program will include the exist VMs into the cluster)


        """
        #pprint(arguments)
        self.cm_config = cm_config()
        self.cm_mongo = cm_mongo()
        self.user = cm_user()
       
        # -----------------------------
        # TODO::
        # add VMs to cluster
        # -----------------------------
       
        if arguments['start'] and arguments['CLUSTER_NAME']:
            '''Starts a cluster'''

            # Initialize default variables. e.g. userid, default cloud and
            # default keypair
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            
            userinfo = self.user.info(userid)
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]
        
            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith('key '):
                    keycontent = keycontent[4:]
                cm_keys_mongo(userid).check_register_key(userid, def_cloud, key, keycontent)
                keynamenew = _keyname_sanitation(userid, key)
            else:
                Console.warning("No sshkey found. Please Upload one")
                return
            
            clustername = arguments['CLUSTER_NAME']
            s_name = "cluster-{0}-{1}-{2}".format(userid, clustername, get_rand_string())
            # TEMP FOR HADOOP CLUSTER
            if clustername != "hadoop":
                Console.warning('hadoop is only available cluster')
                return
            
            # 1. keypair for the communication between master and worker nodes
            privatekey, publickey = generate_keypair()
            t_url = \
            "https://raw.githubusercontent.com/cloudmesh/cloudmesh/dev1.3/heat-templates/ubuntu-14.04/hadoop-cluster/hadoop-cluster.yaml"
            param = {'KeyName': keynamenew,
                     'PublicKeyString': publickey,
                     'PrivateKeyString': privatekey}
            log.debug(def_cloud, userid, s_name, t_url, param, publickey,
                      privatekey)
            res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            log.debug(res)
            if 'error' in res:
                print (res['error']['message'])
            return res

        elif arguments['list']:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)
            self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            stacks = self.cm_mongo.stacks(cm_user_id=userid)
            launchers = self.filter_launcher(
                stacks,
                {"search": "contain",
                 "key": "stack_name",
                 "value": "launcher"}
                )
            log.debug(launchers)

            d = {}
            for k0, v0 in launchers.iteritems():
                for k1, v1 in launchers[k0].iteritems():
                    d[v1['id']] = v1
            columns = ['stack_name', 'description', 'stack_status',
                       'creation_time', 'cm_cloud']
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]

            if arguments['--format']:
                if arguments['--format'] not in ['table', 'json', 'csv']:
                    Console.error("please select printing format among table, json and csv")
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            shell_commands_dict_output(d,
                                       print_format=p_format,
                                       firstheader="launcher_id",
                                       header=columns
                                       # vertical_table=True
                                       )

        elif arguments['login']:
            Console.error("Not implemented")
            return

        elif arguments['stop'] and arguments['CLUSTER_NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            c_id = arguments['CLUSTER_NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud,
                                             cm_user_id=userid,
                                             server=c_id)
            log.debug(res)
            return res

        elif arguments['create']:
            try:
                config = cm_config()
            except:
                Console.error("Failed to load the cloudmesh yaml file")
                return
            username = config['cloudmesh']['profile']['username']
            cloudname = arguments['--cloud'] or CloudManage().get_selected_cloud(username)
            temp_dir_name = ".temp_cluster_create_" + username + "_0"
            while os.path.isdir("./{0}".format(temp_dir_name)):
                temp_dir_name = temp_dir_name[:-1] + str(int(temp_dir_name[-1]) + 1)
            dir_name = temp_dir_name
            
            #NumOfVM = None
            GroupName = None

            vm_login_name = "ubuntu"

            
            temp_key_name = "sshkey_temp"
            _key = "-i ./{0}/{1}".format(dir_name, temp_key_name)
            StrictHostKeyChecking = "-o StrictHostKeyChecking=no"
            
            res = None
            to_print = []
            '''
            try:
                NumOfVM = abs(int(argument['--count']))
            except:
                Console.error("<count> must be an integer")
                return
            '''
            if arguments['--group'] == '':
                Console.error("<group> cannot be empty")
                return
            else:
                GroupName = arguments['--group']
            
            if arguments['--ln']:
                if arguments['--ln'] == '':
                    Console.error("<LoginName> cannot be empty")
                    return
                else:
                    vm_login_name = arguments['--ln']
            
            if not arguments['--force']:
                # Moved the import inside of this function
                # If this import goes up to the top, monodb connection will be
                # estabilished. Due to that reason, this import stays here
                # Hyungro Lee 12/01/2014
                from cloudmesh.experiment.group import GroupManagement
                GroupManage = GroupManagement(username)
                groups_list = GroupManage.get_groups_names_list()
                if GroupName in groups_list:
                    vms_in_group_list = GroupManage.list_items_of_group(GroupName, _type="VM")["VM"]
                    if len(vms_in_group_list) != 0:
                        if yn_choice("The group you provide exists and it has VMs in it, " + \
                                     "do you want to proceed? (if you choose yes, these exist " +\
                                     "VMs will be included in the cluster, this could also " +\
                                     "rewrite the key on the exist VMs)",
                                     default='n',
                                     tries=3):
                            pass
                        else:
                            return
                
            # start VMs 
            print ("starting VMs...")
            arguments_temp = arguments
            arguments_temp['start'] = True
            arguments_temp['--name'] = None
            
            vmclass = VMcommand(arguments_temp)
            res = vmclass.call_procedure()
            if res == False: return
        
            def string_to_dict(s):
                h = s.find("{")
                t = s.rfind("}")
                return json.loads(s[h:t+1])
            
            def check_public_ip_existence(d):
                temp = d['addresses']['private']
                for item in temp:
                    if item["OS-EXT-IPS:type"] == "floating":
                        return True
                return False
            
            def get_ip(d, kind="floating"): # kind is either floating or fixed
                temp = d['addresses']['private']
                for item in temp:
                    if item["OS-EXT-IPS:type"] == kind:
                        return item['addr']#.encode('ascii')
                return "FAIL: doesn't exist"
            
            # check all VMs are active
            command_refresh = "vm list --refresh --group={0} --format=json".format(GroupName)
            def _help0(d):
                for k, v in d.iteritems():
                    if v['status'] != 'ACTIVE':
                        return False
                return True
                
            proceed = False
            repeat_index = 1
            while proceed != True:
                if repeat_index > 10:
                    Console.warning("Please check the network")
                    return
                print ("checking({0})...".format(repeat_index))
                time.sleep(5)
                res = str(cm(command_refresh))
                res = string_to_dict(res)
                if _help0(res):
                    proceed = True
                else:
                    repeat_index = repeat_index + 1
                    continue
            
            # assign ip to all VMs
            print ("assigning public ips...")
            for k, v in res.iteritems():
                if not check_public_ip_existence(v):
                    cm("vm ip assign --id={0}".format(k.encode('ascii')))
            
            def _help(d):
                for k, v in d.iteritems():
                    if check_public_ip_existence(v) != True:
                        return False
                return True
            
            # make sure all VMs have been assigned a public ip
            proceed = False
            repeat_index = 1
            while proceed != True:
                if repeat_index > 10:
                    Console.warning("Please check the network")
                    return
                print ("checking({0})...".format(repeat_index))
                time.sleep(5)
                res = str(cm(command_refresh))
                res = string_to_dict(res)
                if _help(res):
                    proceed = True
                else:
                    repeat_index = repeat_index + 1
                    continue
                
            
            # -------------------------
            # key handler
            userinfo = cm_user().info(username)
            key = None
            if "key" in userinfo["defaults"]:
                key = userinfo["defaults"]["key"]
            elif len(userinfo["keys"]["keylist"].keys()) > 0:
                key = userinfo["keys"]["keylist"].keys()[0]
                Console.warning("default key is not set, trying to use a key in the database...")
        
            if key:
                keycontent = userinfo["keys"]["keylist"][key]
                if keycontent.startswith('key '):
                    keycontent = keycontent[4:]
                cm_keys_mongo(username).check_register_key(username, cloudname, key, keycontent)
            else:
                Console.error("No sshkey found. Please Upload one")
                return
            # -------------------------
            
            
            # generate ssh keys for VMs and prepare two files: authorized_keys and hosts
            print ("generating ssh keys...")
            os.popen("mkdir {0}".format(dir_name))

            fa = open("./{0}/authorized_keys_temp".format(dir_name), "w")
            fh = open("./{0}/hosts_temp".format(dir_name), "w")
            fk = open("./{0}/{1}".format(dir_name, temp_key_name), "w")
            
            fk.write(keycontent)
            fk.close()
            os.popen("chmod 644 ./{0}/{1}".format(dir_name, temp_key_name))
            
            for k, v in res.iteritems():
                address_floating = get_ip(v)
                address_fixed = get_ip(v, kind="fixed")
                vm_name = v['name']#.encode('ascii')
                to_print.append("{0} {1}, {2}".format(vm_name, address_floating, address_fixed))
                fh.write(address_floating + "  " + vm_name + "\n"
                         + address_fixed + "  " + vm_name + "-i\n")
                os.popen("ssh {2} {3} {0}@{1} \"ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa\""\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking))
                temp = os.popen("ssh {2} {3} {0}@{1} \"cat ~/.ssh/id_rsa.pub\""\
                                .format(vm_login_name, address_floating, _key, StrictHostKeyChecking)).read()
                fa.write(temp)
                
            fa.close()
            fh.close()
            
            # copy the files to VMs
            print ("copying the files...")
            os.popen("mkdir ./{0}/oops".format(dir_name))
            for k, v in res.iteritems():
                address_floating = get_ip(v)
                os.popen("scp {2} {3} {0}@{1}:~/.ssh/authorized_keys ./{4}/"\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name))
                os.popen("cat ./{0}/authorized_keys_temp >> ./{0}/authorized_keys"\
                         .format(dir_name))
                os.popen("scp {2} {3} ./{4}/authorized_keys {0}@{1}:~"\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name))
                os.popen("ssh {2} {3} {0}@{1} \"sudo mv authorized_keys ~/.ssh/\""\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking))
                os.popen("rm ./{0}/authorized_keys".format(dir_name))
                
                os.popen("cp ./{0}/hosts_temp ./{0}/oops/".format(dir_name))
                os.popen("mv ./{0}/oops/hosts_temp ./{0}/oops/hosts".format(dir_name))
                fh0 = open("./{0}/oops/hosts".format(dir_name), "a")
                os.popen("scp {2} {3} {0}@{1}:/etc/hosts ./{4}/"\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name))
                with open("./{0}/hosts".format(dir_name)) as f0:
                    content = f0.readlines()
                for item in content:
                    fh0.write(item + "\n")
                fh0.close()
                os.popen("scp {2} {3} ./{4}/oops/hosts {0}@{1}:~"\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking, dir_name))
                os.popen("ssh {2} {3} {0}@{1} \"sudo mv hosts /etc/\""\
                         .format(vm_login_name,address_floating, _key, StrictHostKeyChecking))
                os.popen("rm ./{0}/oops/hosts".format(dir_name))
            
            
            print ("finishing...")
            os.popen("rm -rf {0}".format(dir_name))
            print ("DONE.")
            
            print ("cluster group: ", GroupName)
            for item in to_print:
                print (item)
            print ("(host name for private ips will have -i at the end of VM name, e.g. testVM -> testVM-i)")
Exemplo n.º 28
0
def delete_vm(username,
              cloudname,
              server_id_list=None,
              preview=False,
              refresh=False):
    '''
    delete vms of a cloud of a user, this function provides several ways to find and
    delete vms
    :param server_id_list:: the list of VMs(id) to delete
    :param preview:: True if the user wants to preview and confirm before start to delete

    TODO: what if fail, how to acknowledge it
          range search: now if prefix not given, all vms whose index are in the range will
          be deleted, regardless of its prefix
          it looks like even though delete a vm and return {msg: seccess}, sometimes refresh
          after 5 sec, it might be still there
    '''
    # changed the scope of this import
    # Benefit: other functions are not affected with this import
    # drawback: hard to see which module is being loaded in this file
    # Hyungro Lee 12/01/2014
    from cloudmesh.experiment.group_usage import remove_vm_from_group_while_deleting
 
    try:
        mongo = cm_mongo()
    except:
        Console.error("There is a problem with the mongo server")
        return False
    if refresh:
        mongo.activate(cm_user_id=username, names=[cloudname])
        mongo.refresh(cm_user_id=username,
                        names=[cloudname],
                        types=['servers'])
    serverdata = mongo.servers(
                        clouds=[cloudname], cm_user_id=username)[cloudname]
    # -------------------------
    # preview and confirm
    confirm_deletion = True
    if preview:
        if server_id_list == []:
            Console.warning("no vm meets the condition")
            return False
        else:    
            resserverdata = {}
            for i in server_id_list:
                resserverdata[i] = serverdata[i]
            cloudobj = CloudManage()
            itemkeys = {"openstack":
                        [
                            ['name', 'name'],
                            ['status', 'status'],
                            ['addresses', 'addresses'],
                            ['id', 'id'],
                            ['flavor', 'flavor', 'id'],
                            ['image', 'image', 'id'],
                            ['user_id', 'cm_user_id'],
                            ['metadata', 'metadata'],
                            ['key_name', 'key_name'],
                            ['created', 'created'],
                            ['cloud', 'cm_cloud']
                        ],
                        "ec2":
                        [
                            ["name", "id"],
                            ["status", "extra", "status"],
                            ["addresses", "public_ips"],
                            ["flavor", "extra", "instance_type"],
                            ['id', 'id'],
                            ['image', 'extra', 'imageId'],
                            ["user_id", 'user_id'],
                            ["metadata", "metadata"],
                            ["key_name", "extra", "key_name"],
                            ["created", "extra", "launch_time"]
                        ],
                        "aws":
                        [
                            ["name", "name"],
                            ["status", "extra", "status"],
                            ["addresses", "public_ips"],
                            ["flavor", "extra", "instance_type"],
                            ['id', 'id'],
                            ['image', 'extra', 'image_id'],
                            ["user_id", "user_id"],
                            ["metadata", "metadata"],
                            ["key_name", "extra", "key_name"],
                            ["created", "extra", "launch_time"]
                        ],
                        "azure":
                        [
                            ['name', 'name'],
                            ['status', 'status'],
                            ['addresses', 'vip'],
                            ['flavor', 'flavor', 'id'],
                            ['id', 'id'],
                            ['image', 'image', 'id'],
                            ['user_id', 'user_id'],
                            ['metadata', 'metadata'],
                            ['key_name', 'key_name'],
                            ['created', 'created'],
                        ]
                        }

            cloudobj.print_cloud_servers(username=username,
                                         cloudname=cloudname,
                                         itemkeys=itemkeys,
                                         refresh=False,
                                         output=False,
                                         serverdata=resserverdata)
            if yn_choice("confirm to delete these vms?", default='n', tries=3):
                pass
            else:
                confirm_deletion = False

    # -------------------------
    # deleting
    if confirm_deletion:
        if server_id_list == []:
            return

        watch = time.time()

        useQueue = False
        if useQueue:
            # not functioning
            cloudmanager = mongo.clouds[username][cloudname]["manager"]
            cm_type = mongo.get_cloud_info(username, cloudname)['cm_type']
            package = "cloudmesh.iaas.%s.queue" % cm_type
            name = "tasks"
            imported = getattr(__import__(package, fromlist=[name]), name)
            queue_name = "%s-%s" % (cm_type, "servers")
            for i in server_id_list:
                tempservername = serverdata[i]['name'].encode("ascii")
                banner(
                    "Deleting vm->{0} on cloud->{1}".format(tempservername, cloudname))
                result = imported.vm_delete.apply_async(
                    (cloudname, i, username), queue=queue_name)
                print("job status:", result.state)
                try:
                    remove_vm_from_group_while_deleting(username, tempservername)
                except Exception, err:
                    Console.error(str(err))
                    return
                # print result.traceback  #########
            imported.wait.apply_async(
                args=None, kwargs={'t': 10}, queue=queue_name)
            handleip = imported.release_unused_public_ips.apply_async(
                (cloudname, username), queue=queue_name)
            handlerefresh = imported.refresh.apply_async(args=None,
                                                         kwargs={'cm_user_id': username,
                                                                 'names': [cloudname],
                                                                 'types': ['servers']},
                                                         queue=queue_name)

            # print handleip.state
            # print handleip.traceback
            # print handlerefresh.state
            # print handlerefresh.traceback
            if preview:
                print("to check realtime vm status: list vm --refresh")
        else:
            for i in server_id_list:
                tempservername = serverdata[i]['name'].encode("ascii")
                banner(
                    "Deleting vm->{0} on cloud->{1}".format(tempservername, cloudname))
                result = mongo.vm_delete(cloudname, i, username)
                pprint(result)
                try:
                    remove_vm_from_group_while_deleting(username, tempservername)
                except Exception, err:
                    Console.error(str(err))
                    return
            time.sleep(5)
            mongo.release_unused_public_ips(cloudname, username)
            mongo.refresh(username, names=[cloudname], types=['servers'])
Exemplo n.º 29
0
                if arguments['--format'] not in ALLOWED_PRINT_FORMAT:
                    Console.error(
                        "wrong print format: {0}. (allowed print format: {1})".
                        format(arguments['--format'],
                               ", ".join(ALLOWED_PRINT_FORMAT)))
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None

            columns = None
            if arguments['--column'] and arguments['--column'] != "all":
                columns = [x.strip() for x in arguments['--column'].split(',')]
            if columns == []:
                Console.warning("please provide column names for --column")
                return

            vmobj = VMs()
            vmobj._helper_vm_cli_printer(vms_dict,
                                         print_format=p_format,
                                         columns=columns,
                                         detailed=arguments['--detail'])

        elif arguments['remove']:
            try:
                clusterobj.delete(arguments['<name>'],
                                  grouponly=arguments['--grouponly'])
            except Exception, err:
                Console.error(str(err))
                return