Ejemplo n.º 1
0
        def _manage_service(service, arguments):
            banner(service.name)
            if arguments.stop:
                service.stop()
                print(service.status())

            elif arguments.start:
                service.start()
                print(service.status())

            elif arguments.info:
                parameters = service.info()
                print(Printer.attribute(parameters))
Ejemplo n.º 2
0
    def test_01_status(self):
        HEADING()
        r = self.database.status()
        # pprint(r)
        assert "Connection refused" not in r

        d = {}
        for field in ['uptime', 'pid', 'version', 'host']:
            d[field] = r[field]

        print(Printer.attribute(d))

        assert d is not None
Ejemplo n.º 3
0
    def benchmark(cls, sysinfo=True):
        """
        prints out all timers in a convenient benchmark tabble
        :return:
        :rtype:
        """

        #
        # PRINT PLATFORM
        #
        if sysinfo:
            data_platform = systeminfo()
            print(
                Printer.attribute(data_platform,
                                  ["Machine Arribute", "Time/s"]))

        #
        # PRINT TIMERS
        #
        timers = StopWatch.keys()

        data_timers = {}
        for timer in timers:
            data_timers[timer] = {
                'time': round(StopWatch.get(timer), 2),
                'timer': timer
            }
            for attribute in [
                    "node", "system", "machine", "mac_version", "win_version"
            ]:
                data_timers[timer][attribute] = data_platform[attribute]

        # print(Printer.attribute(data_timers, header=["Command", "Time/s"]))
        print(
            Printer.write(data_timers,
                          order=[
                              "timer", "time", "node", "system", "mac_version",
                              "win_version"
                          ]))

        print()
        print(
            Printer.write(data_timers,
                          order=[
                              "timer", "time", "node", "system", "mac_version",
                              "win_version"
                          ],
                          output="csv"))
Ejemplo n.º 4
0
    def do_source(self, args, arguments):
        """
        ::

          Usage:
              source list
              source install [--protocol=PROTOCOL]


          This command does some useful things.

          Arguments:
              KEY   a file name

          Options:
              -f      specify the file
              --protocol=PROTOCOL   [default: ssh]
              --output=OUTPUT       [default: table]

          Description:

             you cen specify in your yaml file the location of where you
             manage your source code.

             cloudmesh:
                source:
                  common: ~/Desktop/github/cloudmesh
                  cmd5: ~/Desktop/github/cloudmesh
                  openapi: ~/Desktop/github/cloudmesh
                  sys: ~/Desktop/github/cloudmesh
                  cm: ~/Desktop/github/cloudmesh-community


            Once you have this included and cms cm is installed, you can than for
            example do an update from source with

                cms source install

            This has the advantage that all cms directories pull the new code
            from git. It assumes that you have installed the source
            previously with

                pip install -e .

            in each of the directories

        """

        #

        map_parameters(arguments, "source", "protocol")

        config = Config()
        m = Source(config, protocol=arguments.protocol)

        if arguments.list:

            print(Printer.attribute(config, output=arguments["output"]))

        elif arguments["install"]:

            m.install()

        elif arguments.clone:

            m.clone()

        elif arguments.update:

            m.update()

        return ""
