Exemple #1
0
 def node_info(clusterid, nodeid=None, format='table'):
     if nodeid:
         url = Comet.url("cluster/{}/compute/{}/".format(clusterid, nodeid))
     else:
         url = Comet.url("cluster/{}/frontend/".format(clusterid))
     r = Comet.get(url)
     return Printer.attribute(
         r,
         #order=Cluster.NODEINFO_ORDER,
         output=format)
Exemple #2
0
 def node_info(clusterid, nodeid=None, format='table'):
     if nodeid:
         url = Comet.url("cluster/{}/compute/{}/"
                         .format(clusterid, nodeid))
     else:
         url = Comet.url("cluster/{}/frontend/"
                         .format(clusterid))
     r = Comet.get(url)
     return Printer.attribute(r,
                              #order=Cluster.NODEINFO_ORDER,
                              output=format)
Exemple #3
0
 def rename_node(clusterid, old_compute_name, new_compute_name):
     url = Comet.url("cluster/{}/compute/{}/rename".format(
         clusterid, old_compute_name))
     data = {"name": "%s" % new_compute_name}
     ret = ""
     r = Comet.post(url, data=data)
     # print (r)
     if r is not None:
         if '' != r.strip():
             ret = r
         else:
             ret = "Request Accepted."
     return ret
Exemple #4
0
 def rename_node(clusterid, old_compute_name, new_compute_name):
     url = Comet.url("cluster/{}/compute/{}/rename"\
                     .format(clusterid, old_compute_name))
     data = {"name":"%s" % new_compute_name}
     ret = ""
     r = Comet.post(url, data=data)
     print (r)
     if r is not None:
         if '' != r.strip():
             ret = r
         else:
             ret = "Requeset Accepted."
     return ret
Exemple #5
0
 def node_info(clusterid, nodeid=None, format='table'):
     if nodeid:
         url = Comet.url("cluster/{}/compute/{}/"
                         .format(clusterid, nodeid))
     else:
         url = Comet.url("cluster/{}/frontend/"
                         .format(clusterid))
     r = Comet.get(url)
     ret = "ERROR: Node not available. Please check the cluster/node name!\n"
     if r:
         ret = Printer.attribute(r,
                              #order=Cluster.NODEINFO_ORDER,
                              output=format)
     return ret
Exemple #6
0
    def check_nodes_computesets(clusterid, computenodeids):
        hosts_param = hostlist.expand_hostlist(computenodeids)
        hosts_param_set = set(hosts_param)
        nodes_free = True
        nodes_allocated = False
        nodes_checked = False
        # computesetid = -1
        computesets = Comet.get_computeset()
        # get all active computeset and put nodes into a set
        allhosts = set()
        for computeset in computesets:
            if computeset["cluster"] == clusterid \
                    and (computeset["state"] in Cluster.ACTIVE_COMPUTESETS):
                computesetid = computeset["id"]
                # print (computesetid)
                for compute in computeset["computes"]:
                    allhosts.add(compute["name"])

        # all nodes allocated
        if hosts_param_set <= allhosts:
            nodes_allocated = True
            nodes_free = False
            nodes_checked = True
        # at least one specified host not in any Active computeset
        else:
            for host in hosts_param:
                # some specified nodes are in Active computeset
                if host in allhosts:
                    nodes_free = False
                    nodes_checked = True
                    break

        # print ("nodes_checked: %s" % nodes_checked)
        # print ("nodes_free: %s" % nodes_free)
        return [nodes_free, nodes_allocated]
Exemple #7
0
    def check_nodes_computesets(clusterid, computenodeids):
        hosts_param = hostlist.expand_hostlist(computenodeids)
        hosts_param_set = set(hosts_param)
        nodes_free = True
        nodes_allocated = False
        nodes_checked = False
        # computesetid = -1
        computesets = Comet.get_computeset()
        # get all active computeset and put nodes into a set
        allhosts = set()
        for computeset in computesets:
            if computeset["cluster"] == clusterid \
                    and (computeset["state"] in Cluster.ACTIVE_COMPUTESETS):
                computesetid = computeset["id"]
                # print (computesetid)
                for compute in computeset["computes"]:
                    allhosts.add(compute["name"])

        # all nodes allocated
        if hosts_param_set <= allhosts:
            nodes_allocated = True
            nodes_free = False
            nodes_checked = True
        # at least one specified host not in any Active computeset
        else:
            for host in hosts_param:
                # some specified nodes are in Active computeset
                if host in allhosts:
                    nodes_free = False
                    nodes_checked = True
                    break

        # print ("nodes_checked: %s" % nodes_checked)
        # print ("nodes_free: %s" % nodes_free)
        return [nodes_free, nodes_allocated]
Exemple #8
0
def comet_logon(request):
    c = None
    try:
        c = Comet.logon()
        print("LOGON OK")
        return render(request, 'cloudmesh_portal/comet/logon_error.jinja')
    except:
        return c
Exemple #9
0
 def computeset_terminate(computesetid):
     ret = ''
     url = Comet.url("computeset/")
     action = "shutdown"
     puturl = "{:}{:}/{}".format(url, computesetid, action)
     # print (puturl)
     r = Comet.put(puturl)
     if r is not None:
         if '' != r.strip():
             ret = r
         else:
             ret = "Request Accepted. " \
                   "In the process of terminating the computeset"
     else:
         ret = "Problem executing the request. " \
               "Check if the computeset exists"
     return ret
Exemple #10
0
 def computeset_terminate(computesetid):
     ret = ''
     url = Comet.url("computeset/")
     action = "shutdown"
     puturl = "{:}{:}/{}".format(url, computesetid, action)
     # print (puturl)
     r = Comet.put(puturl)
     if r is not None:
         if '' != r.strip():
             ret = r
         else:
             ret = "Request Accepted. " \
                   "In the process of terminating the computeset"
     else:
         ret = "Problem executing the request. " \
               "Check if the computeset exists"
     return ret
Exemple #11
0
def comet_logon(request):
    c = None
    try:
        c = Comet.logon()
        print("LOGON OK")
        return render(request,
                      'cloudmesh_portal_comet/logon_error.jinja')
    except:
        return c
Exemple #12
0
    def attach_iso(isoname, clusterid, nodeids=None, action='Attaching'):
        ret = ''
        # print ("Attaching ISO image")
        # print ("isoname: %s" % isoname)
        # print ("cluster: %s" % clusterid)
        # print ("node: %s" % nodeid)

        if isoname != '':
            isoname = "public/{}".format(isoname)

        urls = {}
        # attaching to compute node
        if nodeids:
            nodeids = hostlist.expand_hostlist(nodeids)
            for nodeid in nodeids:
                url = Comet.url("cluster/{}/compute/{}/attach_iso?iso_name={}") \
                    .format(clusterid, nodeid, isoname)
                urls["Node {}".format(nodeid)] = url
        else:
            # attaching to fronend node
            url = Comet.url("cluster/{}/frontend/attach_iso?iso_name={}") \
                .format(clusterid, isoname)
            urls['Frontend'] = url
        # data = {"iso_name": "%s" % isoname}
        # print ("url: %s" % url)
        # print ("data: %s" % data)
        tofrom = {}
        tofrom['Attaching'] = 'to'
        tofrom['Detaching'] = 'from'
        for node, url in urls.items():
            r = Comet.put(url)
            # print (r)
            if r is not None:
                if '' != r.strip():
                    ret += r
                else:
                    ret += "Request Accepted. {} the iso image {} {} of cluster {}\n" \
                        .format(action, tofrom[action], node, clusterid)
            else:
                ret += "Something wrong during {} the iso image {} {} of cluster {}!" \
                       "Please check the command and try again\n" \
                    .format(action, tofrom[action], node, clusterid)
        return ret
Exemple #13
0
    def attach_iso(isoname, clusterid, nodeids=None, action='Attaching'):
        ret = ''
        # print ("Attaching ISO image")
        # print ("isoname: %s" % isoname)
        #print ("cluster: %s" % clusterid)
        # print ("node: %s" % nodeid)

        if isoname != '':
            isoname = "public/{}".format(isoname)

        urls = {}
        # attaching to compute node
        if nodeids:
            nodeids = hostlist.expand_hostlist(nodeids)
            for nodeid in nodeids:
                url = Comet.url("cluster/{}/compute/{}/attach_iso?iso_name={}")\
                                .format(clusterid, nodeid, isoname)
                urls["Node {}".format(nodeid)] = url
        else:
            # attaching to fronend node
            url = Comet.url("cluster/{}/frontend/attach_iso?iso_name={}")\
                            .format(clusterid, isoname)
            urls['Frontend'] = url
        #data = {"iso_name": "%s" % isoname}
        # print ("url: %s" % url)
        #print ("data: %s" % data)
        tofrom = {}
        tofrom['Attaching'] = 'to'
        tofrom['Detaching'] = 'from'
        for node, url in urls.iteritems():
            r = Comet.put(url)
            # print (r)
            if r is not None:
                if '' != r.strip():
                    ret += r
                else:
                    ret += "Requeset Accepted. {} the image {} {} of cluster {}\n"\
                            .format(action, tofrom[action], node, clusterid)
            else:
                ret += "Something wrong during {} the image {} {} of cluster {}!"\
                       "Please check the command and try again\n"\
                       .format(action, tofrom[action], node, clusterid)
        return ret
Exemple #14
0
def comet_console(request, cluster, node=None):
    # noinspection PyUnusedLocal
    c = comet_logon(request)
    context = {"title": "Comet Virtual Cluster Console",
               "cluster": cluster,
               "node": node or "FE"}
    url = Comet.console_url(cluster, node)
    # print (url)
    context["url"] = url
    return render(request,
                  'cloudmesh_portal_comet/console.jinja',
                  context)
Exemple #15
0
 def computeset(id=None):
     computesets = Comet.get_computeset(id)
     if computesets is not None:
         if 'cluster' in computesets:
             result = Cluster.output_computeset(computesets)
         else:
             result = ''
             for acomputeset in computesets:
                 result += Cluster.output_computeset(acomputeset)
     else:
         result = "No computeset exists with the specified ID"
     return result
Exemple #16
0
def comet_console(request, cluster, node=None):
    # noinspection PyUnusedLocal
    c = comet_logon(request)
    context = {
        "title": "Comet Virtual Cluster Console",
        "cluster": cluster,
        "node": node or "FE"
    }
    url = Comet.console_url(cluster, node)
    # print (url)
    context["url"] = url
    return render(request, 'cloudmesh_portal/comet/console.jinja', context)
Exemple #17
0
 def computeset(id=None, cluster=None, state=None, allocation=None):
     computesets = Comet.get_computeset(id)
     if computesets is not None:
         if 'cluster' in computesets:
             result = Cluster.output_computeset(computesets, state='ALL')
         else:
             result = ''
             for acomputeset in computesets:
                 result += Cluster.output_computeset(acomputeset,
                                                     cluster,
                                                     state,
                                                     allocation)
     else:
         result = "No computeset exists with the specified ID"
     return result
Exemple #18
0
 def computeset(id=None, cluster=None, state=None, allocation=None):
     #
     # state could be one of
     # ['created' or 'submitted' or 'failed' or 'running'
     #   or 'cancelled' or 'ending' or 'completed']
     computesets = Comet.get_computeset(id, state)
     if computesets is not None:
         if 'cluster' in computesets:
             result = Cluster.output_computeset(computesets)
         else:
             result = ''
             for acomputeset in computesets:
                 result += Cluster.output_computeset(
                     acomputeset, cluster, allocation)
     else:
         result = "No computeset exists with the specified ID"
     return result
Exemple #19
0
 def computeset(id=None, cluster=None, state=None, allocation=None):
     #
     # state could be one of
     # ['created' or 'submitted' or 'failed' or 'running' 
     #   or 'cancelled' or 'ending' or 'completed']
     computesets = Comet.get_computeset(id, state)
     if computesets is not None:
         if 'cluster' in computesets:
             result = Cluster.output_computeset(computesets)
         else:
             result = ''
             for acomputeset in computesets:
                 result += Cluster.output_computeset(acomputeset,
                                                     cluster,
                                                     allocation)
     else:
         result = "No computeset exists with the specified ID"
     return result
Exemple #20
0
    def computeset_start(clusterid,
                         computenodeids=None,
                         numnodes=None,
                         allocation=None,
                         walltime=None):
        ret = ''
        # print ("clusterid: %s" % clusterid)
        # print ("computenodeids: %s" % computenodeids)
        # print ("numnodes: %s" % numnodes)
        # print ("allocation: %s" % allocation)
        # print ("walltime: %s" % walltime)
        data = None
        # validating and initiating allocation and walltime values
        if not allocation:
            cluster = Cluster.list(clusterid, format='rest')
            # use the first one if no provided
            allocation = cluster[0]['allocations'][0]
        if not walltime:
            walltime = Cluster.WALLTIME_MINS

        # preparing parameters for the post call
        # num of nodes to start
        #
        # Now it accepts
        # {"cluster":"vc3","computes":"compute[1-2]"},
        # {"cluster":"vc3","computes":["compute1","compute2"]} and
        # {"cluster":"vc3","count":2}
        #
        if numnodes:
            data = {
                "cluster": "%s" % clusterid,
                "count": "%s" % numnodes,
                "walltime_mins": "%s" % walltime,
                "allocation": "%s" % allocation
            }
        # computenodeids in hostlist format
        elif computenodeids:
            nodes_free = Cluster.check_nodes_computesets(
                clusterid, computenodeids)[0]
            if not nodes_free:
                Console.error("Some nodes are already in active computesets",
                              traceflag=False)
                return ret
            else:
                data = {
                    "computes": "%s" % computenodeids,
                    "cluster": "%s" % clusterid,
                    "walltime_mins": "%s" % walltime,
                    "allocation": "%s" % allocation
                }

        # print("Issuing request to poweron nodes...")
        posturl = Comet.url("computeset/")
        # print ("POST data: %s" % data)
        r = Comet.post(posturl, data=data)
        # print("RETURNED RESULTS:")
        # print (r)
        if 'cluster' in r:
            if 'state' in r and \
                            r['state'] in Cluster.PENDING_COMPUTESETS:
                computesetid = r['id']
                ret = 'Request accepted! Check status with:\n' \
                      'comet cluster {}\n'.format(clusterid) + \
                      'or:\n' \
                      'comet computeset {}\n'.format(computesetid)
            else:
                # in case of some internal problem
                ret = ''
        elif 'error' in r:
            ret = "An error occurred: {}".format(r['error'])
        else:
            ret = "An internal error occured. " \
                  "Please submit a ticket with the " \
                  "following info:\n {}\n" \
                .format(r)
        return ret
