From 513abf46040ec0cc4d8d3549664464114071ceae Mon Sep 17 00:00:00 2001 From: Davide Depau Date: Sun, 19 Apr 2020 02:12:28 +0200 Subject: [PATCH] Add helpers --- helpers.star | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 helpers.star diff --git a/helpers.star b/helpers.star new file mode 100644 index 0000000..b49dd24 --- /dev/null +++ b/helpers.star @@ -0,0 +1,151 @@ +PLATFORMS = { + 'aarch64': {'os': 'linux', 'arch': 'arm64'}, + 'x86_64': {'os': 'linux', 'arch': 'amd64'}, + 'armv7h': {'os': 'linux', 'arch': 'arm'}, + 'i686': {'os': 'linux', 'arch': '386'}, + 'i386': {'os': 'linux', 'arch': '386'}, + 'pentium4': {'os': 'linux', 'arch': '386'} +} +DOCKER_IMAGE = "depau/drone-makepkg:{arch}" + +def generate(config): + special_archs = {arch: cfg for arch, cfg in config.items() if arch in PLATFORMS.keys()} + + others = config.get("all") or config.get("others") or None + + if others: + if "arch_matrix" not in config: + fail("arch_matrix is required if all/others is defined") + + matrix = config["arch_matrix"] + + # Ensure that arch_matrix won't override a specifically-defined arch + for arch in matrix: + if arch in special_archs: + fail("arch '{}' is defined in both arch_matrix and top level config".format(arch)) + + archs = {arch: others for arch in matrix} + else: + archs = {} + + archs.update(special_archs) + + pipelines = [] + + for arch, archconfig in archs: + pipelines.append(generate_pipeline(config, arch, archconfig)) + + if len(pipelines) == 1: + return pipelines[0] + else: + return pipelines + + +def generate_pipeline(config, arch, archconfig): + pipeline = { + 'kind': 'pipeline', + 'type': 'docker', + 'name': arch, + 'platform': PLATFORMS[arch] + } + + steps = [] + + # Add submodules step + if config.get('clone_recursive'): + steps.append({ + 'name': 'submodules', + 'image': 'alpine/git', + 'commands': ['git submodule update --recursive --remote'] + }) + + # Add packages steps + pull_set = False + for pkgconfig in archconfig: + stepcfg = step_git(pkgconfig) if is_git(pkgconfig) else step_aur(pkgconfig) + stepcfg['image'] = DOCKER_IMAGE.format(arch=arch) + + if not pull_set: + # Ensure the image is pulled in the first step + stepcfg['pull'] = 'always' + pull_set = True + + steps.append(stepcfg) + + steps += upload_steps(arch) + + if config.get('master_only', True): + # Allow builds only in the master branch by default + for step in steps: + step['when'] = {'branch': ['master']} + + pipeline['steps'] = steps + return pipeline + + +def is_git(pkgconfig): + if type(pkgconfig) == dict: + return 'git' in pkgconfig + else: + return pkgconfig == "." or "/" in pkgconfig + + +def step_aur(pkgconfig): + stepcfg = {'settings': {}} + + if type(pkgconfig) == str: + stepcfg['name'] = pkgconfig + stepcfg['settings']['aur'] = pkgconfig + elif type(pkgconfig) == dict: + if 'aur' not in pkgconfig: + fail("{} is not a valid aur package definition".format(pkgconfig)) + stepcfg['name'] = pkgconfig.get('name') or pkgconfig['aur'] + stepcfg['settings'].update({k, v for k, v in pkgconfig.items() if k != 'name'}) + else: + fail("{} is not a valid aur package definition".format(pkgconfig)) + + return stepcfg + + +def _gitname(repo_path): + if repo_path == ".": + # Local git repo, side-by-side with the Drone config using this module + return "local" + else: + return repo_path.split('/')[-1].replace('.git', '') + + +def step_git(pkgconfig): + stepcfg = {'settings': {}} + + if type(pkgconfig) == str: + stepcfg['name'] = _gitname(pkgconfig) + if pkgconfig != ".": + stepcfg['settings']['git'] = pkgconfig + elif type(pkgconfig) == dict: + if 'git' not in pkgconfig: + fail("{} is not a valid git package definition".format(pkgconfig)) + stepcfg['name'] = pkgconfig.get('name') or _gitname(pkgconfig['git']) + if pkgconfig['git'] != ".": + stepcfg['settings']['git'] = pkgconfig['git'] + stepcfg['settings'].update({k, v for k, v in pkgconfig.items() if k not in ('git', 'name')}) + else: + fail("{} is not a valid git package definition".format(pkgconfig)) + + +# TODO: probably it's better not to hardcode everything here +def upload_steps(arch): + return [{ + 'name': 'upload', + 'image': 'plugins/s3', + 'settings': [ + 'endpoint': 'https://objstor.depau.eu', + 'bucket': 'archlinux-packages', + 'access_key': {'from_secret': 'minio_user'}, + 'secret_key': {'from_secret': 'minio_passwd'}, + 'source': 'out/*', + 'target': '/' + arch, + 'strip_prefix': True, + 'path_style': True + ] + }]