diff options
author | cmtice <cmtice@google.com> | 2015-05-27 16:59:37 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2015-06-10 06:17:17 +0000 |
commit | e5bc63bbed4e001b080c4ce0b18c5c78900d4786 (patch) | |
tree | d9b603a7bbe68ece5ffcd5d312d73a34e48f4704 /utils | |
parent | 0495c7175f1160877f121872241f44386705f667 (diff) | |
download | toolchain-utils-e5bc63bbed4e001b080c4ce0b18c5c78900d4786.tar.gz |
Implement new global locking scheme for machines.
This CL implements a new machine locking mechanism using
Autotest Front End servers. When locking/unlocking a lab machine,
it uses the ChromeOS HW lab server; when locking/unlocking a local
machine, it uses a local AFE server on chrotomation2.
BUG=None
TEST=Tested the script manually to lock/unlock machines and query status.
Also tested with image_chromeos and with crosperf scripts.
Change-Id: I2793bc1f7dc056e725694e81ded656d9f49d227b
Reviewed-on: https://chrome-internal-review.googlesource.com/217560
Reviewed-by: Luis Lozano <llozano@chromium.org>
Reviewed-by: David Sharp <dhsharp@google.com>
Tested-by: Caroline Tice <cmtice@google.com>
Commit-Queue: Caroline Tice <cmtice@google.com>
Diffstat (limited to 'utils')
-rw-r--r-- | utils/locks.py | 49 | ||||
-rw-r--r-- | utils/machines.py | 26 | ||||
-rw-r--r-- | utils/misc.py | 40 |
3 files changed, 75 insertions, 40 deletions
diff --git a/utils/locks.py b/utils/locks.py new file mode 100644 index 00000000..f846764b --- /dev/null +++ b/utils/locks.py @@ -0,0 +1,49 @@ +#!/usr/bin/python + +# Copyright 2015 The Chromium OS Authors. All rights reserved. + +"""Utilities for locking machines.""" + + +import os +import sys +import time + +import afe_lock_machine +import file_lock_machine + +import logger + + +def AcquireLock(machines, chromeos_root, timeout=1200): + """Acquire lock for machine(s) with timeout, using AFE server for locking.""" + start_time = time.time() + locked = True + sleep_time = min(10, timeout/10.0) + while True: + try: + afe_lock_machine.AFELockManager(machines, False, chromeos_root, + None).UpdateMachines(True) + break + except Exception as e: + if time.time() - start_time > timeout: + locked = False + logger.GetLogger().LogWarning( + "Could not acquire lock on this machine: {0} within {1} seconds. %s" + .format(repr(machines), timeout, str(e))) + break + time.sleep(sleep_time) + return locked + + +def ReleaseLock(machines, chromeos_root): + """Release locked machine(s), using AFE server for locking.""" + unlocked = True + try: + afe_lock_machine.AFELockManager(machines, False, chromeos_root, + None).UpdateMachines(False) + except Exception as e: + unlocked = False + logger.GetLogger().LogWarning("Could not unlock %s. %s" % + (repr(machines), str(e))) + return unlocked diff --git a/utils/machines.py b/utils/machines.py new file mode 100644 index 00000000..81763073 --- /dev/null +++ b/utils/machines.py @@ -0,0 +1,26 @@ +#!/usr/bin/python + +# Copyright 2015 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Utilities relating to machine-specific functions.""" + +from utils import command_executer + + +def MachineIsPingable(machine, logging_level='average'): + """Checks to see if a machine is responding to 'ping'. + + Args: + machine: String containing the name or ip address of the machine to check. + logging_level: The logging level with which to initialize the + command_executer (from command_executor.LOG_LEVEL enum list). + + Returns: + Boolean indicating whether machine is responding to ping or not. + """ + ce = command_executer.GetCommandExecuter(log_level=logging_level) + cmd = 'ping -c 1 -w 3 %s' % machine + status = ce.RunCommand(cmd) + return status == 0 diff --git a/utils/misc.py b/utils/misc.py index 6ebef65f..b2af8fed 100644 --- a/utils/misc.py +++ b/utils/misc.py @@ -16,8 +16,6 @@ import sys import time import traceback -import lock_machine - import command_executer import logger @@ -300,43 +298,6 @@ def GetAllImages(chromeos_root, board): return out.splitlines() -def AcquireLock(lock_file, timeout=1200): - """Acquire a lock with timeout.""" - start_time = time.time() - locked = False - abs_path = os.path.abspath(lock_file) - dir_path = os.path.dirname(abs_path) - sleep_time = min(10, timeout/10.0) - reason = "pid: {0}, commad: {1}".format(os.getpid(), - sys.argv[0]) - if not os.path.exists(dir_path): - try: - with lock_machine.FileCreationMask(0002): - os.makedirs(dir_path) - except OSError: - print "Cannot create dir {0}, exiting...".format(dir_path) - exit(0) - while True: - locked = (lock_machine.Lock(lock_file).NonBlockingLock(True, reason)) - if locked: - break - time.sleep(sleep_time) - if time.time() - start_time > timeout: - logger.GetLogger().LogWarning( - "Could not acquire lock on this file: {0} within {1} seconds." - "Manually remove the file if you think the lock is stale" - .format(abs_path, timeout)) - break - return locked - - -def ReleaseLock(lock_file): - lock_file = os.path.abspath(lock_file) - ret = lock_machine.Lock(lock_file).Unlock(True) - assert ret, ("Could not unlock {0}," - "Please remove it manually".format(lock_file)) - - def IsFloat(text): if text is None: return False @@ -602,4 +563,3 @@ def BooleanPrompt(prompt='Do you want to continue?', default=True, # common prefix between the two... elif false_value.startswith(response): return False - |