-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapplejack.py
executable file
·86 lines (70 loc) · 4.05 KB
/
applejack.py
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
#!/usr/bin/env python3
from applejack.downloader import XLDevOpsPlatformDownloader
from applejack.renderer import Renderer
from applejack.builder import ImageBuilder
from applejack import all_products, all_product_configs, load_product_config, target_systems
import click
class ProductConfigType(click.Choice):
name = 'product_config'
def __init__(self, choices):
click.Choice.__init__(self, choices)
def convert(self, value, param, ctx):
if value:
product = super(ProductConfigType, self).convert(value, param, ctx)
return load_product_config(product)
else:
return None
_shared_opts = [
click.option('--product', multiple=True, help="The product to build the files / images for.", type=ProductConfigType(all_products())),
click.option('--xl-version', help="Product version, e.g. 8.1.0", required=True),
click.option('--registry', help="Docker registry to use, either to pull from when used with render or to push to when used with build", default='xebialabs'),
click.option('--suffix', help="The (optional) suffix attached to the Docker and Git commit tags. Only used when a new version of the Docker images is released for the same product version"),
]
def shared_opts(func):
for option in reversed(_shared_opts):
func = option(func)
return func
@click.group(help="Render and build the Dockerfile templates")
def applejack():
print("🦄 Whee! Let's go!")
pass
@applejack.command(help="Render the templates")
@shared_opts
@click.option('--commit', '-c', is_flag=True, help="Commit and tag the generated Dockerfiles.")
def render(**kwargs):
renderer = Renderer(kwargs)
for product in (kwargs['product'] or all_product_configs()):
for target_os in target_systems(product):
print("Generating %s Dockerfile for %s" % (product['name'], target_os))
renderer.render(target_os, product)
if kwargs['commit']:
print("Commiting generated Dockerfiles")
renderer.commit_rendered()
@applejack.command(help="Build the Docker images from the generated templates")
@shared_opts
@click.option('--push', '-p', is_flag=True, help="Push the Docker images to the hub.")
@click.option('--download-source', help="Download source: dist (default) or nexus.", default='dist', type=click.Choice(["dist", "nexus", "localm2", "zip"]))
@click.option('--download-username', help="Username to use to download product ZIP.")
@click.option('--download-password', help="Password to use to download product ZIP.")
@click.option('--target-os', multiple=True, help="The target container OS to build and/or push.")
@click.option('--use-cache', is_flag=True, help="Don't download product ZIP if already downloaded, don't pull the base image and use the Docker build cache")
@click.option('--m2location', help="The location of the .m2 repository for localm2 download sources. Defaults to ~/.m2/repository")
@click.option('--zipfile', help="The actual zip file to use when --download-source=zip")
def build(**kwargs):
for product_conf in (kwargs['product'] or all_product_configs()):
downloader = XLDevOpsPlatformDownloader(kwargs, product_conf)
if 'repositories' in product_conf.keys() and (not kwargs['use_cache'] or not downloader.is_cached()):
print("Going to download product ZIP for %s version %s" %
(product_conf['name'], kwargs['xl_version']))
downloader.download_product()
builder = ImageBuilder(kwargs, product_conf)
for target_os in (kwargs['target_os'] or target_systems(product_conf)):
print("Building Docker image for %s %s" % (product_conf['name'], target_os))
image_id = builder.build_docker_image(target_os, is_slim=False)
slim_image_id = builder.build_docker_image(target_os, is_slim=True)
if kwargs['push']:
builder.push_image(image_id, target_os, is_slim=False)
builder.push_image(slim_image_id, target_os, is_slim=True)
if __name__ == '__main__':
applejack()
print("🦄 🌈 Friendship is Magic... Like Docker 🦄")