Ejemplo n.º 5
0
    def do_multipass(self, args, arguments):
        """
        ::

          Usage:
                multipass deploy [--dryrun]
                multipass list [--output=OUTPUT] [--dryrun]
                multipass images [--output=OUTPUT] [--dryrun]
                multipass create NAMES [--image=IMAGE]
                                       [--size=SIZE]
                                       [--mem=MEMORY]
                                       [--cpus=CPUS]
                                       [--cloud-init=FILE]
                                       [--dryrun]
                multipass delete NAMES [--output=OUTPUT][--dryrun]
                multipass destroy NAMES [--output=OUTPUT][--dryrun]
                multipass shell NAMES [--dryrun]
                multipass run COMMAND NAMES [--output=OUTPUT] [--dryrun]
                multipass info NAMES [--output=OUTPUT] [--dryrun]
                multipass suspend NAMES [--output=OUTPUT] [--dryrun]
                multipass resume NAMES [--output=OUTPUT] [--dryrun]
                multipass start NAMES [--output=OUTPUT] [--dryrun]
                multipass stop NAMES [--output=OUTPUT] [--dryrun]
                multipass reboot NAMES [--output=OUTPUT] [--dryrun]
                multipass mount SOURCE DESTINATION [--dryrun]
                multipass umount SOURCE [--dryrun]
                multipass transfer SOURCE DESTINATION [--dryrun]
                multipass set key=VALUE [--dryrun]
                multipass get [key] [--dryrun]
                multipass deploy [--dryrun]
                multipass rename NAMES [--dryrun]
                multipass version

          Interface to multipass

          Options:
               --output=OUTPUT    the output format [default: table]. Other
                                  values are yaml, csv and json.

               --image=IMAGE      the image name to be used to create a VM.

               --cpus=CPUS        Number of CPUs to allocate.
                                  Minimum: 1, default: 1.

               --size=SIZE        Disk space to allocate. Positive integers,
                                  in bytes, or with K, M, G suffix.
                                  Minimum: 512M, default: 5G.

               --mem=MEMORY       Amount of memory to allocate. Positive
                                  integers, in bytes, or with K, M, G suffix.
                                  Minimum: 128M, default: 1G.

               --cloud-init=FILE  Path to a user-data cloud-init configuration

          Arguments:
              NAMES   the names of the virtual machine

          Description:

              The NAMES can be a parameterized hostname such as

                red[0-1,5] = red0,red1,red5

          Commands:

            First you can see the supported multipass images with

                cms multipass images

            Create and launch a new vm using

                cms multipass create NAMES

                Optionally you can provide image name, size, memory,
                number of cpus to create an instance.

            Start one or multiple multipass vms with

                cms multipass start NAMES

            Stop one or multiple vms with

                cms multipass stop NAMES

            Gets all multipass internal key values with

              cms multipass get

            Gets a specific internal key.

              cms multipass get KEY

              Known keys
       
                  client.gui.autostart
                  client.primary-name
                  local.driver

                  are there more?

            Reboot (stop and then start) vms with

                cms multipass reboot NAMES

            Delete one of multiple vms without purging with

                cms multipass delete NAMES

            Destory multipass vms (delete and purge) with

                cms multipass destroy NAMES

                Caution: Once destroyed everything in vm will be deleted
                         and cannot be recovered.

            WHEN YOU IMPLEMENT A FUNCTION INCLUDE MINIMAL
              DOCUMENTATION HERE
        """
        name = arguments.NAME

        map_parameters(arguments, "dryrun", "refresh", "cloud", "image",
                       "size", "mem", "cpus", "cloud-init", "output")
        # so we can use arguments.cloudinit
        arguments["cloudinit"] = arguments["--cloud-init"]

        image = arguments.image
        variables = Variables()

        arguments.output = Parameter.find("output", arguments, variables,
                                          "table")

        names = Parameter.expand(arguments.NAMES)

        VERBOSE(arguments)

        if arguments.version:

            if arguments.dryrun:
                banner("dryrun list")
            else:
                provider = Provider()
                version = provider.version()
                del version["name"]

                print(Printer.attribute(version, header=["Program",
                                                         "Version"]))

            return ""

        elif arguments.list:

            if arguments.dryrun:
                banner("dryrun list")
            else:
                provider = Provider()
                list = provider.list()

                print(
                    provider.Print(list, kind='image',
                                   output=arguments.output))

            return ""

        elif arguments.images:

            if arguments.dryrun:
                banner("dryrun images")
            else:

                provider = Provider()
                images = provider.images()

                print(
                    provider.Print(images,
                                   kind='image',
                                   output=arguments.output))

            return ""

        elif arguments.run:

            if arguments.dryrun:
                banner("dryrun run")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"run {name} {arguments.COMMAND}")
                else:

                    provider = Provider()
                    provider.run(name, arguments.COMMAND)

            return ""

        elif arguments.create:

            result = ""

            if arguments.dryrun:
                banner("create")

            timeout = 360
            group = None
            kwargs = {
                "cloud_init": arguments.cloud_init,
                "cpus": arguments.cpus,
                "memory": arguments.mem
            }

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun create {name} {image}")
                else:
                    provider = Provider()
                    result = provider.create(name, image, arguments.size,
                                             timeout, group, **kwargs)
                    VERBOSE(result)

            return result

        elif arguments.start:

            result = ""

            if arguments.dryrun:
                banner("start")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun start {name}")
                else:
                    provider = Provider()
                    result = provider.start(name)
                    VERBOSE(result)

            return result

        elif arguments.stop:

            result = ""

            if arguments.dryrun:
                banner("stop")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun stop {name}")
                else:
                    provider = Provider(name=name)
                    result = provider.stop(name)
                    VERBOSE(result)

            return result

        elif arguments.delete:

            result = ""

            if arguments.dryrun:
                banner("delete")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun delete {name}")
                else:
                    provider = Provider()
                    # Default purge is false. Is this ok?
                    result = provider.delete(name)
                    VERBOSE(result)

            return result

        elif arguments.info:

            result = ""

            if arguments.dryrun:
                banner(f"info {name}")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun info {name}")
                else:
                    provider = Provider()
                    # Default purge is false. Is this ok?
                    result = provider.info(name)
                    VERBOSE(result)

            return result

        elif arguments.rename:

            result = ""

            if arguments.dryrun:
                banner(f"Current Name: {names[0]}" f"New Name: {names[1]}")

            if names.len() > 2:
                Console.error("You have entered too many names."
                              "Only enter TWO names at a time.")
            else:
                old_name = names[0]
                new_name = names[1]

                provider = Provider()
                result = provider.rename(old_name, new_name)
                VERBOSE(result)

            return result

        elif arguments.suspend:

            result = ""

            if arguments.dryrun:
                banner("suspend")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun suspend {name}")
                else:
                    provider = Provider()
                    result = provider.suspend(name)
                    VERBOSE(result)

            return result

        elif arguments.resume:

            result = ""

            if arguments.dryrun:
                banner("resume")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun resume {name}")
                else:
                    provider = Provider()
                    result = provider.resume(name)
                    VERBOSE(result)

            return result

        elif arguments.destroy:

            result = ""

            if arguments.dryrun:
                banner("destroy")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun destroy {name}")
                else:
                    provider = Provider()
                    result = provider.destroy(name)
                    VERBOSE(result)

            return result

        elif arguments.reboot:

            result = ""

            if arguments.dryrun:
                banner("reboot")

            for name in names:
                if arguments.dryrun:
                    Console.ok(f"dryrun reboot {name}")
                else:
                    provider = Provider()
                    result = provider.reboot(name)
                    VERBOSE(result)

            return result

        elif arguments.shell:

            if len(names) > 1:
                Console.error("shell must only have one host")
                return ""

            name = names[0]

            if arguments.dryrun:
                banner("dryrun shell {name}")
            else:
                provider = Provider()
                provider.shell()

            return ""

        elif arguments.info:

            if arguments.dryrun:
                banner("dryrun info")
            else:
                provider = Provider()
                info = provider.info()
                print(
                    provider.Print(info, kind='info', output=arguments.output))

            return ""

        elif arguments.mount:

            if arguments.dryrun:
                banner(
                    f"dryrun mount {arguments.SOURCE} {arguments.DESTINATION}")
            else:
                provider = Provider()
                provider.mount(arguments.SOURCE, arguments.DESTINATION)

                # list the mounts and display as table

            return ""

        elif arguments.deploy:
            provider = Deploy(dryrun=arguments.dryrun)
            provider.install()

        else:
            Console.error("Not yet implemented")
        return ""
