xref: /aosp_15_r20/external/autotest/server/autoserv_utils.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
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
7import sys
8
9import common
10
11from autotest_lib.client.common_lib import control_data
12from autotest_lib.client.common_lib import global_config
13
14AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER',
15                                                 'drone_installation_directory')
16autoserv_directory = os.path.join(AUTOTEST_INSTALL_DIR, 'server')
17autoserv_path = os.path.join(autoserv_directory, 'autoserv')
18
19
20def autoserv_run_job_command(autoserv_directory,
21                             machines,
22                             results_directory=None,
23                             extra_args=[],
24                             job=None,
25                             queue_entry=None,
26                             verbose=True,
27                             write_pidfile=True,
28                             fast_mode=False,
29                             ssh_verbosity=0,
30                             no_console_prefix=False,
31                             ssh_options=None,
32                             use_packaging=True,
33                             in_lab=False,
34                             host_attributes=None,
35                             use_virtualenv=False,
36                             host_info_subdir='',
37                             companion_hosts=None,
38                             dut_servers=None,
39                             is_cft=False):
40    """
41    Construct an autoserv command from a job or host queue entry.
42
43    @param autoserv_directory: Absolute path to directory containing the
44                               autoserv executable.
45    @param machines: A machine or comma separated list of machines to run
46                     job on. Leave as None or empty string for hostless job
47                     (String).
48    @param results_directory: Absolute path to directory in which to deposit
49                             results.
50    @param extra_args: Additional arguments to pass to autoserv
51                       (List of Strings).
52    @param job: Job object. If supplied, -u owner, -l name, and --test-retry,
53                and -c or -s (client or server) parameters will be added.
54    @param queue_entry: HostQueueEntry object. If supplied and no job
55                        was supplied, this will be used to lookup the job.
56    @param verbose: Boolean (default: True) for autoserv verbosity.
57    @param write_pidfile: Boolean (default: True) for whether autoserv should
58                          write a pidfile.
59    @param fast_mode: bool to use fast mode (disables slow autotest features).
60    @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the
61                          verbosity level of ssh. Default: 0.
62    @param no_console_prefix: If true, supress timestamps and other prefix info
63                              in autoserv console logs.
64    @param ssh_options: A string giving extra arguments to be tacked on to
65                        ssh commands.
66    @param use_packaging Enable install modes that use the packaging system.
67    @param in_lab: If true, informs autoserv it is running within a lab
68                   environment. This information is useful as autoserv knows
69                   the database is available and can make database calls such
70                   as looking up host attributes at runtime.
71    @param host_attributes: Dict of host attributes to pass into autoserv.
72    @param use_virtualenv: Whether to run autoserv inside of virtualenv. In
73                           general this should be set to True in our production
74                           lab, and probably False in most other use cases
75                           (moblab, local testing) until we rollout virtualenv
76                           support everywhere. Default: False.
77    @param host_info_subdir: When set, a sub-directory of the results directory
78                             where host info file(s) are stored.
79    @param companion_hosts: a str or list of hosts to be used as companions
80                            for the and provided to test. NOTE: these are
81                            different than  machines, where each host is a host
82                            that the test would be run on.
83    @param dut_servers: a str or list of hosts to be used as DUT server
84                            provided to test.
85
86    @returns The autoserv command line as a list of executable + parameters.
87
88    """
89    script_name = 'virtualenv_autoserv' if use_virtualenv else 'autoserv'
90
91    full_script_path = os.path.join(autoserv_directory, script_name)
92
93    # virtualenv_autoserv is a `POSIX shell script, ASCII text executable`.
94    # Calling with `sys.executable` would fail because python doesn't
95    # interpret shebangs itself.
96    if use_virtualenv:
97        command = [full_script_path]
98    else:
99        command = [sys.executable, full_script_path]
100
101    if write_pidfile:
102        command.append('-p')
103
104    if results_directory:
105        command += ['-r', results_directory]
106    if host_info_subdir:
107        command += ['--local-only-host-info', 'True']
108        command += ['--host-info-subdir', host_info_subdir]
109
110    if machines:
111        command += ['-m', machines]
112
113    if companion_hosts:
114        if not isinstance(companion_hosts, list):
115            companion_hosts = [companion_hosts]
116        command += ['-ch', ",".join(companion_hosts)]
117
118    if dut_servers:
119        if not isinstance(dut_servers, list):
120            dut_servers = [dut_servers]
121        command += ['--dut_servers', ",".join(dut_servers)]
122
123    if ssh_verbosity:
124        command += ['--ssh_verbosity', str(ssh_verbosity)]
125
126    if ssh_options:
127        command += ['--ssh_options', ssh_options]
128
129    if no_console_prefix:
130        command += ['--no_console_prefix']
131
132    if job or queue_entry:
133        if not job:
134            job = queue_entry.job
135
136        owner = getattr(job, 'owner', None)
137        name = getattr(job, 'name', None)
138        control_type = getattr(job, 'control_type', None)
139
140
141        if owner:
142            command += ['-u', owner]
143        if name:
144            command += ['-l', name]
145        if control_type is not None: # still want to enter if control_type==0
146            control_type_value = control_data.CONTROL_TYPE.get_value(
147                    control_type)
148            if control_type_value == control_data.CONTROL_TYPE.CLIENT:
149                command.append('-c')
150            elif control_type_value == control_data.CONTROL_TYPE.SERVER:
151                command.append('-s')
152
153    if host_attributes:
154        command += ['--host_attributes', repr(host_attributes)]
155
156    if verbose:
157        command.append('--verbose')
158
159    if fast_mode:
160        command.append('--disable_sysinfo')
161        command.append('--no_collect_crashinfo')
162
163    if not use_packaging:
164        command.append('--no_use_packaging')
165
166    if in_lab:
167        command.extend(['--lab', 'True'])
168
169    if is_cft:
170        command.append('--CFT')
171
172    py_version = os.getenv('PY_VERSION')
173    if py_version:
174        command.extend(['--py_version', py_version])
175
176    return command + extra_args
177