Пример #1
0
def vm_login(cloud=None, server=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    message = ''
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")

    server = clouds.servers(cm_user_id=g.user.id)[cloud][server]

    #
    # BUG MESSAGE IS NOT PROPAGATED
    #
    if cloud == "aws":
        userid = "ubuntu"  # temporary
        public_dns = server['extra']['dns_name']
        message = "ssh -i [your private key file] %s@%s" % (userid, public_dns)
        return render_template('success.html', error=message)
    elif cloud == "azure":
        userid = "root"
        # ["addresses"]["private"][0]["addr"] # temporary
        public_dns = server["vip"]
        message = "ssh -i [your private key file] %s@%s" % (userid, public_dns)
        return render_template('success.html', error=message)

    if len(server['addresses'][server['addresses'].keys()[0]]) < 2:
        message = 'Cannot Login Now, Public IP not assigned'
        log.info("{0}".format(message))

    else:
        message = 'Logged in Successfully'
        ip = server['addresses'][server['addresses'].keys()[0]][1]['addr']
        #
        # BUG: login must be based on os
        # TODO: loginbug
        #

        c = cm_mongo()
        images = c.images([cloud], g.user.id)[cloud]

        image = server['image']['id']

        imagename = images[image]['name']
        print imagename

        if "ubuntu" in imagename:
            loginname = "ubuntu"
        elif "centos" in imagename:
            loginname = "root"
        elif "debian" in imagename:
            loginname = "root"
        else:
            userdata = g.user
            loginname = userdata.id

        link = 'ubuntu@' + ip
        webbrowser.open("ssh://" + link)

    return redirect('/mesh/servers')
Пример #2
0
def vm_login(cloud=None, server=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    message = ''
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")

    server = clouds.servers(cm_user_id=g.user.id)[cloud][server]

    #
    # BUG MESSAGE IS NOT PROPAGATED
    #
    if cloud == "aws":
        userid = "ubuntu"  # temporary
        public_dns = server['extra']['dns_name']
        message = "ssh -i [your private key file] %s@%s" % (userid, public_dns)
        return render_template('success.html', error=message)
    elif cloud == "azure":
        userid = "root"
        # ["addresses"]["private"][0]["addr"] # temporary
        public_dns = server["vip"]
        message = "ssh -i [your private key file] %s@%s" % (userid, public_dns)
        return render_template('success.html', error=message)

    if len(server['addresses'][server['addresses'].keys()[0]]) < 2:
        message = 'Cannot Login Now, Public IP not assigned'
        log.info("{0}".format(message))

    else:
        message = 'Logged in Successfully'
        ip = server['addresses'][server['addresses'].keys()[0]][1]['addr']
        #
        # BUG: login must be based on os
        # TODO: loginbug
        #

        c = cm_mongo()
        images = c.images([cloud], g.user.id)[cloud]

        image = server['image']['id']

        imagename = images[image]['name']
        print imagename

        if "ubuntu" in imagename:
            loginname = "ubuntu"
        elif "centos" in imagename:
            loginname = "root"
        elif "debian" in imagename:
            loginname = "root"
        else:
            userdata = g.user
            loginname = userdata.id

        link = 'ubuntu@' + ip
        webbrowser.open("ssh://" + link)

    return redirect('/mesh/servers')
Пример #3
0
    def import_cloud_to_mongo(d, cloudname, username):
        '''
        insert a cloud to db_clouds
        additionally, add values cm_cloud, cm_kind=cloud, cm_user_id
        before use this function, check whether cloud exists in db_clouds
        cloud name duplicate is not allowed
        '''
        if d['cm_type'] in ['openstack']:
            if d['credentials']['OS_USERNAME']:
                del d['credentials']['OS_USERNAME']
            if d['credentials']['OS_PASSWORD']:
                del d['credentials']['OS_PASSWORD']
            if d['credentials']['OS_TENANT_NAME']:
                del d['credentials']['OS_TENANT_NAME']
        elif d['cm_type'] in ['ec2', 'aws']:
            if d['credentials']['EC2_ACCESS_KEY']:
                del d['credentials']['EC2_ACCESS_KEY']
            if d['credentials']['EC2_SECRET_KEY']:
                del d['credentials']['EC2_SECRET_KEY']
        elif d['cm_type'] in ['azure']:
            if d['credentials']['subscriptionid']:
                del d['credentials']['subscriptionid']

        d['cm_cloud'] = cloudname
        d['cm_kind'] = 'cloud'
        d['cm_user_id'] = username

        # remove default part from yaml
        if d['default']:
            del d['default']

        mongo = cm_mongo()
        mongo.db_clouds.insert(d)
Пример #4
0
    def do_limits(self, args, arguments):
        """
        Usage:
            limits [CLOUD]
            limits help | -h

        Current usage data with limits on a selected project (tenant)

        Arguments:
          
          CLOUD          Cloud name to see the usage
          help           Prints this message

        Options:

           -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print (self.do_limits.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            usage_with_limits = self.cm_mongo.usage_with_limits(def_cloud, userid)

            print(row_table(usage_with_limits, order=None, labels=["Limits",
                                                                   "(Used/Max)"]))

            return usage_with_limits
Пример #5
0
    def do_quota(self, args, arguments):
        """
        Usage:
            quota [CLOUD]
            quota help | -h

        quota limit on a current project (tenant)

        Arguments:
          
          CLOUD          Cloud name to see the usage
          help           Prints this message

        Options:

           -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print(self.do_quota.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            quota = self.cm_mongo.quota(def_cloud, userid)
            print(row_table(quota, order=None, labels=["Variable", "Value"]))
            return quota
Пример #6
0
    def import_cloud_to_mongo(d, cloudname, username):
        '''
        insert a cloud to db_clouds
        additionally, add values cm_cloud, cm_kind=cloud, cm_user_id
        before use this function, check whether cloud exists in db_clouds
        cloud name duplicate is not allowed
        '''
        if d['cm_type'] in ['openstack']:
            if d['credentials']['OS_USERNAME']:
                del d['credentials']['OS_USERNAME']
            if d['credentials']['OS_PASSWORD']:
                del d['credentials']['OS_PASSWORD']
            if d['credentials']['OS_TENANT_NAME']:
                del d['credentials']['OS_TENANT_NAME']
        elif d['cm_type'] in ['ec2', 'aws']:
            if d['credentials']['EC2_ACCESS_KEY']:
                del d['credentials']['EC2_ACCESS_KEY']
            if d['credentials']['EC2_SECRET_KEY']:
                del d['credentials']['EC2_SECRET_KEY']
        elif d['cm_type'] in ['azure']:
            if d['credentials']['subscriptionid']:
                del d['credentials']['subscriptionid']

        d['cm_cloud'] = cloudname
        d['cm_kind'] = 'cloud'
        d['cm_user_id'] = username

        # remove default part from yaml
        if d['default']:
            del d['default']

        mongo = cm_mongo()
        mongo.db_clouds.insert(d)
Пример #7
0
def refresh(cloud=None, server=None, service_type=None):

    clouds = cm_mongo()
    cm_user_id = g.user.id
    clouds.activate(cm_user_id=cm_user_id)

    log.info("-> refresh {0} {1}".format(cloud, service_type))
    userinfo = getCurrentUserinfo()
    # print "REQ", redirect(request.args.get('next') or '/').__dict__
    cloud_names = None
    # cloud field could be empty thus in that position it could be the types
    if cloud is None or cloud in ['servers', 'flavors', 'images', 'users']:
        # cloud_names = config.active()
        cloud_names = userinfo["defaults"]["activeclouds"]
    else:
        cloud_names = [cloud]

    if service_type is None:
        # if both cloud and service_type are none, it's coming from the
        # refresh button from home page. So return to home
        if cloud is None:
            return redirect('/')
        else:
            clouds.refresh(cm_user_id=cm_user_id,
                           names=cloud_names,
                           types=['servers', 'images', 'flavors'])
            return redirect('/mesh/{0}'.format(cloud))
    else:
        clouds.refresh(cm_user_id=cm_user_id,
                       names=cloud_names,
                       types=[service_type])
        return redirect('/mesh/{0}'.format(service_type))
Пример #8
0
    def _the_clouds(self):
        try:
            self.config = cm_config()
            self.user = self.config.username()
            self.mongoClass = cm_mongo()

            print "Activating clouds ..."
            try:
                self.mongoClass.activate(cm_user_id=self.user)
                print "Activation done."
                self.clouds_activated = True
            except:
                print "Activation failed"
                self.clouds_activated = False

            self.vmi = vm_interface(
                self.user, self.config.default_cloud, self.mongoClass)
            defaults = cm_shell_defaults()
            self.defDict = defaults.createDefaultDict()
            if self._check_activation(self.user) is False:
                print "No active clouds found. " \
                      "Please register and activate a cloud."

        except Exception:
            print 'Unexpected error at cmd VM: ', \
                  sys.exc_info()[0], sys.exc_info()[1]
Пример #9
0
def vm_info(cloud=None, server=None):

    print "TYTYTYT"
    clouds = cm_mongo()
    print "TYTYTYT"
    clouds.activate(cm_user_id=g.user.id)
    print "TYTYTYT"

    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # print clouds.servers()[cloud]

    # a trick to deal with diffe1rent type of server_id
    # (string in FG; or int in e.g. hp_cloud)
    """
    try:
        if "%s" % int(server) == server:
            server = int(server)
    except:
        pass
    """

    # clouds.servers()[cloud][server]['cm_vm_id'] = server
    # clouds.servers()[cloud][server]['cm_cloudname'] = cloud

    return render_template(
        "mesh/cloud/vm_info.html",
        updated=time_now,
        keys="",
        server=clouds.servers(cm_user_id=g.user.id)[cloud][server],
        id=server,
        cloudname=cloud,
        table_printer=table_printer,
    )
Пример #10
0
def vm_info(cloud=None, server=None):

    print "TYTYTYT"
    clouds = cm_mongo()
    print "TYTYTYT"
    clouds.activate(cm_user_id=g.user.id)
    print "TYTYTYT"

    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # print clouds.servers()[cloud]

    # a trick to deal with diffe1rent type of server_id
    # (string in FG; or int in e.g. hp_cloud)
    '''
    try:
        if "%s" % int(server) == server:
            server = int(server)
    except:
        pass
    '''

    # clouds.servers()[cloud][server]['cm_vm_id'] = server
    # clouds.servers()[cloud][server]['cm_cloudname'] = cloud

    return render_template(
        'mesh/cloud/vm_info.html',
        updated=time_now,
        keys="",
        server=clouds.servers(cm_user_id=g.user.id)[cloud][server],
        id=server,
        cloudname=cloud,
        table_printer=table_printer)
Пример #11
0
def refresh(types):

    user = cm_config().get("cloudmesh.hpc.username")

    c = cm_mongo()
    c.activate(user)
    c.refresh(user, types=types)
Пример #12
0
def refresh_by_queue(cloud=None, service_type=None):
    """ Similar to refresh but using Celery task queue"""

    log.info("-> refresh by queue {0} {1}".format(cloud, service_type))
    cloud_names = None
    cm_user_id = g.user.id
    clouds = cm_mongo()

    if cloud is None or cloud in ['servers', 'flavors', 'images', 'users']:
        userinfo = getCurrentUserinfo()
        cloud_names = userinfo["defaults"]["activeclouds"]
    else:
        cloud_names = [cloud]

    for cloud_entry in cloud_names:
        cm_type = clouds.get_cloud_info(cm_user_id, cloud_entry)['cm_type']
        # Celery task queue
        package = "cloudmesh.iaas.%s.queue" % cm_type
        name = "tasks"
        imported = getattr(__import__(package, fromlist=[name]), name)
        queue_name = "%s-%s" % (cm_type, service_type)
        imported.refresh.apply_async((cm_user_id, cloud_names, [service_type]),
                                     queue=queue_name)

    if service_type is None and cloud is None:
        return redirect('/')
    else:
        return redirect('/mesh/{0}'.format(service_type))
Пример #13
0
def add_item_to_group(username, groupname, _type, value, refresh=False):
    '''
    this is a more spicific function to add items to groups, e.g. using command group add item
    NAME VM VALUE, check whether vm exists before add it to group
    '''
    GroupManage = GroupManagement(username)
    # if type is VM, check whether the VM exists, in this case, value is the VM name
    if _type.upper() == "VM":
        mongo = cm_mongo()
        if refresh:
            mongo.activate(cm_user_id=username)
            mongo.refresh(cm_user_id=username, types=['servers'])
        servers_dict = mongo.servers(cm_user_id=username)
        _vm_exists = False
        for k, v in servers_dict.iteritems():
            for k0, v0 in v.iteritems():
                if 'name' in v0 and v0['name'] == value:
                    _vm_exists = True
                    break
            if _vm_exists:
                break
        if _vm_exists:
            GroupManage.add_item_to_group(groupname, _type, value)
        else:
            raise Exception("VM '{0}' doesn't exist".format(value))
    else:
        GroupManage.add_item_to_group(groupname, _type, value)
Пример #14
0
 def get_working_cloud_name(self):
     '''
     get the name of a cloud to work on, if CLOUD not given, will pick the
     selected or default cloud
     '''
     cloudname = None
     cloudobj = CloudManage()
     mongo = cm_mongo()
     if self.arguments['--cloud']:
         cloud = cloudobj.get_clouds(
             self.username, getone=True, cloudname=self.arguments['--cloud'])
         if cloud is None:
             Console.error(
                 "could not find cloud '{0}'".format(self.arguments['--cloud']))
             return False
         else:
             cloudname = self.arguments['--cloud']
     else:
         cloudname = cloudobj.get_selected_cloud(self.username)
     if cloudname not in mongo.active_clouds(self.username):
         Console.warning(
             "cloud '{0}' is not active, to activate a cloud: cloud on [CLOUD]".format(cloudname))
         return False
     else:
         return cloudname
Пример #15
0
def refresh_by_queue(cloud=None, service_type=None):
    """ Similar to refresh but using Celery task queue"""

    log.info("-> refresh by queue {0} {1}".format(cloud, service_type))
    cloud_names = None
    cm_user_id = g.user.id
    clouds = cm_mongo()

    if cloud is None or cloud in ['servers', 'flavors', 'images', 'users']:
        userinfo = getCurrentUserinfo()
        cloud_names = userinfo["defaults"]["activeclouds"]
    else:
        cloud_names = [cloud]

    for cloud_entry in cloud_names:
        cm_type = clouds.get_cloud_info(cm_user_id, cloud_entry)['cm_type']
        # Celery task queue
        package = "cloudmesh.iaas.%s.queue" % cm_type
        name = "tasks"
        imported = getattr(__import__(package, fromlist=[name]), name)
        queue_name = "%s-%s" % (cm_type, service_type)
        imported.refresh.apply_async((cm_user_id, cloud_names,
                                      [service_type]), queue=queue_name)

    if service_type is None and cloud is None:
        return redirect('/')
    else:
        return redirect('/mesh/{0}'.format(service_type))
Пример #16
0
def refresh(types):

    user = cm_config().get("cloudmesh.hpc.username")

    c = cm_mongo()
    c.activate(user)
    c.refresh(user, types=types)
Пример #17
0
def refresh(cloud=None, server=None, service_type=None):

    clouds = cm_mongo()
    cm_user_id = g.user.id
    clouds.activate(cm_user_id=cm_user_id)

    log.info("-> refresh {0} {1}".format(cloud, service_type))
    userinfo = getCurrentUserinfo()
    # print "REQ", redirect(request.args.get('next') or '/').__dict__
    cloud_names = None
    # cloud field could be empty thus in that position it could be the types
    if cloud is None or cloud in ["servers", "flavors", "images", "users"]:
        # cloud_names = config.active()
        cloud_names = userinfo["defaults"]["activeclouds"]
    else:
        cloud_names = [cloud]

    if service_type is None:
        # if both cloud and service_type are none, it's coming from the
        # refresh button from home page. So return to home
        if cloud is None:
            return redirect("/")
        else:
            clouds.refresh(cm_user_id=cm_user_id, names=cloud_names, types=["servers", "images", "flavors"])
            return redirect("/mesh/{0}".format(cloud))
    else:
        clouds.refresh(cm_user_id=cm_user_id, names=cloud_names, types=[service_type])
        return redirect("/mesh/{0}".format(service_type))
Пример #18
0
    def do_quota(self, args, arguments):
        """
        Usage:
            quota [CLOUD]
            quota help | -h

        quota limit on a current project (tenant)

        Arguments:
          
          CLOUD          Cloud name to see the usage
          help           Prints this message

        Options:

           -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print (self.do_quota.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            quota = self.cm_mongo.quota(def_cloud, userid)
            print(row_table(quota, order=None, labels=["Variable", "Value"]))
            return quota
Пример #19
0
 def _load_mongo(self):
     if not self.mongo_loaded:
         try:
             self.mongoClass = cm_mongo()
             self.mongo_loaded = True
         except:
             print "ERROR: could not access Mongodb. " \
                   "Have you started the mongo server?"
Пример #20
0
 def __init__(self):
     self.filename = config_file("/cloudmesh.yaml")
     self.config = cm_config(filename=self.filename)
     self.cm_user_id = self.config.get("cloudmesh.hpc.username")
     self.clouds = self.config.get("cloudmesh.clouds")
     self.user_obj = cm_user()
     self.profile = self.config.profile()
     self.mongo = cm_mongo()
Пример #21
0
 def __init__(self):
     self.filename = config_file("/cloudmesh.yaml")
     self.config = cm_config(filename=self.filename)
     self.cm_user_id = self.config.get("cloudmesh.hpc.username")
     self.clouds = self.config.get("cloudmesh.clouds")
     self.user_obj = cm_user()
     self.profile = self.config.profile()
     self.mongo = cm_mongo()
Пример #22
0
 def _load_mongo(self):
     if not self.mongo_loaded:
         try:
             self.mongoClass = cm_mongo()
             self.mongo_loaded = True
         except:
             print("ERROR: could not access Mongodb. "
                   "Have you started the mongo server?")
Пример #23
0
 def __init__(self, user):
     """
     initializes based on cm_config and returns pointer
     to the keys dict.
     """
     self.mongo = cm_mongo()
     self.user_info = self.mongo.db_user.find_one({'cm_user_id': user})
     self.defaults_info = self.mongo.db_defaults.find_one(
         {'cm_user_id': user})
Пример #24
0
def delete_vm_confirm():

    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config()
    c = cm_mongo()
    c.activate(cm_user_id=g.user.id)

    userdata = g.user
    username = userdata.id
    user_obj = cm_user()
    user = user_obj.info(username)

    clouds = c.servers(cm_user_id=username)
    images = c.images(cm_user_id=username)
    flavors = c.flavors(cm_user_id=username)

    os_attributes = [
        "name",
        "status",
        "addresses",
        "flavor",
        "id",
        "image",
        "user_id",
        "metadata",
        "key_name",
        "created",
    ]
    cloud_filters = None
    filtered_clouds = clouds
    cloud = request.form["cloud"]
    select = request.form.getlist("selection_" + cloud)
    if select != None and cloud != None:
        session["delete_selection"] = (cloud, select)
    if "delete_selection" in session:
        print "writing selection to session"
        # print filtered_clouds
        selected_cloud_data = {}
        selected_cloud_data[cloud] = get_selected_clouds(
            filtered_clouds[session["delete_selection"][0]], session["delete_selection"][1]
        )
        return render_template(
            "mesh/cloud/delete_vms.html",
            address_string=address_string,
            attributes=os_attributes,
            updated=time_now,
            clouds=selected_cloud_data,
            config=config,
            user=user,
            images=images,
            flavors=flavors,
            filters=cloud_filters,
        )

    else:
        return render_template("error.html", type="Deleting VMs", error="No VMs to delete. ")
Пример #25
0
def manage_keypairs(cloud=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    userinfo = getCurrentUserinfo()
    username = userinfo["cm_user_id"]
    keys = userinfo["keys"]["keylist"]

    # currently we do the registration only for openstack
    # not yet sure if other clouds support this
    # or if we have implemented them if they also support
    if cloud in clouds.clouds[g.user.id] and clouds.clouds[
            g.user.id][cloud]['cm_type'] in ['openstack', 'ec2', 'aws']:
        cloudmanager = clouds.clouds[g.user.id][cloud]['manager']
        if request.method == 'POST':
            action = request.form['action']
            keyname = request.form["keyname"]
            # remove beginning 'key ' part
            keycontent = keys[keyname]
            if keycontent.startswith('key '):
                keycontent = keycontent[4:]
            # print keycontent
            keynamenew = _keyname_sanitation(username, keyname)
            if action == 'register':
                log.debug("trying to register a key")
                r = cloudmanager.keypair_add(keynamenew, keycontent)
                # pprint(r)
            else:
                log.debug("trying to deregister a key")
                r = cloudmanager.keypair_remove(keynamenew)
            return jsonify(**r)
        else:
            registered = {}
            keysRegistered = cloudmanager.keypair_list()
            keynamesRegistered = []
            if "keypairs" in keysRegistered:
                keypairsRegistered = keysRegistered["keypairs"]
                for akeypair in keypairsRegistered:
                    keyname = akeypair['keypair']['name']
                    keynamesRegistered.append(keyname)
            # pprint(keynamesRegistered)
            for keyname in keys.keys():
                keynamenew = _keyname_sanitation(username, keyname)
                # print keynamenew
                if keynamenew in keynamesRegistered:
                    registered[keyname] = True
                else:
                    registered[keyname] = False
            return render_template('mesh/cloud/keypairs.html',
                                   keys=keys,
                                   registered=registered,
                                   cloudname=cloud)
    else:
        return render_template(
            'error.html',
            error="Setting keypairs for this cloud is not yet enabled")
Пример #26
0
def refresh(cm_user_id=None, names=None, types=None):

    if isinstance(names, str):
        names = [names]
    if isinstance(types, str):
        types = [types]

    clouds = cm_mongo()
    clouds.activate(cm_user_id=cm_user_id, names=names)
    clouds.refresh(cm_user_id=cm_user_id, names=names, types=types)
Пример #27
0
def manage_keypairs(cloud=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    userinfo = getCurrentUserinfo()
    username = userinfo["cm_user_id"]
    keys = userinfo["keys"]["keylist"]

    # currently we do the registration only for openstack
    # not yet sure if other clouds support this
    # or if we have implemented them if they also support
    if cloud in clouds.clouds[g.user.id] and clouds.clouds[g.user.id][cloud]['cm_type'] in ['openstack', 'ec2', 'aws']:
        cloudmanager = clouds.clouds[g.user.id][cloud]['manager']
        if request.method == 'POST':
            action = request.form['action']
            keyname = request.form["keyname"]
            # remove beginning 'key ' part
            keycontent = keys[keyname]
            if keycontent.startswith('key '):
                keycontent = keycontent[4:]
            # print keycontent
            keynamenew = _keyname_sanitation(username, keyname)
            if action == 'register':
                log.debug("trying to register a key")
                r = cloudmanager.keypair_add(keynamenew, keycontent)
                # pprint(r)
            else:
                log.debug("trying to deregister a key")
                r = cloudmanager.keypair_remove(keynamenew)
            return jsonify(**r)
        else:
            registered = {}
            keysRegistered = cloudmanager.keypair_list()
            keynamesRegistered = []
            if "keypairs" in keysRegistered:
                keypairsRegistered = keysRegistered["keypairs"]
                for akeypair in keypairsRegistered:
                    keyname = akeypair['keypair']['name']
                    keynamesRegistered.append(keyname)
            # pprint(keynamesRegistered)
            for keyname in keys.keys():
                keynamenew = _keyname_sanitation(username, keyname)
                # print keynamenew
                if keynamenew in keynamesRegistered:
                    registered[keyname] = True
                else:
                    registered[keyname] = False
            return render_template('mesh/cloud/keypairs.html',
                                   keys=keys,
                                   registered=registered,
                                   cloudname=cloud)
    else:
        return render_template('error.html',
                               error="Setting keypairs for this cloud is not yet enabled")
Пример #28
0
    def do_limits(self, args, arguments):
        """
        ::
        
          Usage:
              limits [CLOUD] [--format=json]
              limits help | -h

          Current usage data with limits on a selected project (tenant)

          Arguments:

            CLOUD          Cloud name to see the usage
            help           Prints this message

          Options:

             -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print (self.do_limits.__doc__)
        else:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)

            cloudid = arguments["CLOUD"]
            if cloudid is None:
                cloudid = self.get_cloud_name(userid)
            # if an id is still not found print error
            if cloudid is None:
                Console.error('Please set a default cloud.')
                return

            usage_with_limits = self.cm_mongo.usage_with_limits(cloudid,
                                                                userid)

            if arguments["--format"] is None:
                print(row_table(usage_with_limits,
                                order=None,
                                labels=[
                                    "Limits",
                                    "(Used/Max)"
                                    ]))

            elif 'json' in arguments["--format"]:
                print(json.dumps(usage_with_limits, indent=4))
            else:
                Console.error('Quota is not supported.')

            return usage_with_limits
Пример #29
0
def assign_public_ip(cloud=None, server=None):

    config = cm_config()
    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    mycloud = config.cloud(cloud)
    if not mycloud.has_key("cm_automatic_ip") or mycloud["cm_automatic_ip"] is False:
        clouds.assign_public_ip(cloud, server, g.user.id)
        clouds.refresh(names=[cloud], types=["servers"], cm_user_id=g.user.id)
        return redirect("/mesh/servers")
Пример #30
0
def _helper(username, prefix=None, idx=None, raw=False):
    mongo = cm_mongo()
    # New activation for userinfo added to cm_mongo.
    # mongo.activate is not required to use vmname() - Sep 25th, 2014
    # mongo.activate(username)
    if not raw:
        if prefix or idx:
            print("updating... next vm name:")
        else:
            print("next vm name:")
    print(mongo.vmname(prefix=prefix, idx=idx, cm_user_id=username))
Пример #31
0
def _helper(username, prefix=None, idx=None, raw=False):
    mongo = cm_mongo()
    # New activation for userinfo added to cm_mongo.
    # mongo.activate is not required to use vmname() - Sep 25th, 2014
    # mongo.activate(username)
    if not raw:
        if prefix or idx:
            print("updating... next vm name:")
        else:
            print("next vm name:")
    print(mongo.vmname(prefix=prefix, idx=idx, cm_user_id=username))
Пример #32
0
def assign_public_ip(username=None, cloudname=None, serverid=None):

    config = cm_config()
    mongo = cm_mongo()
    mongo.activate(cm_user_id=username)

    mycloud = config.cloud(cloudname)
    if not mycloud.has_key('cm_automatic_ip') or mycloud['cm_automatic_ip'] is False:
        mongo.assign_public_ip(cloudname, serverid, username)
        mongo.refresh(
            names=[cloudname], types=["servers"], cm_user_id=username)
Пример #33
0
def shell_command_flavor(arguments):
    """
    ::

      Usage:
          flavor
          flavor CLOUD... [--refresh]
          flavor -h | --help
          flavor --version

     Options:
         -h                   help message
         --refresh            refresh flavors of IaaS

      Arguments:
          CLOUD    Name of the IaaS cloud e.g. india_openstack_grizzly.

      Description:
         flavor command provides list of available flavors. Flavor describes
         virtual hardware configurations such as size of memory, disk, cpu cores.

      Result:

      Examples:
          $ flavor india_openstack_grizzly

    """

    # log.info(arguments)
    cloud_names = arguments['CLOUD']
    # clouds in c.flavors treats None value as a ALL clouds
    if not cloud_names:
        cloud_names = None
    config = cm_config()
    username = config.username()
    c = cm_mongo()
    c.activate(cm_user_id=username)
    if arguments['--refresh']:
        c.refresh(cm_user_id=username, names=cloud_names, types=['flavors'])
    flavors_dict = c.flavors(cm_user_id=username, clouds=cloud_names)

    your_keys = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    flavors = _select_flavors(flavors_dict, your_keys)

    _display(flavors)
Пример #34
0
def mongo_flavors():
    if not with_active_clouds():
        error = "No Active Clouds set!"
        msg = "Please <a href='/mesh/register/clouds'>Register and Activate</a> a Cloud First"
        return render_template('error.html',
                               type="Refreshing Clouds",
                               error=error,
                               msg=msg)
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config()

    # getting user info
    userdata = g.user
    username = userdata.id
    user_obj = cm_user()
    user = user_obj.info(username)

    c = cm_mongo()
    c.activate(cm_user_id=username)
    # c.refresh(types=["flavors"])
    clouds = c.flavors(cm_user_id=username)

    os_attributes = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    # fake entry

    # user['defaults']['pagestatus'] = {'sierra': {"open": "false", "flavor": "3"}}

    # commented by HC on Nov. 8, 2013
    # The following check is repetive because these attributes are added
    #    by 'cm_user.init_defaults(username)'
    #    this method will be called when page is loaded
    """
    if 'pagestatus' not in user['defaults']:
        user['defaults']['pagestatus'] = init_user_pagestatus([cloud_name for cloud_name in clouds])
    """
    # ONLY for debug, if the Accordion does not work, please uncomment it
    # log.debug("mesh_flavors, before render, user defaults: {0}".format(user['defaults']))
    return render_template('mesh/cloud/mesh_flavors.html',
                           address_string=address_string,
                           attributes=os_attributes,
                           updated=time_now,
                           clouds=clouds,
                           user=user,
                           config=config)
Пример #35
0
def mongo_flavors():
    if not with_active_clouds():
        error = "No Active Clouds set!"
        msg = "Please <a href='/mesh/register/clouds'>Register and Activate</a> a Cloud First"
        return render_template('error.html',
                               type="Refreshing Clouds",
                               error=error,
                               msg=msg)
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config()

    # getting user info
    userdata = g.user
    username = userdata.id
    user_obj = cm_user()
    user = user_obj.info(username)

    c = cm_mongo()
    c.activate(cm_user_id=username)
    # c.refresh(types=["flavors"])
    clouds = c.flavors(cm_user_id=username)

    os_attributes = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    # fake entry

    # user['defaults']['pagestatus'] = {'sierra': {"open": "false", "flavor": "3"}}

    # commented by HC on Nov. 8, 2013
    # The following check is repetive because these attributes are added
    #    by 'cm_user.init_defaults(username)'
    #    this method will be called when page is loaded
    """
    if 'pagestatus' not in user['defaults']:
        user['defaults']['pagestatus'] = init_user_pagestatus([cloud_name for cloud_name in clouds])
    """
    # ONLY for debug, if the Accordion does not work, please uncomment it
    # log.debug("mesh_flavors, before render, user defaults: {0}".format(user['defaults']))
    return render_template('mesh/cloud/mesh_flavors.html',
                           address_string=address_string,
                           attributes=os_attributes,
                           updated=time_now,
                           clouds=clouds,
                           user=user,
                           config=config)
Пример #36
0
def shell_command_flavor(arguments):
    """
    ::

      Usage:
          flavor
          flavor CLOUD... [--refresh]
          flavor -h | --help
          flavor --version

     Options:
         -h                   help message
         --refresh            refresh flavors of IaaS

      Arguments:
          CLOUD    Name of the IaaS cloud e.g. india_openstack_grizzly.

      Description:
         flavor command provides list of available flavors. Flavor describes
         virtual hardware configurations such as size of memory, disk, cpu cores.

      Result:

      Examples:
          $ flavor india_openstack_grizzly

    """

    # log.info(arguments)
    cloud_names = arguments['CLOUD']
    # clouds in c.flavors treats None value as a ALL clouds
    if not cloud_names:
        cloud_names = None
    config = cm_config()
    username = config.username()
    c = cm_mongo()
    c.activate(cm_user_id=username)
    if arguments['--refresh']:
        c.refresh(cm_user_id=username, names=cloud_names, types=['flavors'])
    flavors_dict = c.flavors(cm_user_id=username, clouds=cloud_names)

    your_keys = [
        'id',
        'name',
        'vcpus',
        'ram',
        'disk',
        'cm_refresh',
    ]

    flavors = _select_flavors(flavors_dict, your_keys)

    _display(flavors)
Пример #37
0
def mongo_users():
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config_server()
    adminclouds = config.get("cloudmesh.server.keystone").keys()

    username = g.user.id
    userinfo = getCurrentUserinfo()
    activeclouds = []
    if 'activeclouds' in userinfo['defaults']:
        activeclouds = userinfo['defaults']['activeclouds']
    usersinclouds = []
    for cloud in adminclouds:
        if cloud in activeclouds:
            usersinclouds.append(cloud)

    c = cm_mongo()
    c.activate(cm_user_id=username)

    clouds = {}
    clouds = c.users(usersinclouds)
    # print "TYTYTYT", len(clouds), type(clouds), clouds.keys()
    # print len(clouds['sierra_openstack_grizzly'])


    """
    for cloud in clouds:
        print cloud
        for server in clouds[cloud]:
            print server
            for attribute in clouds[cloud][server]:
                print attribute, clouds[cloud][server][attribute]
    """

    attributes = {"grizzly":
                    [
                        [ "Name", "name"],
                        [ "Firstname", "firstname"],
                        [ "Lastname", "lastname"],
                        [ "Id" , "id"],
                        # [ "TenentId" , "tenantId"],
                        [ "E-mail" , "email"],
                        [ "Enabled" , "enabled"],
                        [ "Refresh", "cm_refresh"]
                    ]
                  }

    return render_template('mesh/mesh_users.html',
                           address_string=address_string,
                           cloud_attributes=attributes,
                           updated=time_now,
                           clouds=clouds,
                           config=config)
Пример #38
0
def shell_command_security_group(arguments):
    """
    ::

      Usage:
          security_group list <cm_cloud>...
          security_group add <cm_cloud> <label> <parameters>  [NOT IMPLEMENTED]
          security_group delete <cm_cloud> <label>            [NOT IMPLEMENTED]
      security_group -h | --help
          security_group --version

     Options:
         -h                   help message

      Arguments:
          cm_cloud    Name of the IaaS cloud e.g. india_openstack_grizzly.

      Description:
         security_group command provides list of available security_groups.

      Result:

      Examples:
          $ security_group list india_openstack_grizzly

    """

    # log.info(arguments)

    cloud_names = arguments['<cm_cloud>']
    # None value means ALL clouds in c.security_groups() function
    if not cloud_names:
        cloud_names = None
    config = cm_config()
    username = config.username()
    c = cm_mongo()
    c.activate(cm_user_id=username)
    security_groups_dict = c.security_groups(
        cm_user_id=username, clouds=cloud_names)
    your_keys = {"openstack":
                 [
                     ['id', 'id'],
                     ['name', 'name'],
                     ['description', 'description'],
                     ['cm_refresh', 'cm_refresh']
                 ],
                 "ec2": [],
                 "azure": [],
                 "aws": []
                 }

    security_groups = _select_security_groups(security_groups_dict, your_keys)
    _display(security_groups)
Пример #39
0
    def do_limits(self, args, arguments):
        """
        ::
        
          Usage:
              limits [CLOUD] [--format=json]
              limits help | -h

          Current usage data with limits on a selected project (tenant)

          Arguments:

            CLOUD          Cloud name to see the usage
            help           Prints this message

          Options:

             -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print(self.do_limits.__doc__)
        else:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)

            cloudid = arguments["CLOUD"]
            if cloudid is None:
                cloudid = self.get_cloud_name(userid)
            # if an id is still not found print error
            if cloudid is None:
                Console.error('Please set a default cloud.')
                return

            usage_with_limits = self.cm_mongo.usage_with_limits(
                cloudid, userid)

            if arguments["--format"] is None:
                print(
                    row_table(usage_with_limits,
                              order=None,
                              labels=["Limits", "(Used/Max)"]))

            elif 'json' in arguments["--format"]:
                print(json.dumps(usage_with_limits, indent=4))
            else:
                Console.error('Quota is not supported.')

            return usage_with_limits
Пример #40
0
def assign_public_ip(cloud=None, server=None):

    config = cm_config()
    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    mycloud = config.cloud(cloud)
    if not mycloud.has_key(
            'cm_automatic_ip') or mycloud['cm_automatic_ip'] is False:
        clouds.assign_public_ip(cloud, server, g.user.id)
        clouds.refresh(names=[cloud], types=["servers"], cm_user_id=g.user.id)
        return redirect('/mesh/servers')
Пример #41
0
 def __init__(self, user):
     """
     initializes based on cm_config and returns pointer
     to the keys dict.
     """
     self.mongo = cm_mongo()
     self.user_info = self.mongo.db_user.find_one(
         {'cm_user_id': user}
     )
     self.defaults_info = self.mongo.db_defaults.find_one(
         {'cm_user_id': user}
     )
Пример #42
0
def delete_vms(cloud=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    # donot do refresh before delete, this will cause all the vms to get deleted
    f_cloud = clouds.clouds[g.user.id][cloud]
    for id, server in f_cloud['servers'].iteritems():
        log.info("-> delete {0} {1}".format(cloud, id))
        clouds.vm_delete(cloud, id, g.user.id)
    time.sleep(7)
    f_cloud['servers'] = {}
    return redirect('/mesh/servers')
Пример #43
0
def delete_vms(cloud=None):

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    # donot do refresh before delete, this will cause all the vms to get deleted
    f_cloud = clouds.clouds[g.user.id][cloud]
    for id, server in f_cloud["servers"].iteritems():
        log.info("-> delete {0} {1}".format(cloud, id))
        clouds.vm_delete(cloud, id, g.user.id)
    time.sleep(7)
    f_cloud["servers"] = {}
    return redirect("/mesh/servers")
Пример #44
0
def delete_vm(cloud=None, server=None):
    log.info("-> delete {0} {1}".format(cloud, server))

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    # if (cloud == 'india'):
    #  r = cm("--set", "quiet", "delete:1", _tty_in=True)
    clouds.vm_delete(cloud, server, g.user.id)
    time.sleep(5)
    clouds.release_unused_public_ips(cloud, g.user.id)
    clouds.refresh(names=[cloud], types=["servers"], cm_user_id=g.user.id)
    return redirect("/mesh/servers")
Пример #45
0
def delete_vm(cloud=None, server=None):
    log.info("-> delete {0} {1}".format(cloud, server))

    clouds = cm_mongo()
    clouds.activate(cm_user_id=g.user.id)

    # if (cloud == 'india'):
    #  r = cm("--set", "quiet", "delete:1", _tty_in=True)
    clouds.vm_delete(cloud, server, g.user.id)
    time.sleep(5)
    clouds.release_unused_public_ips(cloud, g.user.id)
    clouds.refresh(names=[cloud], types=["servers"], cm_user_id=g.user.id)
    return redirect('/mesh/servers')
Пример #46
0
def mongo_users():
    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config_server()
    adminclouds = config.get("cloudmesh.server.keystone").keys()

    username = g.user.id
    userinfo = getCurrentUserinfo()
    activeclouds = []
    if 'activeclouds' in userinfo['defaults']:
        activeclouds = userinfo['defaults']['activeclouds']
    usersinclouds = []
    for cloud in adminclouds:
        if cloud in activeclouds:
            usersinclouds.append(cloud)

    c = cm_mongo()
    c.activate(cm_user_id=username)

    clouds = {}
    clouds = c.users(usersinclouds)
    # print "TYTYTYT", len(clouds), type(clouds), clouds.keys()
    # print len(clouds['sierra'])
    """
    for cloud in clouds:
        print cloud
        for server in clouds[cloud]:
            print server
            for attribute in clouds[cloud][server]:
                print attribute, clouds[cloud][server][attribute]
    """

    attributes = {
        "grizzly": [
            ["Name", "name"],
            ["Firstname", "firstname"],
            ["Lastname", "lastname"],
            ["Id", "id"],
            # [ "TenentId" , "tenantId"],
            ["E-mail", "email"],
            ["Enabled", "enabled"],
            ["Refresh", "cm_refresh"]
        ]
    }

    return render_template('mesh/mesh_users.html',
                           address_string=address_string,
                           cloud_attributes=attributes,
                           updated=time_now,
                           clouds=clouds,
                           config=config)
Пример #47
0
def delete_vm_confirm():

    time_now = datetime.now().strftime("%Y-%m-%d %H:%M")
    # filter()
    config = cm_config()
    c = cm_mongo()
    c.activate(cm_user_id=g.user.id)

    userdata = g.user
    username = userdata.id
    user_obj = cm_user()
    user = user_obj.info(username)

    clouds = c.servers(cm_user_id=username)
    images = c.images(cm_user_id=username)
    flavors = c.flavors(cm_user_id=username)

    os_attributes = [
        'name', 'status', 'addresses', 'flavor', 'id', 'image', 'user_id',
        'metadata', 'key_name', 'created'
    ]
    cloud_filters = None
    filtered_clouds = clouds
    cloud = request.form["cloud"]
    select = request.form.getlist("selection_" + cloud)
    if select is not None and cloud is not None:
        session["delete_selection"] = (cloud, select)
    if "delete_selection" in session:
        print "writing selection to session"
        # print filtered_clouds
        selected_cloud_data = {}
        selected_cloud_data[cloud] = get_selected_clouds(
            filtered_clouds[session["delete_selection"][0]],
            session["delete_selection"][1])
        return render_template('mesh/cloud/delete_vms.html',
                               address_string=address_string,
                               attributes=os_attributes,
                               updated=time_now,
                               clouds=selected_cloud_data,
                               config=config,
                               user=user,
                               images=images,
                               flavors=flavors,
                               filters=cloud_filters)

    else:
        return render_template('error.html',
                               type="Deleting VMs",
                               error="No VMs to delete. ")
Пример #48
0
    def do_quota(self, args, arguments):
        """
        ::
        
          Usage:
              quota [CLOUD] [--format=json]
              quota help | -h

          quota limit on a current project (tenant)

          Arguments:

            CLOUD          Cloud name to see the usage
            help           Prints this message

          Options:

             -v       verbose mode

        """

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

        if arguments["help"] or arguments["-h"]:
            print (self.do_quota.__doc__)
        else:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)

            cloudid = arguments["CLOUD"]
            if cloudid is None:
                cloudid = self.get_cloud_name(userid)
            # if an id is still not found print error
            if cloudid is None:
                Console.error('Please set a default cloud.')
                return

            quota = self.cm_mongo.quota(cloudid, userid)
            if arguments["--format"] is None:
                print(
                    row_table(quota, order=None, labels=["Variable", "Value"]))
            elif 'json' in arguments["--format"]:
                print(json.dumps(quota, indent=4))
            else:
                Console.error('Quota is not supported.')
            return quota
Пример #49
0
    def do_quota(self, args, arguments):
        """
        ::
        
          Usage:
              quota [CLOUD] [--format=json]
              quota help | -h

          quota limit on a current project (tenant)

          Arguments:

            CLOUD          Cloud name to see the usage
            help           Prints this message

          Options:

             -v       verbose mode

        """

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

        if arguments["help"] or arguments["-h"]:
            print(self.do_quota.__doc__)
        else:
            userid = self.cm_config.username()
            self.cm_mongo.activate(userid)

            cloudid = arguments["CLOUD"]
            if cloudid is None:
                cloudid = self.get_cloud_name(userid)
            # if an id is still not found print error
            if cloudid is None:
                Console.error('Please set a default cloud.')
                return

            quota = self.cm_mongo.quota(cloudid, userid)
            if arguments["--format"] is None:
                print(
                    row_table(quota, order=None, labels=["Variable", "Value"]))
            elif 'json' in arguments["--format"]:
                print(json.dumps(quota, indent=4))
            else:
                Console.error('Quota is not supported.')
            return quota
Пример #50
0
    def do_usage(self, args, arguments):
        """
        Usage:
            usage [CLOUD] [--start=START] [--end=END]
            usage help | -h

        Usage data on a current project (tenant)

        Arguments:
          
          CLOUD          Cloud name to see the usage
          START          start date of usage (YYYY-MM-DD)
          END            end date of usage (YYYY-MM-DD)
          help           Prints this message

        Options:

           -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print(self.do_usage.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            usage = self.cm_mongo.usage(def_cloud, userid)
            # server usages need to be supressed.
            # e.g. {u'hours': 24.00000006388889, u'uptime': 1960234,
            # u'started_at': u'2014-10-07T23:03:57.000000', u'ended_at': None,
            # u'name': u'hrlee-server-2zuvke4wujud', u'tenant_id':
            # u'3e6eaf1d913a48f694a7bc0fbb027507', u'instance_id':
            # u'2c9d24e0-7453-4f83-84b7-f8c0254a574f', u'state':
            # u'active', u'memory_mb': 2048, u'vcpus': 1, u'flavor':
            # u'm1.small', u'local_gb': 20}
            try:
                usage['server_usages'] = str(len(
                    usage['server_usages'])) + " vms"
            except:
                pass

            print(row_table(usage, order=None, labels=["Variable", "Value"]))

            return usage
Пример #51
0
 def get_working_server_id(self, cloudname):
     """
      returns the id of a VM
      In the command line, the user can provide either VM name or VM id to 
      identify a VM, this function figures out what the users input and check
      the existence of the VM, then returns the VM id or False if check 
      failed
      
      :param cloudname: cloud name
      :type cloudname: string
      :return: VM id, otherwise False
      :rtype: string or boolean
     """
     serverid = None
     mongo = cm_mongo()
     mongo.activate(cm_user_id=self.username, names=[cloudname])
     mongo.refresh(self.username, names=[cloudname], types=['servers'])
     serverdata = mongo.servers(
         clouds=[cloudname], cm_user_id=self.username)[cloudname]
     vmname = self.arguments['--name'] or self.arguments['NAME']
     if vmname:
         ls = []
         for k, v in serverdata.iteritems():
             if vmname == v['name']:
                 ls.append(k)
         if len(ls) > 1:
             Console.warning("There are more than one VM named {0}, please use VM id instead"
                             .format(vmname))
             return False
         elif len(ls) == 0:
             Console.error(
                 "Could not find VM named {0}".format(vmname))
             return False
         else:
             serverid = ls[0]
     elif self.arguments['--id']:
         for k, v in serverdata.iteritems():
             if self.arguments['--id'] == k:
                 serverid = self.arguments['--id']
                 break
         if serverid is None:
             Console.error(
                 "Could not find VM with id {0}".format(self.arguments['--id']))
             return False
     else:
         Console.warning("Please specify a VM name or id")
         return False
     return serverid
Пример #52
0
    def do_usage(self, args, arguments):
        """
        Usage:
            usage [CLOUD] [--start=START] [--end=END]
            usage help | -h

        Usage data on a current project (tenant)

        Arguments:
          
          CLOUD          Cloud name to see the usage
          START          start date of usage (YYYY-MM-DD)
          END            end date of usage (YYYY-MM-DD)
          help           Prints this message

        Options:

           -v       verbose mode

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

        if arguments["help"] or arguments["-h"]:
            print (self.do_usage.__doc__)
        else:
            userid = self.cm_config.username()
            def_cloud = self.get_cloud_name(userid)
            self.cm_mongo.activate(userid)
            usage = self.cm_mongo.usage(def_cloud, userid)
            # server usages need to be supressed.
            # e.g. {u'hours': 24.00000006388889, u'uptime': 1960234,
            # u'started_at': u'2014-10-07T23:03:57.000000', u'ended_at': None,
            # u'name': u'hrlee-server-2zuvke4wujud', u'tenant_id':
            # u'3e6eaf1d913a48f694a7bc0fbb027507', u'instance_id':
            # u'2c9d24e0-7453-4f83-84b7-f8c0254a574f', u'state':
            # u'active', u'memory_mb': 2048, u'vcpus': 1, u'flavor':
            # u'm1.small', u'local_gb': 20} 
            try:
                usage['server_usages'] = str(len(usage['server_usages'])) + " vms"
            except:
                pass

            print(row_table(usage, order=None, labels=["Variable", "Value"]))

            return usage