• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2
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
11
12AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER',
13                                                 'drone_installation_directory')
14autoserv_directory = os.path.join(AUTOTEST_INSTALL_DIR, 'server')
15autoserv_path = os.path.join(autoserv_directory, 'autoserv')
16
17
18def autoserv_run_job_command(autoserv_directory, machines,
19                             results_directory=None, extra_args=[], job=None,
20                             queue_entry=None, verbose=True,
21                             write_pidfile=True, fast_mode=False,
22                             ssh_verbosity=0,
23                             no_console_prefix=False,
24                             ssh_options=None,
25                             use_packaging=True,
26                             in_lab=False,
27                             host_attributes=None,
28                             use_virtualenv=False,
29                             host_info_subdir=''):
30    """
31    Construct an autoserv command from a job or host queue entry.
32
33    @param autoserv_directory: Absolute path to directory containing the
34                               autoserv executable.
35    @param machines: A machine or comma separated list of machines to run
36                     job on. Leave as None or empty string for hostless job
37                     (String).
38    @param results_directory: Absolute path to directory in which to deposit
39                             results.
40    @param extra_args: Additional arguments to pass to autoserv
41                       (List of Strings).
42    @param job: Job object. If supplied, -u owner, -l name, and --test-retry,
43                and -c or -s (client or server) parameters will be added.
44    @param queue_entry: HostQueueEntry object. If supplied and no job
45                        was supplied, this will be used to lookup the job.
46    @param verbose: Boolean (default: True) for autoserv verbosity.
47    @param write_pidfile: Boolean (default: True) for whether autoserv should
48                          write a pidfile.
49    @param fast_mode: bool to use fast mode (disables slow autotest features).
50    @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the
51                          verbosity level of ssh. Default: 0.
52    @param no_console_prefix: If true, supress timestamps and other prefix info
53                              in autoserv console logs.
54    @param ssh_options: A string giving extra arguments to be tacked on to
55                        ssh commands.
56    @param use_packaging Enable install modes that use the packaging system.
57    @param in_lab: If true, informs autoserv it is running within a lab
58                   environment. This information is useful as autoserv knows
59                   the database is available and can make database calls such
60                   as looking up host attributes at runtime.
61    @param host_attributes: Dict of host attributes to pass into autoserv.
62    @param use_virtualenv: Whether to run autoserv inside of virtualenv. In
63                           general this should be set to True in our production
64                           lab, and probably False in most other use cases
65                           (moblab, local testing) until we rollout virtualenv
66                           support everywhere. Default: False.
67    @param host_info_subdir: When set, a sub-directory of the results directory
68                             where host info file(s) are stored.
69
70    @returns The autoserv command line as a list of executable + parameters.
71
72    """
73    script_name = 'virtualenv_autoserv' if use_virtualenv else 'autoserv'
74    command = [os.path.join(autoserv_directory, script_name)]
75
76    if write_pidfile:
77        command.append('-p')
78
79    if results_directory:
80        command += ['-r', results_directory]
81    if host_info_subdir:
82        command += ['--local-only-host-info', 'True']
83        command += ['--host-info-subdir', host_info_subdir]
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        control_type = getattr(job, 'control_type', None)
104
105
106        if owner:
107            command += ['-u', owner]
108        if name:
109            command += ['-l', name]
110        if control_type is not None: # still want to enter if control_type==0
111            control_type_value = control_data.CONTROL_TYPE.get_value(
112                    control_type)
113            if control_type_value == control_data.CONTROL_TYPE.CLIENT:
114                command.append('-c')
115            elif control_type_value == control_data.CONTROL_TYPE.SERVER:
116                command.append('-s')
117
118    if host_attributes:
119        command += ['--host_attributes', repr(host_attributes)]
120
121    if verbose:
122        command.append('--verbose')
123
124    if fast_mode:
125        command.append('--disable_sysinfo')
126        command.append('--no_collect_crashinfo')
127
128    if not use_packaging:
129        command.append('--no_use_packaging')
130
131    if in_lab:
132        command.extend(['--lab', 'True'])
133
134    return command + extra_args
135