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 ' ,
)
2022-02-08 03:07:55 +03:00
parser . add_argument (
' --key ' ,
help = ' gpg key to sign ' ,
)
2022-02-09 01:33:34 +03:00
parser . add_argument (
' --remote ' ,
help = ' remote to sync images ' ,
)
2021-06-21 15:25:14 +03:00
parser . add_argument (
' --built-images-dir ' ,
help = ' path to already built image for stages other then build ' ,
)
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 ' ,
)
2021-07-29 14:20:56 +03:00
parser . add_argument (
' --force-rebuild ' ,
action = ' store_true ' ,
help = ' forces rebuild ' ,
)
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 )
2022-02-08 02:29:13 +03:00
config_override = { }
2022-03-04 17:24:44 +03:00
2022-02-09 01:33:34 +03:00
def args_to_override ( key ) :
if ( value := getattr ( args , key ) ) is not None :
config_override [ key ] = value
2022-02-08 02:29:13 +03:00
if args . force_rebuild :
config_override [ ' rebuild_after ' ] = { ' days ' : 0 }
2022-02-09 01:33:34 +03:00
for arg in [ ' key ' , ' remote ' ] :
args_to_override ( arg )
2022-02-08 02:29:13 +03:00
2021-06-21 15:25:14 +03:00
cb = cloud_build . CB (
config = args . config ,
tasks = args . tasks ,
built_images_dir = args . built_images_dir ,
2022-02-08 02:29:13 +03:00
config_override = config_override ,
2021-06-21 15:25:14 +03:00
)
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 :
2021-04-10 01:41:07 +03:00
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 )