def wbatch_new_file(file_name): """Create new Windows batch file""" global OUT_FILE hf.create_dir(WBATCH_OUT_DIR) OUT_FILE = os.path.join(WBATCH_OUT_DIR, file_name) open(OUT_FILE, "a").close()
def create_vdi(path, size): # Make sure target directory exists hf.create_dir(os.path.dirname(path)) logger.info("Creating disk (size: %s MB):\n\t%s", size, path) vbm("createhd", "--format", "VDI", "--filename", path, "--size", str(size))
def vm_ssh(vm_name, *args, **kwargs): logger.info('%s(): caller: %s()', log_utils.get_fname(1), log_utils.get_fname(2)) key_path = get_osbash_private_key() live_log = kwargs.pop('log_file', None) show_err = kwargs.pop('show_err', True) try: target = "{}@{}".format(conf.vm_shell_user, conf.vm[vm_name].ssh_ip) target_port = str(conf.vm[vm_name].ssh_port) # To avoid getting stuck on broken ssh connection, disable # connection sharing (ControlPath) and use a timeout when connecting. full_args = [ "ssh", "-q", "-i", key_path, "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", "-o", "ControlPath=none", "-p", target_port, target ] + list(args) logger.debug("vm_ssh: %s", ' '.join(full_args)) ssh_log = os.path.join(conf.log_dir, "ssh.log") with open(ssh_log, 'a') as logf: print(' '.join(full_args), file=logf) if live_log: logger.debug("Writing live log for ssh call at %s.", live_log) hf.create_dir(os.path.dirname(live_log)) # Unbuffered log file with open(live_log, 'a', 0) as live_logf: ret = subprocess.call(full_args, stderr=subprocess.STDOUT, stdout=live_logf) if ret: err_msg = "ssh returned status {}.".format(ret) logger.error("%s", err_msg) # Indicate error in status dir open(os.path.join(conf.status_dir, "error"), 'a').close() raise EnvironmentError output = None else: try: output = subprocess.check_output(full_args, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: if show_err: logger.exception("vm_ssh: Aborting.") traceback.print_exc(file=sys.stdout) sys.exit(1) raise EnvironmentError except subprocess.CalledProcessError as err: logger.debug("ERROR ssh %s", full_args) logger.debug("ERROR rc %s", err.returncode) logger.debug("ERROR output %s", err.output) raise EnvironmentError return output
def create_vdi(path, size): logger.info('%s(): caller: %s()', log_utils.get_fname(1), log_utils.get_fname(2)) # Make sure target directory exists hf.create_dir(os.path.dirname(path)) logger.info("Creating disk (size: %s MB):\n\t%s", size, path) vbm("createhd", "--format", "VDI", "--filename", path, "--size", str(size))
def download(self, url, target_path=None): try: logger.debug("Trying to download: %s to %s", url, target_path) logger.debug("Proxy: %s", self.get_urllib_proxy()) response = urllib2.urlopen(url) if target_path: # Make sure target directory exits hf.create_dir(os.path.dirname(target_path)) with open(target_path, 'wb') as out: try: out.write(response.read()) except urllib2.URLError as err: # Download failed, remove empty file os.remove(target_path) else: return response.read() except (urllib2.URLError, httplib.BadStatusLine) as err: logger.debug("download() failed, %s for %s", type(err), url) raise EnvironmentError
def vm_ssh(vm_name, *args, **kwargs): key_path = get_osbash_private_key() live_log = kwargs.pop('log_file', None) show_err = kwargs.pop('show_err', True) try: target = "{}@{}".format(conf.vm_shell_user, conf.vm[vm_name].ssh_ip) target_port = str(conf.vm[vm_name].ssh_port) # To avoid getting stuck on broken ssh connection, disable # connection sharing (ControlPath) and use a timeout when connecting. full_args = ["ssh", "-q", "-i", key_path, "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=10", "-o", "ControlPath=none", "-p", target_port, target] + list(args) logger.debug("vm_ssh: %s", ' '.join(full_args)) ssh_log = os.path.join(conf.log_dir, "ssh.log") with open(ssh_log, 'a') as logf: print(' '.join(full_args), file=logf) if live_log: logger.debug("Writing live log for ssh call at %s.", live_log) hf.create_dir(os.path.dirname(live_log)) # Unbuffered log file with open(live_log, 'a', 0) as live_logf: ret = subprocess.call(full_args, stderr=subprocess.STDOUT, stdout=live_logf) if ret: err_msg = "ssh returned status {}.".format(ret) logger.error("%s", err_msg) # Indicate error in status dir open(os.path.join(conf.status_dir, "error"), 'a').close() raise EnvironmentError output = None else: try: output = subprocess.check_output(full_args, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: if show_err: logger.exception("vm_ssh: Aborting.") traceback.print_exc(file=sys.stdout) sys.exit(1) raise EnvironmentError except subprocess.CalledProcessError as err: logger.debug("ERROR ssh %s", full_args) logger.debug("ERROR rc %s", err.returncode) logger.debug("ERROR output %s", err.output) raise EnvironmentError return output