Exemplo n.º 1
0
def process_arguments(arguments):
    version = cm4.__version__

    arguments = dotdict(arguments)

    if arguments.get("--version"):
        print(version)

    elif arguments.admin and arguments.mongo:
        print("MONGO")
        result = cm4.mongo.MongoDBController.process_arguments(arguments)
        print(result)

    elif arguments.get("vm"):
        result = cm4.vm.Vm.process_arguments(arguments)
        print(result)

    elif arguments.get('aws'):
        cm4.aws.CommandAWS.process_arguments(arguments)

    elif arguments.get("vagrant"):
        cm4.vagrant.vagrant_basic.process_arguments(arguments)

    elif arguments.get("vcluster"):
        cm4.vcluster.VirtualCluster.process_arguments(arguments)

    elif arguments.get("batch"):
        cm4.batch.Batch.process_arguments(arguments)

    elif arguments.get("openstack"):
        cm4.openstack.OpenstackCM.process_arguments(arguments)

    elif arguments.get("data"):
        cm4.data.data.process_arguments(arguments)

    elif arguments.get("set"):
        config = Config()

        if arguments.get("cloud"):
            config.set("default.cloud", arguments.get("CLOUD"))

        elif arguments.get("group"):
            config.set("default.group", arguments.get("GROUP"))

        elif arguments.get("role"):
            config.set("default.role", arguments.get("ROLE"))

        elif arguments.get("cluster"):
            config.set("default.cluster", arguments.get("CLUSTER"))

        elif arguments.get("experiment"):
            config.set("default.experiment", arguments.get("EXPERIMENT"))

        elif arguments.get("host"):
            config.set("default.host", arguments.get("HOST"))

        elif arguments.get("--key") and arguments.get("--value"):
            config.set(arguments.get("--key"), arguments.get("--value"))

        print("Config has been updated.")
Exemplo n.º 2
0
 def convert(line):
     line = line.replace("(", "")
     line = line.replace(")", "")
     line = line.replace(",", "")
     entry = line.split(" ")
     data = dotdict()
     data.name = entry[0]
     data.provider = entry[1]
     data.date = entry[2]
     return data
Exemplo n.º 3
0
Arquivo: vm.py Projeto: kimballXD/cm
        def convert(line):
            entry = (' '.join(line.split())).split(' ')
            data = dotdict()
            data.id = entry[0]
            data.name = entry[1]
            data.provider = entry[2]
            data.state = entry[3]
            data.directory = entry[4]

            return data
Exemplo n.º 4
0
Arquivo: vm.py Projeto: kimballXD/cm
    def create(cls, **kwargs):

        arg = dotdict(kwargs)

        if not os.path.exists(arg.name):
            os.makedirs(arg.name)

        config = cls.vagrantfile(**kwargs)

        with open('{name}/Vagrantfile'.format(**arg), 'w') as f:
            f.write(config)
Exemplo n.º 5
0
def defaults():
    """
    default values
    :return: a number of default values for memory, image, and script
    :rtype: dotdict
    """
    d = dotdict()
    d.memory = 1024
    # d.image = "ubuntu/xenial64"
    d.image = "ubuntu/trusty64"
    d.port = 8080
    d.script = None
    return d
Exemplo n.º 6
0
Arquivo: vm.py Projeto: kimballXD/cm
    def vagrantfile(cls, **kwargs):

        arg = dotdict(kwargs)

        provision = kwargs.get("script", None)

        if provision is not None:
            arg.provision = 'config.vm.provision "shell", inline: <<-SHELL\n'
            for line in textwrap.dedent(provision).split("\n"):
                if line.strip() != "":
                    arg.provision += 12 * " " + "    " + line + "\n"
            arg.provision += 12 * " " + "  " + "SHELL\n"
        else:
            arg.provision = ""

        # not sure how I2 gets found TODO verify, comment bellow is not enough
        # the 12 is derived from the indentation of Vagrant in the script
        # TODO we may need not just port 80 to forward
        script = textwrap.dedent("""
            Vagrant.configure(2) do |config|

              config.vm.define "{name}"
              config.vm.hostname = "{name}"
              config.vm.box = "{image}"
              config.vm.box_check_update = true
              config.vm.network "forwarded_port", guest: 80, host: {port}
              config.vm.network "private_network", type: "dhcp"

              # config.vm.network "public_network"
              # config.vm.synced_folder "../data", "/vagrant_data"
              config.vm.provider "virtualbox" do |vb|
                 # vb.gui = true
                 vb.memory = "{memory}"
              end
              {provision}
            end
        """.format(**arg))

        return script
Exemplo n.º 7
0
Arquivo: vm.py Projeto: kimballXD/cm
    def boot(cls, **kwargs):

        arg = dotdict(kwargs)
        arg.cwd = kwargs.get("cwd", None)

        vms = cls.to_dict(cls.list())

        if arg.name in vms:
            Console.error("vm {name} already booted".format(**arg),
                          traceflag=False)
            return None
        # print result

        else:
            cls.create(**kwargs)
            Console.ok("{name} created".format(**arg))
            Console.ok("{name} booting ...".format(**arg))

            result = Shell.execute("vagrant", ["up", arg.name], cwd=arg.name)
            Console.ok("{name} ok.".format(**arg))

            return result
Exemplo n.º 8
0
def do_vbox(argv):
    """
    ::

      Usage:
        vbox version [--format=FORMAT]
        vbox image list [--format=FORMAT]
        vbox image find NAME
        vbox image add NAME
        vbox vm list [--format=FORMAT] [-v]
        vbox vm delete NAME
        vbox vm config NAME
        vbox vm ip NAME [--all]
        vbox 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]
    """
    arg = dotdict(docopt(do_vbox.__doc__, argv))
    arg.format = arg["--format"] or "table"
    arg.verbose = arg["-v"]
    arg.all = arg["--all"]

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

    elif arg.image and arg.list:
        l = cm4.vagrant.image.list(verbose=arg.verbose)
        _LIST_PRINT(l, arg.format, order=["name", "provider", "date"])

    elif arg.image and arg.add:
        l = cm4.vagrant.image.add(arg.NAME)
        print(l)

    elif arg.image and arg.find:
        l = cm4.vagrant.image.find(arg.NAME)
        print(l)

    elif arg.vm and arg.list:
        l = cm4.vagrant.vm.list()
        _LIST_PRINT(l,
                    arg.format,
                    order=["name", "state", "id", "provider", "directory"])

    elif arg.create and arg.list:

        result = Shell.cat("{NAME}/Vagrantfile".format(**arg))
        print(result)

    elif arg.create:

        d = defaults()

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

        cm4.vagrant.vm.create(name=arg.NAME,
                              memory=arg.memory,
                              image=arg.image,
                              script=arg.script)

    elif arg.config:

        # arg.NAME
        d = cm4.vagrant.vm.info(name=arg.NAME)

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

        print(result)

    elif arg.ip:

        data = []
        result = cm4.vagrant.vm.execute(arg.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 arg.all:
            d = {}
            i = 0
            for e in data:
                d[str(i)] = e
                i = i + 1
            result = Printer.attribute(d, output=arg.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 arg.boot:

        d = defaults()

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

        cm4.vagrant.vm.boot(name=arg.NAME,
                            memory=arg.memory,
                            image=arg.image,
                            script=arg.script,
                            port=arg.port)

    elif arg.delete:

        result = cm4.vagrant.vm.delete(name=arg.NAME)
        print(result)

    elif arg.ssh:

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

    else:

        print("use help")