Ejemplo n.º 1
0
 def output_computeset(computesetdict):
     result = ""
     if computesetdict["state"] not in ["completed"]:
         result += "\nCluster: {}\tComputesetID: {}\t State: {}\n" \
             .format(computesetdict["cluster"],
                     computesetdict["id"],
                     computesetdict["state"]
                     )
         data = computesetdict["computes"]
         for anode in data:
             for attribute in anode.keys():
                 if "interface" == attribute:
                     macs = []
                     ips = []
                     for ipaddr in anode["interface"]:
                         macs.append(ipaddr["mac"])
                         ips.append(ipaddr["ip"] or "N/A")
                     anode["mac"] = "; ".join(macs)
                     anode["ip"] = "; ".join(ips)
             del anode["interface"]
         result += str(list_printer(data,
                                    order=[
                                        "name",
                                        "state",
                                        "type",
                                        "mac",
                                        "ip",
                                        "cpus",
                                        "cluster",
                                        "host",
                                        "memory",
                                    ],
                                    output="table"))
     return result
Ejemplo n.º 2
0
    def do_ssh(self, args, arguments):
        """
        ::

            Usage:
                ssh table
                ssh list [--format=FORMAT]
                ssh cat
                ssh register NAME PARAMETERS
                ssh ARGUMENTS


            conducts a ssh login on a machine while using a set of
            registered machines specified in ~/.ssh/config

            Arguments:

              NAME        Name or ip of the machine to log in
              list        Lists the machines that are registered and
                          the commands to login to them
              PARAMETERS  Register te resource and add the given
                          parameters to the ssh config file.  if the
                          resoource exists, it will be overwritten. The
                          information will be written in /.ssh/config

            Options:

               -v       verbose mode
               --format=FORMAT   the format in which this list is given
                                 formats incluse table, json, yaml, dict
                                 [default: table]

               --user=USER       overwrites the username that is
                                 specified in ~/.ssh/config

               --key=KEY         The keyname as defined in the key list
                                 or a location that contains a pblic key

            Description:

                ssh list
                    lists the hostsnames  that are present in the
                    ~/.ssh/config file

                ssh cat
                    prints the ~/.ssh/config file

                ssh table
                    prints contents of the ~/.ssh/config file in table format

                ssh register NAME PARAMETERS
                    registers a host i ~/.ssh/config file
                    Parameters are attribute=value pairs
                    Note: Note yet implemented

                ssh ARGUMENTS
                    executes the ssh command with the given arguments
                    Example:
                        ssh myhost

                            conducts an ssh login to myhost if it is defined in
                            ~/.ssh/config file
        """

        # pprint(arguments)

        def read(filename=None):
            if filename is None:
                filename = "~/.ssh/config"
            with open(path_expand("~/.ssh/config"), "r") as f:
                content = f.readlines()
            return "".join(content)

        if arguments["list"]:

            output_format = arguments["--format"]
            banner('List SSH config hosts')
            hosts = ssh_config()
            for host in hosts.list():
                print(host)

        elif arguments["table"]:

            content = read(filename="~/.ssh/config").split("\n")

            entries = []

            def empty():
                return {
                    "host": None,
                    "hostname": None,
                    "user": None,
                    "proxycommand": None,
                    "serveraliveinterval": None,
                    "localforward": None,
                    "forwardx11": None
                }

            entry = empty()
            for line in content:
                line = line.strip()
                if line.startswith("#"):
                    pass
                elif line.strip() == "":
                    pass
                elif "Host " in line:
                    hostname = line.strip().split("Host")[1]
                    entry["host"] = hostname.strip()
                    if entry is not None:
                        entries.append(entry)
                    entry = empty()
                else:
                    attribute, value = line.strip().split(" ", 1)
                    entry[attribute.lower()] = value.strip()

            pprint(entries)
            order = [
                "host", "hostname", "user", "proxycommand",
                "serveraliveinterval", "localforward", "forwardx11"
            ]

            print(list_printer(entries, order=order))

        elif arguments["cat"]:

            print(read(filename="~/.ssh/config"))

        elif arguments["register"]:

            name = arguments["NAME"]
            parameters = arguments["PARAMETERS"]
            Console.ok('register {} {}'.format(name, parameters))
            TODO.implement("Not implemented")

        else:  # ssh ARGUMENTS...

            args = arguments["ARGUMENTS"]
            os.system("ssh {}".format(args))
            return ""

        return ""
