diff options
author | Jeremy Edwards <jeremyedwards@google.com> | 2013-11-02 13:31:57 -0700 |
---|---|---|
committer | Jeremy Edwards <jeremyedwards@google.com> | 2013-11-02 13:31:57 -0700 |
commit | 7a875ce4c9d6363de6134390e4e1f3be8de9f882 (patch) | |
tree | f4d7d84dda2c2c7a269c20863949b43b7a64f4a7 | |
parent | d058722b80b2f9d0cfd8abfc9d3c029b253c3d2f (diff) | |
download | google-compute-image-packages-7a875ce4c9d6363de6134390e4e1f3be8de9f882.tar.gz |
Debug logging support for Google Daemon Accounts Manager. Ignore rules for python.
6 files changed, 36 insertions, 6 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..66b6e4c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +*.pyo +.DS_Store diff --git a/google-daemon/usr/share/google/google_daemon/accounts_manager.py b/google-daemon/usr/share/google/google_daemon/accounts_manager.py index 0386de9..5b31d16 100644 --- a/google-daemon/usr/share/google/google_daemon/accounts_manager.py +++ b/google-daemon/usr/share/google/google_daemon/accounts_manager.py @@ -14,6 +14,7 @@ """Main driver logic for managing accounts on GCE instances.""" +import logging import os import time @@ -36,6 +37,7 @@ class AccountsManager(object): self.interval = interval def Main(self): + logging.debug('AccountsManager main loop') # Run this once per interval forever. while True: # If this is a one-shot execution, then this can be run normally. @@ -63,6 +65,7 @@ class AccountsManager(object): def RegenerateKeysAndCreateAccounts(self): """Regenerate the keys and create accounts as needed.""" + logging.debug('RegenerateKeysAndCreateAccounts') if self.system.IsExecutable('/usr/share/google/first-boot'): self.system.RunCommand('/usr/share/google/first-boot') diff --git a/google-daemon/usr/share/google/google_daemon/accounts_manager_daemon.py b/google-daemon/usr/share/google/google_daemon/accounts_manager_daemon.py index 03a063a..d489112 100755 --- a/google-daemon/usr/share/google/google_daemon/accounts_manager_daemon.py +++ b/google-daemon/usr/share/google/google_daemon/accounts_manager_daemon.py @@ -16,6 +16,7 @@ """Tool for running account manager as a daemon.""" import fcntl +import logging import os import signal @@ -26,6 +27,7 @@ class AccountsManagerDaemon(object): """Creates a daemon process to run the accounts manager in.""" def __init__(self, pidfile, accounts_manager, fcntl_module=fcntl): + logging.debug('Initializing Daemon Module') if not pidfile: pidfile = PIDFILE @@ -43,6 +45,7 @@ class AccountsManagerDaemon(object): # This is a very simplified (with significantly reduced features) version of # the python-daemon library at https://pypi.python.org/pypi/python-daemon/. pid = os.fork() + logging.debug('Forked new process, pid= {0}'.format(pid)) if pid == 0: os.setsid() pid = os.fork() @@ -64,6 +67,7 @@ class AccountsManagerDaemon(object): pidf.write(str(os.getpid())) pidf.close() + logging.debug('Sending signal SIGTERM to shutdown daemon') signal.signal(signal.SIGTERM, self.ShutdownDaemon) self.accounts_manager.Main() @@ -74,9 +78,11 @@ class AccountsManagerDaemon(object): # that the lock can only be grabbed once the accounts manager is done with # it and holding it guarantees that the accounts manager won't start up # again while shutting down. + logging.debug('Acquiring Daemon lock.') lockfile = open(self.accounts_manager.lock_fname, 'r') self.fcntl_module.flock(lockfile.fileno(), fcntl.LOCK_EX) + logging.debug('Shutting down Daemon module.') # Clean up pidfile and terminate. Lock will be released with termination. os.remove(self.pidfile) exception = SystemExit('Terminating on signal number %d' % signal_number) diff --git a/google-daemon/usr/share/google/google_daemon/desired_accounts.py b/google-daemon/usr/share/google/google_daemon/desired_accounts.py index 2d8f93b..3c942b8 100755 --- a/google-daemon/usr/share/google/google_daemon/desired_accounts.py +++ b/google-daemon/usr/share/google/google_daemon/desired_accounts.py @@ -44,6 +44,7 @@ def AccountDataToDictionary(data): if not user in usermap: usermap[user] = [] usermap[user].append(key) + logging.debug('User accounts: {0}'.format(usermap)) return usermap @@ -60,10 +61,12 @@ class DesiredAccounts(object): Returns: A dict of the form: {'username': ['sshkey1, 'sshkey2', ...]}. """ + logging.debug('Getting desired accounts from metadata.') attempt_failures = [] start_time = self.time.time() while self.time.time() - start_time < 10: # Try for 10 seconds. try: + logging.debug('Getting SSH Accounts from {0}'.format(ACCOUNTS_URL)) account_data = self.urllib2.urlopen( urllib2.Request(ACCOUNTS_URL)).read() if attempt_failures: diff --git a/google-daemon/usr/share/google/google_daemon/manage_accounts.py b/google-daemon/usr/share/google/google_daemon/manage_accounts.py index b17cdd7..0e3b533 100755 --- a/google-daemon/usr/share/google/google_daemon/manage_accounts.py +++ b/google-daemon/usr/share/google/google_daemon/manage_accounts.py @@ -40,20 +40,25 @@ from utils import System def Main(accounts, desired_accounts, system, logger, log_handler, lock_file, lock_fname=None, interval=-1, - daemon_mode=False): + daemon_mode=False, debug_mode=False): + if not log_handler: log_handler = system.MakeLoggingHandler( 'accounts-from-metadata', logging.handlers.SysLogHandler.LOG_AUTH) system.SetLoggingHandler(logger, log_handler) + + if debug_mode: + system.EnableDebugLogging(logger) + logging.debug('Running in Debug Mode') accounts_manager = AccountsManager( accounts, desired_accounts, system, lock_file, lock_fname, interval) - - if not daemon_mode: - accounts_manager.Main() - else: + + if daemon_mode: manager_daemon = AccountsManagerDaemon(None, accounts_manager) manager_daemon.StartDaemon() + else: + accounts_manager.Main() if __name__ == '__main__': @@ -61,10 +66,12 @@ if __name__ == '__main__': parser.add_option('--daemon', dest='daemon', action='store_true') parser.add_option('--no-daemon', dest='daemon', action='store_false') parser.add_option('--interval', type='int', dest='interval') + parser.add_option('--debug', dest='debug', action='store_true') parser.set_defaults(interval=60) parser.set_defaults(daemon=False) + parser.set_defaults(debug=False) (options, args) = parser.parse_args() Main(Accounts(system_module=System()), DesiredAccounts(), System(), logging.getLogger(), None, LockFile(), None, options.interval, - options.daemon) + options.daemon, options.debug) diff --git a/google-daemon/usr/share/google/google_daemon/utils.py b/google-daemon/usr/share/google/google_daemon/utils.py index b3bc45b..ae14e04 100755 --- a/google-daemon/usr/share/google/google_daemon/utils.py +++ b/google-daemon/usr/share/google/google_daemon/utils.py @@ -22,6 +22,7 @@ import logging.handlers import os import shutil import subprocess +import sys import tempfile class RunCommandException(Exception): @@ -46,9 +47,16 @@ class System(object): def SetLoggingHandler(self, logger, handler): """Setup logging w/ a specifc handler.""" + handler.setLevel(logging.INFO) logger.setLevel(logging.INFO) logger.addHandler(handler) + def EnableDebugLogging(self, logger): + debug_handler = logging.StreamHandler(sys.stdout) + debug_handler.setLevel(logging.DEBUG) + logger.addHandler(debug_handler) + logger.setLevel(logging.DEBUG) + def OpenFile(self, *args, **kwargs): return open(*args, **kwargs) |