Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: infra/tools/master_manager_launcher/__main__.py

Issue 1128783003: Add master_manager_launch script which launches master_manager scripts for each master on a host. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Add coverage. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/python
agable 2015/05/06 23:29:30 infra/tools is meant to hold things for people to
ghost stip (do not use) 2015/05/07 07:07:21 Done.
2 # Copyright 2015 Google Inc. All Rights Reserved.
3 # pylint: disable=F0401
4
5 """Launch a master_manager script for every master on a host."""
6
7 import argparse
8 import json
9 import logging
10 import operator
11 import os
12 import socket
13 import subprocess
14 import sys
15
16 from infra.libs import logs
17 from infra.libs.service_utils import daemon
18 from infra.services.master_lifecycle import buildbot_state
19 from infra.tools.master_manager_launcher import desired_state_parser
20
21
22 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
23 RUNPY = os.path.abspath(os.path.join(
24 SCRIPT_DIR, os.pardir, os.pardir, os.pardir, 'run.py'))
25
26
27 def parse_args(): # pragma: no cover
28 parser = argparse.ArgumentParser(
29 description='Launches master_manager for every master on a host. NOTE: '
30 'does not perform any action unless --prod is set.')
31 parser.add_argument('build_dir', nargs='?',
32 help='location of the tools/build directory')
33 parser.add_argument('--hostname',
34 default=socket.getfqdn(),
35 help='override local hostname (currently %(default)s)')
36 parser.add_argument('--json-location',
37 default=os.path.join(SCRIPT_DIR, 'desired_master_state.json'),
agable 2015/05/06 23:29:30 Seems like an odd default. Why would the json be i
ghost stip (do not use) 2015/05/07 07:07:21 made it just the current dir.
38 help='desired master state configuration (default: %(default)s)')
39 parser.add_argument('--command-timeout',
40 help='apply a timeout in seconds to each master_manager process')
41 parser.add_argument('--verify', action='store_true',
42 help='verify the desired master state JSON is valid, then exit')
43 parser.add_argument('--prod', action='store_true',
44 help='actually perform actions instead of doing a dry run')
45 logs.add_argparse_options(parser)
46
47 args = parser.parse_args()
48 logs.process_argparse_options(args)
49
50 if not args.verify:
51 if not args.build_dir:
52 parser.error('A build/ directory must be specified.')
53
54 return args
55
56
57 def synthesize_master_manager_cli(master_dict, hostname, prod=False):
agable 2015/05/06 23:29:30 It's not a CLI, it's a command.
ghost stip (do not use) 2015/05/07 07:07:21 Done.
58 """Find the current desired state and synthesize a command for the master."""
59 state = desired_state_parser.get_master_state(master_dict['states'])
60 cli = [
61 RUNPY,
62 'infra.tools.master_manager',
63 master_dict['fulldir'],
64 str(state['desired_state']),
65 str(state['transition_time_utc']),
66 '--hostname', hostname,
67 '--enable-gclient-sync',
68 '--verbose',
69 ]
70
71 if prod:
72 cli.append('--prod')
73
74 return cli
75
76
77 def log_triggered_ignored(triggered, ignored, hostname, logger):
agable 2015/05/06 23:29:30 When/how is this covered?
ghost stip (do not use) 2015/05/07 06:50:57 actually, the whole file doesn't have a test (sinc
agable 2015/05/07 17:59:31 Nope, there's not a better way to show "the whole
78 """Outputs for humans which masters will be managed and which won't."""
79 if ignored:
80 logger.info(
81 '%d masters on host %s left unmanaged (no desired state section):\n%s',
82 len(ignored), hostname, '\n'.join(ignored))
83
84 triggered_master_string = '.'
85 if triggered:
86 triggered_master_string = ':\n'
87 triggered_master_string += '\n'.join(m['dirname'] for m in triggered)
88 logger.info(
89 '%d masters managed for host %s%s',
90 len(triggered), hostname, triggered_master_string)
91
92
93 def main(): # pragma: no cover
94 args = parse_args()
95
96 desired_state = desired_state_parser.load_desired_state_file(
97 args.json_location)
98
99 logger = logging.getLogger(__name__)
agable 2015/05/06 23:29:30 This shouldn't be necessary for the top-level __ma
iannucci 2015/05/06 23:48:05 Additionally, logger is actually one of the (very
ghost stip (do not use) 2015/05/07 06:50:57 Done.
100
101 if desired_state is None:
agable 2015/05/06 23:29:30 Why is not having a desired state an error? The lo
ghost stip (do not use) 2015/05/07 06:50:57 there's a difference between 'I've haven't written
102 return 1
103 if args.verify:
104 return 0 # File checks out, no need to continue.
105
106 triggered, ignored = desired_state_parser.get_masters_for_host(
107 desired_state, args.build_dir, args.hostname)
108 log_triggered_ignored(triggered, ignored, args.hostname, logger)
109
110 commands = [
111 synthesize_master_manager_cli(m, args.hostname, prod=args.prod)
112 for m in triggered]
agable 2015/05/06 23:29:30 nit: outdent closing brace on next line (like you
ghost stip (do not use) 2015/05/07 07:07:21 Done.
113
114 if args.command_timeout:
115 commands = [daemon.add_timeout(c, args.command_timeout) for c in commands]
116
117 for command in commands:
118 logger.info('running %s' % command)
119 subprocess.call(command)
agable 2015/05/06 23:29:30 Yeah we'll definitely need parallelism here.
ghost stip (do not use) 2015/05/07 06:50:57 Acknowledged.
120
121
122 if __name__ == '__main__': # pragma: no cover
123 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698