Exemplo n.º 1
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]
Exemplo n.º 2
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]
Exemplo n.º 3
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
Exemplo n.º 4
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
Exemplo n.º 5
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
Exemplo n.º 6
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
Exemplo n.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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