class TestName: image_test = False vbox = '6.0.4' image_name = "generic/ubuntu1810" size = 1024 cloud = "vagrant" def print_images(self): images = self.p.images() print( Printer.flatwrite(images, sort_keys=["name"], order=["name", "provider", "version"], header=["Name", "Provider", "Version"])) def next_name(self): self.name_generator.incr() self.new_name = str(self.name_generator) return self.new_name def setup(self): banner("setup", c="-") self.user = Config()["cloudmesh.profile.user"] self.name_generator = Name(experiment="exp", group="grp", user=self.user, kind="vm", counter=1) self.name = str(self.name_generator) self.name_generator.incr() self.new_name = str(self.name_generator) self.p = Provider(name=self.cloud) def test_01_version(self): HEADING() r = self.p.version() pprint(r) assert self.vbox_version == r["virtualbox"]["extension"]["version"] assert self.vbox_version == r["virtualbox"]["version"] def test_02_list_os(self): HEADING() ostypes = self.p.list_os() print( Printer.write(ostypes, order=[ "id", "64_bit", "description", "family_descr", "family_id" ], header=[ "id", "64_bit", "description", "family_descr", "family_id" ])) def test_03_name(self): HEADING() print(self.name) assert self.name == "exp-grp-{user}-vm-1".format(user=self.user) def test_04_list_images(self): HEADING() self.print_images() def test_05_delete_image(self): HEADING() if self.image_test: name = "generic/ubuntu1810" try: images = self.p.delete_image(self.image_name) print("delete", self.image_name) except: print("image", self.image_name, "nor found") self.print_images() else: print("not executed as image_test is not True. ok") def test_06_add_image(self): HEADING() if self.image_test: images = self.p.add_image(self.image_name) print("I", images) self.print_images() assert images.status == 0 else: print("not executed as image_test is not True. ok") def test_07_list_vm(self): HEADING() vms = self.p.info() pprint(vms) print( Printer.flatwrite( vms, order=[ "vagrant.name", "vbox.name", "vagrant.id", "vagrant.provider", "vagrant.state", "vagrant.hostname" ], header=["name", "vbox", "id", "provider", "state", "hostname"])) def test_10_create(self): HEADING() name = self.next_name() print("Name", name) self.p.create( name=self.name, image=self.image_name, size=self.size, # username as the keypair name based on # the key implementation logic ex_keyname=self.user, ex_security_groups=['default']) directory = Path( path_expand("~/.cloudmesh/vagrant/exp-grp-gregor-vm-1")) assert directory.is_dir() time.sleep(5) nodes = self.p.list() pprint(nodes) node = self.p.find(nodes, name=self.name) pprint(node) nodes = self.p.list(raw=False) for node in nodes: if node.name == self.name: self.testnode = node break assert node is not None
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