Ejemplo n.º 6
0
    def do_vbox(self, args, arguments):
        """
        ::

          Usage:
            vbox version [--output=OUTPUT]
            vbox image list [--output=OUTPUT]
            vbox image find KEYWORDS...
            vbox image add NAME
            vbox image delete NAME
            vbox vm info NAME
            vbox vm list [--output=OUTPUT] [-v]
            vbox vm delete NAME
            vbox vm ip [NAME] [--all]
            vbox vm create [NAME] ([--memory=MEMORY] [--image=IMAGE] [--port=PORT] [--script=SCRIPT]  | list)
            vbox vm boot [NAME] ([--memory=MEMORY] [--image=IMAGE] [--port=PORT] [--script=SCRIPT] | list)
            vbox vm ssh [NAME] [-e COMMAND]
        """

        arguments.output = arguments["--output"] or "table"
        arguments.verbose = arguments["-v"]
        arguments.all = arguments["--all"]

        #
        # ok
        #
        def list_images():
            images = Provider().images()
            _LIST_PRINT(images,
                        arguments.output,
                        order=["name", "provider", "version"])

        #
        # ok
        #
        # noinspection PyShadowingNames
        def image_command(func):
            try:
                images = func(arguments.NAME)
                print(images)
                images()
            except Exception as e:
                print(e)
            return ""

        #
        # ok
        #
        if arguments.version:
            p = Provider()

            versions = {
                "cm": {
                    "attribute": "cm",
                    "description": "Cloudmesh vbox Version",
                    "version": __version__
                },
                "vbox": {
                    "attribute": "vbox",
                    "description": "Vagrant Version",
                    "version": p.version()
                }
            }
            result = Printer.write(
                versions,
                order=["attribute", "version", "description"],
                output=arguments.output)
            print(result)

        #
        # ok
        #
        elif arguments.image and arguments.list:
            list_images()
        #
        # ok
        #
        elif arguments.image and arguments.delete:
            image_command(Provider().delete_image)
        #
        # ok
        #
        elif arguments.image and arguments.put:
            image_command(Provider().add_image)

        #
        # ok
        #
        elif arguments.image and arguments.find:
            Provider().find_image(arguments.KEYWORDS)
            return ""

        #
        # ok, but only vagrant details
        #
        elif arguments.vm and arguments.list:

            provider = Provider().vagrant_nodes()
            _LIST_PRINT(provider,
                        arguments.output,
                        order=["name", "state", "id", "provider", "directory"])
            return ""

        #
        # unclear: this function is unclear
        #
        elif arguments.create and arguments.list:

            result = Shell.cat("{NAME}/Vagrantfile".format(**arguments))
            if result is not None:
                print(result)
            return ""

        elif arguments.create:

            d = defaults()

            print("LLLL", d)

            arguments.memory = arguments["--memory"] or d.memory
            arguments.image = arguments["--image"] or d.image
            arguments.script = arguments["--script"] or d.script
            arguments.port = arguments["--port"] or d.port

            server = Provider()
            server.create(**arguments)

        elif arguments.info:

            # arguments.NAME
            d = Provider().info(name=arguments.NAME)

            result = Printer.write(d, output=arguments.output)

            print(result)

        elif arguments.ip:

            data = []
            result = Provider().execute(arguments.NAME, "ifconfig")
            if result is not None:
                lines = result.splitlines()[:-1]
                for line in lines:
                    if "inet addr" in line:
                        line = line.replace("inet addr", "ip")
                        line = ' '.join(line.split())
                        _adresses = line.split(" ")
                        address = {}
                        for element in _adresses:
                            attribute, value = element.split(":")
                            address[attribute] = value
                        data.append(address)
            if arguments.all:
                d = {}
                i = 0
                for e in data:
                    d[str(i)] = e
                    i = i + 1
                result = Printer.attribute(d, output=arguments.output)
                print(result)
            else:
                for element in data:
                    ip = element['ip']
                    if ip == "127.0.0.1" or ip.startswith("10."):
                        pass
                    else:
                        print(element['ip'])

        elif arguments.boot:

            d = defaults()

            pprint(d)

            arguments.memory = arguments["--memory"] or d.memory
            arguments.image = arguments["--image"] or d.image
            arguments.script = arguments["--script"] or d.script
            arguments.port = arguments["--port"] or d.port

            node = Provider().boot(name=arguments.NAME,
                                   memory=arguments.memory,
                                   image=arguments.image,
                                   script=arguments.script,
                                   port=arguments.port)

        elif arguments.delete:

            result = Provider().delete(name=arguments.NAME)
            print(result)

        elif arguments.ssh:

            if arguments.COMMAND is None:
                os.system("cd {NAME}; vbox ssh {NAME}".format(**arguments))
            else:
                result = Provider().execute(arguments.NAME, arguments.COMMAND)
                if result is not None:
                    lines = result.splitlines()[:-1]
                    for line in lines:
                        print(line)

        else:

            print("use help")

        result = ""
        return result
Ejemplo n.º 7
0
    def benchmark(cls, sysinfo=True, csv=True, tag=None):
        """
        prints out all timers in a convenient benchmark table
        :return:
        :rtype:
        """

        #
        # PRINT PLATFORM
        #

        print()
        data_platform = systeminfo()

        data_platform['cpu_count'] = multiprocessing.cpu_count()
        mem = psutil.virtual_memory()
        try:
            data_platform['mem_total'] = humanize.naturalsize(mem.total, \
                                                              binary=True)
        except:
            pass
        try:
            data_platform['mem_available'] = humanize.naturalsize(
                mem.available, binary=True)
        except:
            pass
        try:
            data_platform['mem_percent'] = str(mem.percent) + "%"
        except:
            pass
        try:
            data_platform['mem_used'] = humanize.naturalsize(mem.used,
                                                             binary=True)
        except:
            pass
        try:
            data_platform['mem_free'] = humanize.naturalsize(mem.free,
                                                             binary=True)
        except:
            pass
        try:
            data_platform['mem_active'] = humanize.naturalsize(mem.active,
                                                               binary=True)
        except:
            pass
        try:
            data_platform['mem_inactive'] = humanize.naturalsize(mem.inactive,
                                                                 binary=True)
        except:
            pass
        try:
            data_platform['mem_wired'] = humanize.naturalsize(mem.wired,
                                                              binary=True)
        except:
            pass
        # svmem(total=17179869184, available=6552825856, percent=61.9,

        if sysinfo:
            print(
                Printer.attribute(data_platform,
                                  ["Machine Attribute", "Value"]))

        #
        # PRINT TIMERS
        #
        timers = StopWatch.keys()

        if len(timers) > 0:

            data_timers = {}
            for timer in timers:
                data_timers[timer] = {
                    'start':
                    time.strftime("%Y-%m-%d %H:%M:%S",
                                  time.gmtime(StopWatch.timer_start[timer])),
                    'time':
                    StopWatch.get(timer, digits=3),
                    'timer':
                    timer,
                    'tag':
                    tag or ''
                }
                for attribute in [
                        "node", "user", "system", "machine", "mac_version",
                        "win_version"
                ]:
                    data_timers[timer][attribute] = data_platform[attribute]

            # print(Printer.attribute(data_timers, header=["Command", "Time/s"]))
            print(
                Printer.write(data_timers,
                              order=[
                                  "timer", "time", "start", "tag", "node",
                                  "user", "system", "mac_version",
                                  "win_version"
                              ]))

            print()
            if csv:
                print(
                    Printer.write(data_timers,
                                  order=[
                                      "timer", "time", "start"
                                      "tag", "node", "user", "system",
                                      "mac_version", "win_version"
                                  ],
                                  output="csv"))

        else:

            print("ERROR: No timers found")
