def restore_default_df_path(self): if self.is_running(): Executor.exec_sync(self.cmd.shutdown_vm(force=True)) time.sleep(3) Executor.exec_sync(self.cmd.sharedfolder_remove()) self.config.restore_default("data_folder") fullpath = get_full_path(self.config["data_folder"]) if not os.path.exists(fullpath): os.makedirs(fullpath) res = Executor.exec_sync(self.cmd.sharedfolder_setup(fullpath)) if res[0] != 0: raise IslandManagerException( "Datafolder reset finished with error: %s" % res[2])
def set_new_datafolder(self, new_path): if self.is_running(): Executor.exec_sync(self.cmd.shutdown_vm(force=True)) time.sleep(3) new_path_full = get_full_path(new_path) if not os.path.exists(new_path_full): os.makedirs(new_path_full) Executor.exec_sync(self.cmd.sharedfolder_remove()) self.config['data_folder'] = new_path_full self.config.save() res = Executor.exec_sync(self.cmd.sharedfolder_setup(new_path_full)) if res[0] != 0: raise IslandManagerException( "Datafolder setup finished with error: %s" % res[2])
def is_running(self): res = Executor.exec_sync(self.cmd.vminfo()) log.debug("IS RUNNING RES: %s" % str(res[1])) if res[0] != 0: return False vminfo = parse_vminfo_output(res[1]) return vminfo["VMState"] == "running"
def is_vbox_up_to_date(self): log.debug("Checking vbox version.") res = Executor.exec_sync(self.cmd.vboxmanage_version()) v_output = list(filter(None, res[1].split("\n"))) version = re.sub(r'[^\d^\.]', "", v_output[-1]) version = [int(i) for i in version.split('.')] version[2] = int(str(version[2])[0:2]) return (res[0] == 0 and version[0] == 5 and version[1] >= 2 and version[2] >= 18) or version[0] >= 6
def get_vm_list(self): try: execres = Executor.exec_sync(self.cmd.listvms()) if execres[0] != 0: return None res = [] lines = execres[1].split("\n") for line in lines: res.append([i.strip("\"{}") for i in line.split(" ")]) return res except Exception as e: log.error("Error getting vms list: %s" % str(e))
def wait_guest_additions(self): for i in range(10): # Check version 1.0.0 if (path.exists(self.config.get_stats_path()) and \ time() - lstat(self.config.get_stats_path()).st_mtime < 1.5) or \ Executor.exec_sync(self.cmd.ls_on_guest())[0] == 0: # Check version < 1.0.0 print("Looks like guestcontrol is available on Islands VM! Returning...") return print("Awaiting for initial setup to complete..") sleep(15) raise IslandSetupError("Guest additions installation timed out. Please restart setup")
def worker(): if self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) return if not self.is_running(): state_emitter(States.STARTING_UP) Executor.exec_sync(self.cmd.start_vm(headless)) t1 = time.time() while not self.is_boot_complete() and (time.time() - t1) < timeout: if self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) return time.sleep(4) if self.is_running(): state_emitter(States.RUNNING) elif self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) else: print("Startup error. VM hasn't started up") state_emitter(States.UNKNOWN)
def test_config(self): c = IMConfig("win32", "../", "../", "../os_defaults/") cmd = Cmd(c) command = cmd.ip_a_eth1_onguest() res = Exec.exec_sync(command) response = [line for line in res[1].split("\n") if "eth1" in line] for line in response: search_res = re.search(r'(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)', line) if search_res: ip = search_res.group() print(ip)
def get_islands_ip(self): res = Executor.exec_sync( self.cmd.vm_guestproperty( property="/VirtualBox/GuestInfo/Net/1/V4/IP")) if res[0] == 0 and re.search( r'(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)', res[1]): try: ip = res[1].split(": ")[1].strip() log.debug("Found IP address: %s" % res[1]) return ip except Exception as e: log.warning("Error getting IP address: %s" % str(e)) log.debug(res[1])
def is_islands_vm_exist(self): try: execres = Executor.exec_sync(self.cmd.listvms()) if execres[0] != 0: return False lines = execres[1].split("\n") pattern = re.compile("^\"%s\".*" % self.__config['vmname']) for line in lines: if pattern.match(line): return True return False except Exception as e: print("is_islands_vm_exist EXCEPTION!: " + str(e)) return False
def worker(): if self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) return if self.is_running(): state_emitter(States.SHUTTING_DOWN) Executor.exec_sync(self.cmd.shutdown_vm(force)) t1 = time.time() while self.is_running() and time.time() - t1 < timeout: if self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) return else: time.sleep(4) if self.setup.is_setup_required(): state_emitter(States.SETUP_REQUIRED) elif not self.is_running(): state_emitter(States.NOT_RUNNING) elif self.is_running(): log.debug("ERROR shutting down") state_emitter(States.RUNNING) else: log.error("Fatal error: Island state is unknown") state_emitter(States.UNKNOWN)
def _linux_install_proceed(self): """ Assuming that vbox installer is already downloaded :return: """ self.message("Running virtualbox installation script...") # Checking whether the installer really exists if self.path_to_vbox_distr is None or not path.exists( self.path_to_vbox_distr): log.error("Vbox installer not found. Aborting...") raise FileNotFoundError("Virtualbox installer not found") log.debug("Installer found") # Checking if user is root sudo_command = "" if getuid() != 0: log.debug("Checking sudo") sudo_flavors = ["gksudo", "pkexec", "kdesudo"] for f in sudo_flavors: if which(f) is not None: sudo_command = f break if sudo_command == "": raise Exception( "Cannot elevate rights. Please restart as root or install virtualbox manually" ) log.debug("Adding execute rights to the installer") res, stdout, stderr = Executor.exec_sync("chmod +x %s" % self.path_to_vbox_distr) if res != 0: raise Exception( "Unable to add execution rights for the Virtualbox installer.") log.debug("VBOX install: all prerequisites checked. Continuing...") self.message("Installing Virtualbox. This may take a few minutes.") cmd = "%s %s" % (sudo_command, self.path_to_vbox_distr) res, stdout, stderr = self.install_vbox_linux(cmd) self.message(stdout) self.complete(True, "Virtualbox installed successfully.")
def setup_host_only_adapter(self): """ This assumes that VM is already imported There is no way to check whether vboxnet0 interface exists, so we issue erroneous command to modify it if exitcode is 1 - interface doesn't exists, so we create it if exitcode is 2 - interface found and we can add it to our VM Otherwise there is some other error and we raise it """ res = Executor.exec_sync(self.cmd.hostonly_config()) if res[0] == 1: Executor.exec_sync(self.cmd.hostonly_create()) elif res[0] != 2: raise Exception(res[2]) """Installing adapter onto vm """ self.message("Enabling DHCP...") Executor.exec_sync(self.cmd.hostonly_enable_dhcp()) return Executor.exec_sync(self.cmd.hostonly_setup())
def is_boot_complete(self): # Version 1.0.0 check return (os.path.exists(self.config.get_stats_path()) and \ time.time() - os.lstat(self.config.get_stats_path()).st_mtime < 1.5) or \ Executor.exec_sync(self.cmd.ls_on_guest())[0] == 0 # Version <1.0.0 check
def test_vbox_uninstall(self): res = Executor.exec_sync( """osascript -e 'do shell script "{mpuntpoint}VirtualBox_Uninstall.tool --unattended" with administrator privileges' """.format( mpuntpoint=self.config['vbox_distro_mountpoint']) ) print("done")
def test_get_ip(self): res = Executor.exec_sync('vboxmanage guestcontrol Island run --exe "/sbin/ip" ' '--username root --password islands --wait-stdout -- ip a | grep eth1') a = re.search(r'(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)', res[1]).group() print(a)
def test_vm_setup(self): res = Executor.exec_sync( """vboxmanage guestcontrol Island run --exe "/usr/bin/wget" --username root --password islands --wait-stdout --wait-stderr -- wget "https://raw.githubusercontent.com/viocost/islands/dev/installer/vbox_full_setup.sh" -O "/root/isetup.sh" """, verbose=True) print(Executor.exec_sync( """vboxmanage guestcontrol Island run --exe "/bin/chmod" --username root --password islands --wait-stdout --wait-stderr -- chmod +x /root/isetup.sh """, True))
def start_vm(headless=True): headless = " --type headless " if headless else "" cmd = "{vboxmanage} startvm Island {headless}".format(vboxmanage=self.config["vboxmanage"], headless=headless) return Executor.exec_sync(cmd)
def _get_guest_properties(self): return Executor.exec_sync(self.cmd.vm_guestproperty())
def delete_islands_vm(self): return Executor.exec_sync(self.cmd.delete_vm())
def _get_vminfo(self): return Executor.exec_sync(self.cmd.vminfo())
def start_vm(self, headless=True): return Executor.exec_sync(self.cmd.start_vm(headless))
def setup_shared_folder(self, data_folder_path=""): fullpath = get_full_path(data_folder_path) if not path.exists(fullpath): makedirs(fullpath) return Executor.exec_sync(self.cmd.sharedfolder_setup(fullpath))
def insert_guest_additions_image(self): return Executor.exec_sync(self.cmd.insert_guest_additions())
def test_safe_exec(self): a = Executor.exec_sync('curl {link} -o ~/Downloads/test.dmg'.format(link=self.config['vbox_download'])) print(str(a))
def onvm_get_setup_script(self): return Executor.exec_sync(self.cmd.onvm_get_setup_script())
def shutdown_vm(self, force=False): return Executor.exec_sync(self.cmd.shutdown_vm(force=force))
def onvm_chmodx_install_script(self): return Executor.exec_sync(self.cmd.onvm_chmodx_install_script())
def stop_island_sync(self, force=True): return Executor.exec_sync(self.cmd.shutdown_vm(force))
def is_vbox_installed(self): com = self.cmd.vboxmanage_version() res = Executor.exec_sync(com) log.debug("is_vbox_installed 0: %s 1: %s 2:%s " % (str(res[0]), str(res[1]), str(res[2]))) return res[0] == 0