Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 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 | |
| 6 | import os |
| 7 | |
| 8 | import common |
| 9 | from autotest_lib.client.common_lib import control_data |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import global_config |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 11 | try: |
| 12 | # test that imports autoserv_utils for vm_tests |
| 13 | from autotest_lib.scheduler import drone_manager |
| 14 | except ImportError as e: |
| 15 | drone_manager = None |
| 16 | pass |
| 17 | |
| 18 | AUTOTEST_INSTALL_DIR = global_config.global_config.get_config_value('SCHEDULER', |
| 19 | 'drone_installation_directory') |
| 20 | autoserv_directory = os.path.join(AUTOTEST_INSTALL_DIR, 'server') |
| 21 | autoserv_path = os.path.join(autoserv_directory, 'autoserv') |
| 22 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 23 | |
| 24 | def autoserv_run_job_command(autoserv_directory, machines, |
| 25 | results_directory=None, extra_args=[], job=None, |
| 26 | queue_entry=None, verbose=True, |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 27 | write_pidfile=True, fast_mode=False, |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 28 | ssh_verbosity=0, |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 29 | no_console_prefix=False, |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 30 | ssh_options=None, |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame^] | 31 | use_packaging=True, |
| 32 | in_lab=False): |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 33 | """ |
| 34 | Construct an autoserv command from a job or host queue entry. |
| 35 | |
| 36 | @param autoserv_directory: Absolute path to directory containing the |
| 37 | autoserv executable. |
| 38 | @param machines: A machine or comma separated list of machines to run |
| 39 | job on. Leave as None or empty string for hostless job |
| 40 | (String). |
| 41 | @param results_directory: Absolute path to directory in which to deposit |
| 42 | results. |
| 43 | @param extra_args: Additional arguments to pass to autoserv |
| 44 | (List of Strings). |
| 45 | @param job: Job object. If supplied, -u owner, -l name, and --test-retry, |
| 46 | and -c or -s (client or server) parameters will be added. |
| 47 | @param queue_entry: HostQueueEntry object. If supplied and no job |
| 48 | was supplied, this will be used to lookup the job. |
| 49 | @param verbose: Boolean (default: True) for autoserv verbosity. |
| 50 | @param write_pidfile: Boolean (default: True) for whether autoserv should |
| 51 | write a pidfile. |
Christopher Wiley | f6b5aae | 2013-07-09 10:14:02 -0700 | [diff] [blame] | 52 | @param fast_mode: bool to use fast mode (disables slow autotest features). |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 53 | @param ssh_verbosity: integer between 0 and 3 (inclusive) which sents the |
| 54 | verbosity level of ssh. Default: 0. |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 55 | @param no_console_prefix: If true, supress timestamps and other prefix info |
| 56 | in autoserv console logs. |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 57 | @param ssh_options: A string giving extra arguments to be tacked on to |
| 58 | ssh commands. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 59 | @param use_packaging Enable install modes that use the packaging system. |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame^] | 60 | @param in_lab: If true, informs autoserv it is running within a lab |
| 61 | environment. This information is useful as autoserv knows |
| 62 | the database is available and can make database calls such |
| 63 | as looking up host attributes at runtime. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 64 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 65 | @returns The autoserv command line as a list of executable + parameters. |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 66 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 67 | """ |
| 68 | command = [os.path.join(autoserv_directory, 'autoserv')] |
| 69 | |
| 70 | if write_pidfile: |
| 71 | command.append('-p') |
| 72 | |
| 73 | if results_directory: |
| 74 | command += ['-r', results_directory] |
| 75 | |
| 76 | if machines: |
| 77 | command += ['-m', machines] |
| 78 | |
Aviv Keshet | c14951a | 2013-08-12 18:17:35 -0700 | [diff] [blame] | 79 | if ssh_verbosity: |
| 80 | command += ['--ssh_verbosity', str(ssh_verbosity)] |
| 81 | |
Aviv Keshet | c5947fa | 2013-09-04 14:06:29 -0700 | [diff] [blame] | 82 | if ssh_options: |
| 83 | command += ['--ssh_options', ssh_options] |
| 84 | |
Aviv Keshet | e43bccf | 2013-08-14 14:11:59 -0700 | [diff] [blame] | 85 | if no_console_prefix: |
| 86 | command += ['--no_console_prefix'] |
| 87 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 88 | if job or queue_entry: |
| 89 | if not job: |
| 90 | job = queue_entry.job |
| 91 | |
| 92 | owner = getattr(job, 'owner', None) |
| 93 | name = getattr(job, 'name', None) |
| 94 | test_retry = getattr(job, 'test_retry', None) |
| 95 | control_type = getattr(job, 'control_type', None) |
| 96 | |
| 97 | |
| 98 | if owner: |
| 99 | command += ['-u', owner] |
| 100 | if name: |
| 101 | command += ['-l', name] |
| 102 | if test_retry: |
| 103 | command += ['--test-retry='+str(test_retry)] |
| 104 | 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 | |
| 112 | if verbose: |
| 113 | command.append('--verbose') |
| 114 | |
Christopher Wiley | f6b5aae | 2013-07-09 10:14:02 -0700 | [diff] [blame] | 115 | if fast_mode: |
| 116 | command.append('--disable_sysinfo') |
| 117 | command.append('--no_collect_crashinfo') |
| 118 | |
Dan Shi | b669cbd | 2013-09-13 11:17:17 -0700 | [diff] [blame] | 119 | if not use_packaging: |
| 120 | command.append('--no_use_packaging') |
| 121 | |
Simran Basi | 1bf60eb | 2015-12-01 16:39:29 -0800 | [diff] [blame^] | 122 | if in_lab: |
| 123 | command.extend(['--lab', 'True']) |
| 124 | |
Aviv Keshet | 308e736 | 2013-05-21 14:43:16 -0700 | [diff] [blame] | 125 | return command + extra_args |
beeps | 5e2bb4a | 2013-10-28 11:26:45 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | def _autoserv_command_line(machines, extra_args, job=None, queue_entry=None, |
| 129 | verbose=True): |
| 130 | """ |
| 131 | @returns The autoserv command line as a list of executable + parameters. |
| 132 | |
| 133 | @param machines - string - A machine or comma separated list of machines |
| 134 | for the (-m) flag. |
| 135 | @param extra_args - list - Additional arguments to pass to autoserv. |
| 136 | @param job - Job object - If supplied, -u owner, -l name, --test-retry, |
| 137 | and client -c or server -s parameters will be added. |
| 138 | @param queue_entry - A HostQueueEntry object - If supplied and no Job |
| 139 | object was supplied, this will be used to lookup the Job object. |
| 140 | """ |
| 141 | if drone_manager is None: |
| 142 | raise ImportError('Unable to import drone_manager in autoserv_utils') |
| 143 | |
| 144 | return autoserv_run_job_command(autoserv_directory, |
| 145 | machines, results_directory=drone_manager.WORKING_DIRECTORY, |
| 146 | extra_args=extra_args, job=job, queue_entry=queue_entry, |
| 147 | verbose=verbose) |