1# -*- coding: utf-8 -*- 2# 3# Copyright 2019 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7"""Utilities for locking machines.""" 8 9from __future__ import print_function 10 11import time 12 13import lock_machine 14 15from cros_utils import logger 16 17 18def AcquireLock(machines, chromeos_root, timeout=1200): 19 """Acquire lock for machine(s) with timeout.""" 20 start_time = time.time() 21 locked = True 22 sleep_time = min(10, timeout / 10.0) 23 while True: 24 try: 25 lock_machine.LockManager(machines, False, 26 chromeos_root).UpdateMachines(True) 27 break 28 except Exception as e: 29 if time.time() - start_time > timeout: 30 locked = False 31 logger.GetLogger().LogWarning( 32 'Could not acquire lock on {0} within {1} seconds: {2}'.format( 33 repr(machines), timeout, str(e))) 34 break 35 time.sleep(sleep_time) 36 return locked 37 38 39def ReleaseLock(machines, chromeos_root): 40 """Release locked machine(s).""" 41 unlocked = True 42 try: 43 lock_machine.LockManager(machines, False, 44 chromeos_root).UpdateMachines(False) 45 except Exception as e: 46 unlocked = False 47 logger.GetLogger().LogWarning( 48 'Could not unlock %s. %s' % (repr(machines), str(e))) 49 return unlocked 50