Beispiel #1
0
    def run(self, connection, max_end_time):
        connection = super().run(connection, max_end_time)
        # this may be the device namespace - the lxc namespace may not be
        # accessible
        lxc_name = None
        protocols = [
            protocol for protocol in self.job.protocols
            if protocol.name == LxcProtocol.name
        ]
        if protocols:
            lxc_name = protocols[0].lxc_name
        if not lxc_name:
            self.logger.debug("No LXC device requested")
            return connection

        # If there is no device_info then this action should be idempotent.
        if 'device_info' not in self.job.device:
            return connection

        device_info = self.job.device.get('device_info', [])
        device_info_file = os.path.join(self.mkdtemp(), 'device-info.yaml')
        with open(device_info_file, 'w') as device_info_obj:
            yaml.dump(device_info, device_info_obj)
        self.logger.debug("device info file '%s' created with:\n %s",
                          device_info_file, device_info)
        logging_url = master_cert = slave_cert = ipv6 = None
        job_id = self.job.job_id
        # pylint: disable=no-member
        if self.logger.handler:
            logging_url = self.logger.handler.logging_url
            master_cert = self.logger.handler.master_cert
            slave_cert = self.logger.handler.slave_cert
            ipv6 = self.logger.handler.ipv6
            job_id = self.logger.handler.job_id
        # The rules file will be created in job's temporary directory with
        # the name something like '100-lava-lxc-hikey-2808.rules'
        # where, 100 is just an arbitrary number which specifies loading
        # priority for udevd
        job_prefix = self.job.parameters["dispatcher"].get("prefix", "")
        rules_file_name = '100-lava-' + job_prefix + lxc_name + '.rules'
        rules_file = os.path.join(self.mkdtemp(), rules_file_name)
        lines = []
        for device in device_info:
            data = {
                'serial_number': str(device.get('board_id', '')),
                'vendor_id': device.get('usb_vendor_id'),
                'product_id': device.get('usb_product_id'),
                'fs_label': device.get('fs_label'),
                'lxc_name': lxc_name,
                'device_info_file': device_info_file,
                'logging_url': logging_url,
                'master_cert': master_cert,
                'slave_cert': slave_cert,
                'ipv6': ipv6,
                'job_id': job_id
            }
            str_lxc_udev_rule = lxc_udev_rule(data)
            if device.get('parent', False):
                str_lxc_udev_rule += lxc_udev_rule_parent(data)
            lines.append(str_lxc_udev_rule)
        if lines:
            self.logger.info("udev rules file '%s' created", rules_file)
        with open(rules_file, "w") as f_obj:
            f_obj.write("\n".join(lines))
        for line in lines:
            self.logger.debug(line)
        # Create symlink to rules file inside UDEV_RULES_DIR
        # See https://projects.linaro.org/browse/LAVA-1227
        os.symlink(rules_file, os.path.join(UDEV_RULES_DIR, rules_file_name))
        self.logger.info("'%s' symlinked to '%s'",
                         os.path.join(UDEV_RULES_DIR, rules_file_name),
                         rules_file)

        # Reload udev rules.
        reload_cmd = ['udevadm', 'control', '--reload-rules']
        # FIXME: if reload fails, that is a LAVABug.
        cmd_out = self.run_command(reload_cmd,
                                   allow_fail=True,
                                   allow_silent=True)
        if cmd_out is False:
            self.logger.debug('Reloading udev rules failed')
        else:
            self.logger.debug('udev rules reloaded.')
        return connection
Beispiel #2
0
    def run(self, connection, max_end_time):
        connection = super().run(connection, max_end_time)
        # this may be the device namespace - the lxc namespace may not be
        # accessible
        lxc_name = None
        protocols = [
            protocol for protocol in self.job.protocols
            if protocol.name == LxcProtocol.name
        ]
        if protocols:
            lxc_name = protocols[0].lxc_name
        if not lxc_name:
            self.logger.debug("No LXC device requested")
            return connection

        # If there is no device_info then this action should be idempotent.
        if "device_info" not in self.job.device:
            return connection

        device_info = self.job.device.get("device_info", [])
        device_info_file = os.path.join(self.mkdtemp(), "device-info.yaml")
        with open(device_info_file, "w") as device_info_obj:
            yaml_safe_dump(device_info, device_info_obj)
        self.logger.debug("device info file '%s' created with:\n %s",
                          device_info_file, device_info)
        logging_url = master_cert = slave_cert = socks_proxy = ipv6 = None
        job_id = self.job.job_id

        if self.logger.handler:
            logging_url = self.logger.handler.logging_url
            master_cert = self.logger.handler.master_cert
            slave_cert = self.logger.handler.slave_cert
            socks_proxy = self.logger.handler.socks_proxy
            ipv6 = self.logger.handler.ipv6
            job_id = self.logger.handler.job_id
        # The rules file will be created in job's temporary directory with
        # the name something like '100-lava-lxc-hikey-2808.rules'
        # where, 100 is just an arbitrary number which specifies loading
        # priority for udevd
        job_prefix = self.job.parameters["dispatcher"].get("prefix", "")
        rules_file_name = "100-lava-" + job_prefix + lxc_name + ".rules"
        rules_file = os.path.join(self.mkdtemp(), rules_file_name)
        lines = []
        for device in device_info:
            data = {
                "serial_number": str(device.get("board_id", "")),
                "vendor_id": device.get("usb_vendor_id"),
                "product_id": device.get("usb_product_id"),
                "fs_label": device.get("fs_label"),
                "lxc_name": lxc_name,
                "device_info_file": device_info_file,
                "logging_url": logging_url,
                "master_cert": master_cert,
                "slave_cert": slave_cert,
                "socks_proxy": socks_proxy,
                "ipv6": ipv6,
                "job_id": job_id,
            }
            str_lxc_udev_rule = lxc_udev_rule(data)
            if device.get("parent", False):
                str_lxc_udev_rule += lxc_udev_rule_parent(data)
            lines.append(str_lxc_udev_rule)
        if lines:
            self.logger.info("udev rules file '%s' created", rules_file)
        with open(rules_file, "w") as f_obj:
            f_obj.write("\n".join(lines))
        for line in lines:
            self.logger.debug(line)
        # Create symlink to rules file inside UDEV_RULES_DIR
        # See https://projects.linaro.org/browse/LAVA-1227
        os.symlink(rules_file, os.path.join(UDEV_RULES_DIR, rules_file_name))
        self.logger.info(
            "'%s' symlinked to '%s'",
            os.path.join(UDEV_RULES_DIR, rules_file_name),
            rules_file,
        )

        # Reload udev rules.
        reload_cmd = ["udevadm", "control", "--reload-rules"]
        # FIXME: if reload fails, that is a LAVABug.
        cmd_out = self.run_command(reload_cmd,
                                   allow_fail=True,
                                   allow_silent=True)
        if cmd_out is False:
            self.logger.debug("Reloading udev rules failed")
        else:
            self.logger.debug("udev rules reloaded.")
        return connection