asharif | b237bca | 2013-02-15 20:54:57 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python2.6 |
| 2 | # |
| 3 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 4 | |
| 5 | """Script to lock/unlock machines. |
| 6 | |
| 7 | """ |
| 8 | |
| 9 | __author__ = "asharif@google.com (Ahmad Sharif)" |
| 10 | |
| 11 | import datetime |
| 12 | import getpass |
| 13 | import glob |
| 14 | import optparse |
| 15 | import os |
| 16 | import sys |
| 17 | import tc_enter_chroot |
| 18 | import build_chromeos |
| 19 | import setup_chromeos |
| 20 | import socket |
| 21 | from utils import command_executer |
| 22 | from utils import utils |
| 23 | from utils import logger |
| 24 | |
| 25 | |
| 26 | LOCK_DIR = "locks" |
| 27 | LOCK_USERNAME = "mobiletc-prebuild" |
| 28 | REASON_FILE = "reason.txt" |
| 29 | UMASK_COMMAND = "umask a+rwx" |
| 30 | |
| 31 | |
| 32 | # TODO(asharif): Use duration? |
| 33 | def LockMachine(machine, unlock=False, duration=None, reason=None): |
| 34 | ce = command_executer.GetCommandExecuter() |
| 35 | l = logger.GetLogger() |
| 36 | locks_dir = os.path.join("/home", LOCK_USERNAME, LOCK_DIR) |
| 37 | |
| 38 | if not os.path.exists(locks_dir): |
| 39 | l.LogError("Locks dir: %s must exist" % locks_dir) |
| 40 | return 1 |
| 41 | |
| 42 | machine_lock_dir = os.path.join(locks_dir, machine) |
| 43 | |
| 44 | if unlock: |
| 45 | lock_program = "rm -r" |
| 46 | else: |
| 47 | lock_program = "%s && mkdir" % UMASK_COMMAND |
| 48 | command = ("%s %s" % |
| 49 | (lock_program, |
| 50 | machine_lock_dir)) |
| 51 | retval = ce.RunCommand(command) |
| 52 | if retval: return retval |
| 53 | |
| 54 | reason_file = os.path.join(machine_lock_dir, "reason.txt") |
| 55 | if not unlock: |
| 56 | if not reason: |
| 57 | reason = "" |
| 58 | full_reason = ("Locked by: %s on %s: %s" % |
| 59 | (getpass.getuser(), |
| 60 | str(datetime.datetime.now()), |
| 61 | reason)) |
| 62 | command = ("%s && echo \"%s\" > %s" % |
| 63 | (UMASK_COMMAND, |
| 64 | full_reason, |
| 65 | reason_file)) |
| 66 | retval = ce.RunCommand(command) |
| 67 | if retval: return retval |
| 68 | |
| 69 | return 0 |
| 70 | |
| 71 | |
| 72 | def ListLocks(machine=None): |
| 73 | if not machine: |
| 74 | machine = "*" |
| 75 | locks_dir = os.path.join("/home", LOCK_USERNAME, LOCK_DIR) |
| 76 | print "Machine: Reason" |
| 77 | print "---------------" |
| 78 | for current_dir in glob.glob(os.path.join(locks_dir, machine)): |
| 79 | f = open(os.path.join(current_dir, REASON_FILE)) |
| 80 | reason = f.read() |
| 81 | reason = reason.strip() |
| 82 | print "%s: %s" % (os.path.basename(current_dir), reason) |
| 83 | return 0 |
| 84 | |
| 85 | |
| 86 | def Main(argv): |
| 87 | """The main function.""" |
| 88 | # Common initializations |
| 89 | ce = command_executer.GetCommandExecuter() |
| 90 | l = logger.GetLogger() |
| 91 | |
| 92 | parser = optparse.OptionParser() |
| 93 | parser.add_option("-m", |
| 94 | "--machine", |
| 95 | dest="machine", |
| 96 | help="The machine to be locked.") |
| 97 | parser.add_option("-r", |
| 98 | "--reason", |
| 99 | dest="reason", |
| 100 | help="The lock reason.") |
| 101 | parser.add_option("-u", |
| 102 | "--unlock", |
| 103 | dest="unlock", |
| 104 | action="store_true", |
| 105 | default=False, |
| 106 | help="Use this to unlock.") |
| 107 | parser.add_option("-l", |
| 108 | "--list_locks", |
| 109 | dest="list_locks", |
| 110 | action="store_true", |
| 111 | default=False, |
| 112 | help="Use this to list locks.") |
| 113 | |
| 114 | options = parser.parse_args(argv)[0] |
| 115 | |
| 116 | if not options.list_locks and not options.machine: |
| 117 | l.LogError("Either --list_locks or --machine option is needed.") |
| 118 | return 1 |
| 119 | |
| 120 | machine = options.machine |
| 121 | unlock = options.unlock |
| 122 | reason = options.reason |
| 123 | |
| 124 | # Canonicalize machine name |
| 125 | if machine: |
| 126 | machine = socket.gethostbyaddr(machine)[0] |
| 127 | |
| 128 | if options.list_locks: |
| 129 | retval = ListLocks(machine) |
| 130 | else: |
| 131 | retval = LockMachine(machine, unlock=unlock, reason=reason) |
| 132 | return retval |
| 133 | |
| 134 | if __name__ == "__main__": |
| 135 | retval = Main(sys.argv) |
| 136 | sys.exit(retval) |