blob: e2f54a62d7dcf180e13719600242f29982900b38 [file] [log] [blame]
# Copyright (c) 2012 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.
import logging
import signal
import common
from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
"""HostLockManager class, for the dynamic_suite module.
A HostLockManager instance manages locking and unlocking a set of autotest DUTs.
A caller can lock or unlock one or more DUTs. If the caller fails to unlock()
locked hosts before the instance is destroyed, it will attempt to unlock() the
hosts automatically, but this is to be avoided.
Sample usage:
manager = host_lock_manager.HostLockManager()
# do things
class HostLockManager(object):
@attribute _afe: an instance of AFE as defined in server/
@attribute _locked_hosts: a set of DUT hostnames.
@attribute LOCK: a string.
@attribute UNLOCK: a string.
LOCK = 'lock'
UNLOCK = 'unlock'
def locked_hosts(self):
"""@returns set of locked hosts."""
return self._locked_hosts
def locked_hosts(self, hosts):
"""Sets value of locked_hosts.
@param hosts: a set of strings.
self._locked_hosts = hosts
def __init__(self, afe=None):
@param afe: an instance of AFE as defined in server/
self._afe = afe or frontend_wrappers.RetryingAFE(timeout_min=30,
# Keep track of hosts locked by this instance.
self._locked_hosts = set()
def __del__(self):
if self._locked_hosts:
logging.warning('Caller failed to unlock %r! Forcing unlock now.',
def _check_host(self, host, operation):
"""Checks host for desired operation.
@param host: a string, hostname.
@param operation: a string, LOCK or UNLOCK.
@returns a string: host name, if desired operation can be performed on
host or None otherwise.
mod_host = host.split('.')[0]
host_info = self._afe.get_hosts(hostname=mod_host)
if not host_info:
logging.warning('Skip unknown host %s.', host)
return None
host_info = host_info[0]
if operation == self.LOCK and host_info.locked:
err = ('Contention detected: %s is locked by %s at %s.' %
(mod_host, host_info.locked_by, host_info.lock_time))
return None
elif operation == self.UNLOCK and not host_info.locked:'%s not locked.', mod_host)
return None
return mod_host
def lock(self, hosts):
"""Attempt to lock hosts in AFE.
@param hosts: a list of strings, host names.
@returns a boolean, True == at least one host from hosts is locked.
# Filter out hosts that we may have already locked
new_hosts = set(hosts).difference(self._locked_hosts)'Attempt to lock %s', new_hosts)
if not new_hosts:
return False
return self._host_modifier(new_hosts, self.LOCK)
def unlock(self, hosts=None):
"""Unlock hosts in AFE.
@param hosts: a list of strings, host names.
@returns a boolean, True == at least one host from self._locked_hosts is
# Filter out hosts that we did not lock
updated_hosts = self._locked_hosts
if hosts:
unknown_hosts = set(hosts).difference(self._locked_hosts)
logging.warning('Skip unknown hosts: %s', unknown_hosts)
updated_hosts = set(hosts) - unknown_hosts'Valid hosts: %s', updated_hosts)
updated_hosts = updated_hosts.intersection(self._locked_hosts)
if not updated_hosts:
return False'Unlocking hosts: %s', updated_hosts)
return self._host_modifier(updated_hosts, self.UNLOCK)
def _host_modifier(self, hosts, operation):
"""Helper that runs the modify_hosts() RPC with specified args.
@param: hosts, a set of strings, host names.
@param operation: a string, LOCK or UNLOCK.
@returns a boolean, if operation succeeded on at least one host in
updated_hosts = set()
for host in hosts:
mod_host = self._check_host(host, operation)
if mod_host is not None:
updated_hosts.add(mod_host)'host_modifier: updated_hosts = %s', updated_hosts)
if not updated_hosts:'host_modifier: no host to update')
return False
kwargs = {'locked': True if operation == self.LOCK else False}'modify_hosts',
host_filter_data={'hostname__in': list(updated_hosts)},
if operation == self.LOCK:
self._locked_hosts = self._locked_hosts.union(updated_hosts)
elif operation == self.UNLOCK:
self._locked_hosts = self._locked_hosts.difference(updated_hosts)
return True
class HostsLockedBy(object):
"""Context manager to make sure that a HostLockManager will always unlock
its machines. This protects against both exceptions and SIGTERM."""
def _make_handler(self):
def _chaining_signal_handler(signal_number, frame):
# self._old_handler can also be signal.SIG_{IGN,DFL} which are ints.
if callable(self._old_handler):
self._old_handler(signal_number, frame)
return _chaining_signal_handler
def __init__(self, manager):
@param manager: The HostLockManager used to lock the hosts.
self._manager = manager
self._old_handler = signal.SIG_DFL
def __enter__(self):
self._old_handler = signal.signal(signal.SIGTERM, self._make_handler())
def __exit__(self, exntype, exnvalue, backtrace):
signal.signal(signal.SIGTERM, self._old_handler)