Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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 | # |
George Burgess IV | 2124be5 | 2022-04-21 10:27:37 -0700 | [diff] [blame] | 4 | # Copyright 2019 The ChromiumOS Authors. All rights reserved. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 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 | from cros_utils import command_executer |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 19 | from cros_utils import logger |
| 20 | from cros_utils import machines |
Denis Nikitin | 444382a | 2022-05-16 12:30:45 -0700 | [diff] [blame^] | 21 | import file_lock_machine |
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 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 24 | class LockException(Exception): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 25 | """Base class for exceptions in this module.""" |
| 26 | |
| 27 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 28 | class MachineNotPingable(LockException): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 29 | """Raised when machine does not respond to ping.""" |
| 30 | |
| 31 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 32 | class LockingError(LockException): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 33 | """Raised when server fails to lock/unlock machine as requested.""" |
| 34 | |
| 35 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 36 | class DontOwnLock(LockException): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 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 | |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 41 | class MachineType(enum.Enum): |
| 42 | """Enum class to hold machine type.""" |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 43 | LOCAL = 'local' |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 44 | CROSFLEET = 'crosfleet' |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 45 | |
| 46 | |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 47 | class LockManager(object): |
| 48 | """Class for locking/unlocking machines vie three different modes. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 49 | |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame] | 50 | This class contains methods for checking the locked status of machines, |
Zhizhou Yang | 0ee6a57 | 2020-01-22 15:55:19 -0800 | [diff] [blame] | 51 | and for changing the locked status. It handles HW lab machines and local |
| 52 | machines, using appropriate locking mechanisms for each. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 53 | """ |
| 54 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 55 | CROSFLEET_PATH = 'crosfleet' |
Zhizhou Yang | 13446a7 | 2019-10-29 16:50:14 -0700 | [diff] [blame] | 56 | |
| 57 | # TODO(zhizhouy): lease time may needs to be dynamically adjusted. For now we |
| 58 | # set it long enough to cover the period to finish nightly rotation tests. |
George Burgess IV | b35bdfe | 2020-05-02 10:52:07 -0700 | [diff] [blame] | 59 | LEASE_MINS = 1439 |
Zhizhou Yang | 13446a7 | 2019-10-29 16:50:14 -0700 | [diff] [blame] | 60 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 61 | CROSFLEET_CREDENTIAL = ('/usr/local/google/home/mobiletc-prebuild' |
| 62 | '/sheriff_utils/credentials/skylab' |
| 63 | '/chromeos-swarming-credential.json') |
Ryan Beltran | 20d36f9 | 2022-03-26 00:13:03 +0000 | [diff] [blame] | 64 | SWARMING = '~/cipd_binaries/swarming' |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 65 | SUCCESS = 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 66 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 67 | def __init__(self, |
| 68 | remotes, |
| 69 | force_option, |
| 70 | chromeos_root, |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 71 | locks_dir='', |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 72 | log=None): |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 73 | """Initializes an LockManager object. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 74 | |
| 75 | Args: |
| 76 | 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] | 77 | and ip addresses should be represented as strings. If the list is |
| 78 | empty, the lock manager will get all known machines. |
| 79 | force_option: A Boolean indicating whether or not to force an unlock of |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 80 | a machine that was locked by someone else. |
| 81 | chromeos_root: The ChromeOS chroot to use for the autotest scripts. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 82 | locks_dir: A directory used for file locking local devices. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 83 | log: If not None, this is the logger object to be used for writing out |
| 84 | informational output messages. It is expected to be an instance of |
Caroline Tice | a8af9a7 | 2016-07-20 12:52:59 -0700 | [diff] [blame] | 85 | Logger class from cros_utils/logger.py. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 86 | """ |
| 87 | self.chromeos_root = chromeos_root |
| 88 | self.user = getpass.getuser() |
| 89 | self.logger = log or logger.GetLogger() |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 90 | self.ce = command_executer.GetCommandExecuter(self.logger) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 91 | |
cmtice | d1172b4 | 2015-06-12 15:14:09 -0700 | [diff] [blame] | 92 | sys.path.append(chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 93 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 94 | self.locks_dir = locks_dir |
| 95 | |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 96 | self.machines = list(set(remotes)) or [] |
| 97 | self.toolchain_lab_machines = self.GetAllToolchainLabMachines() |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 98 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 99 | if not self.machines: |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame] | 100 | self.machines = self.toolchain_lab_machines |
Caroline Tice | 6b16138 | 2016-09-15 15:03:46 -0700 | [diff] [blame] | 101 | self.force = force_option |
| 102 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 103 | self.local_machines = [] |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 104 | self.crosfleet_machines = [] |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 105 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 106 | def CheckMachine(self, machine, error_msg): |
| 107 | """Verifies that machine is responding to ping. |
| 108 | |
| 109 | Args: |
| 110 | machine: String containing the name or ip address of machine to check. |
| 111 | error_msg: Message to print if ping fails. |
| 112 | |
| 113 | Raises: |
| 114 | MachineNotPingable: If machine is not responding to 'ping' |
| 115 | """ |
| 116 | if not machines.MachineIsPingable(machine, logging_level='none'): |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 117 | cros_machine = machine + '.cros' |
| 118 | if not machines.MachineIsPingable(cros_machine, logging_level='none'): |
| 119 | raise MachineNotPingable(error_msg) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 120 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 121 | def GetAllToolchainLabMachines(self): |
| 122 | """Gets a list of all the toolchain machines in the ChromeOS HW lab. |
| 123 | |
| 124 | Returns: |
| 125 | A list of names of the toolchain machines in the ChromeOS HW lab. |
| 126 | """ |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 127 | machines_file = os.path.join(os.path.dirname(__file__), 'crosperf', |
| 128 | 'default_remotes') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 129 | machine_list = [] |
| 130 | with open(machines_file, 'r') as input_file: |
| 131 | lines = input_file.readlines() |
| 132 | for line in lines: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 133 | _, remotes = line.split(':') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 134 | remotes = remotes.strip() |
| 135 | for r in remotes.split(): |
| 136 | machine_list.append(r.strip()) |
| 137 | return machine_list |
| 138 | |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 139 | def GetMachineType(self, m): |
| 140 | """Get where the machine is located. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 141 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 142 | Args: |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 143 | m: String containing the name or ip address of machine. |
| 144 | |
| 145 | Returns: |
| 146 | Value of the type in MachineType Enum. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 147 | """ |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 148 | if m in self.local_machines: |
| 149 | return MachineType.LOCAL |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 150 | if m in self.crosfleet_machines: |
| 151 | return MachineType.CROSFLEET |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 152 | |
| 153 | def PrintStatusHeader(self): |
| 154 | """Prints the status header lines for machines.""" |
| 155 | print('\nMachine (Board)\t\t\t\t\tStatus') |
| 156 | print('---------------\t\t\t\t\t------') |
| 157 | |
| 158 | def PrintStatus(self, m, state, machine_type): |
| 159 | """Prints status for a single machine. |
| 160 | |
| 161 | Args: |
| 162 | m: String containing the name or ip address of machine. |
| 163 | state: A dictionary of the current state of the machine. |
| 164 | machine_type: MachineType to determine where the machine is located. |
| 165 | """ |
| 166 | if state['locked']: |
Zhizhou Yang | 5a53a33 | 2019-10-07 13:27:37 -0700 | [diff] [blame] | 167 | print('%s (%s)\t\t%slocked by %s since %s' % |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 168 | (m, state['board'], '\t\t' if machine_type == MachineType.LOCAL |
| 169 | else '', state['locked_by'], state['lock_time'])) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 170 | else: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 171 | print('%s (%s)\t\t%sunlocked' % |
| 172 | (m, state['board'], |
| 173 | '\t\t' if machine_type == MachineType.LOCAL else '')) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 174 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 175 | def AddMachineToLocal(self, machine): |
| 176 | """Adds a machine to local machine list. |
| 177 | |
| 178 | Args: |
| 179 | machine: The machine to be added. |
| 180 | """ |
| 181 | if machine not in self.local_machines: |
| 182 | self.local_machines.append(machine) |
| 183 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 184 | def AddMachineToCrosfleet(self, machine): |
| 185 | """Adds a machine to crosfleet machine list. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 186 | |
| 187 | Args: |
| 188 | machine: The machine to be added. |
| 189 | """ |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 190 | if machine not in self.crosfleet_machines: |
| 191 | self.crosfleet_machines.append(machine) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 192 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 193 | def ListMachineStates(self, machine_states): |
| 194 | """Gets and prints the current status for a list of machines. |
| 195 | |
| 196 | Prints out the current status for all of the machines in the current |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 197 | LockManager's list of machines (set when the object is initialized). |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 198 | |
| 199 | Args: |
| 200 | machine_states: A dictionary of the current state of every machine in |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 201 | the current LockManager's list of machines. Normally obtained by |
| 202 | calling LockManager::GetMachineStates. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 203 | """ |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 204 | self.PrintStatusHeader() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 205 | for m in machine_states: |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 206 | machine_type = self.GetMachineType(m) |
| 207 | state = machine_states[m] |
| 208 | self.PrintStatus(m, state, machine_type) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 209 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 210 | def UpdateLockInCrosfleet(self, should_lock_machine, machine): |
| 211 | """Ask crosfleet to lease/release a machine. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 212 | |
| 213 | Args: |
| 214 | should_lock_machine: Boolean indicating whether to lock the machine (True) |
| 215 | or unlock the machine (False). |
| 216 | machine: The machine to update. |
| 217 | |
| 218 | Returns: |
| 219 | True if requested action succeeded, else False. |
| 220 | """ |
| 221 | try: |
| 222 | if should_lock_machine: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 223 | ret = self.LeaseCrosfleetMachine(machine) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 224 | else: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 225 | ret = self.ReleaseCrosfleetMachine(machine) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 226 | except Exception: |
| 227 | return False |
| 228 | return ret |
| 229 | |
| 230 | def UpdateFileLock(self, should_lock_machine, machine): |
| 231 | """Use file lock for local machines, |
| 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 | |
| 238 | Returns: |
| 239 | True if requested action succeeded, else False. |
| 240 | """ |
| 241 | try: |
| 242 | if should_lock_machine: |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 243 | ret = file_lock_machine.Machine(machine, self.locks_dir).Lock( |
| 244 | True, sys.argv[0]) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 245 | else: |
| 246 | ret = file_lock_machine.Machine(machine, self.locks_dir).Unlock(True) |
| 247 | except Exception: |
| 248 | return False |
| 249 | return ret |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 250 | |
| 251 | def UpdateMachines(self, lock_machines): |
| 252 | """Sets the locked state of the machines to the requested value. |
| 253 | |
| 254 | The machines updated are the ones in self.machines (specified when the |
| 255 | class object was intialized). |
| 256 | |
| 257 | Args: |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 258 | lock_machines: Boolean indicating whether to lock the machines (True) or |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 259 | unlock the machines (False). |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 260 | |
| 261 | Returns: |
| 262 | A list of the machines whose state was successfully updated. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 263 | """ |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 264 | updated_machines = [] |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 265 | action = 'Locking' if lock_machines else 'Unlocking' |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 266 | for m in self.machines: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 267 | # TODO(zhizhouy): Handling exceptions with more details when locking |
| 268 | # doesn't succeed. |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 269 | machine_type = self.GetMachineType(m) |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 270 | if machine_type == MachineType.CROSFLEET: |
| 271 | ret = self.UpdateLockInCrosfleet(lock_machines, m) |
Zhizhou Yang | 5322d4a | 2019-09-30 13:10:29 -0700 | [diff] [blame] | 272 | elif machine_type == MachineType.LOCAL: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 273 | ret = self.UpdateFileLock(lock_machines, m) |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 274 | |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 275 | if ret: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 276 | self.logger.LogOutput('%s %s machine succeeded: %s.' % |
| 277 | (action, machine_type.value, m)) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 278 | updated_machines.append(m) |
| 279 | else: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 280 | self.logger.LogOutput('%s %s machine failed: %s.' % |
| 281 | (action, machine_type.value, m)) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 282 | |
| 283 | self.machines = updated_machines |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 284 | return updated_machines |
| 285 | |
| 286 | def _InternalRemoveMachine(self, machine): |
| 287 | """Remove machine from internal list of machines. |
| 288 | |
| 289 | Args: |
| 290 | machine: Name of machine to be removed from internal list. |
| 291 | """ |
| 292 | # Check to see if machine is lab machine and if so, make sure it has |
| 293 | # ".cros" on the end. |
| 294 | cros_machine = machine |
| 295 | if machine.find('rack') > 0 and machine.find('row') > 0: |
| 296 | if machine.find('.cros') == -1: |
| 297 | cros_machine = cros_machine + '.cros' |
| 298 | |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 299 | self.machines = [ |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame] | 300 | m for m in self.machines if m not in (cros_machine, machine) |
Caroline Tice | f6ef439 | 2017-04-06 17:16:05 -0700 | [diff] [blame] | 301 | ] |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 302 | |
| 303 | def CheckMachineLocks(self, machine_states, cmd): |
| 304 | """Check that every machine in requested list is in the proper state. |
| 305 | |
| 306 | If the cmd is 'unlock' verify that every machine is locked by requestor. |
| 307 | If the cmd is 'lock' verify that every machine is currently unlocked. |
| 308 | |
| 309 | Args: |
| 310 | machine_states: A dictionary of the current state of every machine in |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 311 | the current LockManager's list of machines. Normally obtained by |
| 312 | calling LockManager::GetMachineStates. |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 313 | cmd: The user-requested action for the machines: 'lock' or 'unlock'. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 314 | |
| 315 | Raises: |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 316 | DontOwnLock: The lock on a requested machine is owned by someone else. |
| 317 | """ |
Zhizhou Yang | 5534af8 | 2020-01-15 16:25:04 -0800 | [diff] [blame] | 318 | for k, state in machine_states.items(): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 319 | if cmd == 'unlock': |
| 320 | if not state['locked']: |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 321 | self.logger.LogWarning('Attempt to unlock already unlocked machine ' |
| 322 | '(%s).' % k) |
| 323 | self._InternalRemoveMachine(k) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 324 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 325 | # TODO(zhizhouy): Crosfleet doesn't support host info such as locked_by. |
| 326 | # Need to update this when crosfleet supports it. |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 327 | if (state['locked'] and state['locked_by'] |
| 328 | and state['locked_by'] != self.user): |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 329 | raise DontOwnLock('Attempt to unlock machine (%s) locked by someone ' |
| 330 | 'else (%s).' % (k, state['locked_by'])) |
| 331 | elif cmd == 'lock': |
| 332 | if state['locked']: |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 333 | self.logger.LogWarning( |
| 334 | 'Attempt to lock already locked machine (%s)' % k) |
cmtice | f3eb803 | 2015-07-27 13:55:52 -0700 | [diff] [blame] | 335 | self._InternalRemoveMachine(k) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 336 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 337 | def GetMachineStates(self, cmd=''): |
| 338 | """Gets the current state of all the requested machines. |
| 339 | |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame] | 340 | Gets the current state of all the requested machines. Stores the data in a |
| 341 | dictionary keyed by machine name. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 342 | |
| 343 | Args: |
| 344 | cmd: The command for which we are getting the machine states. This is |
| 345 | important because if one of the requested machines is missing we raise |
| 346 | an exception, unless the requested command is 'add'. |
| 347 | |
| 348 | Returns: |
Zhizhou Yang | bf7ee87 | 2019-10-14 17:36:09 -0700 | [diff] [blame] | 349 | A dictionary of machine states for all the machines in the LockManager |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 350 | object. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 351 | """ |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 352 | machine_list = {} |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 353 | for m in self.machines: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 354 | # For local or crosfleet machines, we simply set {'locked': status} for |
| 355 | # them |
| 356 | # TODO(zhizhouy): This is a quick fix since crosfleet cannot return host |
| 357 | # info as afe does. We need to get more info such as locked_by when |
| 358 | # crosfleet supports that. |
Zhizhou Yang | 0ee6a57 | 2020-01-22 15:55:19 -0800 | [diff] [blame] | 359 | values = { |
| 360 | 'locked': 0 if cmd == 'lock' else 1, |
| 361 | 'board': '??', |
| 362 | 'locked_by': '', |
| 363 | 'lock_time': '' |
| 364 | } |
| 365 | machine_list[m] = values |
Zhizhou Yang | 4713fd1 | 2019-09-24 10:32:00 -0700 | [diff] [blame] | 366 | |
Zhizhou Yang | 5a53a33 | 2019-10-07 13:27:37 -0700 | [diff] [blame] | 367 | self.ListMachineStates(machine_list) |
| 368 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 369 | return machine_list |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 370 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 371 | def CheckMachineInCrosfleet(self, machine): |
| 372 | """Run command to check if machine is in Crosfleet or not. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 373 | |
| 374 | Returns: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 375 | True if machine in crosfleet, else False |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 376 | """ |
| 377 | credential = '' |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 378 | if os.path.exists(self.CROSFLEET_CREDENTIAL): |
Ryan Beltran | 20d36f9 | 2022-03-26 00:13:03 +0000 | [diff] [blame] | 379 | credential = '--service-account-json %s' % self.CROSFLEET_CREDENTIAL |
| 380 | server = '--server https://chromeos-swarming.appspot.com' |
| 381 | dimensions = '--dimension dut_name=%s' % machine.rstrip('.cros') |
| 382 | |
| 383 | cmd = f'{self.SWARMING} bots {server} {credential} {dimensions}' |
George Burgess IV | 900d6e7 | 2020-05-02 10:28:55 -0700 | [diff] [blame] | 384 | exit_code, stdout, stderr = self.ce.RunCommandWOutput(cmd) |
| 385 | if exit_code: |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 386 | raise ValueError('Querying bots failed (2); stdout: %r; stderr: %r' % |
| 387 | (stdout, stderr)) |
George Burgess IV | 900d6e7 | 2020-05-02 10:28:55 -0700 | [diff] [blame] | 388 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 389 | # The command will return a json output as stdout. If machine not in |
| 390 | # crosfleet, stdout will look like this: |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 391 | # { |
| 392 | # "death_timeout": "600", |
| 393 | # "now": "TIMESTAMP" |
| 394 | # } |
| 395 | # Otherwise there will be a tuple starting with 'items', we simply detect |
| 396 | # this keyword for result. |
Ryan Beltran | 20d36f9 | 2022-03-26 00:13:03 +0000 | [diff] [blame] | 397 | return stdout != '[]' |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 398 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 399 | def LeaseCrosfleetMachine(self, machine): |
| 400 | """Run command to lease dut from crosfleet. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 401 | |
| 402 | Returns: |
| 403 | True if succeeded, False if failed. |
| 404 | """ |
| 405 | credential = '' |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 406 | if os.path.exists(self.CROSFLEET_CREDENTIAL): |
| 407 | credential = '-service-account-json %s' % self.CROSFLEET_CREDENTIAL |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 408 | cmd = (('%s dut lease -minutes %s %s %s %s') % |
Denis Nikitin | 444382a | 2022-05-16 12:30:45 -0700 | [diff] [blame^] | 409 | (self.CROSFLEET_PATH, self.LEASE_MINS, credential, '-host', |
| 410 | machine.rstrip('.cros'))) |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 411 | # Wait 8 minutes for server to start the lease task, if not started, |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 412 | # we will treat it as unavailable. |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 413 | check_interval_time = 480 |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 414 | retval = self.ce.RunCommand(cmd, command_timeout=check_interval_time) |
| 415 | return retval == self.SUCCESS |
| 416 | |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 417 | def ReleaseCrosfleetMachine(self, machine): |
| 418 | """Run command to release dut from crosfleet. |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 419 | |
| 420 | Returns: |
| 421 | True if succeeded, False if failed. |
| 422 | """ |
| 423 | credential = '' |
Christopher Di Bella | 53e9fbe | 2021-03-18 20:31:06 +0000 | [diff] [blame] | 424 | if os.path.exists(self.CROSFLEET_CREDENTIAL): |
| 425 | credential = '-service-account-json %s' % self.CROSFLEET_CREDENTIAL |
Ryan Beltran | 20d36f9 | 2022-03-26 00:13:03 +0000 | [diff] [blame] | 426 | |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 427 | cmd = (('%s dut abandon %s %s') % |
| 428 | (self.CROSFLEET_PATH, credential, machine.rstrip('.cros'))) |
Zhizhou Yang | cdd9e34 | 2019-09-19 20:56:32 -0700 | [diff] [blame] | 429 | retval = self.ce.RunCommand(cmd) |
| 430 | return retval == self.SUCCESS |
| 431 | |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 432 | |
| 433 | def Main(argv): |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 434 | """Parse the options, initialize lock manager and dispatch proper method. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 435 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 436 | Args: |
| 437 | argv: The options with which this script was invoked. |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 438 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 439 | Returns: |
| 440 | 0 unless an exception is raised. |
| 441 | """ |
| 442 | parser = argparse.ArgumentParser() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 443 | |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 444 | parser.add_argument('--list', |
| 445 | dest='cmd', |
| 446 | action='store_const', |
| 447 | const='status', |
| 448 | help='List current status of all known machines.') |
| 449 | parser.add_argument('--lock', |
| 450 | dest='cmd', |
| 451 | action='store_const', |
| 452 | const='lock', |
| 453 | help='Lock given machine(s).') |
| 454 | parser.add_argument('--unlock', |
| 455 | dest='cmd', |
| 456 | action='store_const', |
| 457 | const='unlock', |
| 458 | help='Unlock given machine(s).') |
| 459 | parser.add_argument('--status', |
| 460 | dest='cmd', |
| 461 | action='store_const', |
| 462 | const='status', |
| 463 | help='List current status of given machine(s).') |
| 464 | parser.add_argument('--remote', |
| 465 | dest='remote', |
| 466 | help='machines on which to operate') |
| 467 | parser.add_argument('--chromeos_root', |
| 468 | dest='chromeos_root', |
| 469 | required=True, |
| 470 | help='ChromeOS root to use for autotest scripts.') |
| 471 | parser.add_argument('--force', |
| 472 | dest='force', |
| 473 | action='store_true', |
| 474 | default=False, |
| 475 | help='Force lock/unlock of machines, even if not' |
| 476 | ' current lock owner.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 477 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 478 | options = parser.parse_args(argv) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 479 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 480 | if not options.remote and options.cmd != 'status': |
| 481 | parser.error('No machines specified for operation.') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 482 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 483 | if not os.path.isdir(options.chromeos_root): |
| 484 | parser.error('Cannot find chromeos_root: %s.' % options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 485 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 486 | if not options.cmd: |
| 487 | parser.error('No operation selected (--list, --status, --lock, --unlock,' |
| 488 | ' --add_machine, --remove_machine).') |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 489 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 490 | machine_list = [] |
| 491 | if options.remote: |
| 492 | machine_list = options.remote.split() |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 493 | |
Denis Nikitin | 9de6ecb | 2021-11-05 13:31:19 -0700 | [diff] [blame] | 494 | lock_manager = LockManager(machine_list, options.force, |
| 495 | options.chromeos_root) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 496 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 497 | machine_states = lock_manager.GetMachineStates(cmd=options.cmd) |
| 498 | cmd = options.cmd |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 499 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 500 | if cmd == 'status': |
| 501 | lock_manager.ListMachineStates(machine_states) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 502 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 503 | elif cmd == 'lock': |
| 504 | if not lock_manager.force: |
| 505 | lock_manager.CheckMachineLocks(machine_states, cmd) |
| 506 | lock_manager.UpdateMachines(True) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 507 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 508 | elif cmd == 'unlock': |
| 509 | if not lock_manager.force: |
| 510 | lock_manager.CheckMachineLocks(machine_states, cmd) |
| 511 | lock_manager.UpdateMachines(False) |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 512 | |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 513 | return 0 |
cmtice | e5bc63b | 2015-05-27 16:59:37 -0700 | [diff] [blame] | 514 | |
| 515 | |
| 516 | if __name__ == '__main__': |
Caroline Tice | a448645 | 2015-12-08 13:43:23 -0800 | [diff] [blame] | 517 | sys.exit(Main(sys.argv[1:])) |