Exemple #21
0
    def power(clusterid, subject, param=None, action=None):

        # print("SUBJECT to perform action on: {}".format(subject))
        # print("\ton cluster: {}".format(clusterid))
        # print("\tAction: {}".format(action))
        # print("\tParameter: {}".format(param))

        # the API is now accepting hostlist format directly
        # computeIdsHostlist = hostlist.collect_hostlist(computeids)
        # print (computeIdsHostlist)
        ret = ''
        if 'HOSTS' == subject:
            url = Comet.url("computeset/")

            # data = {
            #    "computes": [{"name": vm, "host": "comet-{:}".format(vm)} for vm in
            #                 computeids], "cluster": "%s" % id}
            data = {"computes": "%s" % param,
                    "cluster": "%s" % clusterid,
                    "walltime_mins": "%s" % Cluster.WALLTIME_MINS}

            # print (data)
            if "on" == action:
                # print("Issuing request to poweron nodes...")
                posturl = url
                # print (data)

                r = Comet.post(posturl, data=data)
                # print("RETURNED RESULTS:")
                # print (r)
                if 'cluster' in r:
                    if 'state' in r and \
                       ('queued' == r['state'] or 'submitted' == r['state']):
                        computesetid = r['id']
                        ret = 'Request accepted! Check status with:\n' \
                              'comet cluster {}\n'.format(clusterid) + \
                              'or:\n' \
                              'comet computeset {}\n'.format(computesetid)
                    else:
                        # in case of some internal problem
                        ret = ''
                elif 'error' in r:
                    ret = "An error occurred: {}".format(r['error'])
                else:
                    ret = "An internal error occured. " \
                          "Please submit a ticket with following info:\n {}\n" \
                        .format(r)
                # print(ret)
            elif action in ["off", "reboot", "reset", "shutdown"]:
                if action in ["off"]:
                    action = "power{}".format(action)
                # print("finding the computesetid of the specified nodes...")
                computesets = Comet.get_computeset()
                # print ("computesets")
                # pprint (computesets)

                is_valid_set = False
                # computesetid = -1
                for computeset in computesets:
                    if computeset["cluster"] == clusterid \
                            and (computeset["state"] == "started" \
                                 or computeset["state"] == "running"):
                        computesetid = computeset["id"]
                        # print (computesetid)
                        hosts = set()
                        for compute in computeset["computes"]:
                            hosts.add(compute["name"])
                        # print (hosts)
                        is_valid_set = True
                        hostsparam = Parameter.expand(param)
                        for host in hostsparam:
                            if host not in hosts:
                                is_valid_set = False
                                break
                    # a cluster could have multiple 'started' set
                    if is_valid_set:
                        break
                if is_valid_set:
                    # print("Issuing request to poweroff nodes...")
                    # print("computesetid: {}".format(computesetid))
                    puturl = "{:}{:}/{}".format(url, computesetid, action)
                    # print (puturl)
                    r = Comet.put(puturl)
                    # print("RETURNED RESULTS:")
                    # print(r)
                    if r is not None:
                        if '' != r.strip():
                            ret = r
                            # print(r)
                        else:
                            ret = "Requeset Accepted. "\
                                  "In the process of {} the nodes".format(action)
                    else:
                        ret = "Unknown error: POWER, HOSTS"
                else:
                    ret = "All the nodes are not in the specified cluster, "\
                          "or they are not running"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        elif 'FE' == subject:
            url = Comet.url("cluster/{}/frontend/".format(clusterid))
            if action in ["on", "off", "reboot", "reset", "shutdown"]:
                if action in ["on", "off"]:
                    action = "power{}".format(action)
                puturl = "{}{}".format(url, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the nodes".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the cluster exists"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        elif 'COMPUTESET' == subject:
            url = Comet.url("computeset/")
            if 'on' == action:
                ret = "NOT SUPPORTED! Use hostslist to specify the hosts to power on!"
            elif action in ["off", "reboot", "reset", "shutdown"]:
                if action in ["off"]:
                    action = "power{}".format(action)
                puturl = "{:}{:}/{}".format(url, param, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the nodes".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the computeset exists"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        elif 'HOST' == subject:
            url = Comet.url("cluster/{}/compute/{}/".format(clusterid, param))
            if action in ["on", "off", "reboot", "reset", "shutdown"]:
                if action in ["on", "off"]:
                    action = "power{}".format(action)
                puturl = "{}{}".format(url, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the nodes".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the node belongs to the cluster"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        return ret
Exemple #22
0
 def stop(id):
     data = {"id": id}
     r = requests.post(Comet.url("cluster/{id}/stop".format(**data)))
     print(r)
Exemple #23
0
    def list(id=None, format="table", sort=None):
        def check_for_error(r):
            if r is not None:
                if 'error' in r:
                    Console.error("An error occurred: {error}".format(**r))
                    raise ValueError("COMET Error")

        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
            check_for_error(r)
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            check_for_error(r)
            if r is None:
                Console.error("Could not find cluster `{}`".format(id))
                return result
            r = [r]
        #
        # stuck state included in cluster data via API
        '''
        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format(id, computeset["state"])
        '''
        #
        # getting account/allocation for each computeset
        # to display in the cluster view
        computeset_account = {}
        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                id = computeset["id"]
                account = computeset["account"]
                if id not in computeset_account:
                    computeset_account[id] = account

        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format (id, computeset["state"])

        if r is not None:
            if format == "rest":
                result = r
            else:
                result = ''
                data = []

                empty = {
                    'cluster': None,
                    'cpus': None,
                    'host': None,
                    "mac": None,
                    'ip': None,
                    'memory': None,
                    'disksize': None,
                    'name': None,
                    'state': None,
                    'type': None,
                    'active_computeset': None,
                    'kind': 'frontend',
                    'admin_state': None
                }

                for cluster in sorted(r, key=lambda x: x["name"]):

                    clients = cluster["computes"]
                    for client in clients:
                        client["kind"] = "compute"
                    frontend = dict(empty)
                    frontend.update(cluster["frontend"])
                    pubip = cluster["frontend"]["pub_ip"]
                    frontend["ip"] = pubip
                    frontend["admin_state"] = cluster["frontend"][
                        "frontend_state"]
                    result += "Cluster: %s\tFrontend: %s\tIP: %s\n" % \
                                (cluster["name"],
                                 cluster["frontend"]["name"],
                                 pubip)
                    if len(clients) > 0:
                        frontend['cluster'] = clients[0]['cluster']
                    else:
                        frontend['cluster'] = frontend['name']
                    data += [frontend]
                    data += clients

                for index, anode in enumerate(data):
                    bnode = dict(anode)
                    if "interface" in bnode:
                        macs = []
                        # ips = []
                        for ipaddr in anode["interface"]:
                            macs.append(ipaddr["mac"])
                            # ips.append(ipaddr["ip"] or "N/A")
                        if format == 'table':
                            bnode["mac"] = "\n".join(macs)
                        else:
                            bnode["mac"] = ";".join(macs)

                        if "active_computeset_state" in anode and \
                                    anode["active_computeset"] is not None and \
                                    anode["active_computeset_state"] is not None:
                            if anode["active_computeset_state"] != 'running':
                                bnode["active_computeset"] = "%s(%s)" % \
                                                    (anode["active_computeset"],
                                                     anode["active_computeset_state"])
                            else:
                                if anode[
                                        "active_computeset"] in computeset_account:
                                    bnode["allocation"] = \
                                        computeset_account[anode["active_computeset"]]

                        if "compute_state" in anode:
                            bnode["admin_state"] = anode["compute_state"]
                        #anode["ip"] = "; ".join(ips)
                        if "ip" not in bnode:
                            bnode["ip"] = None

                    del bnode["interface"]
                    #
                    # stuck state included in cluster data via API
                    '''
                    if bnode["cluster"] in stuck_computesets and \
                                    bnode["name"] in stuck_computesets[bnode["cluster"]]:
                        bnode["active_computeset"] = \
                            stuck_computesets[bnode["cluster"]][bnode["name"]]
                    '''
                    data[index] = bnode

                sort_keys = ('cluster', 'mac')
                if sort:
                    if sort in Cluster.CLUSTER_SORT_KEY:
                        # print (sort)
                        idx = Cluster.CLUSTER_SORT_KEY.index(sort)
                        # print (idx)
                        sortkey = Cluster.CLUSTER_ORDER[idx]
                        # print (sortkey)
                        sort_keys = (sortkey, )
                        # print (sort_keys)
                if "table" == format:
                    result_print = Printer.write(data,
                                                 order=Cluster.CLUSTER_ORDER,
                                                 header=Cluster.CLUSTER_HEADER,
                                                 output=format,
                                                 sort_keys=sort_keys)
                    result += str(result_print)
                else:
                    result_print = Printer.write(data,
                                                 order=Cluster.CLUSTER_ORDER,
                                                 header=Cluster.CLUSTER_HEADER,
                                                 output=format,
                                                 sort_keys=sort_keys)
                    result = result_print
            return result
Exemple #24
0
    def attach_iso(iso_id_name, clusterid, nodeids=None, action='Attaching'):
        ret = ''
        # print ("Attaching ISO image")
        # print ("iso_id_name: %s" % iso_id_name)
        # print ("cluster: %s" % clusterid)
        # print ("node: %s" % nodeid)

        # obtain the list of iso first to support attach by index
        # and easy verification of image name
        isodict = {}
        isos = (Comet.list_iso())
        idx = 0
        for iso in isos:
            if iso.startswith("public/"):
                iso = iso.split("/")[1]
            idx += 1
            isodict[str(idx)] = iso

        if iso_id_name != '':
            if iso_id_name.isdigit():
                if iso_id_name in isodict:
                    iso_id_name = isodict[iso_id_name]
                else:
                    iso_id_name = None
            else:
                if not iso_id_name in list(isodict.values()):
                    iso_id_name = None

        if iso_id_name is not None:
            # currently only those in public directory is accessible
            # for empty string, DO NOT append as that is for detach
            if len(iso_id_name) != 0:
                iso_id_name = "public/{}".format(iso_id_name)
            urls = {}
            # attaching to compute node
            if nodeids:
                nodeids = hostlist.expand_hostlist(nodeids)
                for nodeid in nodeids:
                    url = Comet.url("cluster/{}/compute/{}/attach_iso?iso_name={}") \
                        .format(clusterid, nodeid, iso_id_name)
                    urls["Node {}".format(nodeid)] = url
            else:
                # attaching to fronend node
                url = Comet.url("cluster/{}/frontend/attach_iso?iso_name={}") \
                    .format(clusterid, iso_id_name)
                urls['Frontend'] = url
            # data = {"iso_name": "%s" % iso_id_name}
            # print ("url: %s" % url)
            # print ("data: %s" % data)
            tofrom = {}
            tofrom['Attaching'] = 'to'
            tofrom['Detaching'] = 'from'
            for node, url in urls.items():
                url = url.replace("+", "%2B")
                r = Comet.put(url)
                # print (r)
                if r is not None:
                    if '' != r.strip():
                        ret += r
                    else:
                        ret += "Request Accepted. {} the iso image {} {} of cluster {}\n" \
                            .format(action, tofrom[action], node, clusterid)
                else:
                    ret += "Something wrong during {} the iso image {} {} of cluster {}!" \
                           "Please check the command and try again\n" \
                        .format(action, tofrom[action], node, clusterid)
        else:
            ret = "ERROR: The specified index or image name not matching any from the ISO list!\n"\
                  "Check 'cm comet iso list'"
        return ret
Exemple #25
0
 def power(clusterid, subject, param=None, action=None):
     ret = ''
     # print ("clusterid: %s" % clusterid)
     # print ("subject: %s" % subject)
     # print ("param: %s" % param)
     # print ("action: %s" % action)
     if subject in ['HOSTS']:
         nodes_allocated = Cluster.check_nodes_computesets(clusterid, param)[1]
         if nodes_allocated:
             hosts_param = hostlist.expand_hostlist(param)
             # print (hosts_param)
             for host in hosts_param:
                 url = Comet.url("cluster/{}/compute/{}"
                                 .format(clusterid, host))
                 if action in ["on", "off"]:
                     action = "power{}".format(action)
                 puturl = "{}/{}".format(url, action)
                 # print (puturl)
                 r = Comet.put(puturl)
                 if r is not None:
                     if '' != r.strip():
                         ret = r
                     else:
                         ret += "Request Accepted. " \
                                "In the process of {} node {}\n" \
                             .format(action, host)
                 else:
                     ret += "Problem executing the request. " \
                            "Check if the node {} belongs to the cluster" \
                         .format(host)
                     # print(ret)
         else:
             Console.error("At least one specified node is not in any "
                           "active computesets thus cannot be powered on.\n"
                           "Please start the node(s) with 'comet start' first",
                           traceflag=False)
     elif 'FE' == subject:
         url = Comet.url("cluster/{}/frontend/".format(clusterid))
         if action in ["on", "off", "reboot", "reset", "shutdown"]:
             if action in ["on", "off"]:
                 action = "power{}".format(action)
             puturl = "{}{}".format(url, action)
             # print (puturl)
             r = Comet.put(puturl)
             if r is not None:
                 if '' != r.strip():
                     ret = r
                 else:
                     ret = "Request Accepted. " \
                           "In the process of {} the front-end node".format(action)
             else:
                 ret = "Problem executing the request. " \
                       "Check if the cluster exists"
         else:
             ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
     elif 'COMPUTESET' == subject:
         url = Comet.url("computeset/")
         if action in ["on", "off"]:
             ret = "Action NOT SUPPORTED! Use 'comet start/terminate'!"
         elif action in ["reboot", "reset", "shutdown"]:
             if action in ["off"]:
                 action = "power{}".format(action)
             puturl = "{:}{:}/{}".format(url, param, action)
             # print (puturl)
             r = Comet.put(puturl)
             if r is not None:
                 if '' != r.strip():
                     ret = r
                 else:
                     ret = "Request Accepted. " \
                           "In the process of {} the computeset".format(action)
             else:
                 ret = "Problem executing the request. " \
                       "Check if the computeset exists"
         else:
             ret = "Action not supported! Try these: reboot/reset/shutdown"
     return ret
Exemple #26
0
    def do_comet(self, args, arguments):
        """
        ::

            Usage:
               comet init
               comet active [ENDPOINT]
               comet ll [CLUSTERID] [--format=FORMAT] [--endpoint=ENDPOINT]
               comet cluster [CLUSTERID]
                             [--format=FORMAT]
                             [--sort=SORTKEY]
                             [--endpoint=ENDPOINT]
               comet computeset [COMPUTESETID]
                            [--allocation=ALLOCATION]
                            [--cluster=CLUSTERID]
                            [--state=COMPUTESESTATE]
                            [--endpoint=ENDPOINT]
               comet start CLUSTERID [--count=NUMNODES] [COMPUTENODEIDS]
                            [--allocation=ALLOCATION]
                            [--walltime=WALLTIME]
                            [--endpoint=ENDPOINT]
               comet terminate COMPUTESETID [--endpoint=ENDPOINT]
               comet power (on|off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
                            [--endpoint=ENDPOINT]
               comet console [--link] CLUSTERID [COMPUTENODEID]
                            [--endpoint=ENDPOINT]
               comet node info CLUSTERID [COMPUTENODEID] [--format=FORMAT]
                            [--endpoint=ENDPOINT]
               comet node rename CLUSTERID OLDNAMES NEWNAMES
                            [--endpoint=ENDPOINT]
               comet iso list [--endpoint=ENDPOINT]
               comet iso upload [--isoname=ISONAME] PATHISOFILE
                            [--endpoint=ENDPOINT]
               comet iso attach ISOIDNAME CLUSTERID [COMPUTENODEIDS]
                            [--endpoint=ENDPOINT]
               comet iso detach CLUSTERID [COMPUTENODEIDS]
                            [--endpoint=ENDPOINT]

            Options:
                --endpoint=ENDPOINT     Specify the comet nucleus service
                                        endpoint to work with, e.g., dev
                                        or production
                --format=FORMAT         Format is either table, json, yaml,
                                        csv, rest
                                        [default: table]
                --sort=SORTKEY          Sorting key for the table view
                --count=NUMNODES        Number of nodes to be powered on.
                                        When this option is used, the comet system
                                        will find a NUMNODES number of arbitrary nodes
                                        that are available to boot as a computeset
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --walltime=WALLTIME     Walltime requested for the node(s).
                                        Walltime could be an integer value followed
                                        by a unit (m, h, d, w, for minute, hour, day,
                                        and week, respectively). E.g., 3h, 2d
                --isoname=ISONAME       Name of the iso image after being stored remotely.
                                        If not specified, use the original filename
                --state=COMPUTESESTATE  List only computeset with the specified state.
                                        The state could be submitted, running, completed
                --link                  Whether to open the console url or just show the link

            Arguments:
                ENDPOINT        Service endpoint based on the yaml config file.
                                By default it's either dev or production.
                CLUSTERID       The assigned name of a cluster, e.g. vc1
                COMPUTESETID    An integer identifier assigned to a computeset
                COMPUTENODEID   A compute node name, e.g., vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                COMPUTENODEIDS  A set of compute node names in hostlist format,
                                e.g., vm-vc1-[0-3]
                                One single node is also acceptable: vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                NODESPARAM      Specifying the node/nodes/computeset to act on.
                                In case of integer, will be intepreted as a computesetid;
                                in case of a hostlist format, e.g., vm-vc1-[0-3], a group
                                of nodes; or a single host is also acceptable,
                                e.g., vm-vc1-0
                ISONAME         Name of an iso image at remote server
                ISOIDNAME       Index or name of an iso image at the remote server.
                                The index is based on the list from 'comet iso list'.
                PATHISOFILE     The full path to the iso image file to be uploaded
                OLDNAMES        The list of current node names to be renamed, in hostlist
                                format. A single host is also acceptable.
                NEWNAMES        The list of new names to rename to, in hostlist format.
                                A single host is also acceptable.
        """
        # back up of all the proposed commands/options
        """
               comet status
               comet tunnel start
               comet tunnel stop
               comet tunnel status
               comet logon
               comet logoff
               comet ll [CLUSTERID] [--format=FORMAT]
               comet docs
               comet info [--user=USER]
                            [--project=PROJECT]
                            [--format=FORMAT]
               comet cluster [CLUSTERID][--name=NAMES]
                            [--user=USER]
                            [--project=PROJECT]
                            [--hosts=HOSTS]
                            [--start=TIME_START]
                            [--end=TIME_END]
                            [--hosts=HOSTS]
                            [--format=FORMAT]
               comet computeset [COMPUTESETID]
               comet start ID
               comet stop ID
               comet power on CLUSTERID [NODESPARAM]
                            [--allocation=ALLOCATION]
                            [--walltime=WALLTIME]
               comet power (off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
               comet console CLUSTERID [COMPUTENODEID]
               comet delete [all]
                              [--user=USER]
                              [--project=PROJECT]
                              [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
                              [--host=HOST]
               comet delete --file=FILE
               comet update [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
               comet add [--user=USER]
                           [--project=PROJECT]
                           [--host=HOST]
                           [--description=DESCRIPTION]
                           [--start=TIME_START]
                           [--end=TIME_END]
                           NAME
               comet add --file=FILENAME

            Options:
                --user=USER           user name
                --name=NAMES          Names of the vcluster
                --start=TIME_START    Start time of the vcluster, in
                                      YYYY/MM/DD HH:MM:SS format.
                                      [default: 1901-01-01]
                --end=TIME_END        End time of the vcluster, in YYYY/MM/DD
                                      HH:MM:SS format. In addition a duratio
                                      can be specified if the + sign is the
                                      first sig The duration will than be
                                      added to the start time.
                                      [default: 2100-12-31]
                --project=PROJECT     project id
                --host=HOST           host name
                --description=DESCRIPTION  description summary of the vcluster
                --file=FILE           Adding multiple vclusters from one file
                --format=FORMAT       Format is either table, json, yaml,
                                      csv, rest
                                      [default: table]
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --walltime=WALLTIME     Walltime requested for the node(s)

            Arguments:
                FILENAME  the file to open in the cwd if . is
                          specified. If file in in cwd
                          you must specify it with ./FILENAME

            Opens the given URL in a browser window.
        """

        """
        if not arguments["tunnel"] and Comet.tunnelled and not Comet.is_tunnel():
            Console.error("Please establish a tunnel first with:")
            print
            print ("    comet tunnel start")
            print
            return ""

        try:

            if not arguments["tunnel"]:
                logon = Comet.logon()
                if logon is False:
                    Console.error("Could not logon")
                    return ""
        except:
            Console.error("Could not logon")
        # pprint (arguments)
        output_format = arguments["--format"] or "table"

        if arguments["status"]:

            Comet.state()

        elif arguments["tunnel"] and arguments["start"]:

            Comet.tunnel(True)

        elif arguments["tunnel"] and arguments["stop"]:

            Comet.tunnel(False)

        elif arguments["tunnel"] and arguments["status"]:

            Comet.state()

        elif arguments["logon"]:

            if self.context.comet_token is None:
                if Comet.logon():
                    Console.ok("logging on")
                    self.context.comet_token = Comet.token
                else:
                    Console.error("could not logon")
            else:
                Console.error("already logged on")

        elif arguments["logoff"]:

            if self.context.comet_token is None:
                Console.error("not logged in")
            else:
                if Comet.logoff():
                    Console.ok("Logging off")
                    self.context.comet_token = None
                else:
                    Console.error(
                        "some issue while logging off. Maybe comet not reachable")

        elif arguments["docs"]:

            Comet.docs()

        elif arguments["info"]:

            Console.error("not yet implemented")

        elif arguments["add"]:

            print ("add the cluster")

        elif arguments["start"]:

            cluster_id = arguments["ID"]
            print("start", cluster_id)
            Cluster.start(cluster_id)

        elif arguments["stop"]:

            cluster_id = arguments["ID"]
            print("stop", cluster_id)
            Cluster.stop(cluster_id)

        elif arguments["ll"]:

        """
        if arguments["init"]:
            print ("Initializing the comet configuration file...")
            config = ConfigDict("cloudmesh.yaml")
            # for unit testing only.
            cometConf = config["cloudmesh.comet"]
            endpoints = []
            # print (cometConf.keys())
            if "endpoints" in cometConf.keys():
                endpoints = cometConf["endpoints"].keys()
                if len(endpoints) < 1:
                    Console.error("No service endpoints available. "
                                  "Please check the config template",
                                  traceflag=False)
                    return ""
            if "username" in cometConf.keys():
                default_username = cometConf['username']
                # print (default_username)
                if 'TBD' == default_username:
                    set_default_user = \
                        input("Set a default username (RETURN to skip): ")
                    if set_default_user:
                        config.data["cloudmesh"]["comet"]["username"] = \
                            set_default_user
                        config.save()
                        Console.ok("Comet default username set!")
            if "active" in cometConf.keys():
                active_endpoint = cometConf['active']
                set_active_endpoint = \
                    input("Set the active service endpoint to use. "
                          "The availalbe endpoints are - %s [%s]: "
                          % ("/".join(endpoints),
                             active_endpoint)
                          )
                if set_active_endpoint:
                    if set_active_endpoint in endpoints:
                        config.data["cloudmesh"]["comet"]["active"] = \
                            set_active_endpoint
                        config.save()
                        Console.ok("Comet active service endpoint set!")
                    else:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s"
                                      % "/".join(endpoints),
                                      traceflag=False)

            if cometConf['active'] in endpoints:
                endpoint_url = cometConf["endpoints"] \
                    [cometConf['active']]["nucleus_base_url"]
                api_version = cometConf["endpoints"] \
                    [cometConf['active']]["api_version"]
                set_endpoint_url = \
                    input("Set the base url for the nucleus %s service [%s]: " \
                          % (cometConf['active'],
                             endpoint_url)
                          )
                if set_endpoint_url:
                    if set_endpoint_url != endpoint_url:
                        config.data["cloudmesh"]["comet"]["endpoints"] \
                            [cometConf['active']]["nucleus_base_url"] \
                            = set_endpoint_url
                        config.save()
                        Console.ok("Service base url set!")

                set_api_version = \
                    input("Set the api version for the nucleus %s service [%s]: " \
                          % (cometConf['active'],
                             api_version)
                          )
                if set_api_version:
                    if set_api_version != api_version:
                        config.data["cloudmesh"]["comet"]["endpoints"] \
                            [cometConf['active']]["api_version"] \
                            = set_api_version
                        config.save()
                        Console.ok("Service api version set!")
                print("Authenticating to the nucleus %s " \
                      "service and obtaining the apikey..." \
                      % cometConf['active'])
                Comet.get_apikey(cometConf['active'])

            return ''
            # Comet.get_apikey()
        if arguments["active"]:
            config = ConfigDict("cloudmesh.yaml")
            cometConf = config["cloudmesh.comet"]
            endpoint = arguments["ENDPOINT"] or None
            # parameter specified, intended to change
            if endpoint:
                if "endpoints" in cometConf.keys():
                    endpoints = cometConf["endpoints"].keys()
                    if endpoint in endpoints:
                        config.data["cloudmesh"] \
                                   ["comet"] \
                                   ["active"] = endpoint
                        config.save()
                        Console.ok("Comet active service endpoint set"
                                   " to: %s" % endpoint)
                    else:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s."
                                              % "/".join(endpoints),
                                     traceflag = False)
                else:
                    Console.error("No available endpoint to set. "
                                  "Check config file!",
                                  traceflag=False)
            else:
                if "active" in cometConf.keys():
                    active_endpoint = cometConf['active']
                    Console.ok("Current active service endpoint is: %s"
                                 % active_endpoint)
                else:
                    Console.error("Cannot set active endpoint. "
                                  "Check config file!",
                                  traceflag = False)
        try:
            endpoint = None
            config = ConfigDict("cloudmesh.yaml")
            cometConf = config["cloudmesh.comet"]
            if arguments["--endpoint"]:
                endpoint = arguments["--endpoint"]
                if "endpoints" in cometConf.keys():
                    endpoints = cometConf["endpoints"].keys()
                    if endpoint not in endpoints:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s."
                                              % "/".join(endpoints),
                                     traceflag = False)
                        return ''
            logon = Comet.logon(endpoint=endpoint)
            if logon is False:
                Console.error("Could not logon. Please try first:\n"
                              "cm comet init",
                              traceflag = False)
                return ""
        except:
            Console.error("Could not logon",
                          traceflag = False)

        output_format = arguments["--format"] or "table"

        if arguments["ll"]:
            cluster_id = arguments["CLUSTERID"] or None

            print(Cluster.simple_list(cluster_id, format=output_format))

        elif arguments["cluster"]:

            cluster_id = arguments["CLUSTERID"]
            sortkey = arguments["--sort"]
            print(Cluster.list(cluster_id, format=output_format, sort=sortkey))

        elif arguments["computeset"]:
            computeset_id = arguments["COMPUTESETID"] or None
            cluster = arguments["--cluster"] or None
            state = arguments["--state"] or None
            allocation = arguments["--allocation"] or None
            cluster = arguments["--cluster"] or None
            print (Cluster.computeset(computeset_id, cluster, state, allocation))
        elif arguments["start"]:
            clusterid = arguments["CLUSTERID"]
            numnodes = arguments["--count"] or None
            computenodeids = arguments["COMPUTENODEIDS"] or None

            # check allocation information for the cluster
            cluster = Cluster.list(clusterid, format='rest')
            try:
                allocations = cluster[0]['allocations']
            except:
                # print (cluster)
                Console.error("No allocation available for the specified cluster."\
                              "Please check with the comet help team",
                              traceflag=False)
                return ""

            # checking whether the computesetids is in valid hostlist format
            if computenodeids:
                try:
                    hosts_param = hostlist.expand_hostlist(computenodeids)
                except hostlist.BadHostlist:
                    Console.error("Invalid hosts list specified!",
                                  traceflag=False)
                    return ""
            elif numnodes:
                try:
                    param = int(numnodes)
                except ValueError:
                    Console.error("Invalid count value specified!",
                                  traceflag=False)
                    return ""
                if param <= 0:
                    Console.error("count value has to be greather than zero",
                                  traceflag=False)
                    return ""
                numnodes = param
            else:
                Console.error("You have to specify either the count of nodes, " \
                              "or the names of nodes in hostlist format",
                              traceflag=False)
                return ""

            walltime = arguments["--walltime"] or None
            allocation = arguments["--allocation"] or None

            # validating walltime and allocation parameters
            walltime = Cluster.convert_to_mins(walltime)
            if not walltime:
                print("No valid walltime specified. " \
                      "Using system default (2 days)")
            if not allocation:
                if len(allocations) == 1:
                    allocation = allocations[0]
                else:
                    allocation = Cluster.display_get_allocation(allocations)

            # issuing call to start a computeset with specified parameters
            print(Cluster.computeset_start(clusterid,
                                           computenodeids,
                                           numnodes,
                                           allocation,
                                           walltime)
                  )
        elif arguments["terminate"]:
            computesetid = arguments["COMPUTESETID"]
            print(Cluster.computeset_terminate(computesetid))
        elif arguments["power"]:
            clusterid = arguments["CLUSTERID"] or None
            fuzzyparam = arguments["NODESPARAM"] or None

            # parsing nodesparam for proper action
            if fuzzyparam:
                try:
                    param = int(fuzzyparam)
                    subject = 'COMPUTESET'
                except ValueError:
                    param = fuzzyparam
                    try:
                        hosts_param = hostlist.expand_hostlist(fuzzyparam)
                        subject = 'HOSTS'
                    except hostlist.BadHostlist:
                        Console.error("Invalid hosts list specified!",
                                      traceflag=False)
                        return ""
            else:
                subject = 'FE'
                param = None

            if arguments["on"]:
                action = "on"
            elif arguments["off"]:
                action = "off"
            elif arguments["reboot"]:
                action = "reboot"
            elif arguments["reset"]:
                action = "reset"
            elif arguments["shutdown"]:
                action = "shutdown"
            else:
                action = None
            print (Cluster.power(clusterid,
                                subject,
                                param,
                                action)
                  )
        elif arguments["console"]:
            clusterid = arguments["CLUSTERID"]
            linkonly = False
            if arguments["--link"]:
                linkonly = True
            nodeid = None
            if 'COMPUTENODEID' in arguments:
                nodeid = arguments["COMPUTENODEID"]
            Comet.console(clusterid, nodeid, linkonly)
        elif arguments["iso"]:
            if arguments["list"]:
                isos = (Comet.list_iso())
                idx = 0
                for iso in isos:
                    if iso.startswith("public/"):
                        iso = iso.split("/")[1]
                    idx += 1
                    print ("{}: {}".format(idx, iso))
            if arguments["upload"]:
                isofile = arguments["PATHISOFILE"]
                isofile = os.path.abspath(isofile)
                if os.path.isfile(isofile):
                    if arguments["--isoname"]:
                        filename = arguments["--isoname"]
                    else:
                        filename = os.path.basename(isofile)
                else:
                    print ("File does not exist - {}" \
                          .format(arguments["PATHISOFILE"]))
                    return ""
                print(Comet.upload_iso(filename, isofile))
            elif arguments["attach"]:
                isoidname = arguments["ISOIDNAME"]
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print(Cluster.attach_iso(isoidname, clusterid, computenodeids))
            elif arguments["detach"]:
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print(Cluster.detach_iso(clusterid, computenodeids))
        elif arguments["node"]:
            if arguments["info"]:
                clusterid = arguments["CLUSTERID"]
                nodeid = arguments["COMPUTENODEID"]
                print (Cluster.node_info(clusterid, nodeid=nodeid, format=output_format))
            elif arguments["rename"]:
                clusterid = arguments["CLUSTERID"]
                oldnames = Parameter.expand(arguments["OLDNAMES"])
                newnames = Parameter.expand(arguments["NEWNAMES"])
                if len(oldnames) != len(newnames):
                    Console.error("Length of OLDNAMES and NEWNAMES have to be the same",
                                  traceflag=False)
                    return ""
                else:
                    for newname in newnames:
                        if newname.strip() == "":
                            Console.error("Newname cannot be empty string",
                                          traceflag=False)
                            return ""
                    cluster_data = Cluster.list(clusterid, format="rest")
                    if len(cluster_data) > 0:
                        computes = cluster_data[0]["computes"]
                        nodenames = [x["name"] for x in computes]
                    else:
                        Console.error("Error obtaining the cluster information",
                                      traceflag=False)
                        return ""
                    # check if new names ar not already taken
                    # to be implemented
                    # print (oldnames)
                    # print (newnames)
                    # print (nodenames)
                    oldset = set(oldnames)
                    newset = set(newnames)
                    currentset = set(nodenames)
                    # at least one OLDNAME does not exist
                    if  not oldset <= currentset:
                        Console.error("Not all OLDNAMES are valid", traceflag=False)
                        return ""
                    else:
                        # those unchanged nodes
                        keptset = currentset - oldset
                        # duplication between name of unchanged nodes and
                        # the requested NEWNAMES
                        if keptset & newset != set():
                            Console.error("Not proceeding as otherwise introducing "\
                                          "duplicated names",
                                          traceflag=False)
                        else:
                            for i in range(0,len(oldnames)):
                                oldname = oldnames[i]
                                newname = newnames[i]
                                print ("%s -> %s" % (oldname, newname))
                            confirm = input("Confirm batch renaming (Y/y to confirm, "\
                                            "any other key to abort):")
                            if confirm.lower() == 'y':
                                print ("Conducting batch renaming")
                                for i in range(0,len(oldnames)):
                                    oldname = oldnames[i]
                                    newname = newnames[i]
                                    print (Cluster.rename_node(clusterid,
                                                               oldname,
                                                               newname))
                            else:
                                print ("Action aborted!")

        return ""
