Show More
@@ -1,273 +1,280 b'' | |||||
1 | # cli.py - Command line interface for automation |
|
1 | # cli.py - Command line interface for automation | |
2 | # |
|
2 | # | |
3 | # Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com> |
|
3 | # Copyright 2019 Gregory Szorc <gregory.szorc@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 | # no-check-code because Python 3 native. |
|
8 | # no-check-code because Python 3 native. | |
9 |
|
9 | |||
10 | import argparse |
|
10 | import argparse | |
11 | import os |
|
11 | import os | |
12 | import pathlib |
|
12 | import pathlib | |
13 |
|
13 | |||
14 | from . import ( |
|
14 | from . import ( | |
15 | aws, |
|
15 | aws, | |
16 | HGAutomation, |
|
16 | HGAutomation, | |
17 | windows, |
|
17 | windows, | |
18 | ) |
|
18 | ) | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | SOURCE_ROOT = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent |
|
21 | SOURCE_ROOT = pathlib.Path(os.path.abspath(__file__)).parent.parent.parent.parent | |
22 | DIST_PATH = SOURCE_ROOT / 'dist' |
|
22 | DIST_PATH = SOURCE_ROOT / 'dist' | |
23 |
|
23 | |||
24 |
|
24 | |||
25 | def bootstrap_windows_dev(hga: HGAutomation, aws_region): |
|
25 | def bootstrap_windows_dev(hga: HGAutomation, aws_region): | |
26 | c = hga.aws_connection(aws_region) |
|
26 | c = hga.aws_connection(aws_region) | |
27 | image = aws.ensure_windows_dev_ami(c) |
|
27 | image = aws.ensure_windows_dev_ami(c) | |
28 | print('Windows development AMI available as %s' % image.id) |
|
28 | print('Windows development AMI available as %s' % image.id) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | def build_inno(hga: HGAutomation, aws_region, arch, revision, version): |
|
31 | def build_inno(hga: HGAutomation, aws_region, arch, revision, version): | |
32 | c = hga.aws_connection(aws_region) |
|
32 | c = hga.aws_connection(aws_region) | |
33 | image = aws.ensure_windows_dev_ami(c) |
|
33 | image = aws.ensure_windows_dev_ami(c) | |
34 | DIST_PATH.mkdir(exist_ok=True) |
|
34 | DIST_PATH.mkdir(exist_ok=True) | |
35 |
|
35 | |||
36 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: |
|
36 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: | |
37 | instance = insts[0] |
|
37 | instance = insts[0] | |
38 |
|
38 | |||
39 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) |
|
39 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) | |
40 |
|
40 | |||
41 | for a in arch: |
|
41 | for a in arch: | |
42 | windows.build_inno_installer(instance.winrm_client, a, |
|
42 | windows.build_inno_installer(instance.winrm_client, a, | |
43 | DIST_PATH, |
|
43 | DIST_PATH, | |
44 | version=version) |
|
44 | version=version) | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | def build_wix(hga: HGAutomation, aws_region, arch, revision, version): |
|
47 | def build_wix(hga: HGAutomation, aws_region, arch, revision, version): | |
48 | c = hga.aws_connection(aws_region) |
|
48 | c = hga.aws_connection(aws_region) | |
49 | image = aws.ensure_windows_dev_ami(c) |
|
49 | image = aws.ensure_windows_dev_ami(c) | |
50 | DIST_PATH.mkdir(exist_ok=True) |
|
50 | DIST_PATH.mkdir(exist_ok=True) | |
51 |
|
51 | |||
52 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: |
|
52 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: | |
53 | instance = insts[0] |
|
53 | instance = insts[0] | |
54 |
|
54 | |||
55 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) |
|
55 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) | |
56 |
|
56 | |||
57 | for a in arch: |
|
57 | for a in arch: | |
58 | windows.build_wix_installer(instance.winrm_client, a, |
|
58 | windows.build_wix_installer(instance.winrm_client, a, | |
59 | DIST_PATH, version=version) |
|
59 | DIST_PATH, version=version) | |
60 |
|
60 | |||
61 |
|
61 | |||
62 | def build_windows_wheel(hga: HGAutomation, aws_region, arch, revision): |
|
62 | def build_windows_wheel(hga: HGAutomation, aws_region, arch, revision): | |
63 | c = hga.aws_connection(aws_region) |
|
63 | c = hga.aws_connection(aws_region) | |
64 | image = aws.ensure_windows_dev_ami(c) |
|
64 | image = aws.ensure_windows_dev_ami(c) | |
65 | DIST_PATH.mkdir(exist_ok=True) |
|
65 | DIST_PATH.mkdir(exist_ok=True) | |
66 |
|
66 | |||
67 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: |
|
67 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: | |
68 | instance = insts[0] |
|
68 | instance = insts[0] | |
69 |
|
69 | |||
70 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) |
|
70 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) | |
71 |
|
71 | |||
72 | for a in arch: |
|
72 | for a in arch: | |
73 | windows.build_wheel(instance.winrm_client, a, DIST_PATH) |
|
73 | windows.build_wheel(instance.winrm_client, a, DIST_PATH) | |
74 |
|
74 | |||
75 |
|
75 | |||
76 |
def build_all_windows_packages(hga: HGAutomation, aws_region, revision |
|
76 | def build_all_windows_packages(hga: HGAutomation, aws_region, revision, | |
|
77 | version): | |||
77 | c = hga.aws_connection(aws_region) |
|
78 | c = hga.aws_connection(aws_region) | |
78 | image = aws.ensure_windows_dev_ami(c) |
|
79 | image = aws.ensure_windows_dev_ami(c) | |
79 | DIST_PATH.mkdir(exist_ok=True) |
|
80 | DIST_PATH.mkdir(exist_ok=True) | |
80 |
|
81 | |||
81 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: |
|
82 | with aws.temporary_windows_dev_instances(c, image, 't3.medium') as insts: | |
82 | instance = insts[0] |
|
83 | instance = insts[0] | |
83 |
|
84 | |||
84 | winrm_client = instance.winrm_client |
|
85 | winrm_client = instance.winrm_client | |
85 |
|
86 | |||
86 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) |
|
87 | windows.synchronize_hg(SOURCE_ROOT, revision, instance) | |
87 |
|
88 | |||
88 | for arch in ('x86', 'x64'): |
|
89 | for arch in ('x86', 'x64'): | |
89 | windows.purge_hg(winrm_client) |
|
90 | windows.purge_hg(winrm_client) | |
90 | windows.build_wheel(winrm_client, arch, DIST_PATH) |
|
91 | windows.build_wheel(winrm_client, arch, DIST_PATH) | |
91 | windows.purge_hg(winrm_client) |
|
92 | windows.purge_hg(winrm_client) | |
92 |
windows.build_inno_installer(winrm_client, arch, DIST_PATH |
|
93 | windows.build_inno_installer(winrm_client, arch, DIST_PATH, | |
|
94 | version=version) | |||
93 | windows.purge_hg(winrm_client) |
|
95 | windows.purge_hg(winrm_client) | |
94 |
windows.build_wix_installer(winrm_client, arch, DIST_PATH |
|
96 | windows.build_wix_installer(winrm_client, arch, DIST_PATH, | |
|
97 | version=version) | |||
95 |
|
98 | |||
96 |
|
99 | |||
97 | def terminate_ec2_instances(hga: HGAutomation, aws_region): |
|
100 | def terminate_ec2_instances(hga: HGAutomation, aws_region): | |
98 | c = hga.aws_connection(aws_region, ensure_ec2_state=False) |
|
101 | c = hga.aws_connection(aws_region, ensure_ec2_state=False) | |
99 | aws.terminate_ec2_instances(c.ec2resource) |
|
102 | aws.terminate_ec2_instances(c.ec2resource) | |
100 |
|
103 | |||
101 |
|
104 | |||
102 | def purge_ec2_resources(hga: HGAutomation, aws_region): |
|
105 | def purge_ec2_resources(hga: HGAutomation, aws_region): | |
103 | c = hga.aws_connection(aws_region, ensure_ec2_state=False) |
|
106 | c = hga.aws_connection(aws_region, ensure_ec2_state=False) | |
104 | aws.remove_resources(c) |
|
107 | aws.remove_resources(c) | |
105 |
|
108 | |||
106 |
|
109 | |||
107 | def run_tests_windows(hga: HGAutomation, aws_region, instance_type, |
|
110 | def run_tests_windows(hga: HGAutomation, aws_region, instance_type, | |
108 | python_version, arch, test_flags): |
|
111 | python_version, arch, test_flags): | |
109 | c = hga.aws_connection(aws_region) |
|
112 | c = hga.aws_connection(aws_region) | |
110 | image = aws.ensure_windows_dev_ami(c) |
|
113 | image = aws.ensure_windows_dev_ami(c) | |
111 |
|
114 | |||
112 | with aws.temporary_windows_dev_instances(c, image, instance_type, |
|
115 | with aws.temporary_windows_dev_instances(c, image, instance_type, | |
113 | disable_antivirus=True) as insts: |
|
116 | disable_antivirus=True) as insts: | |
114 | instance = insts[0] |
|
117 | instance = insts[0] | |
115 |
|
118 | |||
116 | windows.synchronize_hg(SOURCE_ROOT, '.', instance) |
|
119 | windows.synchronize_hg(SOURCE_ROOT, '.', instance) | |
117 | windows.run_tests(instance.winrm_client, python_version, arch, |
|
120 | windows.run_tests(instance.winrm_client, python_version, arch, | |
118 | test_flags) |
|
121 | test_flags) | |
119 |
|
122 | |||
120 |
|
123 | |||
121 | def get_parser(): |
|
124 | def get_parser(): | |
122 | parser = argparse.ArgumentParser() |
|
125 | parser = argparse.ArgumentParser() | |
123 |
|
126 | |||
124 | parser.add_argument( |
|
127 | parser.add_argument( | |
125 | '--state-path', |
|
128 | '--state-path', | |
126 | default='~/.hgautomation', |
|
129 | default='~/.hgautomation', | |
127 | help='Path for local state files', |
|
130 | help='Path for local state files', | |
128 | ) |
|
131 | ) | |
129 | parser.add_argument( |
|
132 | parser.add_argument( | |
130 | '--aws-region', |
|
133 | '--aws-region', | |
131 | help='AWS region to use', |
|
134 | help='AWS region to use', | |
132 | default='us-west-1', |
|
135 | default='us-west-1', | |
133 | ) |
|
136 | ) | |
134 |
|
137 | |||
135 | subparsers = parser.add_subparsers() |
|
138 | subparsers = parser.add_subparsers() | |
136 |
|
139 | |||
137 | sp = subparsers.add_parser( |
|
140 | sp = subparsers.add_parser( | |
138 | 'bootstrap-windows-dev', |
|
141 | 'bootstrap-windows-dev', | |
139 | help='Bootstrap the Windows development environment', |
|
142 | help='Bootstrap the Windows development environment', | |
140 | ) |
|
143 | ) | |
141 | sp.set_defaults(func=bootstrap_windows_dev) |
|
144 | sp.set_defaults(func=bootstrap_windows_dev) | |
142 |
|
145 | |||
143 | sp = subparsers.add_parser( |
|
146 | sp = subparsers.add_parser( | |
144 | 'build-all-windows-packages', |
|
147 | 'build-all-windows-packages', | |
145 | help='Build all Windows packages', |
|
148 | help='Build all Windows packages', | |
146 | ) |
|
149 | ) | |
147 | sp.add_argument( |
|
150 | sp.add_argument( | |
148 | '--revision', |
|
151 | '--revision', | |
149 | help='Mercurial revision to build', |
|
152 | help='Mercurial revision to build', | |
150 | default='.', |
|
153 | default='.', | |
151 | ) |
|
154 | ) | |
|
155 | sp.add_argument( | |||
|
156 | '--version', | |||
|
157 | help='Mercurial version string to use', | |||
|
158 | ) | |||
152 | sp.set_defaults(func=build_all_windows_packages) |
|
159 | sp.set_defaults(func=build_all_windows_packages) | |
153 |
|
160 | |||
154 | sp = subparsers.add_parser( |
|
161 | sp = subparsers.add_parser( | |
155 | 'build-inno', |
|
162 | 'build-inno', | |
156 | help='Build Inno Setup installer(s)', |
|
163 | help='Build Inno Setup installer(s)', | |
157 | ) |
|
164 | ) | |
158 | sp.add_argument( |
|
165 | sp.add_argument( | |
159 | '--arch', |
|
166 | '--arch', | |
160 | help='Architecture to build for', |
|
167 | help='Architecture to build for', | |
161 | choices={'x86', 'x64'}, |
|
168 | choices={'x86', 'x64'}, | |
162 | nargs='*', |
|
169 | nargs='*', | |
163 | default=['x64'], |
|
170 | default=['x64'], | |
164 | ) |
|
171 | ) | |
165 | sp.add_argument( |
|
172 | sp.add_argument( | |
166 | '--revision', |
|
173 | '--revision', | |
167 | help='Mercurial revision to build', |
|
174 | help='Mercurial revision to build', | |
168 | default='.', |
|
175 | default='.', | |
169 | ) |
|
176 | ) | |
170 | sp.add_argument( |
|
177 | sp.add_argument( | |
171 | '--version', |
|
178 | '--version', | |
172 | help='Mercurial version string to use in installer', |
|
179 | help='Mercurial version string to use in installer', | |
173 | ) |
|
180 | ) | |
174 | sp.set_defaults(func=build_inno) |
|
181 | sp.set_defaults(func=build_inno) | |
175 |
|
182 | |||
176 | sp = subparsers.add_parser( |
|
183 | sp = subparsers.add_parser( | |
177 | 'build-windows-wheel', |
|
184 | 'build-windows-wheel', | |
178 | help='Build Windows wheel(s)', |
|
185 | help='Build Windows wheel(s)', | |
179 | ) |
|
186 | ) | |
180 | sp.add_argument( |
|
187 | sp.add_argument( | |
181 | '--arch', |
|
188 | '--arch', | |
182 | help='Architecture to build for', |
|
189 | help='Architecture to build for', | |
183 | choices={'x86', 'x64'}, |
|
190 | choices={'x86', 'x64'}, | |
184 | nargs='*', |
|
191 | nargs='*', | |
185 | default=['x64'], |
|
192 | default=['x64'], | |
186 | ) |
|
193 | ) | |
187 | sp.add_argument( |
|
194 | sp.add_argument( | |
188 | '--revision', |
|
195 | '--revision', | |
189 | help='Mercurial revision to build', |
|
196 | help='Mercurial revision to build', | |
190 | default='.', |
|
197 | default='.', | |
191 | ) |
|
198 | ) | |
192 | sp.set_defaults(func=build_windows_wheel) |
|
199 | sp.set_defaults(func=build_windows_wheel) | |
193 |
|
200 | |||
194 | sp = subparsers.add_parser( |
|
201 | sp = subparsers.add_parser( | |
195 | 'build-wix', |
|
202 | 'build-wix', | |
196 | help='Build WiX installer(s)' |
|
203 | help='Build WiX installer(s)' | |
197 | ) |
|
204 | ) | |
198 | sp.add_argument( |
|
205 | sp.add_argument( | |
199 | '--arch', |
|
206 | '--arch', | |
200 | help='Architecture to build for', |
|
207 | help='Architecture to build for', | |
201 | choices={'x86', 'x64'}, |
|
208 | choices={'x86', 'x64'}, | |
202 | nargs='*', |
|
209 | nargs='*', | |
203 | default=['x64'], |
|
210 | default=['x64'], | |
204 | ) |
|
211 | ) | |
205 | sp.add_argument( |
|
212 | sp.add_argument( | |
206 | '--revision', |
|
213 | '--revision', | |
207 | help='Mercurial revision to build', |
|
214 | help='Mercurial revision to build', | |
208 | default='.', |
|
215 | default='.', | |
209 | ) |
|
216 | ) | |
210 | sp.add_argument( |
|
217 | sp.add_argument( | |
211 | '--version', |
|
218 | '--version', | |
212 | help='Mercurial version string to use in installer', |
|
219 | help='Mercurial version string to use in installer', | |
213 | ) |
|
220 | ) | |
214 | sp.set_defaults(func=build_wix) |
|
221 | sp.set_defaults(func=build_wix) | |
215 |
|
222 | |||
216 | sp = subparsers.add_parser( |
|
223 | sp = subparsers.add_parser( | |
217 | 'terminate-ec2-instances', |
|
224 | 'terminate-ec2-instances', | |
218 | help='Terminate all active EC2 instances managed by us', |
|
225 | help='Terminate all active EC2 instances managed by us', | |
219 | ) |
|
226 | ) | |
220 | sp.set_defaults(func=terminate_ec2_instances) |
|
227 | sp.set_defaults(func=terminate_ec2_instances) | |
221 |
|
228 | |||
222 | sp = subparsers.add_parser( |
|
229 | sp = subparsers.add_parser( | |
223 | 'purge-ec2-resources', |
|
230 | 'purge-ec2-resources', | |
224 | help='Purge all EC2 resources managed by us', |
|
231 | help='Purge all EC2 resources managed by us', | |
225 | ) |
|
232 | ) | |
226 | sp.set_defaults(func=purge_ec2_resources) |
|
233 | sp.set_defaults(func=purge_ec2_resources) | |
227 |
|
234 | |||
228 | sp = subparsers.add_parser( |
|
235 | sp = subparsers.add_parser( | |
229 | 'run-tests-windows', |
|
236 | 'run-tests-windows', | |
230 | help='Run tests on Windows', |
|
237 | help='Run tests on Windows', | |
231 | ) |
|
238 | ) | |
232 | sp.add_argument( |
|
239 | sp.add_argument( | |
233 | '--instance-type', |
|
240 | '--instance-type', | |
234 | help='EC2 instance type to use', |
|
241 | help='EC2 instance type to use', | |
235 | default='t3.medium', |
|
242 | default='t3.medium', | |
236 | ) |
|
243 | ) | |
237 | sp.add_argument( |
|
244 | sp.add_argument( | |
238 | '--python-version', |
|
245 | '--python-version', | |
239 | help='Python version to use', |
|
246 | help='Python version to use', | |
240 | choices={'2.7', '3.5', '3.6', '3.7', '3.8'}, |
|
247 | choices={'2.7', '3.5', '3.6', '3.7', '3.8'}, | |
241 | default='2.7', |
|
248 | default='2.7', | |
242 | ) |
|
249 | ) | |
243 | sp.add_argument( |
|
250 | sp.add_argument( | |
244 | '--arch', |
|
251 | '--arch', | |
245 | help='Architecture to test', |
|
252 | help='Architecture to test', | |
246 | choices={'x86', 'x64'}, |
|
253 | choices={'x86', 'x64'}, | |
247 | default='x64', |
|
254 | default='x64', | |
248 | ) |
|
255 | ) | |
249 | sp.add_argument( |
|
256 | sp.add_argument( | |
250 | '--test-flags', |
|
257 | '--test-flags', | |
251 | help='Extra command line flags to pass to run-tests.py', |
|
258 | help='Extra command line flags to pass to run-tests.py', | |
252 | ) |
|
259 | ) | |
253 | sp.set_defaults(func=run_tests_windows) |
|
260 | sp.set_defaults(func=run_tests_windows) | |
254 |
|
261 | |||
255 | return parser |
|
262 | return parser | |
256 |
|
263 | |||
257 |
|
264 | |||
258 | def main(): |
|
265 | def main(): | |
259 | parser = get_parser() |
|
266 | parser = get_parser() | |
260 | args = parser.parse_args() |
|
267 | args = parser.parse_args() | |
261 |
|
268 | |||
262 | local_state_path = pathlib.Path(os.path.expanduser(args.state_path)) |
|
269 | local_state_path = pathlib.Path(os.path.expanduser(args.state_path)) | |
263 | automation = HGAutomation(local_state_path) |
|
270 | automation = HGAutomation(local_state_path) | |
264 |
|
271 | |||
265 | if not hasattr(args, 'func'): |
|
272 | if not hasattr(args, 'func'): | |
266 | parser.print_help() |
|
273 | parser.print_help() | |
267 | return |
|
274 | return | |
268 |
|
275 | |||
269 | kwargs = dict(vars(args)) |
|
276 | kwargs = dict(vars(args)) | |
270 | del kwargs['func'] |
|
277 | del kwargs['func'] | |
271 | del kwargs['state_path'] |
|
278 | del kwargs['state_path'] | |
272 |
|
279 | |||
273 | args.func(automation, **kwargs) |
|
280 | args.func(automation, **kwargs) |
General Comments 0
You need to be logged in to leave comments.
Login now