def invoke(self):
        full_path = os.path.abspath(self.args.JOB)
        job_file = self.retrieve_file(full_path, JOB_FILE_EXTENSIONS)
        job_dir = os.path.dirname(job_file)
        tests_dir = os.path.join(job_dir, TESTS_DIR)

        if os.path.isdir(tests_dir):
            tar_repo = None
            try:
                tar_repo = create_tar(tests_dir)
                encoded_tests = base64_encode(tar_repo)

                json_data = None
                with open(job_file, "r") as json_file:
                    try:
                        json_data = json.load(json_file)
                        set_value(json_data, LAVA_TEST_SHELL_TAR_REPO_KEY,
                                  encoded_tests)
                    except Exception:
                        raise CommandError("Cannot read job file "
                                           "'{0}'.".format(job_file))

                content = json.dumps(json_data, indent=4)
                write_file(job_file, content)

                print >> sys.stdout, "Job definition updated."
            finally:
                if tar_repo and os.path.isfile(tar_repo):
                    os.unlink(tar_repo)
        else:
            raise CommandError("Cannot find tests directory.")
Esempio n. 2
0
 def invoke(self):
     server = AuthenticatingServerProxy(self.args.SERVER,
                                        auth_backend=KeyringAuthBackend())
     if not os.path.exists(self.args.JSON_FILE):
         raise CommandError("No such file: %s" % self.args.JSON_FILE)
     with open(self.args.JSON_FILE, 'rb') as stream:
         command_text = stream.read()
     try:
         job_ids = server.scheduler.submit_job(command_text)
     except xmlrpclib.Fault, e:
         raise CommandError(str(e))
Esempio n. 3
0
 def invoke(self):
     command = self.device_config.hard_reset_command
     if not command:
         raise CommandError(
             "%s does not have a power cycle command configured" %
             self.args.device)
     os.system(command)
def create_tar(paths):
    """Creates a temporary tar file with the provided paths.

    The tar file is not deleted at the end, it has to be delete by who calls
    this function.

    If just a directory is passed, it will be flattened out: its contents will
    be added, but not the directory itself.

    :param paths: List of paths to be included in the tar archive.
    :type list
    :return The path to the temporary tar file.
    """
    paths = to_list(paths)
    try:
        temp_tar_file = tempfile.NamedTemporaryFile(suffix=".tar",
                                                    delete=False)
        with tarfile.open(temp_tar_file.name, "w") as tar_file:
            for path in paths:
                full_path = os.path.abspath(path)
                if os.path.isfile(full_path):
                    arcname = os.path.basename(full_path)
                    tar_file.add(full_path, arcname=arcname)
                elif os.path.isdir(full_path):
                    # If we pass a directory, flatten it out.
                    # List its contents, and add them as they are.
                    for element in os.listdir(full_path):
                        arcname = element
                        tar_file.add(os.path.join(full_path, element),
                                     arcname=arcname)
        return temp_tar_file.name
    except tarfile.TarError:
        raise CommandError("Error creating the temporary tar archive.")
Esempio n. 5
0
 def invoke(self):
     server = AuthenticatingServerProxy(self.args.SERVER,
                                        auth_backend=KeyringAuthBackend())
     try:
         job_id = server.scheduler.resubmit_job(self.args.JOB_ID)
     except xmlrpclib.Fault, e:
         raise CommandError(str(e))
Esempio n. 6
0
def _lookup_ip(lava_network_iface):
    for iface in lava_network_iface:
        line = commands.getoutput("ip address show dev %s" % iface).split()
        if 'inet' in line:
            return line[line.index('inet') + 1].split('/')[0]
    raise CommandError("LAVA_NETWORK_IFACE is set to '%s' "
                       "but no IP address was found for any listed interface." % ", ".join(lava_network_iface))