Ejemplo n.º 8
0
    def do_admin(self, args, arguments):
        """
        ::

          Usage:
            admin mongo install [--brew] [--download=PATH] [--nosudo] [--docker] [--dryrun] [--force]
            admin mongo create
            admin mongo status
            admin mongo stats
            admin mongo version
            admin mongo start
            admin mongo stop
            admin mongo backup FILENAME
            admin mongo load FILENAME
            admin mongo security
            admin mongo password PASSWORD
            admin mongo list [--output=OUTPUT]
            admin mongo ssh
            admin status
            admin system info

          The admin command performs some administrative functions, such as
          installing packages, software and services. It also is used to
          start services and configure them.

          Arguments:
            FILENAME  the filename for backups

          Options:
            -f      specify the file

          Description:

            Mongo DB

              MongoDB is managed through a number of commands.

              The configuration is read from ~/.cloudmesh/cloudmesh.yaml

              First, you need to create a MongoDB database with

                cms admin mongo create

              Second, you need to start it with

                 cms admin mongo start

              Now you can interact with it to find out the status, the stats,
              and the database listing with the commands

                 cms admin mongo status
                 cms admin mongo stats
                 cms admin mongo list

              To stop it from running use the command

                 cms admin mongo stop

              System information about your machine can be returned by

                 cms admin system info

              This can be very useful in case you are filing an issue or bug.

              The command

                cms admin mongo ssh

              is only supported for docker and allows for debugging to login
              to the running container. This function may be disabled in future.

        """

        map_parameters(arguments,
                       "output",
                       "nosudo",
                       "docker",
                       "dryrun",
                       "force")
        arguments.output = arguments.output or "table"

        # VERBOSE(arguments)
        # arguments.PATH = arguments['--download'] or None
        result = None

        if arguments.mongo:

            if arguments.install and arguments.docker:

                installer = MongoInstaller(dryrun=arguments.dryrun,
                                           force=arguments.force)
                r = installer.docker()
                return r

            elif arguments.install:

                print("MongoDB install")
                print(79 * "=")
                # print(arguments.force)
                installer = MongoInstaller(dryrun=arguments.dryrun,
                                           force=arguments.force)

                sudo = not arguments.nosudo
                # if 'linux' in platform.lower() :
                #     print("SUDO:", sudo)
                # r = installer.install(sudo=sudo)
                r = installer.install()
                return r

            elif arguments.status:

                mongo = MongoDBController()
                state = mongo.status()

                if "error" in state["status"]:
                    Console.error(state["message"])
                    print(Printer.attribute(state))
                else:
                    data = dotdict()

                    for pid in state['output']:
                        entry = state['output'][pid]
                        data["pid"] = state['output'][pid]
                        data["command"] = state['output'][pid][
                            'command'].strip()

                    print(Printer.dict(data, order=["pid", "command"]))
                    Console.ok(str(data.pid['pid']) + " " + state["message"])

            elif arguments.version:
                print("MongoDB Version")
                print(79 * "=")
                mongo = MongoDBController()
                r = mongo.version()
                print(r)

            elif arguments.security:

                mongo = MongoDBController()
                mongo.set_auth()
                print()

            elif arguments.create:

                print("MongoDB create")
                MongoDBController().create()

            elif arguments.ssh:

                print("MongoDB ssh")
                MongoDBController().ssh()

            elif arguments.start:

                print("MongoDB start")
                MongoDBController().start(security=True)

            elif arguments.stop:

                print("MongoDB stop")
                MongoDBController().stop()

            elif arguments.backup:

                print("MongoDB backup")
                MongoDBController().dump(arguments.get('FILENAME'))

            elif arguments.load:

                print("MongoDB backup")
                MongoDBController().restore(arguments.get('FILENAME'))

            elif arguments.stats:

                mongo = MongoDBController()
                r = mongo.stats()

                if len(r) > 0:
                    print(Printer.attribute(r))
                    Console.ok("ok")
                else:
                    Console.ok("is your MongoDB server running")

            elif arguments.list:

                mongo = MongoDBController()

                r = mongo.list()

                if len(r) > 0:
                    if arguments.output == 'table':
                        print(Printer.dict(r, order=["name",
                                                     "sizeOnDisk",
                                                     "empty",
                                                     "collections"],
                                           output=arguments.output),
                              )
                    else:
                        print(Printer.write(r, output=arguments.output))
                    Console.ok("ok")
                else:
                    Console.ok("is your MongoDB server running")

        elif arguments.status:

            # config = Config()
            # data = config["cloudmesh.data.mongo"]

            print("Rest Service status")

            print("MongoDB status")

            try:
                mongo = MongoDBController()
                mongo.login()
                if mongo.status()['status'] == 'ok':
                    Console.ok("Mongo is running")
            except Exception as e:
                Console.error("Mongo is not running")
                print(e)

        elif arguments.system:

            s = OperatingSystem.get()
            print(Printer.attribute(s))

        return result
