Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2 |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 4 | """Script to lock/unlock machines.""" |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 5 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 8 | __author__ = 'asharif@google.com (Ahmad Sharif)' |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 9 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 10 | import argparse |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 11 | import datetime |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 12 | import fcntl |
asharif | da8e93b | 2013-02-15 22:49:27 +0000 | [diff] [blame] | 13 | import getpass |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 14 | import glob |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 15 | import json |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 16 | import os |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 17 | import socket |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 18 | import sys |
| 19 | import time |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 20 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 21 | from cros_utils import logger |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 22 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 23 | LOCK_SUFFIX = '_check_lock_liveness' |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 24 | |
Caroline Tice | 6a00af9 | 2015-10-14 11:24:09 -0700 | [diff] [blame] | 25 | # The locks file directory REQUIRES that 'group' only has read/write |
| 26 | # privileges and 'world' has no privileges. So the mask must be |
| 27 | # '0027': 0777 - 0027 = 0750. |
| 28 | LOCK_MASK = 0027 |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 29 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 30 | |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 31 | def FileCheckName(name): |
| 32 | return name + LOCK_SUFFIX |
| 33 | |
| 34 | |
| 35 | def OpenLiveCheck(file_name): |
Caroline Tice | 6a00af9 | 2015-10-14 11:24:09 -0700 | [diff] [blame] | 36 | with FileCreationMask(LOCK_MASK): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 37 | fd = open(file_name, 'a') |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 38 | try: |
| 39 | fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 40 | except IOError: |
| 41 | raise |
| 42 | return fd |
| 43 | |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 44 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 45 | class FileCreationMask(object): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 46 | """Class for the file creation mask.""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 47 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 48 | def __init__(self, mask): |
| 49 | self._mask = mask |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 50 | self._old_mask = None |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 51 | |
| 52 | def __enter__(self): |
| 53 | self._old_mask = os.umask(self._mask) |
| 54 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 55 | def __exit__(self, typ, value, traceback): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 56 | os.umask(self._old_mask) |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 57 | |
| 58 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 59 | class LockDescription(object): |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 60 | """The description of the lock.""" |
| 61 | |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 62 | def __init__(self, desc=None): |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 63 | try: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 64 | self.owner = desc['owner'] |
| 65 | self.exclusive = desc['exclusive'] |
| 66 | self.counter = desc['counter'] |
| 67 | self.time = desc['time'] |
| 68 | self.reason = desc['reason'] |
| 69 | self.auto = desc['auto'] |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 70 | except (KeyError, TypeError): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 71 | self.owner = '' |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 72 | self.exclusive = False |
| 73 | self.counter = 0 |
| 74 | self.time = 0 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 75 | self.reason = '' |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 76 | self.auto = False |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 77 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 78 | def IsLocked(self): |
| 79 | return self.counter or self.exclusive |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 80 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 81 | def __str__(self): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 82 | return ' '.join(['Owner: %s' % self.owner, 'Exclusive: %s' % self.exclusive, |
| 83 | 'Counter: %s' % self.counter, 'Time: %s' % self.time, |
| 84 | 'Reason: %s' % self.reason, 'Auto: %s' % self.auto]) |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 85 | |
| 86 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 87 | class FileLock(object): |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 88 | """File lock operation class.""" |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 89 | FILE_OPS = [] |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 90 | |
| 91 | def __init__(self, lock_filename): |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 92 | self._filepath = lock_filename |
| 93 | lock_dir = os.path.dirname(lock_filename) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 94 | assert os.path.isdir(lock_dir), ("Locks dir: %s doesn't exist!" % lock_dir) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 95 | self._file = None |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 96 | self._description = None |
| 97 | |
| 98 | def getDescription(self): |
| 99 | return self._description |
| 100 | |
| 101 | def getFilePath(self): |
| 102 | return self._filepath |
| 103 | |
| 104 | def setDescription(self, desc): |
| 105 | self._description = desc |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 106 | |
| 107 | @classmethod |
| 108 | def AsString(cls, file_locks): |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 109 | stringify_fmt = '%-30s %-15s %-4s %-4s %-15s %-40s %-4s' |
| 110 | header = stringify_fmt % ('machine', 'owner', 'excl', 'ctr', 'elapsed', |
| 111 | 'reason', 'auto') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 112 | lock_strings = [] |
| 113 | for file_lock in file_locks: |
| 114 | |
| 115 | elapsed_time = datetime.timedelta( |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 116 | seconds=int(time.time() - file_lock.getDescription().time)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 117 | elapsed_time = '%s ago' % elapsed_time |
| 118 | lock_strings.append( |
| 119 | stringify_fmt % |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 120 | (os.path.basename(file_lock.getFilePath), |
| 121 | file_lock.getDescription().owner, |
| 122 | file_lock.getDescription().exclusive, |
| 123 | file_lock.getDescription().counter, |
| 124 | elapsed_time, file_lock.getDescription().reason, |
| 125 | file_lock.getDescription().auto)) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 126 | table = '\n'.join(lock_strings) |
| 127 | return '\n'.join([header, table]) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 128 | |
| 129 | @classmethod |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 130 | def ListLock(cls, pattern, locks_dir): |
| 131 | if not locks_dir: |
| 132 | locks_dir = Machine.LOCKS_DIR |
| 133 | full_pattern = os.path.join(locks_dir, pattern) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 134 | file_locks = [] |
| 135 | for lock_filename in glob.glob(full_pattern): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 136 | if LOCK_SUFFIX in lock_filename: |
| 137 | continue |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 138 | file_lock = FileLock(lock_filename) |
| 139 | with file_lock as lock: |
| 140 | if lock.IsLocked(): |
| 141 | file_locks.append(file_lock) |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 142 | logger.GetLogger().LogOutput('\n%s' % cls.AsString(file_locks)) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 143 | |
| 144 | def __enter__(self): |
Caroline Tice | 6a00af9 | 2015-10-14 11:24:09 -0700 | [diff] [blame] | 145 | with FileCreationMask(LOCK_MASK): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 146 | try: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 147 | self._file = open(self._filepath, 'a+') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 148 | self._file.seek(0, os.SEEK_SET) |
| 149 | |
| 150 | if fcntl.flock(self._file.fileno(), fcntl.LOCK_EX) == -1: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 151 | raise IOError('flock(%s, LOCK_EX) failed!' % self._filepath) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 152 | |
| 153 | try: |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 154 | desc = json.load(self._file) |
| 155 | except (EOFError, ValueError): |
| 156 | desc = None |
| 157 | self._description = LockDescription(desc) |
| 158 | |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 159 | if self._description.exclusive and self._description.auto: |
| 160 | locked_byself = False |
| 161 | for fd in self.FILE_OPS: |
| 162 | if fd.name == FileCheckName(self._filepath): |
| 163 | locked_byself = True |
| 164 | break |
| 165 | if not locked_byself: |
| 166 | try: |
| 167 | fp = OpenLiveCheck(FileCheckName(self._filepath)) |
| 168 | except IOError: |
| 169 | pass |
| 170 | else: |
| 171 | self._description = LockDescription() |
| 172 | fcntl.lockf(fp, fcntl.LOCK_UN) |
| 173 | fp.close() |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 174 | return self._description |
| 175 | # Check this differently? |
| 176 | except IOError as ex: |
| 177 | logger.GetLogger().LogError(ex) |
| 178 | return None |
| 179 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 180 | def __exit__(self, typ, value, traceback): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 181 | self._file.truncate(0) |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 182 | self._file.write(json.dumps(self._description.__dict__, skipkeys=True)) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 183 | self._file.close() |
| 184 | |
| 185 | def __str__(self): |
| 186 | return self.AsString([self]) |
| 187 | |
| 188 | |
| 189 | class Lock(object): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 190 | """Lock class""" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 191 | |
yunlian | 77dc571 | 2013-02-19 21:13:33 +0000 | [diff] [blame] | 192 | def __init__(self, lock_file, auto=True): |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 193 | self._to_lock = os.path.basename(lock_file) |
| 194 | self._lock_file = lock_file |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 195 | self._logger = logger.GetLogger() |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 196 | self._auto = auto |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 197 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 198 | def NonBlockingLock(self, exclusive, reason=''): |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 199 | with FileLock(self._lock_file) as lock: |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 200 | if lock.exclusive: |
| 201 | self._logger.LogError( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 202 | 'Exclusive lock already acquired by %s. Reason: %s' % |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 203 | (lock.owner, lock.reason)) |
| 204 | return False |
| 205 | |
| 206 | if exclusive: |
| 207 | if lock.counter: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 208 | self._logger.LogError('Shared lock already acquired') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 209 | return False |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 210 | lock_file_check = FileCheckName(self._lock_file) |
| 211 | fd = OpenLiveCheck(lock_file_check) |
| 212 | FileLock.FILE_OPS.append(fd) |
| 213 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 214 | lock.exclusive = True |
| 215 | lock.reason = reason |
asharif | da8e93b | 2013-02-15 22:49:27 +0000 | [diff] [blame] | 216 | lock.owner = getpass.getuser() |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 217 | lock.time = time.time() |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 218 | lock.auto = self._auto |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 219 | else: |
| 220 | lock.counter += 1 |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 221 | self._logger.LogOutput('Successfully locked: %s' % self._to_lock) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 222 | return True |
| 223 | |
| 224 | def Unlock(self, exclusive, force=False): |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 225 | with FileLock(self._lock_file) as lock: |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 226 | if not lock.IsLocked(): |
Caroline Tice | 570b2ce | 2013-08-07 12:46:30 -0700 | [diff] [blame] | 227 | self._logger.LogWarning("Can't unlock unlocked machine!") |
| 228 | return True |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 229 | |
| 230 | if lock.exclusive != exclusive: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 231 | self._logger.LogError('shared locks must be unlocked with --shared') |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 232 | return False |
| 233 | |
| 234 | if lock.exclusive: |
asharif | da8e93b | 2013-02-15 22:49:27 +0000 | [diff] [blame] | 235 | if lock.owner != getpass.getuser() and not force: |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 236 | self._logger.LogError("%s can't unlock lock owned by: %s" % |
asharif | da8e93b | 2013-02-15 22:49:27 +0000 | [diff] [blame] | 237 | (getpass.getuser(), lock.owner)) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 238 | return False |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 239 | if lock.auto != self._auto: |
| 240 | self._logger.LogError("Can't unlock lock with different -a" |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 241 | ' parameter.') |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 242 | return False |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 243 | lock.exclusive = False |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 244 | lock.reason = '' |
| 245 | lock.owner = '' |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 246 | |
| 247 | if self._auto: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 248 | del_list = [i |
| 249 | for i in FileLock.FILE_OPS |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 250 | if i.name == FileCheckName(self._lock_file)] |
| 251 | for i in del_list: |
| 252 | FileLock.FILE_OPS.remove(i) |
| 253 | for f in del_list: |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 254 | fcntl.lockf(f, fcntl.LOCK_UN) |
| 255 | f.close() |
yunlian | b0eaee8 | 2013-02-19 21:13:28 +0000 | [diff] [blame] | 256 | del del_list |
| 257 | os.remove(FileCheckName(self._lock_file)) |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 258 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 259 | else: |
| 260 | lock.counter -= 1 |
| 261 | return True |
| 262 | |
| 263 | |
| 264 | class Machine(object): |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 265 | """Machine class""" |
| 266 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 267 | LOCKS_DIR = '/google/data/rw/users/mo/mobiletc-prebuild/locks' |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 268 | |
yunlian | 77dc571 | 2013-02-19 21:13:33 +0000 | [diff] [blame] | 269 | def __init__(self, name, locks_dir=LOCKS_DIR, auto=True): |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 270 | self._name = name |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 271 | self._auto = auto |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 272 | try: |
| 273 | self._full_name = socket.gethostbyaddr(name)[0] |
| 274 | except socket.error: |
| 275 | self._full_name = self._name |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 276 | self._full_name = os.path.join(locks_dir, self._full_name) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 277 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 278 | def Lock(self, exclusive=False, reason=''): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 279 | lock = Lock(self._full_name, self._auto) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 280 | return lock.NonBlockingLock(exclusive, reason) |
| 281 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 282 | def TryLock(self, timeout=300, exclusive=False, reason=''): |
shenhan | e0a6d8a | 2013-02-19 20:42:29 +0000 | [diff] [blame] | 283 | locked = False |
yunlian | 8a3bdcb | 2013-02-19 20:42:48 +0000 | [diff] [blame] | 284 | sleep = timeout / 10 |
shenhan | e0a6d8a | 2013-02-19 20:42:29 +0000 | [diff] [blame] | 285 | while True: |
| 286 | locked = self.Lock(exclusive, reason) |
| 287 | if locked or not timeout >= 0: |
| 288 | break |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 289 | print('Lock not acquired for {0}, wait {1} seconds ...'.format( |
| 290 | self._name, |
| 291 | sleep)) |
shenhan | e0a6d8a | 2013-02-19 20:42:29 +0000 | [diff] [blame] | 292 | time.sleep(sleep) |
| 293 | timeout -= sleep |
| 294 | return locked |
| 295 | |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 296 | def Unlock(self, exclusive=False, ignore_ownership=False): |
yunlian | 8c9419b | 2013-02-19 21:11:29 +0000 | [diff] [blame] | 297 | lock = Lock(self._full_name, self._auto) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 298 | return lock.Unlock(exclusive, ignore_ownership) |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 299 | |
| 300 | |
| 301 | def Main(argv): |
| 302 | """The main function.""" |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 303 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 304 | parser = argparse.ArgumentParser() |
| 305 | parser.add_argument('-r', |
| 306 | '--reason', |
| 307 | dest='reason', |
| 308 | default='', |
| 309 | help='The lock reason.') |
| 310 | parser.add_argument('-u', |
| 311 | '--unlock', |
| 312 | dest='unlock', |
| 313 | action='store_true', |
| 314 | default=False, |
| 315 | help='Use this to unlock.') |
| 316 | parser.add_argument('-l', |
| 317 | '--list_locks', |
| 318 | dest='list_locks', |
| 319 | action='store_true', |
| 320 | default=False, |
| 321 | help='Use this to list locks.') |
| 322 | parser.add_argument('-f', |
| 323 | '--ignore_ownership', |
| 324 | dest='ignore_ownership', |
| 325 | action='store_true', |
| 326 | default=False, |
| 327 | help="Use this to force unlock on a lock you don't own.") |
| 328 | parser.add_argument('-s', |
| 329 | '--shared', |
| 330 | dest='shared', |
| 331 | action='store_true', |
| 332 | default=False, |
| 333 | help='Use this for a shared (non-exclusive) lock.') |
| 334 | parser.add_argument('-d', |
| 335 | '--dir', |
| 336 | dest='locks_dir', |
| 337 | action='store', |
| 338 | default=Machine.LOCKS_DIR, |
| 339 | help='Use this to set different locks_dir') |
| 340 | parser.add_argument('args', nargs='*', help='Machine arg.') |
| 341 | |
| 342 | options = parser.parse_args(argv) |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 343 | |
yunlian | 52a6c57 | 2013-02-19 20:42:07 +0000 | [diff] [blame] | 344 | options.locks_dir = os.path.abspath(options.locks_dir) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 345 | exclusive = not options.shared |
| 346 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 347 | if not options.list_locks and len(options.args) != 2: |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 348 | logger.GetLogger().LogError( |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 349 | 'Either --list_locks or a machine arg is needed.') |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 350 | return 1 |
| 351 | |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 352 | if len(options.args) > 1: |
| 353 | machine = Machine(options.args[1], options.locks_dir, auto=False) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 354 | else: |
| 355 | machine = None |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 356 | |
| 357 | if options.list_locks: |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 358 | FileLock.ListLock('*', options.locks_dir) |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 359 | retval = True |
| 360 | elif options.unlock: |
| 361 | retval = machine.Unlock(exclusive, options.ignore_ownership) |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 362 | else: |
asharif | 455157b | 2013-02-15 21:15:05 +0000 | [diff] [blame] | 363 | retval = machine.Lock(exclusive, options.reason) |
| 364 | |
| 365 | if retval: |
| 366 | return 0 |
| 367 | else: |
| 368 | return 1 |
asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame] | 369 | |
Luis Lozano | f2a3ef4 | 2015-12-15 13:49:30 -0800 | [diff] [blame] | 370 | |
| 371 | if __name__ == '__main__': |
Caroline Tice | 88272d4 | 2016-01-13 09:48:29 -0800 | [diff] [blame] | 372 | sys.exit(Main(sys.argv[1:])) |