def edit_file(file_to_edit):
    """Opens the specified file with the default file editor.

    :param file_to_edit: The file to edit.
    """
    editor = os.environ.get("EDITOR", None)
    if editor is None:
        if has_command("sensible-editor"):
            editor = "sensible-editor"
        elif has_command("xdg-open"):
            editor = "xdg-open"
        else:
            # We really do not know how to open a file.
            print >> sys.stdout, ("Cannot find an editor to open the "
                                  "file '{0}'.".format(file_to_edit))
            print >> sys.stdout, ("Either set the 'EDITOR' environment "
                                  "variable, or install 'sensible-editor' "
                                  "or 'xdg-open'.")
            sys.exit(-1)
    try:
        subprocess.Popen([editor, file_to_edit]).wait()
    except Exception:
        raise CommandError("Error opening the file '{0}' with the "
                           "following editor: {1}.".format(
                               file_to_edit, editor))
Esempio n. 8
0
    def invoke(self):
        real_file_name = ".".join([self.args.DEVICE, DEVICE_FILE_SUFFIX])
        device_conf = get_device_file(real_file_name)

        if device_conf and can_edit_file(device_conf):
            edit_file(device_conf)
        else:
            raise CommandError("Cannot edit file '{0}'".format(real_file_name))
def verify_path_existance(path):
    """Verifies if a given path exists on the file system.

    Raises a CommandError in case it exists.

    :param path: The path to verify.
    """
    if os.path.exists(path):
        raise CommandError("{0} already exists.".format(path))
def verify_path_non_existance(path):
    """Verifies if a given path does not exist on the file system.

    Raises a CommandError in case it does not exist.

    :param path: The path to verify.
    """
    if not os.path.exists(path):
        raise CommandError("{0} does not exists.".format(path))
def get_devices():
    """Gets the devices list from the dispatcher.

    :return A list of DeviceConfig.
    """
    try:
        from lava_dispatcher.config import get_devices
        return get_devices()
    except ImportError:
        raise CommandError("Cannot find lava-dispatcher installation.")
def get_dispatcher_paths():
    """Tries to get the dispatcher paths from lava-dispatcher.

    :return A list of paths.
    """
    try:
        from lava_dispatcher.config import write_path
        return write_path()
    except ImportError:
        raise CommandError("Cannot find lava-dispatcher installation.")
def base64_encode(path):
    """Encode in base64 the provided file.

    :param path: The path to a file.
    :return The file content encoded in base64.
    """
    if os.path.isfile(path):
        encoded_content = StringIO.StringIO()

        try:
            with open(path) as read_file:
                base64.encode(read_file, encoded_content)

            return encoded_content.getvalue().strip()
        except IOError:
            raise CommandError("Cannot read file " "'{0}'.".format(path))
    else:
        raise CommandError("Provided path does not exists or is not a file: "
                           "{0}.".format(path))
def write_file(path, content):
    """Creates a file with the specified content.

    :param path: The path of the file to write.
    :param content: What to write in the file.
    """
    try:
        with open(path, "w") as to_write:
            to_write.write(content)
    except (OSError, IOError):
        raise CommandError("Error writing file '{0}'".format(path))
def execute(cmd_args):
    """Executes the supplied command args.

    :param cmd_args: The command, and its optional arguments, to run.
    :return The command execution return code.
    """
    cmd_args = to_list(cmd_args)
    try:
        return subprocess.check_call(cmd_args)
    except subprocess.CalledProcessError:
        raise CommandError("Error running the following command: "
                           "{0}".format(" ".join(cmd_args)))
def get_device_file(file_name):
    """Retrieves the config file name specified, if it exists.

    :param file_name: The config file name to search.
    :return The path to the file, or None if it does not exist.
    """
    try:
        from lava_dispatcher.config import get_config_file
        return get_config_file(os.path.join(DEFAULT_DEVICES_PATH,
                                            file_name))
    except ImportError:
        raise CommandError("Cannot find lava-dispatcher installation.")
    def run(self, job_file):
        """Runs a job file on the local LAVA dispatcher.

        :param job_file: The job file to run.
        """
        if os.path.isfile(job_file):
            if has_command("lava-dispatch"):
                devices = get_devices()
                if devices:
                    if len(devices) > 1:
                        device_names = [device.hostname for device in devices]
                        device_param = SingleChoiceParameter(
                            "device", device_names)
                        device = device_param.prompt("Device to use: ")
                    else:
                        device = devices[0].hostname
                    execute(["lava-dispatch", "--target", device, job_file])
            else:
                raise CommandError("Cannot find lava-dispatcher installation.")
        else:
            raise CommandError("Job file '{0}' does not exists, or it is not "
                               "a file.".format(job_file))