Ejemplo n.º 9
0
    def do_vbox(self, args, arguments):
        """
        ::

          Usage:
            vbox version [--format=FORMAT]
            vbox image list [--format=FORMAT]
            vbox image find KEYWORDS...
            vbox image add NAME
            vbox image delete NAME
            vbox vm list [--format=FORMAT] [-v]
            vbox vm delete NAME
            vbox vm config [NAME]
            vbox vm ip [NAME] [--all]
            vbox vm create [NAME] ([--memory=MEMORY] [--image=IMAGE] [--script=SCRIPT] | list)
            vbox vm boot [NAME] ([--memory=MEMORY] [--image=IMAGE] [--port=PORT] [--script=SCRIPT] | list)
            vbox vm ssh [NAME] [-e COMMAND]
        """

        arguments.format = arguments["--format"] or "table"
        arguments.verbose = arguments["-v"]
        arguments.all = arguments["--all"]

        print(arguments)

        if arguments.version:
            versions = {
                "vbox": {
                    "attribute": "Vagrant Version",
                    "version": cm4.vbox.version(),
                },
                "cloudmesh-vbox": {
                    "attribute": "cloudmesh vbox Version",
                    "version": __version__
                }
            }
            _LIST_PRINT(versions, arguments.format)

        elif arguments.image and arguments.list:
            l = VboxProvider.list_images()

            _LIST_PRINT(l, arguments.format, order=["name", "provider", "date"])

        elif arguments.image and arguments.add:
            try:
                l = VboxProvider.add_image(arguments.NAME)
                print(l)
            except Exception as e:
                print(e)
            return ""

        elif arguments.image and arguments.delete:
            try:
                l = VboxProvider.delete_image(arguments.NAME)
                print(l)
            except Exception as e:
                print(e)
            return ""


        elif arguments.image and arguments.find_image:

            VboxProvider.find_image(arguments.KEYWORDS)
            return ""

        elif arguments.vm and arguments.list:

            l = VboxProvider().nodes()
            _LIST_PRINT(l,
                        arguments.format,
                        order=["name", "state", "id", "provider", "directory"])
            return ""

        elif arguments.create and arguments.list:

            result = Shell.cat("{NAME}/Vagrantfile".format(**arguments))
            if result is not None:
                print(result)
            return ""

        elif arguments.create:

            d = defaults()

            arguments.memory = arguments["--memory"] or d.memory
            arguments.image = arguments["--image"] or d.image
            arguments.script = arguments["--script"] or d.script

            server = VboxProvider()
            server.create(
                name=arguments.NAME,
                memory=arguments.memory,
                image=arguments.image,
                script=arguments.script)

        elif arguments.config:

            # arguments.NAME
            d = VboxProvider().info(name=arguments.NAME)

            result = Printer.attribute(d, output=arguments.format)

            print(result)

        elif arguments.ip:

            data = []
            result = VboxProvider().execute(arguments.NAME, "ifconfig")
            if result is not None:
                lines = result.splitlines()[:-1]
                for line in lines:
                    if "inet addr" in line:
                        line = line.replace("inet addr", "ip")
                        line = ' '.join(line.split())
                        _adresses = line.split(" ")
                        address = {}
                        for element in _adresses:
                            attribute, value = element.split(":")
                            address[attribute] = value
                        data.append(address)
            if arguments.all:
                d = {}
                i = 0
                for e in data:
                    d[str(i)] = e
                    i = i + 1
                result = Printer.attribute(d, output=arguments.format)
                print(result)
            else:
                for element in data:
                    ip = element['ip']
                    if ip == "127.0.0.1" or ip.startswith("10."):
                        pass
                    else:
                        print(element['ip'])


        elif arguments.boot:

            d = defaults()

            arguments.memory = arguments["--memory"] or d.memory
            arguments.image = arguments["--image"] or d.image
            arguments.script = arguments["--script"] or d.script
            arguments.port = arguments["--port"] or d.port

            node = VboxProvider().boot(
                name=arguments.NAME,
                memory=arguments.memory,
                image=arguments.image,
                script=arguments.script,
                port=arguments.port)

        elif arguments.delete:

            result = VboxProvider().delete(name=arguments.NAME)
            print(result)

        elif arguments.ssh:

            if arguments.COMMAND is None:
                os.system("cd {NAME}; vbox ssh {NAME}".format(**arguments))
            else:
                result = VboxProvider().execute(arguments.NAME, arguments.COMMAND)
                if result is not None:
                    lines = result.splitlines()[:-1]
                    for line in lines:
                        print(line)

        else:

            print("use help")

        result = ""
        return result
Ejemplo n.º 10
0
    def do_jupyter(self, args, arguments):
        """
        ::

          Usage:
                jupyter lab HOST PORT [DIR]
                jupyter tunnel HOST PORT
                jupyter stop HOST PORT
                jupyter open PORT
                jupyter info [PORT]
                jupyter backup

          This command can start a jupyter notebook on a remote machine and
          use it in your browser.

          Arguments:
             USER   The username on the remote machine
             HOST   The hostname of the remote machine
             PORT   The port of the remote machine
             DIR    The directory where the notebooks are located

          Description:

            Step 1: Setting up a .bash_profile file

             If you have your python venv set up you need to add it to the
             .bash_profile on your remote machine. A possible
             profile file could look as follows:

                if [ -f ~/.bash_aliases ]; then
                     . ~/.bash_aliases
                 fi

                 export PATH=$HOME/ENV3/bin:$PATH
                 source $HOME/ENV3/bin/activate

            Step 2: Start the remote notebook server in a terminal

                Note: After the start you will not be able to use that terminal

                cms jupyter start HOST PORT

                Thsi command will aslo establich an SSH tunel and open in
                the web browser jupyter lab

        """

        VERBOSE(arguments)

        jupyter = Jupyter(arguments.HOST, arguments.PORT, arguments.DIR)

        if arguments.lab:

            jupyter.start()

        if arguments.tunnel:

            jupyter.tunnel()

        elif arguments.stop:
            jupyter.stop()
            data = jupyter.info()
            print(Printer.attribute(data))

        elif arguments.open:
            jupyter.open()

        elif arguments.test:
            jupyter.test()

        elif arguments.info:
            data = jupyter.info()
            print(Printer.attribute(data))

        elif arguments.backup:

            data = jupyter.info()
            data.backup = backup_name(data['cwd'])
            print("Generate backup")
            print(f"From: {data.cwd}")
            print(f"To:   {data.backup}")
            if yn_choice("Continue"):
                os.system(f"cp -r -v {data.cwd} {data.backup}")
                Console.ok(f"Backup created at: {data.backup}")

        return ""
