Ejemplo n.º 1
0
 def get_working_cloud_name(self):
     '''
     get the name of a cloud to work on, if CLOUD not given, the function
     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
Ejemplo n.º 2
0
    def __init__(self, arguments):
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error("There is a problem with the configuration yaml files")

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

        self.arguments = arguments
        
        # pprint(self.arguments)
        
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error("There is a problem with the configuration yaml files")
    
        self.username = self.config['cloudmesh']['profile']['username']
        self.refresh_default_setting = get_command_list_refresh_default_setting(self.username)
Ejemplo n.º 3
0
    def __init__(self, arguments):
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error(
                "There is a problem with the configuration yaml files")

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

        self.arguments = arguments

        # pprint(self.arguments)

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

        self.username = self.config['cloudmesh']['profile']['username']
        self.refresh_default_setting = get_command_list_refresh_default_setting(
            self.username)
Ejemplo n.º 4
0
class ListInfo(object):

    def __init__(self, arguments):
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error("There is a problem with the configuration yaml files")

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

        self.arguments = arguments
        
        # pprint(self.arguments)
        
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error("There is a problem with the configuration yaml files")
    
        self.username = self.config['cloudmesh']['profile']['username']
        self.refresh_default_setting = get_command_list_refresh_default_setting(self.username)

    def _list_flavor(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            itemkeys = [
                ['id', 'id'],
                ['name', 'name'],
                ['vcpus', 'vcpus'],
                ['ram', 'ram'],
                ['disk', 'disk'],
                ['refresh time', 'cm_refresh']
            ]
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(
                    cm_user_id=self.username, names=clouds)
                self.cloudmanage.mongo.refresh(
                    cm_user_id=self.username, names=clouds, types=['flavors'])

            # --format
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [x.strip() for x in
                                self.arguments['--column'].split(',')]
                    new_itemkeys = []
                    for item in itemkeys:
                        if item[0] in s_column:
                            new_itemkeys.append(item)
                    itemkeys = new_itemkeys

            for cloud in clouds:
                self.cloudmanage.print_cloud_flavors(username=self.username,
                                                     cloudname=cloud.encode(
                                                         "ascii"),
                                                     itemkeys=itemkeys,
                                                     refresh=False,
                                                     output=False,
                                                     print_format=p_format)
        else:
            return

    def _list_image(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            itemkeys = {"openstack":
                        [
                            # [ "Metadata", "metadata"],
                            ["name", "name"],
                            ["status", "status"],
                            ["id", "id"],
                            ["type_id", "metadata", "instance_type_id"],
                            ["iname", "metadata", "instance_type_name"],
                            ["location", "metadata", "image_location"],
                            ["state", "metadata", "image_state"],
                            ["updated", "updated"],
                            # [ "minDisk" , "minDisk"],
                            ["memory_mb", "metadata",
                                'instance_type_memory_mb'],
                            ["fid", "metadata", "instance_type_flavorid"],
                            ["vcpus", "metadata", "instance_type_vcpus"],
                            # [ "user_id" , "metadata", "user_id"],
                            # [ "owner_id" , "metadata", "owner_id"],
                            # [ "gb" , "metadata", "instance_type_root_gb"],
                            # [ "arch", ""]
                        ],
                        "ec2":
                        [
                            # [ "Metadata", "metadata"],
                            ["state", "extra", "state"],
                            ["name", "name"],
                            ["id", "id"],
                            ["public", "extra", "is_public"],
                            ["ownerid", "extra", "owner_id"],
                            ["imagetype", "extra", "image_type"]
                        ],
                        "azure":
                        [
                            ["name", "label"],
                            ["category", "category"],
                            ["id", "id"],
                            ["size", "logical_size_in_gb"],
                            ["os", "os"]
                        ],
                        "aws":
                        [
                            ["state", "extra", "state"],
                            ["name", "name"],
                            ["id", "id"],
                            ["public", "extra", "ispublic"],
                            ["ownerid", "extra", "ownerid"],
                            ["imagetype", "extra", "imagetype"]
                        ]
                        }
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(
                    cm_user_id=self.username, names=clouds)
                self.cloudmanage.mongo.refresh(
                    cm_user_id=self.username, names=clouds, types=['images'])
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [x.strip() for x in
                                self.arguments['--column'].split(',')]
                    new_itemkeys = {x: [] for x in itemkeys.keys()}
                    for cloud, items in itemkeys.iteritems():
                        for item in items:
                            if item[0] in s_column:
                                new_itemkeys[cloud].append(item)
                    itemkeys = new_itemkeys

            for cloud in clouds:
                self.cloudmanage.print_cloud_images(username=self.username,
                                                    cloudname=cloud.encode(
                                                        "ascii"),
                                                    itemkeys=itemkeys,
                                                    refresh=False, output=False,
                                                    print_format=p_format)

        else:
            return

    def _list_server(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            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'],
                        ]
                        }
                        
            itemkeys_short = {"openstack":
                        [
                            ['name', 'name'],
                            ['status', 'status'],
                            ['addresses', 'addresses'],
                            ['flavor', 'flavor', 'id'],
                            ['image', 'image', 'id']
                        ],
                        "ec2":
                        [
                            ["name", "id"],
                            ["status", "extra", "status"],
                            ["addresses", "public_ips"],
                            ["flavor", "extra", "instance_type"],
                            ['image', 'extra', 'imageId']
                        ],
                        "aws":
                        [
                            ["name", "name"],
                            ["status", "extra", "status"],
                            ["addresses", "public_ips"],
                            ["flavor", "extra", "instance_type"],
                            ['image', 'extra', 'image_id']
                        ],
                        "azure":
                        [
                            ['name', 'name'],
                            ['status', 'status'],
                            ['addresses', 'vip'],
                            ['flavor', 'flavor', 'id'],
                            ['image', 'image', 'id']
                        ]
                        }
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(
                    cm_user_id=self.username, names=clouds)
                self.cloudmanage.mongo.refresh(
                    cm_user_id=self.username, names=clouds, types=['servers'])
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [x.strip() for x in
                                self.arguments['--column'].split(',')]
                    new_itemkeys = {x: [] for x in itemkeys.keys()}
                    for cloud, items in itemkeys.iteritems():
                        for item in items:
                            if item[0] in s_column:
                                new_itemkeys[cloud].append(item)
                    itemkeys = new_itemkeys
            else:
                if not self.arguments['--detail']:
                    itemkeys = itemkeys_short

            for cloud in clouds:
                self.cloudmanage.print_cloud_servers(username=self.username,
                                                     cloudname=cloud.encode(
                                                         "ascii"),
                                                     itemkeys=itemkeys,
                                                     refresh=False,
                                                     output=False,
                                                     print_format=p_format,
                                                     group=self.arguments['--group'])

        else:
            return

    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")

        except Exception, e:
            Console.error("could not connect to the database")
            print(e)
Ejemplo n.º 5
0
            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

        elif arguments['create']:
            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 = ""
            #_key = "-i ./{0}/{1}".format(dir_name, temp_key_name)
            StrictHostKeyChecking = "-o StrictHostKeyChecking=no"
Ejemplo n.º 6
0
    def do_nova(self, args, arguments):
        """
        ::
        
          Usage:
                 nova set CLOUD
                 nova info [CLOUD]          
                 nova help
                 nova ARGUMENTS               

          A simple wrapper for the openstack nova command

          Arguments:

            ARGUMENTS      The arguments passed to nova
            help           Prints the nova manual
            set            reads the information from the current cloud
                           and updates the environment variables if
                           the cloud is an openstack cloud
            info           the environment values for OS

          Options:

             -v       verbose mode

        """
        # log.info(arguments)
        cloud = arguments['CLOUD']
        if not self.config:
            self.config = cm_config()
            self.cm_user_id = self.config.username()

        if not self.mongodb:
            self.mongodb = CloudManage()

        if arguments["help"]:
            os.system("nova help")
            return
        elif arguments["info"]:
            #
            # prints the current os env variables for nova
            #
            d = {}
            # Get default cloud from mongodb
            self.cloud = self.mongodb.get_default_cloud_for_nova(self.cm_user_id)
            self.rcfiles = cm_rc.get_rcfiles()
            self.set_os_environ(self.cloud)

            for attribute in ['OS_USER_ID',
                              'OS_USERNAME',
                              'OS_TENANT_NAME',
                              'OS_AUTH_URL',
                              'OS_CACERT',
                              'OS_PASSWORD',
                              'OS_REGION']:
                try:
                    d[attribute] = os.environ[attribute]
                    # d[attribute] = self.rcfiles[cloud or self.cloud][attribute]
                except:
                    log.warning(sys.exc_info())
                    d[attribute] = None
            print(row_table(d, order=None, labels=["Variable", "Value"]))
            return
        elif arguments["set"]:
            if cloud:   
                self.cloud = cloud

                self.rcfiles = cm_rc.get_rcfiles()
                self.set_os_environ(self.cloud)
                
                # Store to mongodb
                self.mongodb.update_default_cloud_for_nova(self.cm_user_id, self.cloud)

                msg = "{0} is set".format(self.cloud)
                log.info(msg)
                print(msg)
            else:
                print("CLOUD is required")
            #
            # TODO: implemet
            #
            # cloud = get current default
            # if cloud type is openstack:
            #    credentials = get credentials 
            #    set the credentials in the current os system env variables
            #    
        else:
            os.system("nova {0}".format(arguments["ARGUMENTS"]))
            return
Ejemplo n.º 7
0
class cm_shell_nova:
    """opt_example class"""

    def activate_cm_shell_nova(self):
        self.config = None
        self.cm_user_id = None
        self.mongodb = None

        self.register_command_topic('cloud','nova')
        pass

    def set_os_environ(self, cloudname):
        '''Set os environment variables on a given cloudname'''
        try:
            novars = self.rcfiles[cloudname]
            for k, v in novars.iteritems():
                os.environ[k] = v
            # TEMP CODE FOR CACERT
            if cloudname == "india" or cloudname == "india_openstack_havana":
                os.environ['OS_CACERT'] = \
                "{0}/.cloudmesh/india-havana-cacert.pem".format(os.environ['HOME'])
            elif cloudname == "icehouse":
                os.environ['OS_CACERT'] = \
                "{0}/.cloudmesh/clouds/icehouse/cacert.pem".format(os.environ['HOME'])

        except:
            log.warning(sys.exc_info())

    @command
    def do_nova(self, args, arguments):
        """
        ::
        
          Usage:
                 nova set CLOUD
                 nova info [CLOUD]          
                 nova help
                 nova ARGUMENTS               

          A simple wrapper for the openstack nova command

          Arguments:

            ARGUMENTS      The arguments passed to nova
            help           Prints the nova manual
            set            reads the information from the current cloud
                           and updates the environment variables if
                           the cloud is an openstack cloud
            info           the environment values for OS

          Options:

             -v       verbose mode

        """
        # log.info(arguments)
        cloud = arguments['CLOUD']
        if not self.config:
            self.config = cm_config()
            self.cm_user_id = self.config.username()

        if not self.mongodb:
            self.mongodb = CloudManage()

        if arguments["help"]:
            os.system("nova help")
            return
        elif arguments["info"]:
            #
            # prints the current os env variables for nova
            #
            d = {}
            # Get default cloud from mongodb
            self.cloud = self.mongodb.get_default_cloud_for_nova(self.cm_user_id)
            self.rcfiles = cm_rc.get_rcfiles()
            self.set_os_environ(self.cloud)

            for attribute in ['OS_USER_ID',
                              'OS_USERNAME',
                              'OS_TENANT_NAME',
                              'OS_AUTH_URL',
                              'OS_CACERT',
                              'OS_PASSWORD',
                              'OS_REGION']:
                try:
                    d[attribute] = os.environ[attribute]
                    # d[attribute] = self.rcfiles[cloud or self.cloud][attribute]
                except:
                    log.warning(sys.exc_info())
                    d[attribute] = None
            print(row_table(d, order=None, labels=["Variable", "Value"]))
            return
        elif arguments["set"]:
            if cloud:   
                self.cloud = cloud

                self.rcfiles = cm_rc.get_rcfiles()
                self.set_os_environ(self.cloud)
                
                # Store to mongodb
                self.mongodb.update_default_cloud_for_nova(self.cm_user_id, self.cloud)

                msg = "{0} is set".format(self.cloud)
                log.info(msg)
                print(msg)
            else:
                print("CLOUD is required")
            #
            # TODO: implemet
            #
            # cloud = get current default
            # if cloud type is openstack:
            #    credentials = get credentials 
            #    set the credentials in the current os system env variables
            #    
        else:
            os.system("nova {0}".format(arguments["ARGUMENTS"]))
            return
Ejemplo n.º 8
0
class ListInfo(object):
    def __init__(self, arguments):
        self.cloudmanage = CloudManage()
        try:
            self.config = cm_config()
        except:
            Console.error(
                "There is a problem with the configuration yaml files")

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

        self.arguments = arguments

        # pprint(self.arguments)

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

        self.username = self.config['cloudmesh']['profile']['username']
        self.refresh_default_setting = get_command_list_refresh_default_setting(
            self.username)

    def _list_flavor(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            itemkeys = [['id', 'id'], ['name', 'name'], ['vcpus', 'vcpus'],
                        ['ram', 'ram'], ['disk', 'disk'],
                        ['refresh time', 'cm_refresh']]
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(cm_user_id=self.username,
                                                names=clouds)
                self.cloudmanage.mongo.refresh(cm_user_id=self.username,
                                               names=clouds,
                                               types=['flavors'])

            # --format
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [
                        x.strip()
                        for x in self.arguments['--column'].split(',')
                    ]
                    new_itemkeys = []
                    for item in itemkeys:
                        if item[0] in s_column:
                            new_itemkeys.append(item)
                    itemkeys = new_itemkeys

            for cloud in clouds:
                self.cloudmanage.print_cloud_flavors(
                    username=self.username,
                    cloudname=cloud.encode("ascii"),
                    itemkeys=itemkeys,
                    refresh=False,
                    output=False,
                    print_format=p_format)
        else:
            return

    def _list_image(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            itemkeys = {
                "openstack": [
                    # [ "Metadata", "metadata"],
                    ["name", "name"],
                    ["status", "status"],
                    ["id", "id"],
                    ["type_id", "metadata", "instance_type_id"],
                    ["iname", "metadata", "instance_type_name"],
                    ["location", "metadata", "image_location"],
                    ["state", "metadata", "image_state"],
                    ["updated", "updated"],
                    # [ "minDisk" , "minDisk"],
                    ["memory_mb", "metadata", 'instance_type_memory_mb'],
                    ["fid", "metadata", "instance_type_flavorid"],
                    ["vcpus", "metadata", "instance_type_vcpus"],
                    # [ "user_id" , "metadata", "user_id"],
                    # [ "owner_id" , "metadata", "owner_id"],
                    # [ "gb" , "metadata", "instance_type_root_gb"],
                    # [ "arch", ""]
                ],
                "ec2": [
                    # [ "Metadata", "metadata"],
                    ["state", "extra", "state"],
                    ["name", "name"],
                    ["id", "id"],
                    ["public", "extra", "is_public"],
                    ["ownerid", "extra", "owner_id"],
                    ["imagetype", "extra", "image_type"]
                ],
                "azure": [["name", "label"], ["category", "category"],
                          ["id", "id"], ["size", "logical_size_in_gb"],
                          ["os", "os"]],
                "aws": [["state", "extra", "state"], ["name", "name"],
                        ["id", "id"], ["public", "extra", "ispublic"],
                        ["ownerid", "extra", "ownerid"],
                        ["imagetype", "extra", "imagetype"]]
            }
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(cm_user_id=self.username,
                                                names=clouds)
                self.cloudmanage.mongo.refresh(cm_user_id=self.username,
                                               names=clouds,
                                               types=['images'])
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [
                        x.strip()
                        for x in self.arguments['--column'].split(',')
                    ]
                    new_itemkeys = {x: [] for x in itemkeys.keys()}
                    for cloud, items in itemkeys.iteritems():
                        for item in items:
                            if item[0] in s_column:
                                new_itemkeys[cloud].append(item)
                    itemkeys = new_itemkeys

            for cloud in clouds:
                self.cloudmanage.print_cloud_images(
                    username=self.username,
                    cloudname=cloud.encode("ascii"),
                    itemkeys=itemkeys,
                    refresh=False,
                    output=False,
                    print_format=p_format)

        else:
            return

    def _list_server(self):
        self.cloudmanage._connect_to_mongo()
        clouds = self.get_working_cloud_name()
        if clouds:
            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'],
                ]
            }

            itemkeys_short = {
                "openstack": [['name', 'name'], ['status', 'status'],
                              ['addresses', 'addresses'],
                              ['flavor', 'flavor', 'id'],
                              ['image', 'image', 'id']],
                "ec2": [["name", "id"], ["status", "extra", "status"],
                        ["addresses", "public_ips"],
                        ["flavor", "extra", "instance_type"],
                        ['image', 'extra', 'imageId']],
                "aws": [["name", "name"], ["status", "extra", "status"],
                        ["addresses", "public_ips"],
                        ["flavor", "extra", "instance_type"],
                        ['image', 'extra', 'image_id']],
                "azure": [['name', 'name'], ['status', 'status'],
                          ['addresses', 'vip'], ['flavor', 'flavor', 'id'],
                          ['image', 'image', 'id']]
            }
            if self.refresh_default_setting or self.arguments['--refresh']:
                self.cloudmanage.mongo.activate(cm_user_id=self.username,
                                                names=clouds)
                self.cloudmanage.mongo.refresh(cm_user_id=self.username,
                                               names=clouds,
                                               types=['servers'])
            p_format = self.arguments['--format']

            # --column
            # available columns are: id, name, vcpus, ram, disk, refresh time,
            # and all

            if self.arguments['--column']:
                if self.arguments['--column'] != "all":
                    s_column = [
                        x.strip()
                        for x in self.arguments['--column'].split(',')
                    ]
                    new_itemkeys = {x: [] for x in itemkeys.keys()}
                    for cloud, items in itemkeys.iteritems():
                        for item in items:
                            if item[0] in s_column:
                                new_itemkeys[cloud].append(item)
                    itemkeys = new_itemkeys
            else:
                if not self.arguments['--detail']:
                    itemkeys = itemkeys_short

            for cloud in clouds:
                self.cloudmanage.print_cloud_servers(
                    username=self.username,
                    cloudname=cloud.encode("ascii"),
                    itemkeys=itemkeys,
                    refresh=False,
                    output=False,
                    print_format=p_format,
                    group=self.arguments['--group'])

        else:
            return

    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")

        except Exception, e:
            Console.error("could not connect to the database")
            print(e)
Ejemplo n.º 9
0
def delete_vm(username,
              cloudname,
              server_id_list=None,
              preview=False,
              refresh=False):
    '''
    delete vms of a cloud for a user 
    
    this function accepts a list of VM ids and delete them
    
    TODO: what if fail to delete, how to acknowledge it;
          it looks like even though delete a vm and return {msg: seccess}, 
          sometimes refresh after 5 sec, it might be still there
    
    :param username: user name
    :type username: string
    :param cloudname: cloud name
    :type cloudname: string               
    :param server_id_list: the list of VM ids to delete
    :type server_id_list: list
    :param preview: True if the user wants to preview and confirm before start 
                    to delete
    :type preview: boolean
    :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
    :return: False if error occurs
    :rtype: boolean
    '''
    # 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'])
Ejemplo n.º 10
0
def start_vm(username,
             cloudname,
             count=1,
             flavorname=None,
             flavorid=None,
             imagename=None,
             imageid=None,
             groupname=None,
             servername=None):
    '''
    create a VM on a cloud for a user
    
    If flavor or image is provided, the function will check the flavor, image 
    existence;
    it's better to check cloud active status before use this function
    

    TODO: this function should be relocated into a class;
          what if fail to create, how to acknowledge it. no return now because
          of using celery;
          missing security group
        
    :param username: user name
    :type username: string
    :param cloudname: cloud name
    :type cloudname: string
    :param count: give the number of vms to start at one call of this function,
                  the default is 1
    :type count: int
    :param flavorname: flavor name, if flavor name or id is not provided, the
                       function will try to find the user specified default 
                       flavor
    :type flavorname: string
    :param flavorid: flavor id, if flavor name or id is not provided, the
                     function will try to find the user specified default 
                     flavor
    :type flavorid: string
    :param imagename: image name, if image name or id is not provided, the
                      function will try to find the user specified default 
                      image
    :type imagename: string
    :param imageid: image id, if image name or id is not provided, the
                     function will try to find the user specified default 
                     image
    :type imageid: string
    :param groupname: user can specify groupname of the VM, this groupname will
                      be included in the metadata of the VM
    :type groupname: string
    :param servername: user can provide the name of the VM, which will replace 
                       the default prefix+index form
    :type servername: string
    :return:  False if error occurs
    :rtype: boolean
    
    '''
    # Changed scope of this import - hyungro lee 12/01/2014
    from cloudmesh.experiment.group_usage import add_vm_to_group_while_creating

    mongo = cm_mongo()
    userobj = cm_user()
    cloudobj = CloudManage()
    mongo.activate(cm_user_id=username, names=[cloudname])
    userinfo = userobj.info(username)
    key = None
    vm_image_id = None
    vm_flavor_id = None

    error = ''

    # -------------------------
    # refresh server flavor or image
    to_refresh = ["servers"]

    if flavorname is not None or flavorid is not None:
        to_refresh.append("flavors")
    if imagename is not None or imageid is not None:
        to_refresh.append("images")
    if to_refresh != []:
        mongo.refresh(username, names=[cloudname], types=to_refresh)

    # -------------------------
    # get exist VM names list, to prevent names duplicate
    serverdata = mongo.servers(
        clouds=[cloudname],
        cm_user_id=username)[cloudname]
    servers_names_list = []
    for k, v in serverdata.iteritems():
        servers_names_list.append(v['name'])

    # -------------------------
    # flavor handler
    if flavorname is not None or flavorid is not None:
        flavordata = mongo.flavors(
            clouds=[cloudname], cm_user_id=username)[cloudname]
        same_name_count = 0
        for k, v in flavordata.iteritems():
            if flavorname is not None:
                if flavorname == v['name']:
                    vm_flavor_id = k
                    same_name_count = same_name_count + 1
            else:
                if flavorid == k:
                    vm_flavor_id = k
                    break
        if vm_flavor_id is None:
            error = error + "The flavor you provide doesn't exist. "
        if same_name_count > 1:
            error = error + "There are more than one flavor with the name you provide" \
                            "please use flavorid instead or select one by command cloud" \
                            "set flavor [CLOUD]. "
    else:
        try:
            vm_flavor_id = userinfo["defaults"]["flavors"][cloudname]
        except:
            pass
        if vm_flavor_id in [None, 'none']:
            error = error + \
                    "Please specify a default flavor(command: cloud set flavor [CLOUD]). "
    # -------------------------
    # image handler
    if imagename is not None or imageid is not None:
        imagedata = mongo.images(
            clouds=[cloudname], cm_user_id=username)[cloudname]
        same_name_count = 0
        for k, v in imagedata.iteritems():
            if imagename is not None:
                if imagename == v['name']:
                    vm_image_id = k
                    same_name_count = same_name_count + 1
            else:
                if imageid == k:
                    vm_image_id = k
                    break
        if vm_image_id is None:
            error = error + "The image you provide doesn't exist. "
        if same_name_count > 1:
            error = error + "There are more than one image with the name you provide" \
                            "please use imageid instead or select one by command cloud" \
                            "set image [CLOUD]. "
    else:
        try:
            vm_image_id = userinfo["defaults"]["images"][cloudname]
        except:
            pass
        if vm_image_id in [None, 'none']:
            error = error + \
                    "Please specify a default image(command: cloud set flavor [CLOUD]). "

    # -------------------------
    # key handler
    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(username).check_register_key(username, cloudname, key, keycontent)
        keynamenew = _keyname_sanitation(username, key)
    else:
        error = error + \
                "No sshkey found. Please Upload one"
    # -------------------------

    if error != '':
        Console.error(error)
        return False
    # -------------------------
    metadata = {'cm_owner': username}
    if groupname:
        metadata['cm_group'] = groupname

    tmpnamefl = cloudobj.get_flavors(
        cloudname=cloudname, getone=True, id=vm_flavor_id)['name']
    tmpnameim = cloudobj.get_images(
        cloudname=cloudname, getone=True, id=vm_image_id)['name']

    while count > 0:
        userinfo = userobj.info(username)
        if servername:
            prefix = ''
            index = ''
            givenvmname = servername
            tmpnameser = servername
        else:
            prefix = userinfo["defaults"]["prefix"]
            index = userinfo["defaults"]["index"]
            givenvmname = None
            tmpnameser = prefix + '_' + str(index)

        # ------------------------
        # do not allow server name duplicate
        if tmpnameser in servers_names_list:
            Console.error("vm name '{0}' exists, please "
                          "use other names or delete it first".format(tmpnameser))
            if not servername:
                userobj.set_default_attribute(username, "index", int(index) + 1)
            count = count - 1
            continue
        # ------------------------
        # vm start procedure

        banner("Starting vm->{0} on cloud->{1} using image->{2}, flavor->{3}, key->{4}"
               .format(tmpnameser, cloudname, tmpnameim, tmpnamefl, keynamenew))

        useQueue = False
        if useQueue:
            result = mongo.vm_create_queue(cloudname,
                                           prefix,
                                           index,
                                           vm_flavor_id,
                                           vm_image_id,
                                           keynamenew,
                                           meta=metadata,
                                           cm_user_id=username,
                                           givenvmname=givenvmname)
            print("job status:", result.state)
        else:
            result = mongo.vm_create(cloudname,
                                     prefix,
                                     index,
                                     vm_flavor_id,
                                     vm_image_id,
                                     keynamenew,
                                     meta=metadata,
                                     cm_user_id=username,
                                     givenvmname=givenvmname)
            if "server" in result and "adminPass" in result["server"]:
                result["server"]["adminPass"] = "******"
            pprint(result)

        # ------------------------
        # add it to the group in database if groupname provided
        if groupname:
            try:
                add_vm_to_group_while_creating(username, groupname, tmpnameser)
            except Exception, err:
                Console.error(str(err))
                return

        # ------------------------
        # increase index if it is used
        if not servername:
            userobj.set_default_attribute(username, "index", int(index) + 1)
        # ------------------------
        servers_names_list.append(tmpnameser)

        count = count - 1
Ejemplo n.º 11
0
    def do_nova(self, args, arguments):
        """
        ::
        
          Usage:
                 nova set CLOUD
                 nova info [CLOUD]          
                 nova help
                 nova ARGUMENTS               

          A simple wrapper for the openstack nova command

          Arguments:

            ARGUMENTS      The arguments passed to nova
            help           Prints the nova manual
            set            reads the information from the current cloud
                           and updates the environment variables if
                           the cloud is an openstack cloud
            info           the environment values for OS

          Options:

             -v       verbose mode

        """
        # log.info(arguments)
        cloud = arguments['CLOUD']
        if not self.config:
            self.config = cm_config()
            self.cm_user_id = self.config.username()

        if not self.mongodb:
            self.mongodb = CloudManage()

        if arguments["help"]:
            os.system("nova help")
            return
        elif arguments["info"]:
            #
            # prints the current os env variables for nova
            #
            d = {}
            # Get default cloud from mongodb
            self.cloud = self.mongodb.get_default_cloud_for_nova(
                self.cm_user_id)
            self.rcfiles = cm_rc.get_rcfiles()
            self.set_os_environ(self.cloud)

            for attribute in [
                    'OS_USER_ID', 'OS_USERNAME', 'OS_TENANT_NAME',
                    'OS_AUTH_URL', 'OS_CACERT', 'OS_PASSWORD', 'OS_REGION'
            ]:
                try:
                    d[attribute] = os.environ[attribute]
                    # d[attribute] = self.rcfiles[cloud or self.cloud][attribute]
                except:
                    log.warning(sys.exc_info())
                    d[attribute] = None
            print(row_table(d, order=None, labels=["Variable", "Value"]))
            return
        elif arguments["set"]:
            if cloud:
                self.cloud = cloud

                self.rcfiles = cm_rc.get_rcfiles()
                self.set_os_environ(self.cloud)

                # Store to mongodb
                self.mongodb.update_default_cloud_for_nova(
                    self.cm_user_id, self.cloud)

                msg = "{0} is set".format(self.cloud)
                log.info(msg)
                print(msg)
            else:
                print("CLOUD is required")
            #
            # TODO: implemet
            #
            # cloud = get current default
            # if cloud type is openstack:
            #    credentials = get credentials
            #    set the credentials in the current os system env variables
            #
        else:
            os.system("nova {0}".format(arguments["ARGUMENTS"]))
            return
Ejemplo n.º 12
0
class cm_shell_nova:
    """opt_example class"""
    def activate_cm_shell_nova(self):
        self.config = None
        self.cm_user_id = None
        self.mongodb = None

        self.register_command_topic('cloud', 'nova')
        pass

    def set_os_environ(self, cloudname):
        '''Set os environment variables on a given cloudname'''
        try:
            novars = self.rcfiles[cloudname]
            for k, v in novars.iteritems():
                os.environ[k] = v
            # TEMP CODE FOR CACERT
            if cloudname == "india" or cloudname == "india_openstack_havana":
                os.environ['OS_CACERT'] = \
                "{0}/.cloudmesh/india-havana-cacert.pem".format(os.environ['HOME'])
            elif cloudname == "icehouse":
                os.environ['OS_CACERT'] = \
                "{0}/.cloudmesh/clouds/icehouse/cacert.pem".format(os.environ['HOME'])

        except:
            log.warning(sys.exc_info())

    @command
    def do_nova(self, args, arguments):
        """
        ::
        
          Usage:
                 nova set CLOUD
                 nova info [CLOUD]          
                 nova help
                 nova ARGUMENTS               

          A simple wrapper for the openstack nova command

          Arguments:

            ARGUMENTS      The arguments passed to nova
            help           Prints the nova manual
            set            reads the information from the current cloud
                           and updates the environment variables if
                           the cloud is an openstack cloud
            info           the environment values for OS

          Options:

             -v       verbose mode

        """
        # log.info(arguments)
        cloud = arguments['CLOUD']
        if not self.config:
            self.config = cm_config()
            self.cm_user_id = self.config.username()

        if not self.mongodb:
            self.mongodb = CloudManage()

        if arguments["help"]:
            os.system("nova help")
            return
        elif arguments["info"]:
            #
            # prints the current os env variables for nova
            #
            d = {}
            # Get default cloud from mongodb
            self.cloud = self.mongodb.get_default_cloud_for_nova(
                self.cm_user_id)
            self.rcfiles = cm_rc.get_rcfiles()
            self.set_os_environ(self.cloud)

            for attribute in [
                    'OS_USER_ID', 'OS_USERNAME', 'OS_TENANT_NAME',
                    'OS_AUTH_URL', 'OS_CACERT', 'OS_PASSWORD', 'OS_REGION'
            ]:
                try:
                    d[attribute] = os.environ[attribute]
                    # d[attribute] = self.rcfiles[cloud or self.cloud][attribute]
                except:
                    log.warning(sys.exc_info())
                    d[attribute] = None
            print(row_table(d, order=None, labels=["Variable", "Value"]))
            return
        elif arguments["set"]:
            if cloud:
                self.cloud = cloud

                self.rcfiles = cm_rc.get_rcfiles()
                self.set_os_environ(self.cloud)

                # Store to mongodb
                self.mongodb.update_default_cloud_for_nova(
                    self.cm_user_id, self.cloud)

                msg = "{0} is set".format(self.cloud)
                log.info(msg)
                print(msg)
            else:
                print("CLOUD is required")
            #
            # TODO: implemet
            #
            # cloud = get current default
            # if cloud type is openstack:
            #    credentials = get credentials
            #    set the credentials in the current os system env variables
            #
        else:
            os.system("nova {0}".format(arguments["ARGUMENTS"]))
            return