summaryrefslogtreecommitdiff
path: root/google-daemon/usr/share/google/google_daemon/manage_accounts.py
blob: b17cdd7455b125e22d2784d8613a6f92ca2f58bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Main driver logic for managing accounts on GCE instances."""

import logging
import optparse
import os
import sys


def FixPath():
  parent_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
  if os.path.isdir(parent_dir):
    sys.path.append(parent_dir)


FixPath()


from accounts import Accounts
from accounts_manager import AccountsManager
from accounts_manager_daemon import AccountsManagerDaemon
from desired_accounts import DesiredAccounts
from utils import LockFile
from utils import System


def Main(accounts, desired_accounts, system, logger,
         log_handler, lock_file, lock_fname=None, interval=-1,
         daemon_mode=False):
  if not log_handler:
    log_handler = system.MakeLoggingHandler(
        'accounts-from-metadata', logging.handlers.SysLogHandler.LOG_AUTH)
  system.SetLoggingHandler(logger, log_handler)

  accounts_manager = AccountsManager(
      accounts, desired_accounts, system, lock_file, lock_fname, interval)

  if not daemon_mode:
    accounts_manager.Main()
  else:
    manager_daemon = AccountsManagerDaemon(None, accounts_manager)
    manager_daemon.StartDaemon()


if __name__ == '__main__':
  parser = optparse.OptionParser()
  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.set_defaults(interval=60)
  parser.set_defaults(daemon=False)
  (options, args) = parser.parse_args()

  Main(Accounts(system_module=System()), DesiredAccounts(),
       System(), logging.getLogger(), None, LockFile(), None, options.interval,
       options.daemon)