Ejemplo n.º 11
0
    def do_admin(self, args, arguments):
        """
        ::

          Usage:
            admin mongo install [--brew] [--download=PATH]
            admin mongo create
            admin mongo status
            admin mongo stats
            admin mongo version
            admin mongo start
            admin mongo stop
            admin mongo backup FILENAME
            admin mongo load FILENAME
            admin mongo security
            admin mongo password PASSWORD
            admin mongo list
            admin rest status
            admin rest start
            admin rest stop
            admin status
            admin system info
            admin yaml cat
            admin yaml check

          The admin command performs some administrative functions, such as installing packages, software and services.
          It also is used to start services and configure them.

          Arguments:
            FILENAME  the filename for backups

          Options:
            -f      specify the file

          Description:

            Mongo DB

              MongoDB is managed through a number of commands.

              The configuration is read from ~/.cloudmesh/cloudmesh4.yaml

              First, you need to create a MongoDB database with

                cms admin mongo create

              Second, you need to start it with

                 cms admin mongo start

              Now you can interact with it to find out the status, the stats,
              and the database listing with the commands

                 cms admin mongo status
                 cms admin mongo stats
                 cms admin mongo list

              To stop it from running use the command

                 cms admin mongo stop

              System information about your machine can be returned by

                 cms admin system info

              This can be very useful in case you are filing an issue or bug.
        """

        # arguments.PATH = arguments['--download'] or None
        result = None

        if arguments.mongo:

            if arguments.install:

                print("MongoDB install")
                print(79 * "=")
                installer = MongoInstaller()
                r = installer.install()
                return r

            elif arguments.status:

                mongo = MongoDBController()
                state = mongo.status()

                if "error" in state["status"]:
                    Console.error(state["message"])
                    print(Printer.attribute(state))
                else:
                    data = dotdict()

                    for pid in state['output']:
                        entry = state['output'][pid]
                        data["pid"] = state['output'][pid]
                        data["command"] = state['output'][pid][
                            'command'].strip()

                    print(Printer.dict(data, order=["pid", "command"]))
                    Console.ok(str(data.pid['pid']) + " " + state["message"])

            elif arguments.version:
                print("MongoDB Version")
                print(79 * "=")
                mongo = MongoDBController()
                r = mongo.version()
                print(r)

            elif arguments.security:

                mongo = MongoDBController()
                mongo.set_auth()
                print()

            elif arguments.create:

                print("MongoDB create")
                MongoDBController().create()

            elif arguments.start:

                print("MongoDB start")
                MongoDBController().start(security=True)

            elif arguments.stop:

                print("MongoDB stop")
                MongoDBController().stop()

            elif arguments.backup:

                print("MongoDB backup")
                MongoDBController().dump(arguments.get('FILENAME'))

            elif arguments.load:

                print("MongoDB backup")
                MongoDBController().restore(arguments.get('FILENAME'))

            elif arguments.stats:

                mongo = MongoDBController()
                r = mongo.stats()

                if len(r) > 0:
                    print(Printer.attribute(r))
                    Console.ok("ok")
                else:
                    Console.ok("is your MongoDB server running")

            elif arguments.list:

                mongo = MongoDBController()
                r = mongo.list()

                if len(r) > 0:
                    print(
                        Printer.dict(r, order=["name", "sizeOnDisk", "empty"]))
                    Console.ok("ok")
                else:
                    Console.ok("is your MongoDB server running")

        elif arguments.yaml and arguments.cat:

            path = path_expand("~/.cloudmesh/cloudmesh4.yaml")

            secrets = [
                "AZURE_SUBSCRIPTION_ID", "AZURE_TENANTID", "EC2_ACCESS_ID",
                "EC2_SECRET_KEY", "OS_PASSWORD", "MONGO_PASSWORD"
            ]

            with open(path) as f:
                content = f.read().split("\n")

            for line in content:
                if "TBD" not in line:
                    for attribute in secrets:
                        if attribute + ":" in line:
                            line = line.split(":")[0] + ": ********"
                            break
                print(line)
            return ""

        elif arguments.yaml and arguments.check:

            path = path_expand("~/.cloudmesh/cloudmesh4.yaml")
            print()
            r = Shell.live('/Users/grey/.pyenv/shims/yamllint ' + path)
            print(70 * '-')
            print(" line:column  description")
            print()

        elif arguments.rest:

            if arguments.start:

                print("Rest Service start")
                raise NotImplementedError

            elif arguments.stop:

                print("Rest Service stop")
                raise NotImplementedError

            elif arguments.status:

                print("Rest Service status")
                raise NotImplementedError

        elif arguments.status:

            config = Config()
            data = config.data["cloudmesh"]["data"]["mongo"]
            # self.expanduser()

            print("Rest Service status")

            print("MongoDB status")

            mongo = MongoDBController()

            print(mongo)
            # mongo.expanduser()
            # data = mongo.data
            # print ("DDD", data)

            # data["MONGO_VERSION"]  = '.'.join(str(x) for x in mongo.version())

            # print (data)
            # print(Printer.attribute(data))
            # mongo.set_auth()

        elif arguments.system:

            s = OperatingSystem.get()
            print(Printer.attribute(s))

        return result
Ejemplo n.º 12
0
    def do_iu(self, args, arguments):
        """
        ::

          Usage:
                iu lab [--port=PORT]
                iu connect [--port=PORT]
                iu config
                iu allocate
                iu ps
                iu info
                iu kill
                iu status
                iu jupyter [--port=PORT]
                iu port [--port=PORT]
                iu view
                iu [--user=USERNAME]
                   [--host=HOST]
                   [--node=NUMBER]
                   [--gpu=GPUS]
                   [--res=RESERVATION]
                iu res
                iu setup --user=USERNAME
                iu romeo [--user=USERNAME]

          This command allows you to inteactively log into roeo or volta

          Arguments:
              FILE    a file name
              HOST    the host is either rome or volta [default: romeo]
              NUMBER  is a number that specifies where to login
              GPUS    the number of GPU's to be used

          Options:
              --res=RESERVATION  [default: lijguo_11]

          Example:

              cms iu
                 logs in on the first available node, and uses 1 GPU
                 BUG: some reservation are not detected

              cms iu --node=random
                 logs in on the first available node and uses 1 GPU

              cms iu status
                 lists the status of rome and volta. The output will look like:


                    +-------------+-----------+
                    | romeo       | Used GPUs |
                    +-------------+-----------+
                    | r-001       |           |
                    | r-002       | 7         |
                    | r-003       | 7         |
                    | r-004       | 7         |
                    +-------------+-----------+

                    +-------+-----------+
                    | volta | Used GPUs |
                    +-------+-----------+
                    | r-005 | 5         |
                    | r-006 | 7         |
                    +-------+-----------+

                    Users on romeo

                        user1

                    Users on volta

                        user2
        """
        # VERBOSE(arguments)
        config = Config()["cloudmesh.iu"]
        config["port"] = arguments["--port"] or config["port"]

        # pprint (config)

        map_parameters(arguments,
                       "user",
                       "host",
                       "node",
                       "gpu")

        variables = Variables()
        # arguments["user"] = Parameter.find("user", arguments, variables)
        # if arguments.user is None:
        #    config = Config()
        #    arguments.user = config["cloudmesh.iu.user"]

        iu = Manager(config=config)

        if arguments.setup:

            iu.setup()
            return ""

        elif arguments.config:

            iu.config(config)

            return ""

        if arguments.status:

            iu.status(user=arguments.user)

            return ""

        elif arguments.res:

            iu.reservations(user=arguments.user)

            return ""

        elif arguments.allocate:

            iu.allocate(config)

            return ""

        elif arguments.ps:

            found = iu.ps(config)
            print("\n".join(found))
            return ""

        elif arguments.info:

            r = iu.info(config)
            print(Printer.attribute(r))
            return ""


        elif arguments.jupyter:

            found = iu.jupyter(config)
            # print ("\n".join(found))
            return ""

        elif arguments.connect:

            found = iu.connect(config)
            return ""

        elif arguments.port:

            found = iu.set_port(config)
            return ""

        elif arguments.view:

            found = iu.view(config)
            return ""

        elif arguments.lab:

            found = iu.lab(config)
            return ""

        elif arguments.kill:

            found = iu.ps(config)
            for line in found:
                line = line.replace("     ", " ")
                line = line.replace("    ", " ")
                line = line.replace("   ", " ")
                line = line.replace("  ", " ")
                parameter = line.split(" ", 3)
                if "python" in line:
                    id = parameter[1]
                    r = iu.kill(config, id)
                    print(r)
            return ""


        # elif arguments.romeo(user=arguments.user):

        #    iu.reservations(user=arguments.user)

        #    return ""

        else:

            arguments["host"] = Parameter.find("host", arguments, variables,
                                               {"host": "romeo"})
            arguments["node"] = Parameter.find("node", arguments, variables)
            arguments["gpu"] = int(Parameter.find("gpu", arguments, variables,
                                                  {"gpu": "1"}))

            # VERBOSE(arguments)

            banner(f"Login {arguments.host}")

            # iu.login(user=arguments.user,
            #               host=arguments.host,
            #               node=arguments.node,
            #               gpus=arguments.gpu)

            iu.smart_login(user=arguments.user,
                           host=arguments.host,
                           node=arguments.node,
                           gpus=arguments.gpu)

        return ""
