def __init__(self, file_or_obj, tmpdir=None, mibs_paths=None, board=None): # TODO: fix at some point, this tmpdir is already relative to the CM config you # are grabbing? Not ideal as that dir might not be writeable, or a tftp or http URL # at some point - need to use a real local tmpdir or maybe even results so we can # save the resulting artifacts in other tools if mibs_paths is None: mibs_paths = [] if tmpdir is None: tmpdir = tempfile.mkdtemp() assert board, "board is a required argument" if mibs_paths == []: mibs_paths = getattr(board, "mibs_paths", []) if mibs_paths != []: mibs_path_arg = "-M " for mibs_path in mibs_paths: mibs_path_arg = mibs_path_arg + ":" + mibs_path self.mibs_path_arg = mibs_path_arg # TODO: this is all a bit wild here, need to clean up everything.. if isinstance(file_or_obj, cm_cfg): self.cm_cfg = file_or_obj # TODO: this seems like the wrong place to store these but OK self.dir_path = os.path.join(os.path.split(__file__)[0], tmpdir) self.file = self.cm_cfg.original_fname self.file_path = os.path.join(self.dir_path, self.file) else: self.file_path = file_or_obj self.dir_path = os.path.join(os.path.split(file_or_obj)[0], tmpdir) self.file = os.path.split(file_or_obj)[1] # make target tmpdir if it does not exist try: os.makedirs(self.dir_path) except OSError as err: import errno # Reraise the error unless it's about an already existing directory if err.errno != errno.EEXIST or not os.path.isdir(self.dir_path): raise if isinstance(file_or_obj, cm_cfg): self.cm_cfg.save(self.file_path) assert cmd_exists("docsis") assert cmd_exists("tclsh") tclsh = Tkinter.Tcl() assert tclsh.eval( "package require sha1" ), "please run apt-get install tcllib first"
def check_for_cmd_on_host(cmd, msg=None): """Print an error message with a suggestion on how to install the command.""" from boardfarm.lib.common import cmd_exists if not cmd_exists(cmd): logger.error( termcolor.colored( "\nThe command '" + cmd + "' is NOT installed on your system. Please install it.", None, attrs=["bold"], )) if msg is not None: logger.info(cmd + ": " + msg) import sys if sys.platform == "linux2": import platform if "Ubuntu" in platform.dist() or "debian" in platform.dist(): logger.error( "To install run:\n\tsudo apt install <package with " + cmd + ">") exit(1) logger.debug( "To install refer to your system SW app installation instructions")
def download_from_web(url, server, username, password, port): pipe = "" if cmd_exists('pv'): pipe = " pv | " cmd = "curl -n -L -k '%s' 2>/dev/null | %s ssh -p %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -x %s@%s \"mkdir -p /tftpboot/tmp; tmpfile=\`mktemp /tftpboot/tmp/XXXXX\`; cat - > \$tmpfile; chmod a+rw \$tmpfile; echo \$tmpfile\"" % ( url, pipe, port, username, server) return copy_file_to_server(cmd, password)
def scp_to_tftp_server(fname, server, username, password, port): # local file verify it exists first if not os.path.isfile(fname): print_bold("File passed as parameter does not exist! Failing!\n") sys.exit(10) pipe = "" if cmd_exists('pv'): pipe = " pv | " cmd = "cat %s | %s ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -p %s -x %s@%s \"mkdir -p /tftpboot/tmp; tmpfile=\`mktemp /tftpboot/tmp/XXXXX\`; cat - > \$tmpfile; chmod a+rw \$tmpfile; echo \$tmpfile\"" % ( fname, pipe, port, username, server) return copy_file_to_server(cmd, password)
def __init__(self, model, conn_cmd, power_ip, power_outlet, output=sys.stdout, password='******', web_proxy=None, tftp_server=None, tftp_username=None, tftp_password=None, tftp_port=None, connection_type=None, power_username=None, power_password=None, rootfs=None, kernel=None, mgr=None, **kwargs): """Intialize the variables that are used across function which include the tftp_server, credential, power_ip, credentials etc.. :param model: Model of the QEMU device. :type model: string :param conn_cmd: The connection command that is used to connect to QEMU device :type conn_cmd: string :param power_ip: IP Address of power unit to which this device is connected :type power_ip: string :param power_outlet: Outlet # this device is connected :type power_outlet: string :param output: Stores the system standard output, defaults to sys.stdout :type output: string :param password: The password used to connect to the device, defaults to "bigfoot1" :type password: string :param web_proxy: The web proxy to be used, defaults to None :type web_proxy: string :param tftp_server: The tftp_server ip address, defaults to None :type tftp_server: string :param tftp_username: The tftp server userame, defaults to None :type tftp_username: string :param tftp_password: The tftp server password to be used, defaults to None :type tftp_password: string :param tftp_port: The port number that can be used to connect to tftp, defaults to None :type tftp_port: string :param connection_type: The connection type to used to connect. :type connection_type: string :param power_username: The username to be used over power unit connection to the device, defaults to None :type power_username: string :param power_password: The password to be used over power unit connection to the device, defaults to None :type power_password: string :param rootfs: The complete url of the image to be loaded, defaults to None :type rootfs: string :param kernel: The kernel image path to be used to flash to the device, defaults to None :type kernel: string :param ``**kwargs``: Extra set of arguements to be used if any. :type ``**kwargs``: dict :raises: Exception "The QEMU device type requires specifying a rootfs" """ self.consoles = [self] self.dev = mgr assert cmd_exists('qemu-system-i386') if rootfs is None: raise Exception( "The QEMU device type requires specifying a rootfs") def temp_download(url): """Download the image to the temp folder over the QEMU device. :param url: URL where the file is location (URL path of the file to be downloaded), defaults to None :type url: string :returns: The filename of the downloaded file. :rtype: string """ dl_console = bft_pexpect_helper.spawn("bash --noprofile --norc") dl_console.sendline('export PS1="prompt>>"') dl_console.expect_exact("prompt>>") dl_console.sendline('mktemp') dl_console.expect('/tmp/tmp.*') fname = dl_console.match.group(0).strip() dl_console.expect_exact("prompt>>") self.cleanup_files.append(fname) atexit.register(self.run_cleanup_cmd) dl_console.logfile_read = sys.stdout print("Temp downloaded file = %s" % url) dl_console.sendline("curl -n -L -k '%s' > %s" % (url, fname)) dl_console.expect_exact("prompt>>", timeout=500) dl_console.logfile_read = None dl_console.sendline('exit') dl_console.expect(pexpect.EOF) return fname if rootfs.startswith("http://") or rootfs.startswith("https://"): rootfs = temp_download(rootfs) cmd = "%s %s" % (conn_cmd, rootfs) if kernel is not None: if kernel.startswith("http://") or kernel.startswith("https://"): kernel = temp_download(kernel) cmd += " -kernel %s --append root=/dev/hda2" % kernel # check if we can run kvm kvm_chk = bft_pexpect_helper.spawn('sudo kvm-ok') if 0 != kvm_chk.expect(['KVM acceleration can be used', pexpect.EOF]): cmd = cmd.replace('--enable-kvm ', '') self.kvm = False # TODO: add script=no,downscript=no to taps try: bft_pexpect_helper.spawn.__init__(self, command='/bin/bash', args=["-c", cmd], env=self.dev.env) self.expect(pexpect.TIMEOUT, timeout=1) except pexpect.EOF: self.pid = None if 'failed to initialize KVM: Device or resource busy' in self.before or \ 'failed to initialize KVM: Cannot allocate memory' in self.before: cmd = cmd.replace('--enable-kvm ', '') self.kvm = False bft_pexpect_helper.spawn.__init__(self, command='/bin/bash', args=["-c", cmd], env=self.dev.env) else: raise self.cmd = cmd if kernel is None: self.expect(["SYSLINUX", "GNU GRUB"]) self.logfile_read = output atexit.register(self.kill_console_at_exit)