def retrieve_file(path, extensions):
    """Searches for a file that has one of the supported extensions.

    The path of the first file that matches one of the supported provided
    extensions will be returned. The files are examined in alphabetical
    order.

    :param path: Where to look for the file.
    :param extensions: A list of extensions the file to look for should
                       have.
    :return The full path of the file.
    """
    if os.path.isfile(path):
        if check_valid_extension(path, extensions):
            retrieved_path = path
        else:
            raise CommandError("The provided file '{0}' is not "
                               "valid: extension not supported.".format(path))
    else:
        dir_listing = os.listdir(path)
        dir_listing.sort()

        for element in dir_listing:
            if element.startswith("."):
                continue

            element_path = os.path.join(path, element)
            if os.path.isdir(element_path):
                continue
            elif os.path.isfile(element_path):
                if check_valid_extension(element_path, extensions):
                    retrieved_path = element_path
                    break
        else:
            raise CommandError("No suitable file found in '{0}'".format(path))

    return retrieved_path
Esempio n. 19
0
    def invoke(self):
        real_file_name = ".".join([self.args.DEVICE, DEVICE_FILE_SUFFIX])
        device_conf = get_device_file(real_file_name)

        if device_conf:
            try:
                os.remove(device_conf)
                print >> sys.stdout, ("Device configuration file '{0}' "
                                      "removed.".format(real_file_name))
            except OSError:
                raise CommandError("Cannot remove file '{0}' at: {1}.".format(
                    real_file_name, os.path.dirname(device_conf)))
        else:
            print >> sys.stdout, ("No device configuration file '{0}' "
                                  "found.".format(real_file_name))
    def details(self, job_id):
        """Retrieves the details of a LAVA job.

        :param job_id: The ID of the job to look up.
        """
        job_id = str(job_id)

        try:
            server = self.authenticated_server()
            job_details = server.scheduler.job_details(job_id)

            print >> sys.stdout, "\nDetails of job {0}:\n".format(job_id)
            print >> sys.stdout, job_details
        except xmlrpclib.Fault, exc:
            raise CommandError(str(exc))
Esempio n. 21
0
    def put_parameter(self, parameter, value=None, section=None):
        """Adds a Parameter to the config file and cache.

        :param Parameter: The parameter to add.
        :type Parameter
        :param value: The value of the parameter. Defaults to None.
        :param section: The section where this parameter should be stored.
                        Defaults to None.
        """
        if not section:
            section = self._calculate_config_section(parameter)

        if value is None and parameter.value is not None:
            value = parameter.value
        elif value is None:
            raise CommandError("No value assigned to '{0}'.".format(
                parameter.id))
        self.put(parameter.id, value, section)
