-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmason.py
More file actions
executable file
·350 lines (279 loc) · 10.9 KB
/
mason.py
File metadata and controls
executable file
·350 lines (279 loc) · 10.9 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python2
import getpass
import pkg_resources
import click
import os
import requests
import colorama
import packaging.version
from masonlib.platform import Platform
from masonlib.imason import IMason
class Config(object):
"""
Global config object, utilized to set verbosity of logging events
and other flags.
"""
def __init__(self):
self.verbose = False
self.no_colorize = False
pass_config = click.make_pass_decorator(Config, ensure=True)
@click.group()
@click.option('--debug', '-d', is_flag=True, help='show additional debug information where available')
@click.option('--verbose', '-v', help='show verbose artifact and command details', is_flag=True)
@click.option('--access-token', help='optional access token if already available')
@click.option('--id-token', help='optional id token if already available')
@click.option('--no-color', is_flag=True, help='turn off colorized output')
@pass_config
def cli(config, debug, verbose, id_token, access_token, no_color):
"""mason-cli provides command line interfaces that allow you to register, query, build, and deploy
your configurations and packages to your devices in the field."""
_check_version()
config.debug = debug
config.verbose = verbose
config.no_colorize = no_color
if not no_color:
colorama.init(autoreset=True)
platform = Platform(config)
config.mason = platform.get(IMason)
config.mason.set_id_token(id_token)
config.mason.set_access_token(access_token)
@cli.group()
@click.option('--skip-verify', '-s', is_flag=True, help='skip verification of artifact details')
@pass_config
def register(config, skip_verify):
"""Register artifacts to the mason platform."""
config.skip_verify = skip_verify
@register.command()
@click.argument('apks', nargs=-1)
@pass_config
def apk(config, apks):
"""Register apk artifacts.
APK - One or many apk's to be registered to the mason platform.
ex:\n
mason register apk test.apk
multiple in a directory:\n
mason register apk apks/*.apk
"""
for app in apks:
if config.verbose:
click.echo('Registering {}...'.format(app))
if config.mason.parse_apk(app):
config.mason.register(app)
@register.command()
@click.argument('yamls', nargs=-1)
@pass_config
def config(config, yamls):
"""Register config artifacts.
YAML - One or more yaml file describing a configuration.
ex:\n
mason register config test.yml
multiple in a directory:\n
mason register config configs/*.yml
"""
for yaml in yamls:
if config.verbose:
click.echo('Registering {}...'.format(yaml))
if config.mason.parse_os_config(yaml):
config.mason.register(yaml)
@register.command()
@click.argument('binary')
@click.argument('name')
@click.argument('type')
@click.argument('version')
@pass_config
def media(config, binary, name, type, version):
"""Register media artifacts.
NAME - The name of the media artifact.\n
TYPE - The given type of the media artifact.\n
supported:\n
- bootanimation\n
VERSION - The version of the media artifact.
ex:\n
mason register media bootanimation.zip bootanimation 1
"""
if config.verbose:
click.echo('Registering {}...'.format(binary))
if config.mason.parse_media(name, type, version, binary):
config.mason.register(binary)
@cli.command()
@click.argument('project')
@click.argument('version')
@pass_config
def build(config, project, version):
"""Build a registered project.
PROJECT - The name of the configuration project\n
VERSION - The version of the configuration project
The name and the version of the configuration project
can be found in the YAML definition which was registered
to the mason platform.
As an example, a registered yaml:\n
os:\n
name: mason-test\n
version: 5
becomes a build command:\n
mason build mason-test 5
"""
if config.verbose:
click.echo('Starting build for {}:{}...'.format(project, version))
if not config.mason.build(project, version):
exit('Unable to start build')
@cli.group()
@click.option('--skip-verify', '-s', is_flag=True, help='skip verification of deployment')
@click.option('--push', '-p', is_flag=True, default=False, help='push the deployment to devices in the field')
@pass_config
def deploy(config, skip_verify, push):
"""Deploy artifacts to groups."""
config.skip_verify = skip_verify
config.push = push
@deploy.command()
@click.argument('name')
@click.argument('version')
@click.argument('groups', nargs=-1)
@pass_config
def apk(config, name, version, groups):
"""Deploy apk artifacts.
NAME - The package name of the apk to be deployed\n
VERSION - The versionCode of the apk\n
GROUP(s) - The target group(s) to deploy to
As an example, a registered apk:\n
package_name: com.test.app\n
version_code: 3
to deploy to group `development` becomes:\n
mason deploy apk com.test.app 3 development\n
or to deploy to multiple groups:\n
mason deploy apk com.test.app 3 development staging production
this can be used in conjunction with the --push argument
"""
for group in groups:
if config.verbose:
click.echo('Deploying {}:{}...'.format(name, version))
if not config.mason.deploy("apk", name, version, group, config.push):
exit('Unable to deploy item')
@deploy.command()
@click.argument('name')
@click.argument('version')
@click.argument('groups', nargs=-1)
@pass_config
def ota(config, name, version, groups):
"""Deploy ota artifacts.
NAME - The name of the ota to be deployed (usually mason-os)\n
VERSION - The release version (semantic version) of the ota \n
GROUP(s) - The target group(s) to deploy to
As an example, Mason OS 2.0.0 (Nougat Release):\n
name: mason-os\n
release version: 2.0.0
to deploy to group `development` becomes:\n
mason deploy ota mason-os 2.0.0 development\n
or to deploy to multiple groups:\n
mason deploy ota mason-os 2.0.0 development staging production
this can be used in conjunction with the --push argument
"""
for group in groups:
if config.verbose:
click.echo('Deploying {}:{}...'.format(name, version))
if not config.mason.deploy("ota", name, version, group, config.push):
exit('Unable to deploy item')
@deploy.command()
@click.argument('name')
@click.argument('version')
@click.argument('groups', nargs=-1)
@pass_config
def config(config, name, version, groups):
"""Deploy config artifacts.
NAME - The name of the configuration to be deployed\n
VERSION - The version of the configuration to be deployed\n
GROUP(s) - The target group(s) to deploy to
As an example, a registered yaml:\n
os:\n
name: mason-test\n
version: 5\n
to deploy to group `development` becomes:\n
mason deploy config mason-test 5 development
or to deploy to multiple groups:\n
mason deploy config mason-test 5 development staging production
this can be used in conjunction with the --push argument
"""
for group in groups:
if config.verbose:
click.echo('Deploying {}:{}...'.format(name, version))
if not config.mason.deploy("config", name, version, group, config.push):
exit('Unable to deploy item')
@cli.command()
@click.option('--skip-verify', '-s', is_flag=True, help='skip verification of config stage')
@click.argument('yaml')
@pass_config
def stage(config, skip_verify, yaml):
"""Stage a project.
YAML - The configuration file to register and build.
The stage commands allows you to register a configuration file and immediately start a build for it.
"""
config.skip_verify = skip_verify
if config.verbose:
click.echo('Staging {}...'.format(yaml))
if config.mason.parse_os_config(yaml):
config.mason.stage(yaml)
@cli.command()
@click.option('--user', default=None, help='pass in user')
@click.option('--password', default=None, help='pass in password')
@pass_config
def login(config, user, password):
"""Authenticate via user/password."""
if not user or not password:
# Prompt for user name
response = raw_input('User: ')
# Exit on empty user
if not response:
exit('Authentication requires a valid user')
user = response
# Prompt for password
response = getpass.getpass()
# Exit on empty password
if not response:
exit('Authentication requires a valid password')
password = response
if config.verbose:
click.echo('Authing ' + user)
if not config.mason.authenticate(user, password):
exit('Unable to authenticate')
else:
click.echo('User authenticated.')
@cli.command()
@pass_config
def logout(config):
"""Log out of current session."""
if config.mason.logout():
click.echo('Successfully logged out')
@cli.command()
def version():
"""Display mason-cli version."""
try:
our_version = pkg_resources.require("mason-cli")[0].version
click.echo('Mason Platform CLI ' + our_version)
click.echo('Copyright (C) 2019 Mason America (https://www.bymason.com)')
click.echo('License Apache 2.0 <https://www.apache.org/licenses/LICENSE-2.0>')
except pkg_resources.DistributionNotFound:
click.echo('Unable to retrieve version information')
def _check_version():
r = requests.get('https://raw.githubusercontent.com/MasonAmerica/mason-cli/master/VERSION')
current_version = packaging.version.parse(pkg_resources.require("mason-cli")[0].version)
if r.status_code == 200:
if r.text:
remote_version = packaging.version.parse(r.text)
if remote_version > current_version:
if isMasonDocker():
upgrade_command = 'docker pull masonamerica/mason-cli:latest'
else:
upgrade_command = 'pip install --upgrade git+https://git@github.com/MasonAmerica/mason-cli.git'
print '\n==================== NOTICE ====================\n' \
'A newer version \'{}\' of the mason-cli is available.\n' \
'Run:\n' \
' `{}`\n' \
'to upgrade to the latest version.\n' \
'\n' \
'Release notes: https://github.com/MasonAmerica/mason-cli/releases' \
'\n' \
'==================== NOTICE ====================\n'.format(remote_version, upgrade_command)
def isMasonDocker():
return bool(os.environ.get('MASON_CLI_DOCKER', False))
if __name__ == '__main__':
cli()