Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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
Exemplo n.º 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
Exemplo n.º 10
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
Exemplo n.º 11
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
Exemplo n.º 12
0
 def stop(id):
     data = {"id": id}
     r = requests.post(Comet.url("cluster/{id}/stop".format(**data)))
     print(r)
Exemplo n.º 13
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
Exemplo n.º 14
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
Exemplo n.º 15
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
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
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
Exemplo n.º 19
0
 def stop(id):
     data = {"id": id}
     r = requests.post(Comet.url("cluster/{id}/stop".format(**data)))
     print(r)
Exemplo n.º 20
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
Exemplo n.º 21
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
Exemplo n.º 22
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
Exemplo n.º 23
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
Exemplo n.º 24
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
Exemplo n.º 25
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
Exemplo n.º 26
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