cloud-build/cloud-build.py

99 lines
2.2 KiB
Python
Raw Normal View History

2019-03-28 03:28:51 +03:00
#!/usr/bin/python3
2020-04-25 01:04:10 +03:00
from collections.abc import Iterable
2019-03-28 03:28:51 +03:00
import argparse
2020-04-25 01:04:10 +03:00
import yaml
2020-04-20 14:29:27 +03:00
import sys
2019-03-28 03:28:51 +03:00
2020-04-20 14:29:27 +03:00
import cloud_build
2019-07-08 02:14:01 +03:00
2019-03-28 03:28:51 +03:00
PROG = 'cloud-build'
def parse_args():
2020-04-25 01:04:10 +03:00
def is_dict(string):
raw_dict = dict(yaml.safe_load(string))
result = {}
for k, v in raw_dict.items():
key = k.lower()
if not isinstance(v, Iterable) or isinstance(v, str):
result[key] = [v]
else:
result[key] = v
return result
2021-04-10 01:03:47 +03:00
stages = ['build', 'test', 'copy_external_files', 'sign', 'sync']
2019-03-28 03:28:51 +03:00
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
'-c',
'--config',
default=f'/etc/{PROG}/config.yaml',
help='path to config',
)
2021-04-10 01:03:47 +03:00
parser.add_argument(
'--stages',
nargs='+',
default=stages,
choices=stages,
help='list of stages',
)
parser.add_argument(
'--skip-stages',
nargs='+',
default=[],
choices=stages,
help='list of sipping stages',
)
2021-03-17 17:03:28 +03:00
parser.add_argument(
'--create-remote-dirs',
action='store_true',
help='create remote directories',
)
2019-11-01 16:44:46 +03:00
parser.add_argument(
'--no-tests',
action='store_true',
help='disable running tests',
)
2020-05-06 15:24:48 +03:00
parser.add_argument(
'--no-sign',
action='store_true',
help='disable creating check sum and signing it',
)
2020-04-25 01:04:10 +03:00
parser.add_argument(
'--tasks',
default={},
type=is_dict,
help='add tasks to repositories',
)
2019-03-28 03:28:51 +03:00
args = parser.parse_args()
return args
def main():
args = parse_args()
2021-04-10 01:03:47 +03:00
stages = set(args.stages) - set(args.skip_stages)
cb = cloud_build.CB(config=args.config, tasks=args.tasks)
2021-04-10 01:03:47 +03:00
if 'build' in stages:
no_tests = 'test' not in stages
cb.create_images(no_tests=no_tests)
if 'copy_external_files' in stages:
cb.copy_external_files()
if 'sign' in stages:
2020-05-06 15:24:48 +03:00
cb.sign()
2021-04-10 01:03:47 +03:00
if 'sync' in stages:
cb.sync(create_remote_dirs=args.create_remote_dirs)
2019-03-28 03:28:51 +03:00
if __name__ == '__main__':
2020-04-20 14:29:27 +03:00
try:
main()
except cloud_build.Error as e:
print(e, file=sys.stdout)
exit(1)