Exemple #27
0
def comet_status(request):
    # noinspection PyUnusedLocal
    c = comet_logon(request)
    data = json.loads(Cluster.simple_list(format="json"))
    # pprint(data)
    # print (type(data))

    clusters = []
    for key in data:
        total = data[key]["nodes"]
        name = data[key]["name"]
        if name == "comet-fe1":
            name = "free"
        cluster = {
            "name": name,
            "total": total,
            "status": {
                'active': 0,
                'nostate': 0,
                'down': 0,
                'pending': 0,
                'unkown': total
            }
        }
        clusters.append(cluster)

    details = json.loads(Cluster.list(format="json"))

    counter = {}
    for node in list(details.values()):
        clustername = node["cluster"]
        if clustername is not None:
            if clustername not in counter:
                counter[clustername] = {
                    'name': None,
                    'total': 0,
                    'status': {
                        'unkown': 0,
                        'active': 0,
                        'down': 0,
                        'pending': 0,
                        'nostate': 0,
                        'nostate-error': 0
                    }
                }

    # print (counter)
    for key, node in list(details.items()):

        if node['kind'] == 'compute':

            name = node['cluster']
            state = node['state']

            if state in [None, 'None']:
                state = 'unkown'

            # print ("SSSSSSS", state, name, node['kind'])
            counter[name]['status'][state] += 1
            counter[name]['total'] += 1
            counter[name]['name'] = name
    pprint(counter)
    #
    # delete the free nodes for now
    #
    if 'comet-fe1' in counter:
        for count in counter:
            if count != "comet-fe1":
                counter['comet-fe1']['total'] = \
                    counter['comet-fe1']['total'] - counter[count]['total']

        counter['comet-fe1']['name'] = 'free'
    counter_list = []
    for key, cluster in list(counter.items()):
        counter_list.append(cluster)

    # context["clusters"] = counter_list

    Chart.cluster_overview_pie(counter_list, filename='pie.svg')

    #
    # delete the overall count
    #
    if 'comet-fe1' in counter:
        del counter['comet-fe1']
    counter_list = []
    for key, cluster in list(counter.items()):
        counter_list.append(cluster)

    Chart.cluster_overview_pie_vector(counter_list, filename='pie_vector.svg')
    Chart.cluster_overview_radar(counter_list, filename='radar.svg')

    context = {
        'pid': str(Comet.find_tunnel()),
        'tunnel': str(Comet.is_tunnel()),
        'title': "Comet Status"
    }

    return render(request,
                  'cloudmesh_portal_comet/status.jinja',
                  context)
