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

Side by Side Diff: infra/services/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: Address agable's comments. 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
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 # pragma: no cover
8
9 import argparse
10 import json
11 import logging
12 import operator
13 import os
14 import socket
15 import subprocess
16 import sys
17
18 from infra.libs import logs
19 from infra.libs.service_utils import daemon
20 from infra.services.master_lifecycle import buildbot_state
21 from infra.services.master_manager_launcher import desired_state_parser
22
23
24 SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__))
25 RUNPY = os.path.abspath(os.path.join(
26 SCRIPT_DIR, os.pardir, os.pardir, os.pardir, 'run.py'))
27
28
29 def parse_args():
30 parser = argparse.ArgumentParser(
31 description='Launches master_manager for every master on a host. NOTE: '
32 'does not perform any action unless --prod is set.')
33 parser.add_argument('build_dir', nargs='?',
34 help='location of the tools/build directory')
35 parser.add_argument('--hostname',
36 default=socket.getfqdn(),
37 help='override local hostname (currently %(default)s)')
38 parser.add_argument('--json-location',
39 default='desired_master_state.json',
40 help='desired master state configuration (default: %(default)s)')
41 parser.add_argument('--command-timeout',
42 help='apply a timeout in seconds to each master_manager process')
43 parser.add_argument('--verify', action='store_true',
44 help='verify the desired master state JSON is valid, then exit')
45 parser.add_argument('--prod', action='store_true',
46 help='actually perform actions instead of doing a dry run')
47 logs.add_argparse_options(parser)
48
49 args = parser.parse_args()
50 logs.process_argparse_options(args)
51
52 if not args.verify:
53 if not args.build_dir:
54 parser.error('A build/ directory must be specified.')
55
56 return args
57
58
59 def synthesize_master_manager_cmd(master_dict, hostname, prod=False):
60 """Find the current desired state and synthesize a command for the master."""
61 state = desired_state_parser.get_master_state(master_dict['states'])
62 cmd = [
63 RUNPY,
64 'infra.tools.master_manager',
65 master_dict['fulldir'],
66 str(state['desired_state']),
67 str(state['transition_time_utc']),
68 '--hostname', hostname,
69 '--enable-gclient-sync',
70 '--verbose',
71 ]
72
73 if prod:
74 cmd.append('--prod')
75
76 return cmd
77
78
79 def log_triggered_ignored(triggered, ignored, hostname):
80 """Outputs for humans which masters will be managed and which won't."""
81 if ignored:
82 logging.info(
83 '%d masters on host %s left unmanaged (no desired state section):\n%s',
84 len(ignored), hostname, '\n'.join(ignored))
85
86 triggered_master_string = '.'
87 if triggered:
88 triggered_master_string = ':\n'
89 triggered_master_string += '\n'.join(m['dirname'] for m in triggered)
90 logging.info(
91 '%d masters managed for host %s%s',
92 len(triggered), hostname, triggered_master_string)
93
94
95 def main():
96 args = parse_args()
97
98 desired_state = desired_state_parser.load_desired_state_file(
99 args.json_location)
100
101 if args.verify:
102 return 0 # File checks out, no need to continue.
103
104 triggered, ignored = desired_state_parser.get_masters_for_host(
105 desired_state, args.build_dir, args.hostname)
106 log_triggered_ignored(triggered, ignored, args.hostname)
107
108 commands = [
109 synthesize_master_manager_cmd(m, args.hostname, prod=args.prod)
110 for m in triggered
111 ]
112
113 if args.command_timeout:
114 commands = [daemon.add_timeout(c, args.command_timeout) for c in commands]
115
116 for command in commands:
117 logging.info('running %s' % command)
118 subprocess.call(command)
119
120
121 if __name__ == '__main__':
122 sys.exit(main())
OLDNEW
« no previous file with comments | « infra/services/master_manager_launcher/__init__.py ('k') | infra/services/master_manager_launcher/desired_state_parser.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698