blob: 388923391fe72ea597b21f2129dc1d14d2b3213a [file] [log] [blame]
Mike Frysingerd03e6b52019-08-03 12:49:01 -04001#!/usr/bin/python2
Aviv Keshet308e7362013-05-21 14:43:16 -07002# 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
beeps5e2bb4a2013-10-28 11:26:45 -070010from autotest_lib.client.common_lib import global_config
beeps5e2bb4a2013-10-28 11:26:45 -070011
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
Aviv Keshet308e7362013-05-21 14:43:16 -070017
18def autoserv_run_job_command(autoserv_directory, machines,
19 results_directory=None, extra_args=[], job=None,
20 queue_entry=None, verbose=True,
Aviv Keshetc14951a2013-08-12 18:17:35 -070021 write_pidfile=True, fast_mode=False,
Aviv Keshete43bccf2013-08-14 14:11:59 -070022 ssh_verbosity=0,
Aviv Keshetc5947fa2013-09-04 14:06:29 -070023 no_console_prefix=False,
Dan Shib669cbd2013-09-13 11:17:17 -070024 ssh_options=None,
Simran Basi1bf60eb2015-12-01 16:39:29 -080025 use_packaging=True,
Simran Basi14622bb2015-11-25 13:23:40 -080026 in_lab=False,
Aviv Keshetd3e2c282017-03-20 16:56:18 -070027 host_attributes=None,
28 use_virtualenv=False):
Aviv Keshet308e7362013-05-21 14:43:16 -070029 """
30 Construct an autoserv command from a job or host queue entry.
31
32 @param autoserv_directory: Absolute path to directory containing the
33 autoserv executable.
34 @param machines: A machine or comma separated list of machines to run
35 job on. Leave as None or empty string for hostless job
36 (String).
37 @param results_directory: Absolute path to directory in which to deposit
38 results.
39 @param extra_args: Additional arguments to pass to autoserv
40 (List of Strings).
41 @param job: Job object. If supplied, -u owner, -l name, and --test-retry,
42 and -c or -s (client or server) parameters will be added.
43 @param queue_entry: HostQueueEntry object. If supplied and no job
44 was supplied, this will be used to lookup the job.
45 @param verbose: Boolean (default: True) for autoserv verbosity.
46 @param write_pidfile: Boolean (default: True) for whether autoserv should
47 write a pidfile.
Christopher Wileyf6b5aae2013-07-09 10:14:02 -070048 @param fast_mode: bool to use fast mode (disables slow autotest features).
Aviv Keshetc14951a2013-08-12 18:17:35 -070049 @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the
50 verbosity level of ssh. Default: 0.
Aviv Keshete43bccf2013-08-14 14:11:59 -070051 @param no_console_prefix: If true, supress timestamps and other prefix info
52 in autoserv console logs.
Aviv Keshetc5947fa2013-09-04 14:06:29 -070053 @param ssh_options: A string giving extra arguments to be tacked on to
54 ssh commands.
Dan Shib669cbd2013-09-13 11:17:17 -070055 @param use_packaging Enable install modes that use the packaging system.
Simran Basi1bf60eb2015-12-01 16:39:29 -080056 @param in_lab: If true, informs autoserv it is running within a lab
57 environment. This information is useful as autoserv knows
58 the database is available and can make database calls such
59 as looking up host attributes at runtime.
Simran Basi14622bb2015-11-25 13:23:40 -080060 @param host_attributes: Dict of host attributes to pass into autoserv.
Aviv Keshetd3e2c282017-03-20 16:56:18 -070061 @param use_virtualenv: Whether to run autoserv inside of virtualenv. In
62 general this should be set to True in our production
63 lab, and probably False in most other use cases
64 (moblab, local testing) until we rollout virtualenv
65 support everywhere. Default: False.
Dan Shib669cbd2013-09-13 11:17:17 -070066
Aviv Keshet308e7362013-05-21 14:43:16 -070067 @returns The autoserv command line as a list of executable + parameters.
Dan Shib669cbd2013-09-13 11:17:17 -070068
Aviv Keshet308e7362013-05-21 14:43:16 -070069 """
Aviv Keshetd3e2c282017-03-20 16:56:18 -070070 script_name = 'virtualenv_autoserv' if use_virtualenv else 'autoserv'
71 command = [os.path.join(autoserv_directory, script_name)]
Aviv Keshet308e7362013-05-21 14:43:16 -070072
73 if write_pidfile:
74 command.append('-p')
75
76 if results_directory:
77 command += ['-r', results_directory]
78
79 if machines:
80 command += ['-m', machines]
81
Aviv Keshetc14951a2013-08-12 18:17:35 -070082 if ssh_verbosity:
83 command += ['--ssh_verbosity', str(ssh_verbosity)]
84
Aviv Keshetc5947fa2013-09-04 14:06:29 -070085 if ssh_options:
86 command += ['--ssh_options', ssh_options]
87
Aviv Keshete43bccf2013-08-14 14:11:59 -070088 if no_console_prefix:
89 command += ['--no_console_prefix']
90
Aviv Keshet308e7362013-05-21 14:43:16 -070091 if job or queue_entry:
92 if not job:
93 job = queue_entry.job
94
95 owner = getattr(job, 'owner', None)
96 name = getattr(job, 'name', None)
Aviv Keshet308e7362013-05-21 14:43:16 -070097 control_type = getattr(job, 'control_type', None)
98
99
100 if owner:
101 command += ['-u', owner]
102 if name:
103 command += ['-l', name]
Aviv Keshet308e7362013-05-21 14:43:16 -0700104 if control_type is not None: # still want to enter if control_type==0
105 control_type_value = control_data.CONTROL_TYPE.get_value(
106 control_type)
107 if control_type_value == control_data.CONTROL_TYPE.CLIENT:
108 command.append('-c')
109 elif control_type_value == control_data.CONTROL_TYPE.SERVER:
110 command.append('-s')
111
Simran Basi14622bb2015-11-25 13:23:40 -0800112 if host_attributes:
113 command += ['--host_attributes', repr(host_attributes)]
114
Aviv Keshet308e7362013-05-21 14:43:16 -0700115 if verbose:
116 command.append('--verbose')
117
Christopher Wileyf6b5aae2013-07-09 10:14:02 -0700118 if fast_mode:
119 command.append('--disable_sysinfo')
120 command.append('--no_collect_crashinfo')
121
Dan Shib669cbd2013-09-13 11:17:17 -0700122 if not use_packaging:
123 command.append('--no_use_packaging')
124
Simran Basi1bf60eb2015-12-01 16:39:29 -0800125 if in_lab:
126 command.extend(['--lab', 'True'])
127
Aviv Keshet308e7362013-05-21 14:43:16 -0700128 return command + extra_args