• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import os
7
8import common
9from autotest_lib.client.common_lib import control_data
10from autotest_lib.client.common_lib import global_config
11try:
12    # test that imports autoserv_utils for vm_tests
13    from autotest_lib.scheduler import drone_manager
14except ImportError as e:
15    drone_manager = None
16    pass
17
18AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER',
19                                                 'drone_installation_directory')
20autoserv_directory = os.path.join(AUTOTEST_INSTALL_DIR, 'server')
21autoserv_path = os.path.join(autoserv_directory, 'autoserv')
22
23
24def autoserv_run_job_command(autoserv_directory, machines,
25                             results_directory=None, extra_args=[], job=None,
26                             queue_entry=None, verbose=True,
27                             write_pidfile=True, fast_mode=False,
28                             ssh_verbosity=0,
29                             no_console_prefix=False,
30                             ssh_options=None,
31                             use_packaging=True,
32                             in_lab=False,
33                             host_attributes=None,
34                             use_virtualenv=False):
35    """
36    Construct an autoserv command from a job or host queue entry.
37
38    @param autoserv_directory: Absolute path to directory containing the
39                               autoserv executable.
40    @param machines: A machine or comma separated list of machines to run
41                     job on. Leave as None or empty string for hostless job
42                     (String).
43    @param results_directory: Absolute path to directory in which to deposit
44                             results.
45    @param extra_args: Additional arguments to pass to autoserv
46                       (List of Strings).
47    @param job: Job object. If supplied, -u owner, -l name, and --test-retry,
48                and -c or -s (client or server) parameters will be added.
49    @param queue_entry: HostQueueEntry object. If supplied and no job
50                        was supplied, this will be used to lookup the job.
51    @param verbose: Boolean (default: True) for autoserv verbosity.
52    @param write_pidfile: Boolean (default: True) for whether autoserv should
53                          write a pidfile.
54    @param fast_mode: bool to use fast mode (disables slow autotest features).
55    @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the
56                          verbosity level of ssh. Default: 0.
57    @param no_console_prefix: If true, supress timestamps and other prefix info
58                              in autoserv console logs.
59    @param ssh_options: A string giving extra arguments to be tacked on to
60                        ssh commands.
61    @param use_packaging Enable install modes that use the packaging system.
62    @param in_lab: If true, informs autoserv it is running within a lab
63                   environment. This information is useful as autoserv knows
64                   the database is available and can make database calls such
65                   as looking up host attributes at runtime.
66    @param host_attributes: Dict of host attributes to pass into autoserv.
67    @param use_virtualenv: Whether to run autoserv inside of virtualenv. In
68                           general this should be set to True in our production
69                           lab, and probably False in most other use cases
70                           (moblab, local testing) until we rollout virtualenv
71                           support everywhere. Default: False.
72
73    @returns The autoserv command line as a list of executable + parameters.
74
75    """
76    script_name = 'virtualenv_autoserv' if use_virtualenv else 'autoserv'
77    command = [os.path.join(autoserv_directory, script_name)]
78
79    if write_pidfile:
80        command.append('-p')
81
82    if results_directory:
83        command += ['-r', results_directory]
84
85    if machines:
86        command += ['-m', machines]
87
88    if ssh_verbosity:
89        command += ['--ssh_verbosity', str(ssh_verbosity)]
90
91    if ssh_options:
92        command += ['--ssh_options', ssh_options]
93
94    if no_console_prefix:
95        command += ['--no_console_prefix']
96
97    if job or queue_entry:
98        if not job:
99            job = queue_entry.job
100
101        owner = getattr(job, 'owner', None)
102        name = getattr(job, 'name', None)
103        test_retry = getattr(job, 'test_retry', None)
104        control_type = getattr(job, 'control_type', None)
105
106
107        if owner:
108            command += ['-u', owner]
109        if name:
110            command += ['-l', name]
111        if test_retry:
112            command += ['--test-retry='+str(test_retry)]
113        if control_type is not None: # still want to enter if control_type==0
114            control_type_value = control_data.CONTROL_TYPE.get_value(
115                    control_type)
116            if control_type_value == control_data.CONTROL_TYPE.CLIENT:
117                command.append('-c')
118            elif control_type_value == control_data.CONTROL_TYPE.SERVER:
119                command.append('-s')
120
121    if host_attributes:
122        command += ['--host_attributes', repr(host_attributes)]
123
124    if verbose:
125        command.append('--verbose')
126
127    if fast_mode:
128        command.append('--disable_sysinfo')
129        command.append('--no_collect_crashinfo')
130
131    if not use_packaging:
132        command.append('--no_use_packaging')
133
134    if in_lab:
135        command.extend(['--lab', 'True'])
136
137    return command + extra_args
138
139
140def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None,
141                           verbose=True, in_lab=False, use_virtualenv=False):
142    """
143    @returns The autoserv command line as a list of executable + parameters.
144
145    @param machines - string - A machine or comma separated list of machines
146            for the (-m) flag.
147    @param extra_args - list - Additional arguments to pass to autoserv.
148    @param job - Job object - If supplied, -u owner, -l name, --test-retry,
149            and client -c or server -s parameters will be added.
150    @param queue_entry - A HostQueueEntry object - If supplied and no Job
151            object was supplied, this will be used to lookup the Job object.
152    @param in_lab: If true, informs autoserv it is running within a lab
153                   environment. This information is useful as autoserv knows
154                   the database is available and can make database calls such
155                   as looking up host attributes at runtime.
156    @param use_virtualenv: See autoserv_run_job_command.
157    """
158    if drone_manager is None:
159        raise ImportError('Unable to import drone_manager in autoserv_utils')
160
161    return autoserv_run_job_command(autoserv_directory,
162            machines, results_directory=drone_manager.WORKING_DIRECTORY,
163            extra_args=extra_args, job=job, queue_entry=queue_entry,
164            verbose=verbose, in_lab=in_lab, use_virtualenv=use_virtualenv)
165