Exemple #28
0
    def simple_list(id=None, format="table"):
        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            if r is None:
                Console.error("Could not find cluster `{}`"
                              .format(id))
                return result
            r = [r]

        if r is not None:
            if 'error' in r:
                Console.error("An error occurred: {error}".format(**r))
                raise ValueError("COMET Error")
            elif 'error' in r[0]:
                Console.error("An error occurred: {error}".format(**r[0]))
                raise ValueError("COMET Error")

            if format == "rest":
                result = r
            else:
                elements = {}
                for cluster in r:
                    element = {}
                    for attribute in ["project", "name", "description"]:
                        element[attribute] = cluster[attribute]
                        element["nodes"] = len(cluster["computes"])
                    for attribute in cluster["frontend"].keys():
                        element["frontend " + attribute] = cluster["frontend"][
                            attribute]
                    names = []
                    for compute in cluster["computes"]:
                        names.append(compute["name"])

                    element["computes"] = hostlist.collect_hostlist(names)

                    elements[cluster["name"]] = element

                result = dict_printer(elements,
                                      order=[
                                          "name",
                                          "project",
                                          "nodes",
                                          "computes",
                                          "frontend name",
                                          "frontend state",
                                          "frontend type",
                                          "description",
                                      ],
                                      header=[
                                          "Name",
                                          "Project",
                                          "Count",
                                          "Nodes",
                                          "Frontend (Fe)",
                                          "State (Fe)",
                                          "Type (Fe)",
                                          "Description",
                                      ],

                                      output=format)
            return result
Exemple #29
0
 def stop(id):
     data = {"id": id}
     r = requests.post(Comet.url("cluster/{id}/stop".format(**data)))
     print(r)
