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