def execute(self, name, command): """ execute the command on the named host :param name: the name of the host in config :param command: the command to be executed :return: """ if name in ["localhost"]: r = '\n'.join(Shell.sh("-c", command).split()[-1:]) else: r = '\n'.join(Shell.ssh(name, command).split()[-1:]) return r
def list(cls, verbose=False): 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 result = Shell.execute("vagrant", "global-status --prune") if verbose: print(result) if "There are no active" in result: return None lines = [] for line in result.split("\n")[2:]: if line == " ": break else: lines.append(convert(line)) return lines
def execute(cls, name, command, cwd=None): vms = cls.to_dict(cls.list()) arg = "ssh {} -c {}".format(name, command) result = Shell.execute("vagrant", ["ssh", name, "-c", command], cwd=vms[name]["directory"]) return result
def info(cls, name=None): result = Shell.execute("vagrant", ["ssh-config"], cwd=name) lines = result.split("\n") data = {} for line in lines: attribute, value = line.strip().split(" ", 1) if attribute == "IdentityFile": value = value.replace('"', '') data[attribute] = value return data
def list(cls): 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 result = Shell.execute("vagrant", ["box", "list"]) lines = [] for line in result.split("\n"): lines.append(convert(line)) return lines
def darwin(self, brew=False): """ install MongoDB in Darwin system (Mac) """ if brew: print("mongo installer via brew") Brew.install("mongodb") path = Shell.which("mongod") SystemPath.add("{path}".format(path=path)) else: script = """ mkdir -p {MONGO_PATH} mkdir -p {MONGO_HOME} mkdir -p {MONGO_LOG} curl -o /tmp/mongodb.tgz {MONGO_CODE} tar -zxvf /tmp/mongodb.tgz -C {LOCAL}/mongo --strip 1 """.format(**self.data) installer = Script(script) SystemPath.add("{MONGO_HOME}/bin".format(**self.data))
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
def add(cls, name): result = Shell.execute("vagrant", ["box", "add", name]) return result
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")
def suspend(cls, name): result = Shell.execute("vagrant", ["suspend", name]) return result
def resume(cls, name): result = Shell.execute("vagrant", ["resume", name]) return result
def delete(cls, name=None): result = Shell.execute("vagrant", ["destroy", "-f", name], cwd=name) return result