Exemple #30
0
    def power(clusterid, subject, param=None, action=None,
              allocation=None, walltime=None, numnodes=None):

        # print("SUBJECT to perform action on: {}".format(subject))
        # print("\ton cluster: {}".format(clusterid))
        # print("\tAction: {}".format(action))
        # print("\tParameter: {}".format(param))
        # print("\tAllocation: {}".format(allocation))
        # print("\tWalltime: {}".format(walltime))

        # the API is now accepting hostlist format directly
        # computeIdsHostlist = hostlist.collect_hostlist(computeids)
        # print (computeIdsHostlist)
        ret = ''

        #
        # Now it accepts
        # {"cluster":"vc3","computes":"compute[1-2]"},
        # {"cluster":"vc3","computes":["compute1","compute2"]} and
        # {"cluster":"vc3","count":2}
        #
        if subject in ['HOSTS', 'HOST']:
            # power on N arbitrary nodes
            if numnodes:
                if "on" == action:
                    if not allocation:
                        cluster = Cluster.list(clusterid, format='rest')
                        # use the first one if no provided
                        allocation = cluster[0]['allocations'][0]
                    if not walltime:
                        walltime = Cluster.WALLTIME_MINS

                    posturl = Comet.url("computeset/")
                    data = {"cluster":"%s" % clusterid,
                            "count": "%s" % numnodes,
                            "walltime_mins": "%s" % walltime,
                            "allocation": "%s" % allocation}

                    r = Comet.post(posturl, data=data)
                    # print("RETURNED RESULTS:")
                    # print (r)
                    if 'cluster' in r:
                        if 'state' in r and \
                           ('queued' == r['state'] or \
                           'submitted' == r['state'] or \
                           'created' == r['state']):
                            computesetid = r['id']
                            ret = 'Request accepted! Check status with:\n' \
                                  'comet cluster {}\n'.format(clusterid) + \
                                  'or:\n' \
                                  'comet computeset {}\n'.format(computesetid)
                        else:
                            # in case of some internal problem
                            ret = ''
                    elif 'error' in r:
                        ret = "An error occurred: {}".format(r['error'])
                    else:
                        ret = "An internal error occured. "\
                              "Please submit a ticket with the "\
                              "following info:\n {}\n"\
                              .format(r)
                # cannot power off or reboot N arbitrary nodes
                else:
                    ret = "Action NOT SUPPORTED! Try with explicit "\
                          "node name(s) or computeset id"
            # parse based on NODEPARAM parameter
            # could be computeset id; front end; or hostlist named compute nodes
            else:
                hosts_param = hostlist.expand_hostlist(param)
                hosts_param_set = set(hosts_param)
                nodes_free = True
                nodes_allocated = False
                nodes_checked = False
                # computesetid = -1
                computesets = Comet.get_computeset()
                for computeset in computesets:
                    if computeset["cluster"] == clusterid \
                            and (computeset["state"] == "started" or
                                 computeset["state"] == "running"):
                        computesetid = computeset["id"]
                        # print (computesetid)
                        hosts = set()
                        for compute in computeset["computes"]:
                            hosts.add(compute["name"])
                        # print (hosts)
                        if hosts_param_set <= hosts:
                            nodes_allocated = True
                            nodes_free = False
                            nodes_checked = True
                        # at least one specified host not in any Active computeset
                        else:
                            for host in hosts_param:
                                # some specified nodes are in Active computeset
                                if host in hosts:
                                    nodes_free = False
                                    nodes_checked = True
                                    break
                    # a cluster could have multiple 'started' set
                    if nodes_checked:
                        break
                # print ("nodes_checked: %s" % nodes_checked)
                # print ("nodes_allocated: %s" % nodes_allocated)
                # print ("nodes_free: %s" % nodes_free)
                if not (nodes_free or nodes_allocated):
                    ret = "Error: Some nodes are already in active computesets"
                else:
                    if "on" == action:
                        if not allocation:
                            cluster = Cluster.list(clusterid, format='rest')
                            # use the first one if no provided
                            allocation = cluster[0]['allocations'][0]
                        if not walltime:
                            walltime = Cluster.WALLTIME_MINS

                        data = {"computes": "%s" % param,
                                "cluster": "%s" % clusterid,
                                "walltime_mins": "%s" % walltime,
                                "allocation": "%s" % allocation}

                        if nodes_free:
                            # print("Issuing request to poweron nodes...")
                            url = Comet.url("computeset/")
                            posturl = url
                            # print (data)

                            r = Comet.post(posturl, data=data)
                            # print("RETURNED RESULTS:")
                            # print (r)
                            if 'cluster' in r:
                                if 'state' in r and \
                                   ('queued' == r['state'] or 'submitted' == r['state']):
                                    computesetid = r['id']
                                    ret = 'Request accepted! Check status with:\n' \
                                          'comet cluster {}\n'.format(clusterid) + \
                                          'or:\n' \
                                          'comet computeset {}\n'.format(computesetid)
                                else:
                                    # in case of some internal problem
                                    ret = ''
                            elif 'error' in r:
                                ret = "An error occurred: {}".format(r['error'])
                            else:
                                ret = "An internal error occured. "\
                                      "Please submit a ticket with the "\
                                      "following info:\n {}\n"\
                                      .format(r)
                        elif nodes_allocated:
                            ret = ""
                            for host in hosts_param:
                                url = Comet.url("cluster/{}/compute/{}/"
                                                .format(clusterid, host))
                                action = "poweron"
                                puturl = "{}{}".format(url, action)
                                # print (puturl)
                                r = Comet.put(puturl)
                                if r is not None:
                                    if '' != r.strip():
                                        ret += r
                                    else:
                                        ret += "Requeset Accepted. "\
                                               "In the process of power on node {}\n"\
                                               .format(host)
                                else:
                                    ret += "Problem executing the request. "\
                                        "Check if the node {} belongs to the cluster"\
                                        .format(host)
                        # print(ret)
                    elif action in ["off", "reboot", "reset", "shutdown"]:
                        if action in ["off"]:
                            action = "power{}".format(action)
                        if nodes_allocated:
                            ret = ""
                            for host in hosts_param:
                                url = Comet.url("cluster/{}/compute/{}/"
                                                .format(clusterid, host))
                                puturl = "{}{}".format(url, action)
                                # print (puturl)
                                r = Comet.put(puturl)
                                if r is not None:
                                    if '' != r.strip():
                                        ret = r
                                    else:
                                        ret += "Requeset Accepted. "\
                                            "In the process of {} node {}\n"\
                                            .format(action, host)
                                else:
                                    ret += "Problem executing the request. "\
                                        "Check if the node {} belongs to the cluster"\
                                        .format(host)
                        elif nodes_free:
                            ret = "Error: The specified nodes are "\
                                  "not in active computesets"
                    else:
                        ret = "Action not supported! Try these: "\
                              "on/off/reboot/reset/shutdown"
        elif 'FE' == subject:
            url = Comet.url("cluster/{}/frontend/".format(clusterid))
            if action in ["on", "off", "reboot", "reset", "shutdown"]:
                if action in ["on", "off"]:
                    action = "power{}".format(action)
                puturl = "{}{}".format(url, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the front-end node".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the cluster exists"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        elif 'COMPUTESET' == subject:
            url = Comet.url("computeset/")
            if 'on' == action:
                ret = "NOT SUPPORTED! Use hostslist to specify the hosts to power on!"
            elif action in ["off", "reboot", "reset", "shutdown"]:
                if action in ["off"]:
                    action = "power{}".format(action)
                puturl = "{:}{:}/{}".format(url, param, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the nodes".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the computeset exists"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        '''
        elif 'HOST' == subject:
            computesets = Comet.get_computeset()
            pprint (computesets)
            for computeset in computesets:
                if computeset["cluster"] == clusterid \
                        and (computeset["state"] == "started" \
                             or computeset["state"] == "running"):
                    computesetid = computeset["id"]
                    # print (computesetid)
                    hosts = set()
                    for compute in computeset["computes"]:
                        hosts.add(compute["name"])
                    # print (hosts)
                    is_valid_set = True
                    hostsparam = Parameter.expand(param)
                    for host in hostsparam:
                        if host not in hosts:
                            is_valid_set = False
                            break 
            url = Comet.url("cluster/{}/compute/{}/".format(clusterid, param))
            if action in ["on", "off", "reboot", "reset", "shutdown"]:
                if action in ["on", "off"]:
                    action = "power{}".format(action)
                puturl = "{}{}".format(url, action)
                # print (puturl)
                r = Comet.put(puturl)
                if r is not None:
                    if '' != r.strip():
                        ret = r
                    else:
                        ret = "Requeset Accepted. "\
                              "In the process of {} the nodes".format(action)
                else:
                    ret = "Problem executing the request. "\
                          "Check if the node belongs to the cluster"
            else:
                ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
        '''
        # """
        return ret
Exemple #31
0
 def power(clusterid, subject, param=None, action=None):
     ret = ''
     # print ("clusterid: %s" % clusterid)
     # print ("subject: %s" % subject)
     # print ("param: %s" % param)
     # print ("action: %s" % action)
     if subject in ['HOSTS']:
         nodes_allocated = Cluster.check_nodes_computesets(
             clusterid, param)[1]
         if nodes_allocated:
             hosts_param = hostlist.expand_hostlist(param)
             # print (hosts_param)
             for host in hosts_param:
                 url = Comet.url("cluster/{}/compute/{}".format(
                     clusterid, host))
                 if action in ["on", "off"]:
                     action = "power{}".format(action)
                 puturl = "{}/{}".format(url, action)
                 # print (puturl)
                 r = Comet.put(puturl)
                 if r is not None:
                     if '' != r.strip():
                         ret = r
                     else:
                         ret += "Request Accepted. " \
                                "In the process of {} node {}\n" \
                             .format(action, host)
                 else:
                     ret += "Problem executing the request. " \
                            "Check if the node {} belongs to the cluster" \
                         .format(host)
                     # print(ret)
         else:
             Console.error(
                 "At least one specified node is not in any "
                 "active computesets thus cannot be powered on.\n"
                 "Please start the node(s) with 'comet start' first",
                 traceflag=False)
     elif 'FE' == subject:
         url = Comet.url("cluster/{}/frontend/".format(clusterid))
         if action in ["on", "off", "reboot", "reset", "shutdown"]:
             if action in ["on", "off"]:
                 action = "power{}".format(action)
             puturl = "{}{}".format(url, action)
             # print (puturl)
             r = Comet.put(puturl)
             if r is not None:
                 if '' != r.strip():
                     ret = r
                 else:
                     ret = "Request Accepted. " \
                           "In the process of {} the front-end node".format(action)
             else:
                 ret = "Problem executing the request. " \
                       "Check if the cluster exists"
         else:
             ret = "Action not supported! Try these: on/off/reboot/reset/shutdown"
     elif 'COMPUTESET' == subject:
         url = Comet.url("computeset/")
         if action in ["on", "off"]:
             ret = "Action NOT SUPPORTED! Use 'comet start/terminate'!"
         elif action in ["reboot", "reset", "shutdown"]:
             if action in ["off"]:
                 action = "power{}".format(action)
             puturl = "{:}{:}/{}".format(url, param, action)
             # print (puturl)
             r = Comet.put(puturl)
             if r is not None:
                 if '' != r.strip():
                     ret = r
                 else:
                     ret = "Request Accepted. " \
                           "In the process of {} the computeset".format(action)
             else:
                 ret = "Problem executing the request. " \
                       "Check if the computeset exists"
         else:
             ret = "Action not supported! Try these: reboot/reset/shutdown"
     return ret
Exemple #32
0
    def do_comet(self, args, arguments):
        """
        ::

            Usage:
               comet status
               comet tunnel start
               comet tunnel stop
               comet tunnel status
               comet logon
               comet logoff
               comet ll [CLUSTERID] [--format=FORMAT]
               comet docs
               comet info [--user=USER]
                            [--project=PROJECT]
                            [--format=FORMAT]
               comet cluster [CLUSTERID][--name=NAMES]
                            [--user=USER]
                            [--project=PROJECT]
                            [--hosts=HOSTS]
                            [--start=TIME_START]
                            [--end=TIME_END]
                            [--hosts=HOSTS]
                            [--format=FORMAT]
               comet computeset [COMPUTESETID]
               comet start ID
               comet stop ID
               comet power (on|off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
               comet console CLUSTERID [COMPUTENODEID]
               comet delete [all]
                              [--user=USER]
                              [--project=PROJECT]
                              [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
                              [--host=HOST]
               comet delete --file=FILE
               comet update [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
               comet add [--user=USER]
                           [--project=PROJECT]
                           [--host=HOST]
                           [--description=DESCRIPTION]
                           [--start=TIME_START]
                           [--end=TIME_END]
                           NAME
               comet add --file=FILENAME

            Options:
                --user=USER           user name
                --name=NAMES          Names of the vcluster
                --start=TIME_START    Start time of the vcluster, in
                                      YYYY/MM/DD HH:MM:SS format.
                                      [default: 1901-01-01]
                --end=TIME_END        End time of the vcluster, in YYYY/MM/DD
                                      HH:MM:SS format. In addition a duratio
                                      can be specified if the + sign is the
                                      first sig The duration will than be
                                      added to the start time.
                                      [default: 2100-12-31]
                --project=PROJECT     project id
                --host=HOST           host name
                --description=DESCRIPTION  description summary of the vcluster
                --file=FILE           Adding multiple vclusters from one file
                --format=FORMAT       Format is either table, json, yaml,
                                      csv, rest
                                      [default: table]

            Arguments:
                FILENAME  the file to open in the cwd if . is
                          specified. If file in in cwd
                          you must specify it with ./FILENAME

            Opens the given URL in a browser window.
        """

        if not arguments["tunnel"] and Comet.tunnelled and not Comet.is_tunnel():
            Console.error("Please establish a tunnel first with:")
            print
            print ("    comet tunnel start")
            print
            return ""

        try:

            if not arguments["tunnel"]:
                logon = Comet.logon()
                if logon is False:
                    Console.error("Could not logon")
                    return ""
        except:
            Console.error("Could not logon")
        # pprint (arguments)
        output_format = arguments["--format"] or "table"

        if arguments["status"]:

            Comet.state()

        elif arguments["tunnel"] and arguments["start"]:

            Comet.tunnel(True)

        elif arguments["tunnel"] and arguments["stop"]:

            Comet.tunnel(False)

        elif arguments["tunnel"] and arguments["status"]:

            Comet.state()

        elif arguments["logon"]:

            if self.context.comet_token is None:
                if Comet.logon():
                    Console.ok("logging on")
                    self.context.comet_token = Comet.token
                else:
                    Console.error("could not logon")
            else:
                Console.error("already logged on")

        elif arguments["logoff"]:

            if self.context.comet_token is None:
                Console.error("not logged in")
            else:
                if Comet.logoff():
                    Console.ok("Logging off")
                    self.context.comet_token = None
                else:
                    Console.error(
                        "some issue while logging off. Maybe comet not reachable")

        elif arguments["ll"]:

            cluster_id = arguments["CLUSTERID"] or None

            print(Cluster.simple_list(cluster_id, format=output_format))

        elif arguments["docs"]:

            Comet.docs()

        elif arguments["cluster"]:

            cluster_id = arguments["CLUSTERID"]
            print(Cluster.list(cluster_id, format=output_format))

        elif arguments["computeset"]:
            computeset_id = arguments["COMPUTESETID"]
            print (Cluster.computeset(computeset_id))

        elif arguments["info"]:

            Console.error("not yet implemented")

        elif arguments["add"]:

            print ("add the cluster")

        elif arguments["start"]:

            cluster_id = arguments["ID"]
            print("start", cluster_id)
            Cluster.start(cluster_id)

        elif arguments["stop"]:

            cluster_id = arguments["ID"]
            print("stop", cluster_id)
            Cluster.stop(cluster_id)

        elif arguments["power"]:

            clusterid = arguments["CLUSTERID"]
            fuzzyparam = None
            if 'NODESPARAM' in arguments:
                fuzzyparam = arguments["NODESPARAM"]
            param = fuzzyparam

            # no nodes param provided, action on front end
            if not fuzzyparam:
                subject = "FE"
                param = None
            # parse the nodes param
            else:
                try:
                    param = int(fuzzyparam)
                    subject = "COMPUTESET"
                    param = str(param)
                except ValueError:
                    if '[' in fuzzyparam and ']' in fuzzyparam:
                        subject = "HOSTS"
                    else:
                        subject = "HOST"

            if arguments["on"]:
                action = "on"
            elif arguments["off"]:
                action = "off"
            elif arguments["reboot"]:
                action = "reboot"
            elif arguments["reset"]:
                action = "reset"
            elif arguments["shutdown"]:
                action = "shutdown"
            else:
                action = None
            Cluster.power(clusterid, subject, param, action)
        elif arguments["console"]:
            clusterid = arguments["CLUSTERID"]
            nodeid = None
            if 'COMPUTENODEID' in arguments:
                nodeid = arguments["COMPUTENODEID"]
            Comet.console(clusterid, nodeid)
        return ""
Exemple #33
0
    def simple_list(id=None, format="table"):
        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            if r is None:
                Console.error("Could not find cluster `{}`".format(id))
                return result
            r = [r]

        if r is not None:
            if 'error' in r:
                Console.error("An error occurred: {error}".format(**r))
                raise ValueError("COMET Error")
            elif 'error' in r[0]:
                Console.error("An error occurred: {error}".format(**r[0]))
                raise ValueError("COMET Error")

            if format == "rest":
                result = r
            else:
                elements = {}
                for cluster in r:
                    element = {}
                    for attribute in ["project", "name", "description"]:
                        element[attribute] = cluster[attribute]
                        element["nodes"] = len(cluster["computes"])
                    for attribute in cluster["frontend"]:
                        element["frontend " +
                                attribute] = cluster["frontend"][attribute]
                    names = []
                    for compute in cluster["computes"]:
                        names.append(compute["name"])

                    element["computes"] = hostlist.collect_hostlist(names)

                    elements[cluster["name"]] = element

                result = Printer.write(elements,
                                       order=[
                                           "name",
                                           "project",
                                           "nodes",
                                           "computes",
                                           "frontend name",
                                           "frontend state",
                                           "frontend type",
                                           "description",
                                       ],
                                       header=[
                                           "Name",
                                           "Project",
                                           "Count",
                                           "Nodes",
                                           "Frontend (Fe)",
                                           "State (Fe)",
                                           "Type (Fe)",
                                           "Description",
                                       ],
                                       output=format)
            return result
Exemple #34
0
    def list(id=None, format="table", sort=None):

        def check_for_error(r):
            if r is not None:
                if 'error' in r:
                    Console.error("An error occurred: {error}".format(**r))
                    raise ValueError("COMET Error")

        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
            check_for_error(r)
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            check_for_error(r)
            if r is None:
                Console.error("Could not find cluster `{}`"
                              .format(id))
                return result
            r = [r]
        #
        # stuck state included in cluster data via API
        '''
        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format(id, computeset["state"])
        '''
        #
        # getting account/allocation for each computeset
        # to display in the cluster view
        computeset_account = {}
        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                id = computeset["id"]
                account = computeset["account"]
                if id not in computeset_account:
                    computeset_account[id] = account

        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format (id, computeset["state"])

        if r is not None:
            if format == "rest":
                result = r
            else:
                result = ''
                data = []

                empty = {
                    'cluster': None,
                    'cpus': None,
                    'host': None,
                    "mac": None,
                    'ip': None,
                    'memory': None,
                    'disksize': None,
                    'name': None,
                    'state': None,
                    'type': None,
                    'active_computeset': None,
                    'kind': 'frontend',
                    'admin_state': None
                }

                for cluster in sorted(r, key=lambda x: x["name"]):

                    clients = cluster["computes"]
                    for client in clients:
                        client["kind"] = "compute"
                    frontend = dict(empty)
                    frontend.update(cluster["frontend"])
                    pubip = cluster["frontend"]["pub_ip"]
                    frontend["ip"] = pubip
                    frontend["admin_state"] = cluster["frontend"]["frontend_state"]
                    result += "Cluster: %s\tFrontend: %s\tIP: %s\n" % \
                                (cluster["name"],
                                 cluster["frontend"]["name"],
                                 pubip)
                    if len(clients) > 0:
                        frontend['cluster'] = clients[0]['cluster']
                    else:
                        frontend['cluster'] = frontend['name']
                    data += [frontend]
                    data += clients

                for index, anode in enumerate(data):
                    bnode = dict(anode)
                    if "interface" in bnode:
                        macs = []
                        # ips = []
                        for ipaddr in anode["interface"]:
                            macs.append(ipaddr["mac"])
                            # ips.append(ipaddr["ip"] or "N/A")
                        if format == 'table':
                            bnode["mac"] = "\n".join(macs)
                        else:
                            bnode["mac"] = ";".join(macs)

                        if "active_computeset_state" in anode and \
                                    anode["active_computeset"] is not None and \
                                    anode["active_computeset_state"] is not None:
                            if anode["active_computeset_state"] != 'running':
                                bnode["active_computeset"] = "%s(%s)" % \
                                                    (anode["active_computeset"],
                                                     anode["active_computeset_state"])
                            else:
                                if anode["active_computeset"] in computeset_account:
                                    bnode["allocation"] = \
                                        computeset_account[anode["active_computeset"]]

                        if "compute_state" in anode:
                            bnode["admin_state"] = anode["compute_state"]
                        #anode["ip"] = "; ".join(ips)
                        if "ip" not in bnode:
                            bnode["ip"] = None

                    del bnode["interface"]
                    #
                    # stuck state included in cluster data via API
                    '''
                    if bnode["cluster"] in stuck_computesets and \
                                    bnode["name"] in stuck_computesets[bnode["cluster"]]:
                        bnode["active_computeset"] = \
                            stuck_computesets[bnode["cluster"]][bnode["name"]]
                    '''
                    data[index] = bnode

                sort_keys = ('cluster','mac')
                if sort:
                    if sort in Cluster.CLUSTER_SORT_KEY:
                        # print (sort)
                        idx = Cluster.CLUSTER_SORT_KEY.index(sort)
                        # print (idx)
                        sortkey = Cluster.CLUSTER_ORDER[idx]
                        # print (sortkey)
                        sort_keys = (sortkey,)
                        # print (sort_keys)
                if "table" == format:
                    result_print = Printer.write(data,
                                                 order=Cluster.CLUSTER_ORDER,
                                                 header=Cluster.CLUSTER_HEADER,
                                                 output=format,
                                                 sort_keys=sort_keys)
                    result += str(result_print)
                else:
                    result_print = Printer.write(data,
                                                 order=Cluster.CLUSTER_ORDER,
                                                 header=Cluster.CLUSTER_HEADER,
                                                 output=format,
                                                 sort_keys=sort_keys)
                    result = result_print
            return result
Exemple #35
0
    def attach_iso(iso_id_name, clusterid, nodeids=None, action='Attaching'):
        ret = ''
        # print ("Attaching ISO image")
        # print ("iso_id_name: %s" % iso_id_name)
        # print ("cluster: %s" % clusterid)
        # print ("node: %s" % nodeid)

        # obtain the list of iso first to support attach by index
        # and easy verification of image name
        isodict = {}
        isos = (Comet.list_iso())
        idx = 0
        for iso in isos:
            if iso.startswith("public/"):
                iso = iso.split("/")[1]
            idx += 1
            isodict[str(idx)] = iso

        if iso_id_name != '':
            if iso_id_name.isdigit():
                if iso_id_name in isodict:
                    iso_id_name = isodict[iso_id_name]
                else:
                    iso_id_name = None
            else:
                if not iso_id_name in list(isodict.values()):
                    iso_id_name = None

        if iso_id_name is not None:
            iso_id_name = "public/{}".format(iso_id_name)
            urls = {}
            # attaching to compute node
            if nodeids:
                nodeids = hostlist.expand_hostlist(nodeids)
                for nodeid in nodeids:
                    url = Comet.url("cluster/{}/compute/{}/attach_iso?iso_name={}") \
                        .format(clusterid, nodeid, iso_id_name)
                    urls["Node {}".format(nodeid)] = url
            else:
                # attaching to fronend node
                url = Comet.url("cluster/{}/frontend/attach_iso?iso_name={}") \
                    .format(clusterid, iso_id_name)
                urls['Frontend'] = url
            # data = {"iso_name": "%s" % iso_id_name}
            # print ("url: %s" % url)
            # print ("data: %s" % data)
            tofrom = {}
            tofrom['Attaching'] = 'to'
            tofrom['Detaching'] = 'from'
            for node, url in urls.items():
                r = Comet.put(url)
                # print (r)
                if r is not None:
                    if '' != r.strip():
                        ret += r
                    else:
                        ret += "Request Accepted. {} the iso image {} {} of cluster {}\n" \
                            .format(action, tofrom[action], node, clusterid)
                else:
                    ret += "Something wrong during {} the iso image {} {} of cluster {}!" \
                           "Please check the command and try again\n" \
                        .format(action, tofrom[action], node, clusterid)
        else:
            ret = "ERROR: The specified index or image name not matching any from the ISO list!\n"\
                  "Check 'cm comet iso list'"
        return ret
Exemple #36
0
    def list(id=None, format="table"):

        def check_for_error(r):
            if r is not None:
                if 'error' in r:
                    Console.error("An error occurred: {error}".format(**r))
                    raise ValueError("COMET Error")

        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
            check_for_error(r)
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            check_for_error(r)
            if r is None:
                Console.error("Could not find cluster `{}`"
                              .format(id))
                return result
            r = [r]

        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format (id, computeset["state"])

        if r is not None:
            if format == "rest":
                result = r
            else:

                data = []

                empty = {
                    'cluster': None,
                    'cpus': None,
                    'host': None,
                    "mac": None,
                    'ip': None,
                    'memory': None,
                    'disksize': None,
                    'name': None,
                    'state': None,
                    'type': None,
                    'computeset': None,
                    'kind': 'frontend'
                }

                for cluster in r:

                    clients = cluster["computes"]
                    for client in clients:
                        client["kind"] = "compute"
                    frontend = dict(empty)
                    frontend.update(cluster["frontend"])
                    if len(clients) > 0:
                        frontend['cluster'] = clients[0]['cluster']
                    else:
                        frontend['cluster'] = frontend['name']
                    data += [frontend]
                    data += clients

                for index, anode in enumerate(data):
                    bnode = dict(anode)
                    if "interface" in bnode:
                        macs = []
                        #ips = []
                        for ipaddr in anode["interface"]:
                            macs.append(ipaddr["mac"])
                            #ips.append(ipaddr["ip"] or "N/A")
                        if format=='table':
                            bnode["mac"] = "\n".join(macs)
                        else:
                            bnode["mac"] = ";".join(macs)
                        #anode["ip"] = "; ".join(ips)
                    del bnode["interface"]

                    if bnode["cluster"] in stuck_computesets and \
                            bnode["name"] in stuck_computesets[bnode["cluster"]]:
                        bnode["active_computeset"] = \
                            stuck_computesets[bnode["cluster"]][bnode["name"]]
                    data[index] = bnode

                result = list_printer(data,
                                      order=[
                                          "name",
                                          "state",
                                          "kind",
                                          "type",
                                          "mac",
                                          #"ip",
                                          "cpus",
                                          "cluster",
                                          "memory",
                                          "disksize",
                                          "active_computeset"
                                      ],
                                      header=[
                                          "name",
                                          "state",
                                          "kind",
                                          "type",
                                          "mac",
                                          "cpus",
                                          "cluster",
                                          "RAM(M)",
                                          "disk(G)",
                                          "computeset"
                                      ],
                                      output=format,
                                      sort_keys=('cluster','mac'))
            return result
Exemple #37
0
    def list(id=None, format="table"):

        def check_for_error(r):
            if r is not None:
                if 'error' in r:
                    Console.error("An error occurred: {error}".format(**r))
                    raise ValueError("COMET Error")

        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
            check_for_error(r)
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            check_for_error(r)
            if r is None:
                Console.error("Could not find cluster `{}`"
                              .format(id))
                return result
            r = [r]

        if r is not None:
            if format == "rest":
                result = r
            else:

                data = []

                empty = {
                    'cluster': None,
                    'cpus': None,
                    'host': None,
                    "mac": None,
                    'ip': None,
                    'memory': None,
                    'name': None,
                    'state': None,
                    'type': None,
                    'kind': 'frontend'
                }

                for cluster in r:

                    clients = cluster["computes"]
                    for client in clients:
                        client["kind"] = "compute"
                    frontend = dict(empty)
                    frontend.update(cluster["frontend"])
                    data += [frontend]
                    data += clients

                for anode in data:
                    for attribute in anode.keys():
                        if "interface" == attribute:
                            macs = []
                            #ips = []
                            for ipaddr in anode["interface"]:
                                macs.append(ipaddr["mac"])
                                #ips.append(ipaddr["ip"] or "N/A")
                            anode["mac"] = "; ".join(macs)
                            #anode["ip"] = "; ".join(ips)
                    del anode["interface"]

                result = list_printer(data,
                                      order=[
                                          "name",
                                          "state",
                                          "kind",
                                          "type",
                                          "mac",
                                          #"ip",
                                          "cpus",
                                          "cluster",
                                          "memory",
                                      ],
                                      output=format)
            return result
Exemple #38
0
    def computeset_start(clusterid,
                         computenodeids=None,
                         numnodes=None,
                         allocation=None,
                         walltime=None):
        ret = ''
        # print ("clusterid: %s" % clusterid)
        # print ("computenodeids: %s" % computenodeids)
        # print ("numnodes: %s" % numnodes)
        # print ("allocation: %s" % allocation)
        # print ("walltime: %s" % walltime)
        data = None
        # validating and initiating allocation and walltime values
        if not allocation:
            cluster = Cluster.list(clusterid, format='rest')
            # use the first one if no provided
            allocation = cluster[0]['allocations'][0]
        if not walltime:
            walltime = Cluster.WALLTIME_MINS

        # preparing parameters for the post call
        # num of nodes to start
        #
        # Now it accepts
        # {"cluster":"vc3","computes":"compute[1-2]"},
        # {"cluster":"vc3","computes":["compute1","compute2"]} and
        # {"cluster":"vc3","count":2}
        #
        if numnodes:
            data = {"cluster": "%s" % clusterid,
                    "count": "%s" % numnodes,
                    "walltime_mins": "%s" % walltime,
                    "allocation": "%s" % allocation}
        # computenodeids in hostlist format
        elif computenodeids:
            nodes_free = Cluster.check_nodes_computesets(clusterid, computenodeids)[0]
            if not nodes_free:
                Console.error("Some nodes are already in active computesets",
                              traceflag=False)
                return ret
            else:
                data = {"computes": "%s" % computenodeids,
                        "cluster": "%s" % clusterid,
                        "walltime_mins": "%s" % walltime,
                        "allocation": "%s" % allocation}

        # print("Issuing request to poweron nodes...")
        posturl = Comet.url("computeset/")
        # print ("POST data: %s" % data)
        r = Comet.post(posturl, data=data)
        # print("RETURNED RESULTS:")
        # print (r)
        if 'cluster' in r:
            if 'state' in r and \
                            r['state'] in Cluster.PENDING_COMPUTESETS:
                computesetid = r['id']
                ret = 'Request accepted! Check status with:\n' \
                      'comet cluster {}\n'.format(clusterid) + \
                      'or:\n' \
                      'comet computeset {}\n'.format(computesetid)
            else:
                # in case of some internal problem
                ret = ''
        elif 'error' in r:
            ret = "An error occurred: {}".format(r['error'])
        else:
            ret = "An internal error occured. " \
                  "Please submit a ticket with the " \
                  "following info:\n {}\n" \
                .format(r)
        return ret
Exemple #39
0
def comet_status(request):
    # noinspection PyUnusedLocal
    c = comet_logon(request)
    data = json.loads(Cluster.simple_list(format="json"))
    # pprint(data)
    # print (type(data))

    clusters = []
    for key in data:
        total = data[key]["nodes"]
        name = data[key]["name"]
        if name == "comet-fe1":
            name = "free"
        cluster = {
            "name": name,
            "total": total,
            "status": {
                'active': 0,
                'nostate': 0,
                'down': 0,
                'pending': 0,
                'unkown': total
            }
        }
        clusters.append(cluster)

    details = json.loads(Cluster.list(format="json"))

    counter = {}
    for node in details.values():
        clustername = node["cluster"]
        if clustername is not None:
            if clustername not in counter:
                counter[clustername] = {
                    'name': None,
                    'total': 0,
                    'status': {
                        'unkown': 0,
                        'active': 0,
                        'down': 0,
                        'pending': 0,
                        'nostate': 0,
                        'nostate-error': 0
                    }
                }

    # print (counter)
    for key, node in details.items():

        if node['kind'] == 'compute':

            name = node['cluster']
            state = node['state']

            if state in [None, 'None']:
                state = 'unkown'

            # print ("SSSSSSS", state, name, node['kind'])
            counter[name]['status'][state] += 1
            counter[name]['total'] += 1
            counter[name]['name'] = name
    pprint(counter)
    #
    # delete the free nodes for now
    #
    if 'comet-fe1' in counter:
        for count in counter:
            if count != "comet-fe1":
                counter['comet-fe1']['total'] = \
                    counter['comet-fe1']['total'] - counter[count]['total']

        counter['comet-fe1']['name'] = 'free'
    counter_list = []
    for key, cluster in counter.items():
        counter_list.append(cluster)

    # context["clusters"] = counter_list

    Chart.cluster_overview_pie(counter_list, filename='pie.svg')

    #
    # delete the overall count
    #
    if 'comet-fe1' in counter:
        del counter['comet-fe1']
    counter_list = []
    for key, cluster in counter.items():
        counter_list.append(cluster)

    Chart.cluster_overview_pie_vector(counter_list, filename='pie_vector.svg')
    Chart.cluster_overview_radar(counter_list, filename='radar.svg')

    context = {
        'pid': str(Comet.find_tunnel()),
        'tunnel': str(Comet.is_tunnel()),
        'title': "Comet Status"
    }

    return render(request, 'cloudmesh_portal/comet/status.jinja', context)
Exemple #40
0
    def do_comet(self, args, arguments):
        """
        ::

            Usage:
               comet init
               comet ll [CLUSTERID] [--format=FORMAT]
               comet cluster [CLUSTERID]
                             [--format=FORMAT]
               comet computeset [COMPUTESETID]
                            [--allocation=ALLOCATION]
                            [--cluster=CLUSTERID]
                            [--state=COMPUTESESTATE]
               comet start CLUSTERID [--count=NUMNODES] [COMPUTENODEIDS]
                            [--allocation=ALLOCATION]
                            [--walltime=WALLTIME]
               comet terminate COMPUTESETID
               comet power (on|off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
               comet console CLUSTERID [COMPUTENODEID]
               comet iso list
               comet iso upload [--isoname=ISONAME] PATHISOFILE
               comet iso attach ISONAME CLUSTERID [COMPUTENODEIDS]
               comet iso detach CLUSTERID [COMPUTENODEIDS]
               comet node rename CLUSTERID OLDNAME NEWNAME

            Options:
                --format=FORMAT         Format is either table, json, yaml,
                                        csv, rest
                                        [default: table]
                --count=NUMNODES        Number of nodes to be powered on.
                                        When this option is used, the comet system
                                        will find a NUMNODES number of arbitrary nodes
                                        that are available to boot as a computeset
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --walltime=WALLTIME     Walltime requested for the node(s).
                                        Walltime could be an integer value followed
                                        by a unit (m, h, d, w, for minute, hour, day,
                                        and week, respectively). E.g., 3h, 2d
                --isoname=ISONAME       Name of the iso image after being stored remotely.
                                        If not specified, use the original filename
                --state=COMPUTESESTATE  List only computeset with the specified state.
                                        The state could be submitted, running, completed

            Arguments:
                CLUSTERID       The assigned name of a cluster, e.g. vc1
                COMPUTESETID    An integer identifier assigned to a computeset
                COMPUTENODEID   A compute node name, e.g., vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                COMPUTENODEIDS  A set of compute node names in hostlist format,
                                e.g., vm-vc1-[0-3]
                                One single node is also acceptable: vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                NODESPARAM      Specifying the node/nodes/computeset to act on.
                                In case of integer, will be intepreted as a computesetid;
                                in case of a hostlist format, e.g., vm-vc1-[0-3], a group
                                of nodes; or a single host is also acceptable,
                                e.g., vm-vc1-0
                ISONAME         Name of an iso image at remote server
                PATHISOFILE     The full path to the iso image file to be uploaded
        """
        # back up of all the proposed commands/options
        """
               comet status
               comet tunnel start
               comet tunnel stop
               comet tunnel status
               comet logon
               comet logoff
               comet ll [CLUSTERID] [--format=FORMAT]
               comet docs
               comet info [--user=USER]
                            [--project=PROJECT]
                            [--format=FORMAT]
               comet cluster [CLUSTERID][--name=NAMES]
                            [--user=USER]
                            [--project=PROJECT]
                            [--hosts=HOSTS]
                            [--start=TIME_START]
                            [--end=TIME_END]
                            [--hosts=HOSTS]
                            [--format=FORMAT]
               comet computeset [COMPUTESETID]
               comet start ID
               comet stop ID
               comet power on CLUSTERID [NODESPARAM]
                            [--allocation=ALLOCATION]
                            [--walltime=WALLTIME]
               comet power (off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
               comet console CLUSTERID [COMPUTENODEID]
               comet delete [all]
                              [--user=USER]
                              [--project=PROJECT]
                              [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
                              [--host=HOST]
               comet delete --file=FILE
               comet update [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
               comet add [--user=USER]
                           [--project=PROJECT]
                           [--host=HOST]
                           [--description=DESCRIPTION]
                           [--start=TIME_START]
                           [--end=TIME_END]
                           NAME
               comet add --file=FILENAME

            Options:
                --user=USER           user name
                --name=NAMES          Names of the vcluster
                --start=TIME_START    Start time of the vcluster, in
                                      YYYY/MM/DD HH:MM:SS format.
                                      [default: 1901-01-01]
                --end=TIME_END        End time of the vcluster, in YYYY/MM/DD
                                      HH:MM:SS format. In addition a duratio
                                      can be specified if the + sign is the
                                      first sig The duration will than be
                                      added to the start time.
                                      [default: 2100-12-31]
                --project=PROJECT     project id
                --host=HOST           host name
                --description=DESCRIPTION  description summary of the vcluster
                --file=FILE           Adding multiple vclusters from one file
                --format=FORMAT       Format is either table, json, yaml,
                                      csv, rest
                                      [default: table]
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --walltime=WALLTIME     Walltime requested for the node(s)

            Arguments:
                FILENAME  the file to open in the cwd if . is
                          specified. If file in in cwd
                          you must specify it with ./FILENAME

            Opens the given URL in a browser window.
        """

        """
        if not arguments["tunnel"] and Comet.tunnelled and not Comet.is_tunnel():
            Console.error("Please establish a tunnel first with:")
            print
            print ("    comet tunnel start")
            print
            return ""

        try:

            if not arguments["tunnel"]:
                logon = Comet.logon()
                if logon is False:
                    Console.error("Could not logon")
                    return ""
        except:
            Console.error("Could not logon")
        # pprint (arguments)
        output_format = arguments["--format"] or "table"

        if arguments["status"]:

            Comet.state()

        elif arguments["tunnel"] and arguments["start"]:

            Comet.tunnel(True)

        elif arguments["tunnel"] and arguments["stop"]:

            Comet.tunnel(False)

        elif arguments["tunnel"] and arguments["status"]:

            Comet.state()

        elif arguments["logon"]:

            if self.context.comet_token is None:
                if Comet.logon():
                    Console.ok("logging on")
                    self.context.comet_token = Comet.token
                else:
                    Console.error("could not logon")
            else:
                Console.error("already logged on")

        elif arguments["logoff"]:

            if self.context.comet_token is None:
                Console.error("not logged in")
            else:
                if Comet.logoff():
                    Console.ok("Logging off")
                    self.context.comet_token = None
                else:
                    Console.error(
                        "some issue while logging off. Maybe comet not reachable")

        elif arguments["docs"]:

            Comet.docs()

        elif arguments["info"]:

            Console.error("not yet implemented")

        elif arguments["add"]:

            print ("add the cluster")

        elif arguments["start"]:

            cluster_id = arguments["ID"]
            print("start", cluster_id)
            Cluster.start(cluster_id)

        elif arguments["stop"]:

            cluster_id = arguments["ID"]
            print("stop", cluster_id)
            Cluster.stop(cluster_id)

        elif arguments["ll"]:

        """
        if arguments["init"]:
            print ("Initializing the comet configuration file...")
            config = ConfigDict("cloudmesh.yaml")
            # for unit testing only.
            cometConf = config["cloudmesh.comet"]
            endpoints = []
            # print (cometConf.keys())
            if "endpoints" in cometConf.keys():
                endpoints = cometConf["endpoints"].keys()
                if len(endpoints) < 1:
                    Console.error("No service endpoints available."\
                                  " Please check the config template")
                    return ""
            if "username" in cometConf.keys():
                default_username = cometConf['username']
                # print (default_username)
                if 'TBD' == default_username:
                    set_default_user = \
                        input("Set a default username (RETURN to skip): ")
                    if set_default_user:
                        config.data["cloudmesh"]["comet"]["username"] = \
                            set_default_user
                        config.save()
                        Console.ok("Comet default username set!")
            if "active" in cometConf.keys():
                active_endpoint = cometConf['active']
                set_active_endpoint = \
                        input("Set the active service endpoint to use. "\
                                  "The availalbe endpoints are - %s [%s]: "\
                                   % ("/".join(endpoints),
                                     active_endpoint)
                                 )
                if set_active_endpoint:
                    if set_active_endpoint in endpoints:
                        config.data["cloudmesh"]["comet"]["active"] = \
                                        set_active_endpoint
                        config.save()
                        Console.ok("Comet active service endpoint set!")
                    else:
                        Console.error("The provided endpoint does not match any "\
                                      "available service endpoints. Try %s" \
                                      % "/".join(endpoints) )

            if cometConf['active'] in endpoints:
                endpoint_url = cometConf["endpoints"]\
                               [cometConf['active']]["nucleus_base_url"]
                api_version = cometConf["endpoints"]\
                               [cometConf['active']]["api_version"]
                set_endpoint_url = \
                        input("Set the base url for the nucleus %s service [%s]: "\
                                   % (cometConf['active'],
                                      endpoint_url)
                                 )
                if set_endpoint_url:
                    if set_endpoint_url != endpoint_url:
                        config.data["cloudmesh"]["comet"]["endpoints"]\
                                    [cometConf['active']]["nucleus_base_url"]\
                                    = set_endpoint_url
                        config.save()
                        Console.ok("Service base url set!")

                set_api_version = \
                        input("Set the api version for the nucleus %s service [%s]: "\
                                   % (cometConf['active'],
                                   api_version)
                                 )
                if set_api_version:
                    if set_api_version != api_version:
                        config.data["cloudmesh"]["comet"]["endpoints"]\
                                    [cometConf['active']]["api_version"]\
                                    = set_api_version
                        config.save()
                        Console.ok("Service api version set!")
                print ("Authenticating to the nucleus %s "\
                       "service and obtaining the apikey..." \
                       % cometConf['active'])
                Comet.get_apikey(cometConf['active'])

            return ''
            #Comet.get_apikey()
        try:
            logon = Comet.logon()
            if logon is False:
                Console.error("Could not logon. Please try first:\ncm comet init")
                return ""
        except:
            Console.error("Could not logon")

        output_format = arguments["--format"] or "table"

        if arguments["ll"]:
            cluster_id = arguments["CLUSTERID"] or None

            print(Cluster.simple_list(cluster_id, format=output_format))

        elif arguments["cluster"]:

            cluster_id = arguments["CLUSTERID"]
            print(Cluster.list(cluster_id, format=output_format))

        elif arguments["computeset"]:
            computeset_id = arguments["COMPUTESETID"] or None
            cluster = arguments["--cluster"] or None
            state = arguments["--state"] or None
            allocation = arguments["--allocation"] or None
            cluster = arguments["--cluster"] or None
            print (Cluster.computeset(computeset_id, cluster, state, allocation))
        elif arguments["start"]:
            clusterid = arguments["CLUSTERID"]
            numnodes = arguments["--count"] or None
            computenodeids = arguments["COMPUTENODEIDS"] or None

            # check allocation information for the cluster
            cluster = Cluster.list(clusterid, format='rest')
            try:
                allocations = cluster[0]['allocations']
            except:
                # print (cluster)
                Console.error("No allocation available for the specified cluster." \
                              "Please check with the comet help team")
                return ""

            # checking whether the computesetids is in valid hostlist format
            if computenodeids:
                try:
                    hosts_param = hostlist.expand_hostlist(computenodeids)
                except hostlist.BadHostlist:
                    Console.error("Invalid hosts list specified!",
                                  traceflag=False)
                    return ""
            elif numnodes:
                try:
                    param = int(numnodes)
                except ValueError:
                    Console.error("Invalid count value specified!", traceflag=False)
                    return ""
                if param <= 0:
                    Console.error("count value has to be greather than zero")
                    return ""
                numnodes = param
            else:
                Console.error("You have to specify either the count of nodes, " \
                              "or the names of nodes in hostlist format")
                return ""

            walltime = arguments["--walltime"] or None
            allocation = arguments["--allocation"] or None

            # validating walltime and allocation parameters
            walltime = Cluster.convert_to_mins(walltime)
            if not walltime:
                print ("No valid walltime specified. "\
                       "Using system default (2 days)")
            if not allocation:
                if len(allocations) == 1:
                    allocation = allocations[0]
                else:
                    allocation = Cluster.display_get_allocation(allocations)

            # issuing call to start a computeset with specified parameters
            print (Cluster.computeset_start(clusterid,
                                            computenodeids,
                                            numnodes,
                                            allocation,
                                            walltime)
                  )
        elif arguments["terminate"]:
            computesetid = arguments["COMPUTESETID"]
            print (Cluster.computeset_terminate(computesetid))
        elif arguments["power"]:
            clusterid = arguments["CLUSTERID"] or None
            fuzzyparam = arguments["NODESPARAM"] or None

            # parsing nodesparam for proper action
            if fuzzyparam:
                try:
                    param = int(fuzzyparam)
                    subject = 'COMPUTESET'
                except ValueError:
                    param = fuzzyparam
                    try:
                        hosts_param = hostlist.expand_hostlist(fuzzyparam)
                        subject = 'HOSTS'
                    except hostlist.BadHostlist:
                        Console.error("Invalid hosts list specified!",
                                      traceflag=False)
                        return ""
            else:
                subject = 'FE'
                param = None

            if arguments["on"]:
                action = "on"
            elif arguments["off"]:
                action = "off"
            elif arguments["reboot"]:
                action = "reboot"
            elif arguments["reset"]:
                action = "reset"
            elif arguments["shutdown"]:
                action = "shutdown"
            else:
                action = None
            print (Cluster.power(clusterid,
                                 subject,
                                 param,
                                 action)
                  )
        elif arguments["console"]:
            clusterid = arguments["CLUSTERID"]
            nodeid = None
            if 'COMPUTENODEID' in arguments:
                nodeid = arguments["COMPUTENODEID"]
            Comet.console(clusterid, nodeid)
        elif arguments["iso"]:
            if arguments["list"]:
                isos = (Comet.list_iso())
                idx = 0
                for iso in isos:
                    if iso.startswith("public/"):
                        iso = iso.split("/")[1]
                    idx += 1
                    print ("{}: {}".format(idx, iso))
            if arguments["upload"]:
                isofile = arguments["PATHISOFILE"]
                isofile = os.path.abspath(isofile)
                if os.path.isfile(isofile):
                    if arguments["--isoname"]:
                        filename = arguments["--isoname"]
                    else:
                        filename = os.path.basename(isofile)
                else:
                    print ("File does not exist - {}"\
                                  .format(arguments["PATHISOFILE"]))
                    return ""
                print (Comet.upload_iso(filename, isofile))
            elif arguments["attach"]:
                isoname = arguments["ISONAME"]
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print (Cluster.attach_iso(isoname, clusterid, computenodeids))
            elif arguments["detach"]:
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print (Cluster.detach_iso(clusterid, computenodeids))
        elif arguments["node"]:
            if arguments["rename"]:
                clusterid = arguments["CLUSTERID"]
                oldname = arguments["OLDNAME"]
                newname = arguments["NEWNAME"]
                if newname is None or newname == '':
                    print ("New node name cannot be empty")
                else:
                    print (Cluster.rename_node(clusterid, oldname, newname))

            '''
            # bulk rename

            if arguments["rename"]:
               oldnames = Parameter.expand(arguments["OLDNAME"])
               newnames = Parameter.expand(arguments["NEWNAME"])

               # check if new names ar not already taken
               # to be implemented

               if len(oldnames) == len(newnames):
                   for i in range(0,len(oldnames)):
                       oldname = oldnames[i]
                       newname = newnames[i]
                   if newname is None or newname == '':
                       print ("New node name cannot be empty")
                   else:
                       print (Cluster.rename_node(clusterid, oldname, newname))
            '''

        return ""
Exemple #41
0
    def do_comet(self, args, arguments):
        """
        ::

            Usage:
               comet init
               comet active [ENDPOINT]
               comet ll [CLUSTERID] [--format=FORMAT] [--endpoint=ENDPOINT]
               comet cluster [--concise|--status] [CLUSTERID]
                             [--format=FORMAT]
                             [--sort=SORTKEY]
                             [--endpoint=ENDPOINT]
               comet computeset [COMPUTESETID]
                            [--allocation=ALLOCATION]
                            [--cluster=CLUSTERID]
                            [--state=COMPUTESESTATE]
                            [--endpoint=ENDPOINT]
               comet start CLUSTERID [--count=NUMNODES] [COMPUTENODEIDS]
                            [--allocation=ALLOCATION]
                            [--reservation=RESERVATION]
                            [--walltime=WALLTIME]
                            [--endpoint=ENDPOINT]
               comet terminate COMPUTESETID [--endpoint=ENDPOINT]
               comet power (on|off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
                            [--endpoint=ENDPOINT]
               comet console [--link] CLUSTERID [COMPUTENODEID]
                            [--endpoint=ENDPOINT]
               comet node info CLUSTERID [COMPUTENODEID] [--format=FORMAT]
                            [--endpoint=ENDPOINT]
               comet node rename CLUSTERID OLDNAMES NEWNAMES
                            [--endpoint=ENDPOINT]
               comet iso list [--endpoint=ENDPOINT]
               comet iso upload [--isoname=ISONAME] PATHISOFILE
                            [--endpoint=ENDPOINT]
               comet iso attach ISOIDNAME CLUSTERID [COMPUTENODEIDS]
                            [--endpoint=ENDPOINT]
               comet iso detach CLUSTERID [COMPUTENODEIDS]
                            [--endpoint=ENDPOINT]
               comet reservation (list|create|update|delete)

            Options:
                --endpoint=ENDPOINT     Specify the comet nucleus service
                                        endpoint to work with, e.g., dev
                                        or production
                --format=FORMAT         Format is either table, json, yaml,
                                        csv, rest
                                        [default: table]
                --sort=SORTKEY          Sorting key for the table view
                --count=NUMNODES        Number of nodes to be powered on.
                                        When this option is used, the comet system
                                        will find a NUMNODES number of arbitrary nodes
                                        that are available to boot as a computeset
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --reservation=RESERVATION   Submit the request to an existing reservation
                --walltime=WALLTIME     Walltime requested for the node(s).
                                        Walltime could be an integer value followed
                                        by a unit (m, h, d, w, for minute, hour, day,
                                        and week, respectively). E.g., 3h, 2d
                --isoname=ISONAME       Name of the iso image after being stored remotely.
                                        If not specified, use the original filename
                --state=COMPUTESESTATE  List only computeset with the specified state.
                                        The state could be submitted, running, completed
                --link                  Whether to open the console url or just show the link
                --concise               Concise table view for cluster info
                --status                Cluster table view displays only those columns showing state of nodes

            Arguments:
                ENDPOINT        Service endpoint based on the yaml config file.
                                By default it's either dev or production.
                CLUSTERID       The assigned name of a cluster, e.g. vc1
                COMPUTESETID    An integer identifier assigned to a computeset
                COMPUTENODEID   A compute node name, e.g., vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                COMPUTENODEIDS  A set of compute node names in hostlist format,
                                e.g., vm-vc1-[0-3]
                                One single node is also acceptable: vm-vc1-0
                                If not provided, the requested action will be taken
                                on the frontend node of the specified cluster
                NODESPARAM      Specifying the node/nodes/computeset to act on.
                                In case of integer, will be intepreted as a computesetid;
                                in case of a hostlist format, e.g., vm-vc1-[0-3], a group
                                of nodes; or a single host is also acceptable,
                                e.g., vm-vc1-0
                ISONAME         Name of an iso image at remote server
                ISOIDNAME       Index or name of an iso image at the remote server.
                                The index is based on the list from 'comet iso list'.
                PATHISOFILE     The full path to the iso image file to be uploaded
                OLDNAMES        The list of current node names to be renamed, in hostlist
                                format. A single host is also acceptable.
                NEWNAMES        The list of new names to rename to, in hostlist format.
                                A single host is also acceptable.
        """
        # back up of all the proposed commands/options
        """
               comet status
               comet tunnel start
               comet tunnel stop
               comet tunnel status
               comet logon
               comet logoff
               comet ll [CLUSTERID] [--format=FORMAT]
               comet docs
               comet info [--user=USER]
                            [--project=PROJECT]
                            [--format=FORMAT]
               comet cluster [CLUSTERID][--name=NAMES]
                            [--user=USER]
                            [--project=PROJECT]
                            [--hosts=HOSTS]
                            [--start=TIME_START]
                            [--end=TIME_END]
                            [--hosts=HOSTS]
                            [--format=FORMAT]
               comet computeset [COMPUTESETID]
               comet start ID
               comet stop ID
               comet power on CLUSTERID [NODESPARAM]
                            [--allocation=ALLOCATION]
                            [--walltime=WALLTIME]
               comet power (off|reboot|reset|shutdown) CLUSTERID [NODESPARAM]
               comet console CLUSTERID [COMPUTENODEID]
               comet delete [all]
                              [--user=USER]
                              [--project=PROJECT]
                              [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
                              [--host=HOST]
               comet delete --file=FILE
               comet update [--name=NAMES]
                              [--hosts=HOSTS]
                              [--start=TIME_START]
                              [--end=TIME_END]
               comet add [--user=USER]
                           [--project=PROJECT]
                           [--host=HOST]
                           [--description=DESCRIPTION]
                           [--start=TIME_START]
                           [--end=TIME_END]
                           NAME
               comet add --file=FILENAME

            Options:
                --user=USER           user name
                --name=NAMES          Names of the vcluster
                --start=TIME_START    Start time of the vcluster, in
                                      YYYY/MM/DD HH:MM:SS format.
                                      [default: 1901-01-01]
                --end=TIME_END        End time of the vcluster, in YYYY/MM/DD
                                      HH:MM:SS format. In addition a duratio
                                      can be specified if the + sign is the
                                      first sig The duration will than be
                                      added to the start time.
                                      [default: 2100-12-31]
                --project=PROJECT     project id
                --host=HOST           host name
                --description=DESCRIPTION  description summary of the vcluster
                --file=FILE           Adding multiple vclusters from one file
                --format=FORMAT       Format is either table, json, yaml,
                                      csv, rest
                                      [default: table]
                --allocation=ALLOCATION     Allocation to charge when power on
                                            node(s)
                --walltime=WALLTIME     Walltime requested for the node(s)

            Arguments:
                FILENAME  the file to open in the cwd if . is
                          specified. If file in in cwd
                          you must specify it with ./FILENAME

            Opens the given URL in a browser window.
        """

        """
        if not arguments["tunnel"] and Comet.tunnelled and not Comet.is_tunnel():
            Console.error("Please establish a tunnel first with:")
            print
            print ("    comet tunnel start")
            print
            return ""

        try:

            if not arguments["tunnel"]:
                logon = Comet.logon()
                if logon is False:
                    Console.error("Could not logon")
                    return ""
        except:
            Console.error("Could not logon")
        # pprint (arguments)
        output_format = arguments["--format"] or "table"

        if arguments["status"]:

            Comet.state()

        elif arguments["tunnel"] and arguments["start"]:

            Comet.tunnel(True)

        elif arguments["tunnel"] and arguments["stop"]:

            Comet.tunnel(False)

        elif arguments["tunnel"] and arguments["status"]:

            Comet.state()

        elif arguments["logon"]:

            if self.context.comet_token is None:
                if Comet.logon():
                    Console.ok("logging on")
                    self.context.comet_token = Comet.token
                else:
                    Console.error("could not logon")
            else:
                Console.error("already logged on")

        elif arguments["logoff"]:

            if self.context.comet_token is None:
                Console.error("not logged in")
            else:
                if Comet.logoff():
                    Console.ok("Logging off")
                    self.context.comet_token = None
                else:
                    Console.error(
                        "some issue while logging off. Maybe comet not reachable")

        elif arguments["docs"]:

            Comet.docs()

        elif arguments["info"]:

            Console.error("not yet implemented")

        elif arguments["add"]:

            print ("add the cluster")

        elif arguments["start"]:

            cluster_id = arguments["ID"]
            print("start", cluster_id)
            Cluster.start(cluster_id)

        elif arguments["stop"]:

            cluster_id = arguments["ID"]
            print("stop", cluster_id)
            Cluster.stop(cluster_id)

        elif arguments["ll"]:

        """
        if arguments["init"]:
            print ("Initializing the comet configuration file...")
            config = ConfigDict("cloudmesh.yaml")
            # for unit testing only.
            cometConf = config["cloudmesh.comet"]
            endpoints = []
            # print (cometConf.keys())
            if "endpoints" in cometConf.keys():
                endpoints = cometConf["endpoints"].keys()
                if len(endpoints) < 1:
                    Console.error("No service endpoints available. "
                                  "Please check the config template",
                                  traceflag=False)
                    return ""
            if "username" in cometConf.keys():
                default_username = cometConf['username']
                # print (default_username)
                if 'TBD' == default_username:
                    set_default_user = \
                        input("Set a default username (RETURN to skip): ")
                    if set_default_user:
                        config.data["cloudmesh"]["comet"]["username"] = \
                            set_default_user
                        config.save()
                        Console.ok("Comet default username set!")
            if "active" in cometConf.keys():
                active_endpoint = cometConf['active']
                set_active_endpoint = \
                    input("Set the active service endpoint to use. "
                          "The availalbe endpoints are - %s [%s]: "
                          % ("/".join(endpoints),
                             active_endpoint)
                          )
                if set_active_endpoint:
                    if set_active_endpoint in endpoints:
                        config.data["cloudmesh"]["comet"]["active"] = \
                            set_active_endpoint
                        config.save()
                        Console.ok("Comet active service endpoint set!")
                    else:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s"
                                      % "/".join(endpoints),
                                      traceflag=False)

            if cometConf['active'] in endpoints:
                endpoint_url = cometConf["endpoints"] \
                    [cometConf['active']]["nucleus_base_url"]
                api_version = cometConf["endpoints"] \
                    [cometConf['active']]["api_version"]
                set_endpoint_url = \
                    input("Set the base url for the nucleus %s service [%s]: " \
                          % (cometConf['active'],
                             endpoint_url)
                          )
                if set_endpoint_url:
                    if set_endpoint_url != endpoint_url:
                        config.data["cloudmesh"]["comet"]["endpoints"] \
                            [cometConf['active']]["nucleus_base_url"] \
                            = set_endpoint_url
                        config.save()
                        Console.ok("Service base url set!")

                set_api_version = \
                    input("Set the api version for the nucleus %s service [%s]: " \
                          % (cometConf['active'],
                             api_version)
                          )
                if set_api_version:
                    if set_api_version != api_version:
                        config.data["cloudmesh"]["comet"]["endpoints"] \
                            [cometConf['active']]["api_version"] \
                            = set_api_version
                        config.save()
                        Console.ok("Service api version set!")
                print("Authenticating to the nucleus %s " \
                      "service and obtaining the apikey..." \
                      % cometConf['active'])
                Comet.get_apikey(cometConf['active'])

            return ''
            # Comet.get_apikey()
        if arguments["active"]:
            config = ConfigDict("cloudmesh.yaml")
            cometConf = config["cloudmesh.comet"]
            endpoint = arguments["ENDPOINT"] or None
            # parameter specified, intended to change
            if endpoint:
                if "endpoints" in cometConf.keys():
                    endpoints = cometConf["endpoints"].keys()
                    if endpoint in endpoints:
                        config.data["cloudmesh"] \
                                   ["comet"] \
                                   ["active"] = endpoint
                        config.save()
                        Console.ok("Comet active service endpoint set"
                                   " to: %s" % endpoint)
                    else:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s."
                                              % "/".join(endpoints),
                                     traceflag = False)
                else:
                    Console.error("No available endpoint to set. "
                                  "Check config file!",
                                  traceflag=False)
            else:
                if "active" in cometConf.keys():
                    active_endpoint = cometConf['active']
                    Console.ok("Current active service endpoint is: %s"
                                 % active_endpoint)
                else:
                    Console.error("Cannot set active endpoint. "
                                  "Check config file!",
                                  traceflag = False)
        try:
            endpoint = None
            config = ConfigDict("cloudmesh.yaml")
            cometConf = config["cloudmesh.comet"]
            if arguments["--endpoint"]:
                endpoint = arguments["--endpoint"]
                if "endpoints" in cometConf.keys():
                    endpoints = cometConf["endpoints"].keys()
                    if endpoint not in endpoints:
                        Console.error("The provided endpoint does not match "
                                      "any available service endpoints. Try %s."
                                              % "/".join(endpoints),
                                     traceflag = False)
                        return ''
            logon = Comet.logon(endpoint=endpoint)
            if logon is False:
                Console.error("Could not logon. Please try first:\n"
                              "cm comet init",
                              traceflag = False)
                return ""
        except:
            Console.error("Could not logon",
                          traceflag = False)

        output_format = arguments["--format"] or "table"

        if arguments["ll"]:
            cluster_id = arguments["CLUSTERID"] or None

            print(Cluster.simple_list(cluster_id, format=output_format))

        elif arguments["cluster"]:
            view = "FULL"
            if arguments["--concise"]:
                view = "CONCISE"
            if arguments["--status"]:
                view = "STATE"
            cluster_id = arguments["CLUSTERID"]
            sortkey = arguments["--sort"]
            print(Cluster.list(cluster_id,
                               format=output_format,
                               sort=sortkey,
                               view=view))

        elif arguments["computeset"]:
            computeset_id = arguments["COMPUTESETID"] or None
            cluster = arguments["--cluster"] or None
            state = arguments["--state"] or None
            allocation = arguments["--allocation"] or None
            cluster = arguments["--cluster"] or None
            print (Cluster.computeset(computeset_id, cluster, state, allocation))
        elif arguments["start"]:
            clusterid = arguments["CLUSTERID"]
            numnodes = arguments["--count"] or None
            computenodeids = arguments["COMPUTENODEIDS"] or None

            # check allocation information for the cluster
            cluster = Cluster.list(clusterid, format='rest')
            try:
                allocations = cluster[0]['allocations']
            except:
                # print (cluster)
                Console.error("No allocation available for the specified cluster."\
                              "Please check with the comet help team",
                              traceflag=False)
                return ""

            # checking whether the computesetids is in valid hostlist format
            if computenodeids:
                try:
                    hosts_param = hostlist.expand_hostlist(computenodeids)
                except hostlist.BadHostlist:
                    Console.error("Invalid hosts list specified!",
                                  traceflag=False)
                    return ""
            elif numnodes:
                try:
                    param = int(numnodes)
                except ValueError:
                    Console.error("Invalid count value specified!",
                                  traceflag=False)
                    return ""
                if param <= 0:
                    Console.error("count value has to be greather than zero",
                                  traceflag=False)
                    return ""
                numnodes = param
            else:
                Console.error("You have to specify either the count of nodes, " \
                              "or the names of nodes in hostlist format",
                              traceflag=False)
                return ""

            walltime = arguments["--walltime"] or None
            allocation = arguments["--allocation"] or None

            reservation = arguments["--reservation"] or None
            # validating walltime and allocation parameters
            walltime = Cluster.convert_to_mins(walltime)
            if not walltime:
                print("No valid walltime specified. " \
                      "Using system default (2 days)")
            if not allocation:
                if len(allocations) == 1:
                    allocation = allocations[0]
                else:
                    allocation = Cluster.display_get_allocation(allocations)

            # issuing call to start a computeset with specified parameters
            print(Cluster.computeset_start(clusterid,
                                           computenodeids,
                                           numnodes,
                                           allocation,
                                           reservation,
                                           walltime)
                  )
        elif arguments["terminate"]:
            computesetid = arguments["COMPUTESETID"]
            print(Cluster.computeset_terminate(computesetid))
        elif arguments["power"]:
            clusterid = arguments["CLUSTERID"] or None
            fuzzyparam = arguments["NODESPARAM"] or None

            # parsing nodesparam for proper action
            if fuzzyparam:
                try:
                    param = int(fuzzyparam)
                    subject = 'COMPUTESET'
                except ValueError:
                    param = fuzzyparam
                    try:
                        hosts_param = hostlist.expand_hostlist(fuzzyparam)
                        subject = 'HOSTS'
                    except hostlist.BadHostlist:
                        Console.error("Invalid hosts list specified!",
                                      traceflag=False)
                        return ""
            else:
                subject = 'FE'
                param = None

            if arguments["on"]:
                action = "on"
            elif arguments["off"]:
                action = "off"
            elif arguments["reboot"]:
                action = "reboot"
            elif arguments["reset"]:
                action = "reset"
            elif arguments["shutdown"]:
                action = "shutdown"
            else:
                action = None
            print (Cluster.power(clusterid,
                                subject,
                                param,
                                action)
                  )
        elif arguments["console"]:
            clusterid = arguments["CLUSTERID"]
            linkonly = False
            if arguments["--link"]:
                linkonly = True
            nodeid = None
            if 'COMPUTENODEID' in arguments:
                nodeid = arguments["COMPUTENODEID"]
            Comet.console(clusterid, nodeid, linkonly)
        elif arguments["iso"]:
            if arguments["list"]:
                isos = (Comet.list_iso())
                idx = 0
                for iso in isos:
                    if iso.startswith("public/"):
                        iso = iso.split("/")[1]
                    idx += 1
                    print ("{}: {}".format(idx, iso))
            if arguments["upload"]:
                isofile = arguments["PATHISOFILE"]
                isofile = os.path.abspath(isofile)
                if os.path.isfile(isofile):
                    if arguments["--isoname"]:
                        filename = arguments["--isoname"]
                    else:
                        filename = os.path.basename(isofile)
                else:
                    print ("File does not exist - {}" \
                          .format(arguments["PATHISOFILE"]))
                    return ""
                print(Comet.upload_iso(filename, isofile))
            elif arguments["attach"]:
                isoidname = arguments["ISOIDNAME"]
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print(Cluster.attach_iso(isoidname, clusterid, computenodeids))
            elif arguments["detach"]:
                clusterid = arguments["CLUSTERID"]
                computenodeids = arguments["COMPUTENODEIDS"] or None
                print(Cluster.detach_iso(clusterid, computenodeids))
        elif arguments["node"]:
            if arguments["info"]:
                clusterid = arguments["CLUSTERID"]
                nodeid = arguments["COMPUTENODEID"]
                print (Cluster.node_info(clusterid, nodeid=nodeid, format=output_format))
            elif arguments["rename"]:
                clusterid = arguments["CLUSTERID"]
                oldnames = Parameter.expand(arguments["OLDNAMES"])
                newnames = Parameter.expand(arguments["NEWNAMES"])
                if len(oldnames) != len(newnames):
                    Console.error("Length of OLDNAMES and NEWNAMES have to be the same",
                                  traceflag=False)
                    return ""
                else:
                    for newname in newnames:
                        if newname.strip() == "":
                            Console.error("Newname cannot be empty string",
                                          traceflag=False)
                            return ""
                    cluster_data = Cluster.list(clusterid, format="rest")
                    if len(cluster_data) > 0:
                        computes = cluster_data[0]["computes"]
                        nodenames = [x["name"] for x in computes]
                    else:
                        Console.error("Error obtaining the cluster information",
                                      traceflag=False)
                        return ""
                    # check if new names ar not already taken
                    # to be implemented
                    # print (oldnames)
                    # print (newnames)
                    # print (nodenames)
                    oldset = set(oldnames)
                    newset = set(newnames)
                    currentset = set(nodenames)
                    # at least one OLDNAME does not exist
                    if  not oldset <= currentset:
                        Console.error("Not all OLDNAMES are valid", traceflag=False)
                        return ""
                    else:
                        # those unchanged nodes
                        keptset = currentset - oldset
                        # duplication between name of unchanged nodes and
                        # the requested NEWNAMES
                        if keptset & newset != set():
                            Console.error("Not proceeding as otherwise introducing "\
                                          "duplicated names",
                                          traceflag=False)
                        else:
                            for i in range(0,len(oldnames)):
                                oldname = oldnames[i]
                                newname = newnames[i]
                                print ("%s -> %s" % (oldname, newname))
                            confirm = input("Confirm batch renaming (Y/y to confirm, "\
                                            "any other key to abort):")
                            if confirm.lower() == 'y':
                                print ("Conducting batch renaming")
                                for i in range(0,len(oldnames)):
                                    oldname = oldnames[i]
                                    newname = newnames[i]
                                    print (Cluster.rename_node(clusterid,
                                                               oldname,
                                                               newname))
                            else:
                                print ("Action aborted!")
        elif arguments["reservation"]:
            if arguments["create"] or \
                            arguments["update"] or \
                            arguments["delete"]:
                Console.info("Operation not supported. Please contact XSEDE helpdesk for help!")
            if arguments["list"]:
                if "hpcinfo" in cometConf:
                    hpcinfourl = cometConf["hpcinfo"]["endpoint"]
                else:
                    Console.error("Admin feature not configured for this client", traceflag = False)
                    return ""
                ret = requests.get("%s/reservations/%s" % (hpcinfourl,
                                                           cometConf['active'])
                                  )
                jobs = ret.json()
                result = Printer.write(jobs)
                print (result)
        return ""
