Ejemplo n.º 1
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
Ejemplo n.º 2
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
Ejemplo n.º 3
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
Ejemplo 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
Ejemplo n.º 5
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
Ejemplo n.º 6
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
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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