Ejemplo n.º 13
0
    def do_robot(self, args, arguments):
        """
        ::

          Usage:
                robot welcome
                robot osx install
                robot osx driver
                robot image fetch
                robot probe [--format=FORMAT]
                robot flash erase [--dryrun]
                robot flash python [--dryrun]
                robot test
                robot run PROGRAM
                robot credentials set SSID USERNAME PASSWORD
                robot put (credentials | cred)
                robot list (credentials | cred)
                robot login
                robot set PORT NOT IMPLEMENTED
                robot ls [PATH]
                robot put [-o] SOURCE [DESTINATION]
                robot get PATH
                robot rm PATH
                robot rmdir PATH
                robot dance FILE IPS
                robot inventory list [--cat] [--path=PATH] [ID]
                robot inventory export FILENAME
                robot reset
                
          Arguments:
              FILE   a file name

          Options:
              -f      specify the file
        """

        # pprint(arguments)

        # "wget http://micropython.org/resources/firmware/esp8266-20170108-v1.8.7.bin"

        arguments.dryrun = arguments["--dryrun"]

        def _run(command):
            print(command)
            if arguments.dryrun:
                print(command)
            else:
                os.system(command)

        def _continue(msg):
            if not arguments.dryrun:
                c = yn_choice(msg, default='y')

        if arguments.welcome:
            print(self.Banner.show())

        elif arguments.login:

            p = Probe()
            Console.error(
                "If you do not see a >>> please press the reset button.")
            print(p)

            data = {'tty': p.tty, 'baudrate': "115200"}

            # if 'tty.SLAB_USBtoUART' in p.tty:
            #    data["baudrate"] = "9600"
            # else:
            #    data["baudrate"] = "115200"

            os.system(
                "picocom --imap lfcrlf -b {baudrate} {tty}".format(**data))

        elif arguments.flash and arguments.erase:

            p = Probe()
            print(p.tty)
            print("Please press the right buttons")

            _continue("continue?")
            command = "esptool.py --port {} erase_flash".format(p.tty)
            _run(command)

        elif arguments.flash and arguments.python:

            p = Probe()
            print(p.tty)
            print("Please press the right buttons")

            _continue("continue?")

            d = {
                "baud": str(9600 * 6),
                "dir": ".",
                "image": "esp8266-20170108-v1.8.7.bin",
                "port": p.tty
            }

            if 'tty.SLAB_USBtoUART' in p.tty:
                d["baud"] = str(460800)

            command = "esptool.py --port {port} --baud {baud} write_flash --flash_size=detect -fm dio 0x00000 {image}".format(
                **d)
            _run(command)
            # "esptool.py --port /dev/tty.wchusbserial1410 --baud 9600 write_flash --flash_size=detect -fm dio 0x00000 esp8266-20170108-v1.8.7.bin"

        elif arguments.osx and arguments.install:

            o = sys.platform

            print(o)

            if sys.platform == 'darwin':
                if Shell.command_exists("brew"):
                    pass
                else:
                    os.system(
                        '/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"'
                    )

                os.system("pip install pyserial")
                #
                # INSTALLING COMMANDS WITH BREW
                #
                for package in ["lua", "picocom", "wget"]:
                    try:
                        print("installing command", package)
                        r = Shell.which(package)
                        if r is None:
                            r = Brew.install(package)
                        else:
                            print("    [OK]", package, "already installed")
                    except Exception as e:
                        print("Error", e, type(e))
                #
                # INSTALLING LIBRARIES WITH BREW
                #
                # libusb-compat
                for package in ["libusb", "mosquitto"]:
                    try:
                        print("installing", package)
                        r = Brew.install(package)
                        if r is None:
                            r = Brew.install(package)
                        else:
                            Console.error(
                                package +
                                " package already installed. skipping.")
                    except Exception as e:
                        print("Error", e, type(e))
                #
                # INSTALLING CASK LIBRARIES AND COMMANDS WITH BREW
                #
                # libusb-compat
                for package in ["adafruit-arduino",
                                "pycharm-ce"]:  # "aquamacs"
                    try:
                        print("installing", package)

                        os.system("brew cask install {}".format(package))

                    except Exception as e:
                        print("Error", e, type(e))

            if sys.platform == 'linux':
                Console.error(
                    "Linux not yet supported. Install lua and picocom.")
            return ""

        elif arguments.osx and arguments.driver:

            os.system(
                "brew tap mengbo/ch340g-ch34g-ch34x-mac-os-x-driver https://github.com/mengbo/ch340g-ch34g-ch34x-mac-os-x-driver"
            )
            os.system("brew cask install wch-ch34x-usb-serial-driver")

        elif arguments.probe:

            output_format = arguments["--format"] or "table"
            try:
                p = Probe()
                d = p.probe()

                print(Printer.attribute(d, output=output_format))

            except Exception as e:

                Error.traceback(error=e, debug=True, trace=True)

            return ""

        elif arguments.image and arguments.fetch:

            try:

                if os.path.isfile("esp8266-20170108-v1.8.7.bin"):
                    print("... image already downloaded")
                else:
                    os.system(
                        "wget http://micropython.org/resources/firmware/esp8266-20170108-v1.8.7.bin"
                    )

                    # g = Git()
                    # r = g.fetch()

            except Exception as e:

                Error.traceback(error=e, debug=True, trace=True)

            return ""

        elif arguments.run:

            p = Probe()
            d = {"port": p.tty, "program": arguments.PROGRAM}
            os.system("ampy --port {port} run {program}".format(**d))

        elif arguments.test:

            p = Probe()
            d = {"port": p.tty}
            test = textwrap.dedent("""
            n=3
            print('Count to', n)
            for i in range(1, n+1):
                print(i)
            """)
            with open("test.py", "w") as f:
                f.write(test)
            os.system("ampy --port {port} run test.py".format(**d))

        elif arguments.reset:

            p = Probe()
            d = {"port": p.tty}
            test = textwrap.dedent("""
            import machine
            machine.reset()
            """)
            with open("tmp-reset.py", "w") as f:
                f.write(test)
            os.system("ampy --port {port} run tmp-reset.py".format(**d))
            os.remove("tmp-reset.py")

        elif arguments.credentials and arguments.set:
            try:
                net = Network(ssid=arguments.SSID,
                              username=arguments.USERNAME,
                              password=arguments.PASSWORD)

            except Exception as e:
                Error.traceback(e)

        elif (arguments.credentials or arguments.cred) and arguments.put:
            try:
                filename = path_expand("~/.cloudmesh/robot/credentials.txt")
                p = Probe()
                #   print (p.tty)
                ampy = Ampy(p.tty)
                ampy.put(filename, "credentials.txt", False)
            except Exception as e:
                Error.traceback(e)
                sys.exit(1)

        elif arguments.put:
            try:
                t = StopWatch()
                t.start("put")

                size = os.path.getsize(arguments.SOURCE)

                optimize = arguments["-o"]
                p = Probe()
                ampy = Ampy(p.tty)
                ampy.put(arguments.SOURCE,
                         dest=arguments.DESTINATION,
                         optimize=optimize)
                t.stop("put")
                t.print("Time:", "put")
                print("Rate:", "{0:.2f}".format(size / t.get("put") / 1024),
                      "KB/s")
            except Exception as e:
                Error.traceback(e)

        elif arguments.credentials and arguments.list:
            try:
                p = Probe()
                ampy = Ampy(p.tty)
                filename = path_expand(
                    "~/.cloudmesh/robot/credentials.txt.robot")
                ampy.get("credentials.txt", filename)
                r = Shell.cat(filename)
                print(r)
                os.remove(filename)
            except Exception as e:
                Error.traceback(e)

        elif arguments.ls:
            try:
                p = Probe()
                ampy = Ampy(p.tty)
                r = ampy.ls()
                print(r)
            except Exception as e:
                Error.traceback(e)

        elif arguments.rm:
            try:
                p = Probe()
                ampy = Ampy(p.tty)
                ampy.rm(arguments.PATH)
            except Exception as e:
                Error.traceback(e)

        elif arguments.rmdir:
            try:
                p = Probe()
                ampy = Ampy(p.tty)
                ampy.rmdir(arguments.PATH)
            except Exception as e:
                Error.traceback(e)

        elif arguments.mkdir:
            try:
                p = Probe()
                ampy = Ampy(p.tty)
                ampy.mkdir(arguments.PATH)
            except Exception as e:
                Error.traceback(e)

        elif arguments.dance:

            pprint(arguments)

            from cloudmesh.robot.turtles import Car, Cars
            import turtle

            iplist = Parameter.expand(arguments.IPS)

            print(iplist)

            ips = []
            i = 1
            for ip in iplist:
                spec = [i, ip]
                ips.append(spec)
                i = i + 1
            print("IPS", ips)

            try:

                colors = [
                    'blue', 'red', 'green', 'oragne', 'gray', 'brown', 'cyan',
                    'pink', 'purple', 'tomato'
                ]

                cars = Cars(ips)

                print(cars)
                cars.read_dance(arguments.FILE)

                wn = turtle.Screen()  # creates a graphics window

                # def a():
                for i in range(0, len(ips)):
                    car = Car(i + 1, "robi" + str(i + 1), ips[i], colors[i])
                    cars.add(car)

                cars.run()

                wn.exitonclick()

            except Exception as e:
                Error.traceback(e)

        elif arguments.inventory and arguments.export:

            filename = arguments.FILENAME
            inventory = NetworkInventory(
                path_expand('~/.cloudmesh/robot/inventory.txt'))
            inventory.export(filename)

        elif arguments.inventory:

            def load_inventory(path):
                with open(path) as stream:
                    try:
                        d = yaml.safe_load(stream)
                    except Exception as e:
                        print("problem loading file", e)

                for id in d:
                    d[id]['id'] = str(id)
                return d

            path = path_expand(arguments["--path"]
                               or "~/.cloudmesh/robot/inventory.txt")

            print(path)

            if not os.path.isfile(path):
                print("ERROR: file does not exist")
                sys.exit(1)

            if arguments.ID:
                d = load_inventory(path)
                if arguments["--cat"]:
                    result = d[int(arguments.ID)]

                else:
                    result = Printer.attribute(d[int(arguments.ID)])
                print(result)

            elif arguments["--cat"]:
                with open(path) as stream:
                    try:
                        content = stream.read()
                        print(content)
                    except Exception as e:
                        print("problem loading file", e)

            else:

                d = load_inventory(path)
                table = Printer.dict(
                    d, order=['id', 'name', 'ip', 'mac', 'chipid'])

                print(table)
        '''