Mike Frysinger | d03e6b5 | 2019-08-03 12:49:01 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 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 | |
| 6 | import os |
Derek Beckett | 2976bb7 | 2020-12-14 11:12:47 -0800 | [diff] [blame] | 7 | import sys |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 8 | |
| 9 | import common |
Derek Beckett | 2976bb7 | 2020-12-14 11:12:47 -0800 | [diff] [blame] | 10 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import control_data |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 12 | from autotest_lib.client.common_lib import global_config |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 13 | |
| 14 | AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER', |
| 15 | 'drone_installation_directory') |
| 16 | autoserv_directory = os.path.join(AUTOTEST_INSTALL_DIR, 'server') |
| 17 | autoserv_path = os.path.join(autoserv_directory, 'autoserv') |
| 18 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 19 | |
| 20 | def autoserv_run_job_command(autoserv_directory, machines, |
| 21 | results_directory=None, extra_args=[], job=None, |
| 22 | queue_entry=None, verbose=True, |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 23 | write_pidfile=True, fast_mode=False, |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 24 | ssh_verbosity=0, |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 25 | no_console_prefix=False, |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 26 | ssh_options=None, |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 27 | use_packaging=True, |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 28 | in_lab=False, |
Aviv Keshet | d3e2c28 | 2017-03-20 16:56:18 -0700 | [diff] [blame] | 29 | host_attributes=None, |
Prathmesh Prabhu | 2532d0d | 2020-01-03 15:47:52 -0800 | [diff] [blame] | 30 | use_virtualenv=False, |
| 31 | host_info_subdir=''): |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 32 | """ |
| 33 | Construct an autoserv command from a job or host queue entry. |
| 34 | |
| 35 | @param autoserv_directory: Absolute path to directory containing the |
| 36 | autoserv executable. |
| 37 | @param machines: A machine or comma separated list of machines to run |
| 38 | job on. Leave as None or empty string for hostless job |
| 39 | (String). |
| 40 | @param results_directory: Absolute path to directory in which to deposit |
| 41 | results. |
| 42 | @param extra_args: Additional arguments to pass to autoserv |
| 43 | (List of Strings). |
| 44 | @param job: Job object. If supplied, -u owner, -l name, and --test-retry, |
| 45 | and -c or -s (client or server) parameters will be added. |
| 46 | @param queue_entry: HostQueueEntry object. If supplied and no job |
| 47 | was supplied, this will be used to lookup the job. |
| 48 | @param verbose: Boolean (default: True) for autoserv verbosity. |
| 49 | @param write_pidfile: Boolean (default: True) for whether autoserv should |
| 50 | write a pidfile. |
Christopher Wiley | f6b5aae | 2013-07-09 10:14:02 -0700 | [diff] [blame] | 51 | @param fast_mode: bool to use fast mode (disables slow autotest features). |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 52 | @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the |
| 53 | verbosity level of ssh. Default: 0. |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 54 | @param no_console_prefix: If true, supress timestamps and other prefix info |
| 55 | in autoserv console logs. |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 56 | @param ssh_options: A string giving extra arguments to be tacked on to |
| 57 | ssh commands. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 58 | @param use_packaging Enable install modes that use the packaging system. |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 59 | @param in_lab: If true, informs autoserv it is running within a lab |
| 60 | environment. This information is useful as autoserv knows |
| 61 | the database is available and can make database calls such |
| 62 | as looking up host attributes at runtime. |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 63 | @param host_attributes: Dict of host attributes to pass into autoserv. |
Aviv Keshet | d3e2c28 | 2017-03-20 16:56:18 -0700 | [diff] [blame] | 64 | @param use_virtualenv: Whether to run autoserv inside of virtualenv. In |
| 65 | general this should be set to True in our production |
| 66 | lab, and probably False in most other use cases |
| 67 | (moblab, local testing) until we rollout virtualenv |
| 68 | support everywhere. Default: False. |
Prathmesh Prabhu | 2532d0d | 2020-01-03 15:47:52 -0800 | [diff] [blame] | 69 | @param host_info_subdir: When set, a sub-directory of the results directory |
| 70 | where host info file(s) are stored. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 71 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 72 | @returns The autoserv command line as a list of executable + parameters. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 73 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 74 | """ |
Aviv Keshet | d3e2c28 | 2017-03-20 16:56:18 -0700 | [diff] [blame] | 75 | script_name = 'virtualenv_autoserv' if use_virtualenv else 'autoserv' |
Derek Beckett | 2976bb7 | 2020-12-14 11:12:47 -0800 | [diff] [blame] | 76 | |
| 77 | full_script_path = os.path.join(autoserv_directory, script_name) |
| 78 | |
| 79 | # virtualenv_autoserv is a `POSIX shell script, ASCII text executable`. |
| 80 | # Calling with `sys.executable` would fail because python doesn't |
| 81 | # interpret shebangs itself. |
| 82 | if use_virtualenv: |
| 83 | command = [full_script_path] |
| 84 | else: |
| 85 | command = [sys.executable, full_script_path] |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 86 | |
| 87 | if write_pidfile: |
| 88 | command.append('-p') |
| 89 | |
| 90 | if results_directory: |
| 91 | command += ['-r', results_directory] |
Prathmesh Prabhu | 2532d0d | 2020-01-03 15:47:52 -0800 | [diff] [blame] | 92 | if host_info_subdir: |
Prathmesh Prabhu | 7fec0bf | 2020-01-07 14:23:04 -0800 | [diff] [blame] | 93 | command += ['--local-only-host-info', 'True'] |
Prathmesh Prabhu | 2532d0d | 2020-01-03 15:47:52 -0800 | [diff] [blame] | 94 | command += ['--host-info-subdir', host_info_subdir] |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 95 | |
| 96 | if machines: |
| 97 | command += ['-m', machines] |
| 98 | |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 99 | if ssh_verbosity: |
| 100 | command += ['--ssh_verbosity', str(ssh_verbosity)] |
| 101 | |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 102 | if ssh_options: |
| 103 | command += ['--ssh_options', ssh_options] |
| 104 | |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 105 | if no_console_prefix: |
| 106 | command += ['--no_console_prefix'] |
| 107 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 108 | if job or queue_entry: |
| 109 | if not job: |
| 110 | job = queue_entry.job |
| 111 | |
| 112 | owner = getattr(job, 'owner', None) |
| 113 | name = getattr(job, 'name', None) |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 114 | control_type = getattr(job, 'control_type', None) |
| 115 | |
| 116 | |
| 117 | if owner: |
| 118 | command += ['-u', owner] |
| 119 | if name: |
| 120 | command += ['-l', name] |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 121 | if control_type is not None: # still want to enter if control_type==0 |
| 122 | control_type_value = control_data.CONTROL_TYPE.get_value( |
| 123 | control_type) |
| 124 | if control_type_value == control_data.CONTROL_TYPE.CLIENT: |
| 125 | command.append('-c') |
| 126 | elif control_type_value == control_data.CONTROL_TYPE.SERVER: |
| 127 | command.append('-s') |
| 128 | |
Simran Basi | 14622bb | 2015-11-25 13:23:40 -0800 | [diff] [blame] | 129 | if host_attributes: |
| 130 | command += ['--host_attributes', repr(host_attributes)] |
| 131 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 132 | if verbose: |
| 133 | command.append('--verbose') |
| 134 | |
Christopher Wiley | f6b5aae | 2013-07-09 10:14:02 -0700 | [diff] [blame] | 135 | if fast_mode: |
| 136 | command.append('--disable_sysinfo') |
| 137 | command.append('--no_collect_crashinfo') |
| 138 | |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 139 | if not use_packaging: |
| 140 | command.append('--no_use_packaging') |
| 141 | |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame] | 142 | if in_lab: |
| 143 | command.extend(['--lab', 'True']) |
| 144 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 145 | return command + extra_args |