예제 #1
0
    def _print_default(self, attr=None):
        to_print = self.get_defaults()

        columns = None
        if attr:
            columns = [attr]
        elif self.arguments['--column'] and self.arguments['--column'] != "all":
            columns = [x.strip() for x in self.arguments['--column'].split(',')]
            # ----------------------------------
            # flexible input
            for index, item in enumerate(columns):
                if item in ['format']:
                    columns[index] = "shell_print_format"
                    # ----------------------------------

        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

            # if p_format == 'table' or p_format is None:
            # print(row_table(to_print, order=None, labels=["Default", "Value"]))
        # else:
        shell_commands_dict_output(self.username,
                                   to_print,
                                   print_format=p_format,
                                   header=columns,
                                   oneitem=True,
                                   vertical_table=True)
예제 #2
0
    def _print_default(self, attr=None):
        to_print = self.get_defaults()

        columns = None
        if attr:
            columns = [attr]
        elif self.arguments['--column'] and self.arguments['--column'] != "all":
            columns = [
                x.strip() for x in self.arguments['--column'].split(',')
            ]
            # ----------------------------------
            # flexible input
            for index, item in enumerate(columns):
                if item in ['format']:
                    columns[index] = "shell_print_format"
                    # ----------------------------------

        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

            # if p_format == 'table' or p_format is None:
            # print(row_table(to_print, order=None, labels=["Default", "Value"]))
        # else:
        shell_commands_dict_output(self.username,
                                   to_print,
                                   print_format=p_format,
                                   header=columns,
                                   oneitem=True,
                                   vertical_table=True)
예제 #3
0
    def do_cluster(self, args, arguments):
        """
        ::
        
          Usage:
              cluster list [--format=FORMAT]
              cluster create <name>
                             [--count=<count>]
                             [--ln=<LoginName>]
                             [--cloud=<CloudName>]
                             [--image=<imgName>|--imageid=<imgId>]
                             [--flavor=<flavorName>|--flavorid=<flavorId>]
                             [--force]
              cluster show <name> 
                           [--format=FORMAT] 
                           [--column=COLUMN]
                           [--detail]
              cluster remove <name> 
                             [--grouponly]

          Description:
              Cluster Management
              
              cluster list
                  list the clusters

              cluster create <name> --count=<count> --ln=<LoginName> [options...]
                  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, it is better to choose a unused group name
              
              cluster show <name>
                  show the detailed information about the cluster VMs

              cluster remove <name> [--grouponly]
                  remove the cluster and its VMs, if you want to remove the cluster(group name)
                  without removing the VMs, use --grouponly flag
          
          Arguments:
              <name>        cluster name or group name

          Options:
              --count=<count>            give the number of VMs to add into the cluster
              --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)
              --grouponly                remove the group only without removing the VMs, otherwise 
                                         cluster remove command will remove all the VMs of this cluster
              FORMAT                     output format: table, json, csv
              COLUMN                     customize what information to display, for example:
                                         --column=status,addresses prints the columns status
                                         and addresses
              --detail                   for table print format, a brief version 
                                         is used as default, use this flag to print
                                         detailed table
                                         
        """
        #pprint(arguments)
        # defer the import of Clusters, as this further imports classes from cloudmesh.experiment.group,
        # which connects to mongo automatically due to the using of mongoengine.
        from cloudmesh.shell.clusters import Clusters
        # cannot move up at the beginning.
        
        clusterobj = Clusters()
        userobj = cm_user()
        config = cm_config()
        username = config.username()
        
       
        if arguments['list']:
            try:
                clusters_list = clusterobj.list_clusters()
            except Exception, err:
                Console.error(str(err))
                return
            if arguments['--format']:
                if arguments['--format'] not in ALLOWED_PRINT_FORMAT:
                    Console.error("wrong print format: {0}. (allowed print format: {1})".format(format_type, 
                        ", ".join(ALLOWED_PRINT_FORMAT)))
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None
            shell_commands_dict_output(username,
                                       clusters_list,
                                       print_format=p_format,
                                       firstheader="cluster",
                                       header=[["num of nodes", "num_of_nodes"],
                                               ["num of active nodes", "num_of_active_nodes"]])
