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