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