예제 #4
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))
예제 #5
0
    def do_stack(self, args, arguments):
        """
        ::

          Usage:
              stack start NAME [--template=TEMPLATE] [--param=PARAM]
              stack stop NAME
              stack show NAME
              stack list [--refresh] [--column=COLUMN] [--format=FORMAT]
              stack help | -h

          An orchestration tool (OpenStack Heat)

          Arguments:

            NAME           stack name
            help           Prints this message

          Options:

             -v       verbose mode

        """
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()

        log.info(arguments)

        if arguments["help"] or arguments["-h"]:
            print(self.do_stack.__doc__)
        elif arguments['show'] and arguments['NAME']:
            print ("NOT IMPLEMENTED")
            return
        elif arguments['start'] and arguments['NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            t_url = arguments['--template']
            param = arguments['--param']
            s_name = arguments['NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_create(cloud=def_cloud, cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            print(res)
            return res

        elif arguments['stop'] and arguments['NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_name = arguments['NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud, cm_user_id=userid,
                                       server=s_name)

            return res

        elif arguments['list']:
            userid = self.cm_config.username()
            if arguments['--refresh']:
                self.cm_mongo.activate(userid)
                self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            d = self.cm_mongo.stacks(cm_user_id=userid)
            
            columns = None
            if arguments['--column']:
                if arguments['--column'] != "all":
                    columns = [x.strip() for x in arguments['--column'].split(',')]
            else:
                columns = ['stack_name','description', 'stack_status',
                       'creation_time', 'cm_cloud']
                
            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
            
            for k, v in d.iteritems():
                for k0, v0 in v.iteritems():
                    if '_id' in v0:
                        del v0['_id']
                
                shell_commands_dict_output(userid,
                                           v,
                                           print_format=p_format,
                                           firstheader="stack_id",
                                           header=columns)
예제 #6
0
    elif arguments['show']:
        try:
            res = GroupManage.list_items_of_group(name, _type=type_)
        except Exception, err:
            Console.error(str(err))
            return

        if arguments['--format']:
            p_format = arguments['--format']
        else:
            p_format = None

        shell_commands_dict_output(username,
                                   res,
                                   print_format=p_format,
                                   table_format="key_list",
                                   indexed=True)

    elif arguments["remove"] and arguments['item']:
        try:
            GroupManage.delete_item_of_group(name, type_, value)
        except Exception, err:
            Console.error(str(err))
            return
        Console.ok("item '{0}' of type '{1}' removed from group '{2}'".format(
            value, type_, name))


def main():
    # cmd3_call(shell_command_experiment_group)
예제 #7
0
def shell_command_experiment_group(arguments):
    """
    ::

      Usage:
          group list [--format=FORMAT]
          group create NAME
          group remove NAME
          group add item NAME TYPE VALUE
          group remove item NAME TYPE VALUE
          group show NAME [TYPE] [--format=FORMAT]

      Arguments:

          NAME    name of the group
          TYPE    type of the item in the group, e.g. vm 
          VALUE   value of item to add, e.g. vm name

      Options:

          -v               verbose mode
          --format=FORMAT  output format: table, json, csv

      Description:

         group list           lists the groups
         group create         creates a new group
         group remove         removes a group
         group add item       addes an item of a type to a group
         group remove item    removes an item of a type from a group
         group show           lists items of a group

      Examples:
          group add item sample vm samplevm
              add vm named samplevm to group sample

          group show sample vm --format=json
              list all VMs of group sample in json format
    """

    """
      Example:

         group create experiment_1
         vm start
         last = vm label
         group add experiment_1 vm last

         group create experiment_2
         vm start
         last = vm info label  # prints the vm label /prefix + number
         ipno = vm info ip # prints the ip of the last vm
         ipno = vm info ip gvonlasz_1  # get ip of vm with label gvonlasz_1

         group add expermiment_2 ip ipno

         groups are just tuples

         i can have multiple Kinds in the tuple

      mongoengine

      class groupObject

          def add (... name, kind, attribute ...)
          def printer ( ... kind, printfunction, name...)
          def getter ( .... kind, name)

      def getter ( .... kind, name ...)

         if kind == "vm":
            vm = get vm from mongo
            return vm
         elif kind = "image"
            iamge = get image from mongo
            return iamge
         ....

      def vmPrinter ( .... vm ...)

         print vm.ip
         print vm.name
         ....

      def imagePrinter ( .... image ...)

         print image.size
         print image.name
         ....



      g = groupObject()
      g.printer("vm", cmPrinter)
      g.printer("image", imagePrinter)



       
    """
    # Changed the scope of this import.
    from cloudmesh.experiment.group import GroupManagement
    from cloudmesh.experiment.group_usage import add_item_to_group

    name = arguments["NAME"]
    type_ = arguments["TYPE"]
    value = arguments["VALUE"]

    config = cm_config()
    username = config.username()
    # print username
    user = cm_user()

    GroupManage = GroupManagement(username)

    '''
    if arguments["info"]:

        print("Default experiment group:", user.get_defaults(username)["group"])

    '''
    if arguments["list"]:
        try:
            res = GroupManage.get_groups_names_list()
        except Exception, err:
            Console.error(str(err))
            return
        d = {}
        d["groups"] = res

        if arguments['--format']:
            p_format = arguments['--format']
        else:
            p_format = None

        shell_commands_dict_output(username,
                                   d,
                                   print_format=p_format,
                                   table_format="key_list",
                                   indexed=True)
예제 #8
0
    def do_cluster(self, args, arguments):
        """
        ::
        
          Usage:
              cluster list [--format=FORMAT]
              cluster create <name>
                             [--count=<count>]
                             [--ln=<LoginName>]
                             [--cloud=<CloudName>]
                             [--image=<imgName>|--imageid=<imgId>]
                             [--flavor=<flavorName>|--flavorid=<flavorId>]
                             [--force]
              cluster show <name> 
                           [--format=FORMAT] 
                           [--column=COLUMN]
                           [--detail]
              cluster remove <name> 
                             [--grouponly]

          Description:
              Cluster Management
              
              cluster list
                  list the clusters

              cluster create <name> --count=<count> --ln=<LoginName> [options...]
                  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, it is better to choose a unused group name
              
              cluster show <name>
                  show the detailed information about the cluster VMs

              cluster remove <name> [--grouponly]
                  remove the cluster and its VMs, if you want to remove the cluster(group name)
                  without removing the VMs, use --grouponly flag
          
          Arguments:
              <name>        cluster name or group name

          Options:
              --count=<count>            give the number of VMs to add into the cluster
              --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)
              --grouponly                remove the group only without removing the VMs, otherwise 
                                         cluster remove command will remove all the VMs of this cluster
              FORMAT                     output format: table, json, csv
              COLUMN                     customize what information to display, for example:
                                         --column=status,addresses prints the columns status
                                         and addresses
              --detail                   for table print format, a brief version 
                                         is used as default, use this flag to print
                                         detailed table
                                         
        """
        #pprint(arguments)
        # defer the import of Clusters, as this further imports classes from cloudmesh.experiment.group,
        # which connects to mongo automatically due to the using of mongoengine.
        from cloudmesh.shell.clusters import Clusters
        # cannot move up at the beginning.

        clusterobj = Clusters()
        userobj = cm_user()
        config = cm_config()
        username = config.username()

        if arguments['list']:
            try:
                clusters_list = clusterobj.list_clusters()
            except Exception, err:
                Console.error(str(err))
                return
            if arguments['--format']:
                if arguments['--format'] not in ALLOWED_PRINT_FORMAT:
                    Console.error(
                        "wrong print format: {0}. (allowed print format: {1})".
                        format(format_type, ", ".join(ALLOWED_PRINT_FORMAT)))
                    return
                else:
                    p_format = arguments['--format']
            else:
                p_format = None
            shell_commands_dict_output(
                username,
                clusters_list,
                print_format=p_format,
                firstheader="cluster",
                header=[["num of nodes", "num_of_nodes"],
                        ["num of active nodes", "num_of_active_nodes"]])
예제 #9
0
    def _helper_vm_cli_printer(self, vms_dict,
                               print_format=None,
                               columns=None,
                               refresh=True,
                               detailed=True):
        """
        accept a dict of VMs, change some informtion and get it ready for
        printing out such as tables in command line
        
        :param vms_dict: a dict contains VM information, please refer to the 
                         VM data in mongodb for the data format
        :type vms_dict: dict
        :param print_format: specify the print format, such as table or json
        :type print_format: string
        :param columns: specify the columns to show instead of showing the 
                        entire data
        :type columns: list
        :param refresh: True to refresh the database before processing, the 
                        database has to be refreshed before start processing 
                        here. Since there are many places that refreshes the 
                        database, so if there is a place in the program 
                        refreshes shortly before calling this funtion, to avoid 
                        redundant work, make this argument False
        :type refresh: boolean
        :param detailed: sometimes there is too much information to show and
                         this makes the table view looks messy, so user can
                         make this argument False to show only crucial 
                         information
        :type detailed: boolean
        :return: print the VM information in various format
        """
        clouds_list = []
        if columns:
            temp_columns = []
        for key, value in vms_dict.iteritems():
            if 'cm_cloud' in value and value['cm_cloud'] not in clouds_list:
                clouds_list.append(value['cm_cloud'])
            if '_id' in value:
                del value['_id']
        if print_format == "json" and columns is None:
            print(json.dumps(vms_dict, indent=4))
        else:
            res = {}
            headers = []
            # refresh the image and flavor information to get their names
            if refresh:
                self.mongodb.activate(cm_user_id=self.username,
                                      names=clouds_list)
                self.mongodb.refresh(cm_user_id=self.username,
                                     names=clouds_list,
                                     types=['images', 'flavors'])
            images_dict = self.mongodb.images(clouds=clouds_list,
                                              cm_user_id=self.username)
            flavors_dict = self.mongodb.flavors(clouds=clouds_list,
                                                cm_user_id=self.username)

            for key, value in vms_dict.iteritems():
                res[key] = {}
                cm_type = value['cm_type']
                itemkeys = self._helper_itemkeys(cm_type, detailed=detailed)
                for item in itemkeys:
                    if item[0] not in headers:
                        headers.append(item[0])
                    try:
                        temp = _getFromDict(value, item[1:])
                        # ----------------------------------------
                        # special handlers
                        # ----------------------------------------
                        if item[0] == 'flavor':
                            if temp in flavors_dict[value["cm_cloud"]]:
                                temp = flavors_dict[value["cm_cloud"]][temp]['name']
                            else:
                                temp = "unavailable"

                        elif item[0] == 'image':
                            if temp in images_dict[value["cm_cloud"]]:
                                temp = images_dict[value["cm_cloud"]][temp]['name']
                            else:
                                temp = "unavailable"

                        elif item[0] == 'addresses':
                            temp = address_string(temp)
                        # ----------------------------------------
                        temp_res = temp
                    except:
                        temp_res = None
                    if columns:
                        if item[0] in columns:
                            res[key][item[0]] = temp_res
                            if item[0] not in temp_columns:
                                temp_columns.append(item[0])
                        else:
                            pass
                    else:
                        res[key][item[0]] = temp_res
            if columns:
                columns_to_print = []
                for item in columns:
                    if item in temp_columns:
                        columns_to_print.append(item)
                headers = columns_to_print

            shell_commands_dict_output(self.username,
                                       res,
                                       print_format=print_format,
                                       firstheader="name",
                                       header=headers)
예제 #10
0
    def _show_ip(self):
        '''
        list the ips of VMs
        '''
        mongo = cm_mongo()
        cloudname = self.get_working_cloud_name()
        if not cloudname:
            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 not server_id_list:
            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']
            temp_val = temp.keys()[0]
            temp = temp[temp_val]
            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)
예제 #11
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))
예제 #12
0
    def do_stack(self, args, arguments):
        """
        ::

          Usage:
              stack start NAME [--template=TEMPLATE] [--param=PARAM]
              stack stop NAME
              stack show NAME
              stack list [--refresh] [--column=COLUMN] [--format=FORMAT]
              stack help | -h

          An orchestration tool (OpenStack Heat)

          Arguments:

            NAME           stack name
            help           Prints this message

          Options:

             -v       verbose mode

        """
        self.cm_mongo = cm_mongo()
        self.cm_config = cm_config()
        self.cm_user = cm_user()

        log.info(arguments)

        if arguments["help"] or arguments["-h"]:
            print(self.do_stack.__doc__)
        elif arguments['show'] and arguments['NAME']:
            print("NOT IMPLEMENTED")
            return
        elif arguments['start'] and arguments['NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            t_url = arguments['--template']
            param = arguments['--param']
            s_name = arguments['NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_create(cloud=def_cloud,
                                             cm_user_id=userid,
                                             servername=s_name,
                                             template_url=t_url,
                                             parameters=param)
            print(res)
            return res

        elif arguments['stop'] and arguments['NAME']:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            s_name = arguments['NAME']
            self.cm_mongo.activate(userid)
            res = self.cm_mongo.stack_delete(cloud=def_cloud,
                                             cm_user_id=userid,
                                             server=s_name)

            return res

        elif arguments['list']:
            userid = self.cm_config.username()
            if arguments['--refresh']:
                self.cm_mongo.activate(userid)
                self.cm_mongo.refresh(cm_user_id=userid, types=[self._id])
            d = self.cm_mongo.stacks(cm_user_id=userid)

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

            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

            for k, v in d.iteritems():
                for k0, v0 in v.iteritems():
                    if '_id' in v0:
                        del v0['_id']

                shell_commands_dict_output(userid,
                                           v,
                                           print_format=p_format,
                                           firstheader="stack_id",
                                           header=columns)