Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 3 | # |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 4 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 5 | # Use of this source code is governed by a BSD-style license that can be |
| 6 | # found in the LICENSE file. |
| 7 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 8 | """This module controls locking and unlocking of test machines.""" |
| 9 | |
| 10 | from __future__ import print_function |
| 11 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 12 | import argparse |
| 13 | import getpass |
| 14 | import os |
| 15 | import sys |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 16 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 17 | import file_lock_machine |
| 18 | |
| 19 | from cros_utils import command_executer |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 20 | from cros_utils import logger |
| 21 | from cros_utils import machines |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 22 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 23 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 24 | class AFELockException(Exception): |
| 25 | """Base class for exceptions in this module.""" |
| 26 | |
| 27 | |
| 28 | class MachineNotPingable(AFELockException): |
| 29 | """Raised when machine does not respond to ping.""" |
| 30 | |
| 31 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 32 | class LockingError(AFELockException): |
| 33 | """Raised when server fails to lock/unlock machine as requested.""" |
| 34 | |
| 35 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 36 | class DontOwnLock(AFELockException): |
| 37 | """Raised when user attmepts to unlock machine locked by someone else.""" |
| 38 | # This should not be raised if the user specified '--force' |
| 39 | |
| 40 | |
| 41 | class NoAFEServer(AFELockException): |
| 42 | """Raised when cannot find/access the autotest server.""" |
| 43 | |
| 44 | |
| 45 | class AFEAccessError(AFELockException): |
| 46 | """Raised when cannot get information about lab machine from lab server.""" |
| 47 | |
| 48 | |
| 49 | class AFELockManager(object): |
| 50 | """Class for locking/unlocking machines vie Autotest Front End servers. |
| 51 | |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 52 | This class contains methods for checking the locked status of machines, |
| 53 | and for changing the locked status. It handles HW lab machines (both AFE |
| 54 | and Skylab), and local machines, using appropriate locking mechanisms for |
| 55 | each. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 56 | |
| 57 | !!!IMPORTANT NOTE!!! The AFE server can only be called from the main |
| 58 | thread/process of a program. If you launch threads and try to call it |
| 59 | from a thread, you will get an error. This has to do with restrictions |
| 60 | in the Python virtual machine (and signal handling) and cannot be changed. |
| 61 | """ |
| 62 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 63 | SKYLAB_PATH = '/usr/local/bin/skylab' |
| 64 | LEASE_MINS = 600 |
| 65 | SKYLAB_CREDENTIAL = '/usr/local/google/home/mobiletc-prebuild/' \ |
| 66 | 'chromeos-swarming-1adbe355c97c.json' |
| 67 | SWARMING = 'chromite/third_party/swarming.client/swarming.py' |
| 68 | SUCCESS = 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 69 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 70 | def __init__(self, |
| 71 | remotes, |
| 72 | force_option, |
| 73 | chromeos_root, |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 74 | locks_dir='', |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 75 | log=None): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 76 | """Initializes an AFELockManager object. |
| 77 | |
| 78 | Args: |
| 79 | remotes: A list of machine names or ip addresses to be managed. Names |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 80 | and ip addresses should be represented as strings. If the list is |
| 81 | empty, the lock manager will get all known machines. |
| 82 | force_option: A Boolean indicating whether or not to force an unlock of |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 83 | a machine that was locked by someone else. |
| 84 | chromeos_root: The ChromeOS chroot to use for the autotest scripts. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 85 | locks_dir: A directory used for file locking local devices. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 86 | log: If not None, this is the logger object to be used for writing out |
| 87 | informational output messages. It is expected to be an instance of |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 88 | Logger class from cros_utils/logger.py. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 89 | """ |
| 90 | self.chromeos_root = chromeos_root |
| 91 | self.user = getpass.getuser() |
| 92 | self.logger = log or logger.GetLogger() |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 93 | self.ce = command_executer.GetCommandExecuter(self.logger) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 94 | autotest_path = os.path.join(chromeos_root, |
| 95 | 'src/third_party/autotest/files') |
| 96 | |
cmtice | d1172b4 | 2015-06-12 15:14:09 -0700 | [diff] [blame] | 97 | sys.path.append(chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 98 | sys.path.append(autotest_path) |
| 99 | sys.path.append(os.path.join(autotest_path, 'server', 'cros')) |
| 100 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 101 | self.locks_dir = locks_dir |
| 102 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 103 | # We have to wait to do these imports until the paths above have |
| 104 | # been fixed. |
Yunlian Jiang | d97422a | 2015-12-16 11:06:13 -0800 | [diff] [blame] | 105 | # pylint: disable=import-error |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 106 | from client import setup_modules |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 107 | setup_modules.setup( |
| 108 | base_path=autotest_path, root_module_name='autotest_lib') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 109 | |
| 110 | from dynamic_suite import frontend_wrappers |
| 111 | |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 112 | self.afe = frontend_wrappers.RetryingAFE( |
| 113 | timeout_min=30, delay_sec=10, debug=False, server='cautotest') |
| 114 | |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 115 | self.machines = list(set(remotes)) or [] |
| 116 | self.toolchain_lab_machines = self.GetAllToolchainLabMachines() |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 117 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 118 | if not self.machines: |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 119 | self.machines = self.toolchain_lab_machines |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 120 | self.force = force_option |
| 121 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 122 | self.local_machines = [] |
| 123 | self.skylab_machines = [] |
| 124 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 125 | def CheckMachine(self, machine, error_msg): |
| 126 | """Verifies that machine is responding to ping. |
| 127 | |
| 128 | Args: |
| 129 | machine: String containing the name or ip address of machine to check. |
| 130 | error_msg: Message to print if ping fails. |
| 131 | |
| 132 | Raises: |
| 133 | MachineNotPingable: If machine is not responding to 'ping' |
| 134 | """ |
| 135 | if not machines.MachineIsPingable(machine, logging_level='none'): |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 136 | cros_machine = machine + '.cros' |
| 137 | if not machines.MachineIsPingable(cros_machine, logging_level='none'): |
| 138 | raise MachineNotPingable(error_msg) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 139 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 140 | def GetAllToolchainLabMachines(self): |
| 141 | """Gets a list of all the toolchain machines in the ChromeOS HW lab. |
| 142 | |
| 143 | Returns: |
| 144 | A list of names of the toolchain machines in the ChromeOS HW lab. |
| 145 | """ |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 146 | machines_file = os.path.join( |
| 147 | os.path.dirname(__file__), 'crosperf', 'default_remotes') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 148 | machine_list = [] |
| 149 | with open(machines_file, 'r') as input_file: |
| 150 | lines = input_file.readlines() |
| 151 | for line in lines: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 152 | _, remotes = line.split(':') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 153 | remotes = remotes.strip() |
| 154 | for r in remotes.split(): |
| 155 | machine_list.append(r.strip()) |
| 156 | return machine_list |
| 157 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 158 | def PrintStatusHeader(self, is_lab_machine): |
| 159 | """Prints the status header lines for machines. |
| 160 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 161 | Args: |
| 162 | is_lab_machine: Boolean indicating whether to print HW Lab header or |
| 163 | local machine header (different spacing). |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 164 | """ |
| 165 | if is_lab_machine: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 166 | print('\nMachine (Board)\t\t\t\t\tStatus') |
| 167 | print('---------------\t\t\t\t\t------\n') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 168 | else: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 169 | print('\nMachine (Board)\t\tStatus') |
| 170 | print('---------------\t\t------\n') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 171 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 172 | def AddMachineToLocal(self, machine): |
| 173 | """Adds a machine to local machine list. |
| 174 | |
| 175 | Args: |
| 176 | machine: The machine to be added. |
| 177 | """ |
| 178 | if machine not in self.local_machines: |
| 179 | self.local_machines.append(machine) |
| 180 | |
| 181 | def AddMachineToSkylab(self, machine): |
| 182 | """Adds a machine to skylab machine list. |
| 183 | |
| 184 | Args: |
| 185 | machine: The machine to be added. |
| 186 | """ |
| 187 | if machine not in self.skylab_machines: |
| 188 | self.skylab_machines.append(machine) |
| 189 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 190 | def ListMachineStates(self, machine_states): |
| 191 | """Gets and prints the current status for a list of machines. |
| 192 | |
| 193 | Prints out the current status for all of the machines in the current |
| 194 | AFELockManager's list of machines (set when the object is initialized). |
| 195 | |
| 196 | Args: |
| 197 | machine_states: A dictionary of the current state of every machine in |
| 198 | the current AFELockManager's list of machines. Normally obtained by |
| 199 | calling AFELockManager::GetMachineStates. |
| 200 | """ |
| 201 | local_machines = [] |
| 202 | printed_hdr = False |
| 203 | for m in machine_states: |
| 204 | cros_name = m + '.cros' |
| 205 | if (m in self.toolchain_lab_machines or |
| 206 | cros_name in self.toolchain_lab_machines): |
Caroline Tice | 3f43271 | 2015-12-07 14:51:53 -0800 | [diff] [blame] | 207 | name = m if m in self.toolchain_lab_machines else cros_name |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 208 | if not printed_hdr: |
| 209 | self.PrintStatusHeader(True) |
| 210 | printed_hdr = True |
| 211 | state = machine_states[m] |
| 212 | if state['locked']: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 213 | print('%s (%s)\tlocked by %s since %s' % |
| 214 | (name, state['board'], state['locked_by'], state['lock_time'])) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 215 | else: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 216 | print('%s (%s)\tunlocked' % (name, state['board'])) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 217 | else: |
| 218 | local_machines.append(m) |
| 219 | |
| 220 | if local_machines: |
| 221 | self.PrintStatusHeader(False) |
| 222 | for m in local_machines: |
| 223 | state = machine_states[m] |
| 224 | if state['locked']: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 225 | print('%s (%s)\tlocked by %s since %s' % |
| 226 | (m, state['board'], state['locked_by'], state['lock_time'])) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 227 | else: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 228 | print('%s (%s)\tunlocked' % (m, state['board'])) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 229 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 230 | def UpdateLockInAFE(self, should_lock_machine, machine): |
| 231 | """Calls an AFE server to lock/unlock a machine. |
| 232 | |
| 233 | Args: |
| 234 | should_lock_machine: Boolean indicating whether to lock the machine (True) |
| 235 | or unlock the machine (False). |
| 236 | machine: The machine to update. |
| 237 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 238 | Returns: |
| 239 | True if requested action succeeded, else False. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 240 | """ |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 241 | kwargs = {'locked': should_lock_machine} |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 242 | if should_lock_machine: |
| 243 | kwargs['lock_reason'] = 'toolchain user request (%s)' % self.user |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 244 | |
Caroline Tice | 3f43271 | 2015-12-07 14:51:53 -0800 | [diff] [blame] | 245 | cros_name = machine + '.cros' |
| 246 | if cros_name in self.toolchain_lab_machines: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 247 | machine = cros_name |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 248 | if machine in self.toolchain_lab_machines: |
| 249 | m = machine.split('.')[0] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 250 | afe_server = self.afe |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 251 | |
| 252 | try: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 253 | afe_server.run( |
| 254 | 'modify_hosts', |
| 255 | host_filter_data={'hostname__in': [m]}, |
| 256 | update_data=kwargs) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 257 | except Exception: |
| 258 | return False |
| 259 | return True |
| 260 | |
| 261 | def UpdateLockInSkylab(self, should_lock_machine, machine): |
| 262 | """Ask skylab to lease/release a machine. |
| 263 | |
| 264 | Args: |
| 265 | should_lock_machine: Boolean indicating whether to lock the machine (True) |
| 266 | or unlock the machine (False). |
| 267 | machine: The machine to update. |
| 268 | |
| 269 | Returns: |
| 270 | True if requested action succeeded, else False. |
| 271 | """ |
| 272 | try: |
| 273 | if should_lock_machine: |
| 274 | ret = self.LeaseSkylabMachine(machine) |
| 275 | else: |
| 276 | ret = self.ReleaseSkylabMachine(machine) |
| 277 | except Exception: |
| 278 | return False |
| 279 | return ret |
| 280 | |
| 281 | def UpdateFileLock(self, should_lock_machine, machine): |
| 282 | """Use file lock for local machines, |
| 283 | |
| 284 | Args: |
| 285 | should_lock_machine: Boolean indicating whether to lock the machine (True) |
| 286 | or unlock the machine (False). |
| 287 | machine: The machine to update. |
| 288 | |
| 289 | Returns: |
| 290 | True if requested action succeeded, else False. |
| 291 | """ |
| 292 | try: |
| 293 | if should_lock_machine: |
| 294 | ret = file_lock_machine.Machine(machine, self.locks_dir).Lock( |
| 295 | True, sys.argv[0]) |
| 296 | else: |
| 297 | ret = file_lock_machine.Machine(machine, self.locks_dir).Unlock(True) |
| 298 | except Exception: |
| 299 | return False |
| 300 | return ret |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 301 | |
| 302 | def UpdateMachines(self, lock_machines): |
| 303 | """Sets the locked state of the machines to the requested value. |
| 304 | |
| 305 | The machines updated are the ones in self.machines (specified when the |
| 306 | class object was intialized). |
| 307 | |
| 308 | Args: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 309 | lock_machines: Boolean indicating whether to lock the machines (True) or |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 310 | unlock the machines (False). |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 311 | |
| 312 | Returns: |
| 313 | A list of the machines whose state was successfully updated. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 314 | """ |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 315 | updated_machines = [] |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 316 | action = 'Locking' if lock_machines else 'Unlocking' |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 317 | for m in self.machines: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 318 | # TODO(zhizhouy): Handling exceptions with more details when locking |
| 319 | # doesn't succeed. |
| 320 | machine_type = 'afe' |
| 321 | if m in self.skylab_machines: |
| 322 | ret = self.UpdateLockInSkylab(lock_machines, m) |
| 323 | machine_type = 'skylab' |
| 324 | elif m in self.local_machines: |
| 325 | ret = self.UpdateFileLock(lock_machines, m) |
| 326 | machine_type = 'local' |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 327 | else: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 328 | ret = self.UpdateLockInAFE(lock_machines, m) |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 329 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 330 | if ret: |
| 331 | self.logger.LogOutput( |
| 332 | '%s %s machine succeeded: %s.' % (action, machine_type, m)) |
| 333 | updated_machines.append(m) |
| 334 | else: |
| 335 | self.logger.LogOutput( |
| 336 | '%s %s machine failed: %s.' % (action, machine_type, m)) |
| 337 | |
| 338 | self.machines = updated_machines |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 339 | return updated_machines |
| 340 | |
| 341 | def _InternalRemoveMachine(self, machine): |
| 342 | """Remove machine from internal list of machines. |
| 343 | |
| 344 | Args: |
| 345 | machine: Name of machine to be removed from internal list. |
| 346 | """ |
| 347 | # Check to see if machine is lab machine and if so, make sure it has |
| 348 | # ".cros" on the end. |
| 349 | cros_machine = machine |
| 350 | if machine.find('rack') > 0 and machine.find('row') > 0: |
| 351 | if machine.find('.cros') == -1: |
| 352 | cros_machine = cros_machine + '.cros' |
| 353 | |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 354 | self.machines = [ |
| 355 | m for m in self.machines if m != cros_machine and m != machine |
| 356 | ] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 357 | |
| 358 | def CheckMachineLocks(self, machine_states, cmd): |
| 359 | """Check that every machine in requested list is in the proper state. |
| 360 | |
| 361 | If the cmd is 'unlock' verify that every machine is locked by requestor. |
| 362 | If the cmd is 'lock' verify that every machine is currently unlocked. |
| 363 | |
| 364 | Args: |
| 365 | machine_states: A dictionary of the current state of every machine in |
| 366 | the current AFELockManager's list of machines. Normally obtained by |
| 367 | calling AFELockManager::GetMachineStates. |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 368 | cmd: The user-requested action for the machines: 'lock' or 'unlock'. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 369 | |
| 370 | Raises: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 371 | DontOwnLock: The lock on a requested machine is owned by someone else. |
| 372 | """ |
| 373 | for k, state in machine_states.iteritems(): |
| 374 | if cmd == 'unlock': |
| 375 | if not state['locked']: |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 376 | self.logger.LogWarning('Attempt to unlock already unlocked machine ' |
| 377 | '(%s).' % k) |
| 378 | self._InternalRemoveMachine(k) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 379 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 380 | if state['locked'] and 'locked_by' in state and \ |
| 381 | state['locked_by'] != self.user: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 382 | raise DontOwnLock('Attempt to unlock machine (%s) locked by someone ' |
| 383 | 'else (%s).' % (k, state['locked_by'])) |
| 384 | elif cmd == 'lock': |
| 385 | if state['locked']: |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 386 | self.logger.LogWarning( |
| 387 | 'Attempt to lock already locked machine (%s)' % k) |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 388 | self._InternalRemoveMachine(k) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 389 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 390 | def GetMachineStates(self, cmd=''): |
| 391 | """Gets the current state of all the requested machines. |
| 392 | |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 393 | Gets the current state of all the requested machines. Stores the data in a |
| 394 | dictionary keyed by machine name. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 395 | |
| 396 | Args: |
| 397 | cmd: The command for which we are getting the machine states. This is |
| 398 | important because if one of the requested machines is missing we raise |
| 399 | an exception, unless the requested command is 'add'. |
| 400 | |
| 401 | Returns: |
| 402 | A dictionary of machine states for all the machines in the AFELockManager |
| 403 | object. |
| 404 | |
| 405 | Raises: |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 406 | NoAFEServer: Cannot find the HW Lab AFE server. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 407 | AFEAccessError: An error occurred when querying the server about a |
| 408 | machine. |
| 409 | """ |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 410 | if not self.afe: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 411 | raise NoAFEServer('Error: Cannot connect to main AFE server.') |
| 412 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 413 | machine_list = {} |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 414 | for m in self.machines: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 415 | # For local or skylab machines, we simply set {'locked': status} for them |
| 416 | # TODO(zhizhouy): This is a quick fix since skylab cannot return host info |
| 417 | # as afe does. We need to get more info such as locked_by when skylab |
| 418 | # supports that. |
| 419 | if m in self.local_machines or m in self.skylab_machines: |
| 420 | machine_list[m] = {'locked': 0 if cmd == 'lock' else 1} |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 421 | else: |
| 422 | # For autotest machines, we use afe APIs to get locking info. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 423 | mod_host = m.split('.')[0] |
| 424 | host_info = self.afe.get_hosts(hostname=mod_host) |
| 425 | if not host_info: |
| 426 | raise AFEAccessError('Unable to get information about %s from main' |
| 427 | ' autotest server.' % m) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 428 | host_info = host_info[0] |
| 429 | name = host_info.hostname |
| 430 | values = {} |
| 431 | values['board'] = host_info.platform if host_info.platform else '??' |
| 432 | values['locked'] = host_info.locked |
| 433 | if host_info.locked: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 434 | values['locked_by'] = host_info.locked_by |
| 435 | values['lock_time'] = host_info.lock_time |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 436 | else: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 437 | values['locked_by'] = '' |
| 438 | values['lock_time'] = '' |
| 439 | machine_list[name] = values |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 440 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 441 | return machine_list |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 442 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 443 | def CheckMachineInSkylab(self, machine): |
| 444 | """Run command to check if machine is in Skylab or not. |
| 445 | |
| 446 | Returns: |
| 447 | True if machine in skylab, else False |
| 448 | """ |
| 449 | credential = '' |
| 450 | if os.path.exists(self.SKYLAB_CREDENTIAL): |
| 451 | credential = '--auth-service-account-json %s' % self.SKYLAB_CREDENTIAL |
| 452 | swarming = os.path.join(self.chromeos_root, self.SWARMING) |
| 453 | cmd = (('%s query --swarming https://chromeos-swarming.appspot.com ' \ |
| 454 | "%s 'bots/list?is_dead=FALSE&dimensions=dut_name:%s'") % \ |
| 455 | (swarming, |
| 456 | credential, |
| 457 | machine.rstrip('.cros'))) |
| 458 | ret_tup = self.ce.RunCommandWOutput(cmd) |
| 459 | # The command will return a json output as stdout. If machine not in skylab |
| 460 | # stdout will look like this: |
| 461 | # { |
| 462 | # "death_timeout": "600", |
| 463 | # "now": "TIMESTAMP" |
| 464 | # } |
| 465 | # Otherwise there will be a tuple starting with 'items', we simply detect |
| 466 | # this keyword for result. |
| 467 | if 'items' not in ret_tup[1]: |
| 468 | return False |
| 469 | else: |
| 470 | return True |
| 471 | |
| 472 | def LeaseSkylabMachine(self, machine): |
| 473 | """Run command to lease dut from skylab. |
| 474 | |
| 475 | Returns: |
| 476 | True if succeeded, False if failed. |
| 477 | """ |
| 478 | credential = '' |
| 479 | if os.path.exists(self.SKYLAB_CREDENTIAL): |
| 480 | credential = '-service-account-json %s' % self.SKYLAB_CREDENTIAL |
| 481 | cmd = (('%s lease-dut -minutes %s %s %s') % \ |
| 482 | (self.SKYLAB_PATH, |
| 483 | self.LEASE_MINS, |
| 484 | credential, |
| 485 | machine.rstrip('.cros'))) |
| 486 | # Wait 120 seconds for server to start the lease task, if not started, |
| 487 | # we will treat it as unavailable. |
| 488 | check_interval_time = 120 |
| 489 | retval = self.ce.RunCommand(cmd, command_timeout=check_interval_time) |
| 490 | return retval == self.SUCCESS |
| 491 | |
| 492 | def ReleaseSkylabMachine(self, machine): |
| 493 | """Run command to release dut from skylab. |
| 494 | |
| 495 | Returns: |
| 496 | True if succeeded, False if failed. |
| 497 | """ |
| 498 | credential = '' |
| 499 | if os.path.exists(self.SKYLAB_CREDENTIAL): |
| 500 | credential = '-service-account-json %s' % self.SKYLAB_CREDENTIAL |
| 501 | cmd = (('%s release-dut %s %s') % \ |
| 502 | (self.SKYLAB_PATH, |
| 503 | credential, |
| 504 | machine.rstrip('.cros'))) |
| 505 | retval = self.ce.RunCommand(cmd) |
| 506 | return retval == self.SUCCESS |
| 507 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 508 | |
| 509 | def Main(argv): |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 510 | """Parse the options, initialize lock manager and dispatch proper method. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 511 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 512 | Args: |
| 513 | argv: The options with which this script was invoked. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 514 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 515 | Returns: |
| 516 | 0 unless an exception is raised. |
| 517 | """ |
| 518 | parser = argparse.ArgumentParser() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 519 | |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 520 | parser.add_argument( |
| 521 | '--list', |
| 522 | dest='cmd', |
| 523 | action='store_const', |
| 524 | const='status', |
| 525 | help='List current status of all known machines.') |
| 526 | parser.add_argument( |
| 527 | '--lock', |
| 528 | dest='cmd', |
| 529 | action='store_const', |
| 530 | const='lock', |
| 531 | help='Lock given machine(s).') |
| 532 | parser.add_argument( |
| 533 | '--unlock', |
| 534 | dest='cmd', |
| 535 | action='store_const', |
| 536 | const='unlock', |
| 537 | help='Unlock given machine(s).') |
| 538 | parser.add_argument( |
| 539 | '--status', |
| 540 | dest='cmd', |
| 541 | action='store_const', |
| 542 | const='status', |
| 543 | help='List current status of given machine(s).') |
| 544 | parser.add_argument( |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 545 | '--remote', dest='remote', help='machines on which to operate') |
| 546 | parser.add_argument( |
| 547 | '--chromeos_root', |
| 548 | dest='chromeos_root', |
| 549 | required=True, |
| 550 | help='ChromeOS root to use for autotest scripts.') |
| 551 | parser.add_argument( |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 552 | '--force', |
| 553 | dest='force', |
| 554 | action='store_true', |
| 555 | default=False, |
| 556 | help='Force lock/unlock of machines, even if not' |
| 557 | ' current lock owner.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 558 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 559 | options = parser.parse_args(argv) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 560 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 561 | if not options.remote and options.cmd != 'status': |
| 562 | parser.error('No machines specified for operation.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 563 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 564 | if not os.path.isdir(options.chromeos_root): |
| 565 | parser.error('Cannot find chromeos_root: %s.' % options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 566 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 567 | if not options.cmd: |
| 568 | parser.error('No operation selected (--list, --status, --lock, --unlock,' |
| 569 | ' --add_machine, --remove_machine).') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 570 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 571 | machine_list = [] |
| 572 | if options.remote: |
| 573 | machine_list = options.remote.split() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 574 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 575 | lock_manager = AFELockManager(machine_list, options.force, |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame^] | 576 | options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 577 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 578 | machine_states = lock_manager.GetMachineStates(cmd=options.cmd) |
| 579 | cmd = options.cmd |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 580 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 581 | if cmd == 'status': |
| 582 | lock_manager.ListMachineStates(machine_states) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 583 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 584 | elif cmd == 'lock': |
| 585 | if not lock_manager.force: |
| 586 | lock_manager.CheckMachineLocks(machine_states, cmd) |
| 587 | lock_manager.UpdateMachines(True) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 588 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 589 | elif cmd == 'unlock': |
| 590 | if not lock_manager.force: |
| 591 | lock_manager.CheckMachineLocks(machine_states, cmd) |
| 592 | lock_manager.UpdateMachines(False) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 593 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 594 | elif cmd == 'add': |
| 595 | lock_manager.AddMachinesToLocalServer() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 596 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 597 | elif cmd == 'remove': |
| 598 | lock_manager.RemoveMachinesFromLocalServer() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 599 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 600 | return 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 601 | |
| 602 | |
| 603 | if __name__ == '__main__': |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 604 | sys.exit(Main(sys.argv[1:])) |