def create_dir(path, dir_name=None):
    """Checks if a directory does not exists, and creates it.

    :param path: The path where the directory should be created.
    :param dir_name: An optional name for a directory to be created at
                     path (dir_name will be joined with path).
    :return The path of the created directory."""
    created_dir = path
    if dir_name:
        created_dir = os.path.join(path, dir_name)

    if not os.path.isdir(created_dir):
        try:
            os.makedirs(created_dir)
        except OSError:
            raise CommandError("Cannot create directory "
                               "'{0}'.".format(created_dir))
    return created_dir
    def submit(self, job_file):
        """Submits a job file to a LAVA server.

        :param job_file: The job file to submit.
        :return The job ID on success.
        """
        if os.path.isfile(job_file):
            try:
                jobdata = open(job_file, 'rb').read()
                server = self.authenticated_server()

                job_id = server.scheduler.submit_job(jobdata)
                print >> sys.stdout, ("Job submitted with job "
                                      "ID {0}.".format(job_id))

                return job_id
            except xmlrpclib.Fault, exc:
                raise CommandError(str(exc))
    def status(self, job_id):
        """Retrieves the status of a LAVA job.

        :param job_id: The ID of the job to look up.
        """
        job_id = str(job_id)

        try:
            server = self.authenticated_server()
            job_status = server.scheduler.job_status(job_id)

            status = job_status["job_status"].lower()
            bundle = job_status["bundle_sha1"]

            print >> sys.stdout, "\nJob id: {0}".format(job_id)
            print >> sys.stdout, "Status: {0}".format(status)
            print >> sys.stdout, "Bundle: {0}".format(bundle)
        except xmlrpclib.Fault, exc:
            raise CommandError(str(exc))
    def invoke(self):
        full_path = os.path.abspath(self.args.DIR)

        if os.path.isfile(full_path):
            raise CommandError("'{0}' already exists, and is a "
                               "file.".format(self.args.DIR))

        create_dir(full_path)
        data = self._update_data()

        # Create the directory that will contain the test definition and
        # shell script.
        test_path = create_dir(full_path, TESTS_DIR)
        shell_script = self.create_shell_script(test_path)
        # Let the user modify the file.
        edit_file(shell_script)

        testdef_file = self.create_test_definition(
            os.path.join(test_path, DEFAULT_TESTDEF_FILENAME))

        job = data[JOBFILE_ID]
        self.create_tar_repo_job(os.path.join(full_path, job), testdef_file,
                                 test_path)
def choose_devices_path(paths):
    """Picks the first path that is writable by the user.

    :param paths: A list of paths.
    :return The first path where it is possible to write.
    """
    valid_path = None
    for path in paths:
        path = os.path.join(path, DEFAULT_DEVICES_PATH)
        if os.path.exists(path):
            name = "".join(random.choice(string.ascii_letters)
                           for x in range(6))
            test_file = os.path.join(path, name)
            try:
                fp = open(test_file, 'a')
                fp.close()
            except IOError:
                # Cannot write here.
                continue
            else:
                valid_path = path
                if os.path.isfile(test_file):
                    os.unlink(test_file)
                break
        else:
            try:
                os.makedirs(path)
            except OSError:
                # Cannot write here either.
                continue
            else:
                valid_path = path
                break
    else:
        raise CommandError("Insufficient permissions to create new "
                           "devices.")
    return valid_path
        :return The job ID on success.
        """
        if os.path.isfile(job_file):
            try:
                jobdata = open(job_file, 'rb').read()
                server = self.authenticated_server()

                job_id = server.scheduler.submit_job(jobdata)
                print >> sys.stdout, ("Job submitted with job "
                                      "ID {0}.".format(job_id))

                return job_id
            except xmlrpclib.Fault, exc:
                raise CommandError(str(exc))
        else:
            raise CommandError("Job file '{0}' does not exists, or is not "
                               "a file.".format(job_file))

    def run(self, job_file):
        """Runs a job file on the local LAVA dispatcher.

        :param job_file: The job file to run.
        """
        if os.path.isfile(job_file):
            if has_command("lava-dispatch"):
                devices = get_devices()
                if devices:
                    if len(devices) > 1:
                        device_names = [device.hostname for device in devices]
                        device_param = SingleChoiceParameter(
                            "device", device_names)
                        device = device_param.prompt("Device to use: ")
Esempio n. 28
0
 def device_config(self):
     try:
         return get_device_config(self.args.device)
     except Exception:
         raise CommandError("no such device: %s" % self.args.device)
Esempio n. 29
0
 def invoke(self):
     if self.args.JOB_ID:
         super(status, self).status(self.args.JOB_ID)
     else:
         raise CommandError("It is necessary to specify a job id.")