Exemple #42
0
    def list(id=None, format="table", sort=None, view="FULL"):

        def check_for_error(r):
            if r is not None:
                if 'error' in r:
                    Console.error("An error occurred: {error}".format(**r))
                    raise ValueError("COMET Error")

        result = ""
        if id is None:
            r = Comet.get(Comet.url("cluster/"))
            check_for_error(r)
        else:
            r = Comet.get(Comet.url("cluster/" + id + "/"))
            check_for_error(r)
            if r is None:
                Console.error("Error finding cluster `{}`"
                              .format(id))
                return result
            r = [r]

        #
        # getting account/allocation for each computeset
        # to display in the cluster view
        computeset_account = {}
        # stuck_computesets = {}
        computesets = Comet.get_computeset()

        # pprint (computesets)
        if computesets:
            computesets_submitted = Comet.get_computeset(state="submitted")
            if computesets_submitted:
                computesets += computesets_submitted
            computesets_ending = Comet.get_computeset(state="ending")
            if computesets_ending:
                computesets += computesets_ending
            for computeset in computesets:
                id = computeset["id"]
                account = computeset["account"]
                if id not in computeset_account:
                    computeset_account[id] = account

        # no longer track and display the (possible) stuck
        # computeset in this way
        '''
        stuck_computesets = {}
        computesets = Comet.get_computeset()
        if computesets:
            for computeset in computesets:
                if computeset["state"] in Cluster.STUCK_COMPUTESETS:
                    cluster = computeset["cluster"]
                    id = computeset["id"]
                    nodes = computeset["computes"]

                    if cluster not in stuck_computesets:
                        stuck_computesets[cluster] = {}
                    for node in nodes:
                        stuck_computesets[cluster][node["name"]] = \
                            "{}({})".format (id, computeset["state"])
        '''

        if r is not None:
            if format == "rest":
                result = r
            else:
                result = ''
                data = []

                empty = {
                    'cluster': None,
                    'cpus': None,
                    'host': None,
                    "mac": None,
                    'ip': None,
                    'memory': None,
                    'disksize': None,
                    'name': None,
                    'state': None,
                    'type': None,
                    'active_computeset': None,
                    'kind': 'frontend',
                    'admin_state': None
                }

                for cluster in sorted(r, key=lambda x: x["name"]):

                    clients = cluster["computes"]
                    for client in clients:
                        client["kind"] = "compute"
                    frontend = dict(empty)
                    frontend.update(cluster["frontend"])
                    pubip = cluster["frontend"]["pub_ip"]
                    pubmac = cluster["frontend"]["pub_mac"]
                    frontend["ip"] = pubip
                    frontend["pub_mac"] = pubmac
                    frontend["admin_state"] = cluster["frontend"]["frontend_state"]
                    result += "Cluster: %s\tFrontend: %s\tIP: %s\n" % \
                                (cluster["name"],
                                 cluster["frontend"]["name"],
                                 pubip)
                    if len(clients) > 0:
                        frontend['cluster'] = clients[0]['cluster']
                    else:
                        frontend['cluster'] = frontend['name']
                    data += [frontend]
                    data += clients

                for index, anode in enumerate(data):
                    bnode = dict(anode)
                    pubmac = None
                    if 'pub_mac' in bnode:
                        pubmac = bnode["pub_mac"]
                    if "interface" in bnode:
                        macs = []
                        macs_pub_order = []
                        # ips = []
                        for ipaddr in anode["interface"]:
                            macs.append(ipaddr["mac"])
                            # ips.append(ipaddr["ip"] or "N/A")
                        if pubmac in macs:
                            macs_pub_order.append(pubmac)
                            for mac in macs:
                                if mac != pubmac:
                                    macs_pub_order.append(mac)
                        else:
                            macs_pub_order = macs
                        if format == 'table':
                            bnode["mac"] = "\n".join(macs_pub_order)
                        else:
                            bnode["mac"] = ";".join(macs_pub_order)

                        if "active_computeset_state" in anode and \
                                anode["active_computeset"] is not None and \
                                anode["active_computeset_state"] is not None:
                            # if not running state, show also the status after
                            # the computeset id
                            if anode["active_computeset_state"] != 'running':
                                bnode["active_computeset"] = "%s(%s)" % \
                                                    (anode["active_computeset"],
                                                     anode["active_computeset_state"])
                            # if an active computeset has a valid account
                            # associated with it, display the account/allocation
                            # does this for computeset in all states
                            if anode["active_computeset"] in computeset_account:
                                bnode["allocation"] = \
                                computeset_account[anode["active_computeset"]]

                        if "compute_state" in anode:
                            bnode["admin_state"] = anode["compute_state"]
                        #anode["ip"] = "; ".join(ips)
                        if "ip" not in bnode:
                            bnode["ip"] = None

                    del bnode["interface"]
                    #
                    # stuck state included in cluster data via API
                    '''
                    if bnode["cluster"] in stuck_computesets and \
                                    bnode["name"] in stuck_computesets[bnode["cluster"]]:
                        bnode["active_computeset"] = \
                            stuck_computesets[bnode["cluster"]][bnode["name"]]
                    '''
                    data[index] = bnode

                sort_keys = ('cluster','mac')
                if sort:
                    if sort in Cluster.CLUSTER_SORT_KEY:
                        # print (sort)
                        idx = Cluster.CLUSTER_SORT_KEY.index(sort)
                        # print (idx)
                        sortkey = Cluster.CLUSTER_ORDER[idx]
                        # print (sortkey)
                        sort_keys = (sortkey,)
                        # print (sort_keys)
                if "table" == format:
                    result_print = Printer.write(data,
                            order=Cluster.CLUSTER_TABLE_VIEW[view]["order"],
                            header=Cluster.CLUSTER_TABLE_VIEW[view]["header"],
                            output=format,
                            sort_keys=sort_keys)
                    result += str(result_print)
                else:
                    result_print = Printer.write(data,
                                                 order=Cluster.CLUSTER_ORDER,
                                                 header=Cluster.CLUSTER_HEADER,
                                                 output=format,
                                                 sort_keys=sort_keys)
                    result = result_print
            return result