Ejemplo n.º 3
0
    def do_ssh(self, args, arguments):
        """
        ::

            Usage:
                ssh table
                ssh list [--format=FORMAT]
                ssh cat
                ssh register NAME PARAMETERS
                ssh ARGUMENTS


            conducts a ssh login on a machine while using a set of
            registered machines specified in ~/.ssh/config

            Arguments:

              NAME        Name or ip of the machine to log in
              list        Lists the machines that are registered and
                          the commands to login to them
              PARAMETERS  Register te resource and add the given
                          parameters to the ssh config file.  if the
                          resoource exists, it will be overwritten. The
                          information will be written in /.ssh/config

            Options:

               -v       verbose mode
               --format=FORMAT   the format in which this list is given
                                 formats incluse table, json, yaml, dict
                                 [default: table]

               --user=USER       overwrites the username that is
                                 specified in ~/.ssh/config

               --key=KEY         The keyname as defined in the key list
                                 or a location that contains a pblic key

            Description:

                ssh list
                    lists the hostsnames  that are present in the
                    ~/.ssh/config file

                ssh cat
                    prints the ~/.ssh/config file

                ssh table
                    prints contents of the ~/.ssh/config file in table format

                ssh register NAME PARAMETERS
                    registers a host i ~/.ssh/config file
                    Parameters are attribute=value pairs
                    Note: Note yet implemented

                ssh ARGUMENTS
                    executes the ssh command with the given arguments
                    Example:
                        ssh myhost

                            conducts an ssh login to myhost if it is defined in
                            ~/.ssh/config file
        """
        # pprint(arguments)

        def read(filename="~/.ssh/config"):
            with open(path_expand("~/.ssh/config"), "r") as f:
                content = f.readlines()
            return "".join(content)

        if arguments["list"]:

            output_format = arguments["--format"]
            banner("List SSH config hosts")
            hosts = ssh_config()
            for host in hosts.list():
                print(host)

        elif arguments["table"]:

            content = read(filename="~/.ssh/config").split("\n")

            entries = []

            def empty():
                return {
                    "host": None,
                    "hostname": None,
                    "user": None,
                    "proxycommand": None,
                    "serveraliveinterval": None,
                    "localforward": None,
                    "forwardx11": None,
                }

            entry = empty()
            for line in content:
                line = line.strip()
                if line.startswith("#"):
                    pass
                elif line.strip() == "":
                    pass
                elif "Host " in line:
                    hostname = line.strip().split("Host")[1]
                    entry["host"] = hostname.strip()
                    if entry is not None:
                        entries.append(entry)
                    entry = empty()
                else:
                    attribute, value = line.strip().split(" ", 1)
                    entry[attribute.lower()] = value.strip()

            pprint(entries)
            order = ["host", "hostname", "user", "proxycommand", "serveraliveinterval", "localforward", "forwardx11"]

            print(list_printer(entries, order=order))

        elif arguments["cat"]:

            print(read(filename="~/.ssh/config"))

        elif arguments["register"]:

            name = arguments["NAME"]
            parameters = arguments["PARAMETERS"]
            Console.ok("register {} {}".format(name, parameters))
            TODO.implement("Not implemented")

        else:  # ssh ARGUMENTS...

            args = arguments["ARGUMENTS"]
            os.system("ssh {}".format(args))
            return ""

        return ""
