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