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 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 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