Ejemplo n.º 4
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
Ejemplo n.º 5
0
    def output_computeset(computesetdict, cluster=None, state=None, allocation=None):
        # print (cluster, state, allocation)
        result = ""
        # filter out based on query criteria
        if cluster and 'cluster' in computesetdict:
            if computesetdict["cluster"] != cluster:
                return result
        if state and 'state' in computesetdict:
            if "ALL" != state and computesetdict["state"] != state:
                return result
        if allocation and 'account' in computesetdict:
            if computesetdict["account"] != allocation:
                return result

        if (state and ("ALL" == state or computesetdict["state"] == state)) or \
           (computesetdict["state"] not in Cluster.FINISHED_COMPUTESETS):
            starttime = ''
            endtime = ''
            walltime = ''
            runningTime = ''
            remainingTime = ''
            if 'walltime_mins' in computesetdict:
                walltime = computesetdict["walltime_mins"]
                walltime_seconds = walltime * 60
                walltime = Cluster.format_ddd_hh_mm(walltime_seconds)
            if 'start_time' in computesetdict and \
                computesetdict["start_time"] is not None:
                start_seconds = int(computesetdict["start_time"])
                end_seconds = start_seconds + walltime_seconds
                if computesetdict["state"] in Cluster.FINISHED_COMPUTESETS \
                    or computesetdict["state"] == 'ending':
                    runningSecs = walltime_seconds
                else:
                    runningSecs = int(time.time())-start_seconds
                remainingSecs = walltime_seconds - runningSecs
                starttime = time.strftime("%D %H:%M %Z",
                                    time.localtime(start_seconds))
                endtime = time.strftime("%D %H:%M %Z",
                                    time.localtime(end_seconds))
                runningTime = Cluster.format_ddd_hh_mm(runningSecs)
                remainingTime = Cluster.format_ddd_hh_mm(remainingSecs)

            result += "\nClusterID: {}\tComputesetID: {}\t State: {}\t\tAllocation: {}\n" \
                      "Start (est): {}\t\tEnd (est): {}\n"\
                      "Requested Time (ddd-hh:mm): {}\tRunning Time (est): {}\t\tRemaining Time (est): {}\n"\
                        .format(computesetdict["cluster"],
                                computesetdict["id"],
                                computesetdict["state"],
                                computesetdict["account"],
                                starttime,
                                endtime,
                                walltime,
                                runningTime,
                                remainingTime
                                )
            data = computesetdict["computes"]
            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"]
                data[index] = bnode
            result += str(list_printer(data,
                                       order=[
                                           "name",
                                           "state",
                                           "type",
                                           "mac",
                                           #"ip",
                                           "cpus",
                                           "cluster",
                                           "host",
                                           "memory",
                                       ],
                                       output="table",
                                       sort_keys='cluster'))
        return result
Ejemplo n.º 6
0
    def list(id=None, format="table"):

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

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

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

                data = []

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

                for cluster in r:

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

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

                result = list_printer(data,
                                      order=[
                                          "name",
                                          "state",
                                          "kind",
                                          "type",
                                          "mac",
                                          #"ip",
                                          "cpus",
                                          "cluster",
                                          "memory",
                                      ],
                                      output=format)
            return result
Ejemplo n.º 7
0
    def output_computeset(computesetdict, cluster=None, state=None, allocation=None):
        #print (cluster, state, allocation)
        result = ""
        # filter out based on query criteria
        if cluster and 'cluster' in computesetdict:
            if computesetdict["cluster"] != cluster:
                return result
        if state and 'state' in computesetdict:
            if computesetdict["state"] != state:
                return result
        if allocation and 'account' in computesetdict:
            if computesetdict["account"] != allocation:
                return result

        if (state and computesetdict["state"] == state) or \
           (computesetdict["state"] not in ["completed"]):
            walltime = ''
            starttime = ''
            runningTime = ''
            remainingTime = ''
            if 'walltime_mins' in computesetdict:
                walltime = computesetdict["walltime_mins"]
            if 'start_time' in computesetdict and \
                computesetdict["start_time"] is not None:
                starttime = time.strftime("%D %H:%M",
                                    time.localtime(int(computesetdict["start_time"])))
                if computesetdict["state"] not in ["completed"]:
                    runningTime = (int(time.time())-int(computesetdict["start_time"]))/60
                    remainingTime = int(walltime) - runningTime
            result += "\nCluster: {}\tComputesetID: {}\t State: {}\t\tAllocation: {}\n" \
                      "Requested Walltime (Minutes): {}\tStart Time: {}\n"\
                      "Running Time: {}\t\tRemaining Time (Estimated): {}\n"\
                        .format(computesetdict["cluster"],
                                computesetdict["id"],
                                computesetdict["state"],
                                computesetdict["account"],
                                walltime,
                                starttime,
                                runningTime,
                                remainingTime
                                )
            data = computesetdict["computes"]
            for anode in data:
                for attribute in anode.keys():
                    if "interface" == attribute:
                        macs = []
                        #ips = []
                        for ipaddr in anode["interface"]:
                            macs.append(ipaddr["mac"])
                            #ips.append(ipaddr["ip"] or "N/A")
                        anode["mac"] = "; ".join(macs)
                        #anode["ip"] = "; ".join(ips)
                del anode["interface"]
            result += str(list_printer(data,
                                       order=[
                                           "name",
                                           "state",
                                           "type",
                                           "mac",
                                           #"ip",
                                           "cpus",
                                           "cluster",
                                           "host",
                                           "memory",
                                       ],
                                       output="table"))
        return result