-
Notifications
You must be signed in to change notification settings - Fork 10
/
gdk_build.py
executable file
·67 lines (49 loc) · 2.26 KB
/
gdk_build.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Custom build script for GDK component build operation. This script is not designed to
be executed directly. It is designed to be used by GDK.
Prior to running "gdk component build", the user should:
1) Update the configuration YAML files in artifacts/config and secrets directories, as appropriate
2) Set the desired AWS region in ggk-config.json.
3) Create or update the Home Assistant secret by running create_config_secret.py
Example execution:
gdk component build
"""
import json
import shutil
import yaml
from libs.secret import Secret
from libs.gdk_config import GdkConfig
DIRECTORY_ARTIFACTS = 'artifacts/'
DIRECTORY_BUILD = 'greengrass-build/artifacts/'
FILE_RECIPE_TEMPLATE = 'recipe.json'
FILE_RECIPE = 'greengrass-build/recipes/recipe.json'
FILE_ZIP_BASE = 'home-assistant'
FILE_ZIP_EXT = 'zip'
FILE_DOCKER_COMPOSE = DIRECTORY_ARTIFACTS + 'docker-compose.yml'
def create_recipe():
""" Creates the component recipe, filling in the Docker images and Secret ARN """
print('Creating recipe {}'.format(FILE_RECIPE))
with open(FILE_DOCKER_COMPOSE, encoding="utf-8") as docker_compose_file:
docker_compose_yaml = yaml.safe_load(docker_compose_file)
with open(FILE_RECIPE_TEMPLATE, encoding="utf-8") as recipe_template_file:
recipe_str = recipe_template_file.read()
recipe_str = recipe_str.replace('$SECRET_ARN', secret_value['ARN'])
recipe_str = recipe_str.replace('$DOCKER_IMAGE', docker_compose_yaml['services']['homeassistant']['image'])
recipe_json = json.loads(recipe_str)
recipe_str = json.dumps(recipe_json, indent=2)
with open(FILE_RECIPE, 'w', encoding="utf-8") as recipe_file:
recipe_file.write(recipe_str)
print('Created recipe')
def create_artifacts():
""" Creates the artifacts archive as a ZIP file """
file_name = DIRECTORY_BUILD + gdk_config.name() + '/' + gdk_config.version() + '/' + FILE_ZIP_BASE
print('Creating artifacts archive {}'.format(file_name))
shutil.make_archive(file_name, FILE_ZIP_EXT, DIRECTORY_ARTIFACTS)
print('Created artifacts archive')
gdk_config = GdkConfig()
secret = Secret(gdk_config.region())
secret_value = secret.get()
create_recipe()
create_artifacts()