Skip to content

Commit 918ff3e

Browse files
Furistokylos101
authored andcommitted
Add new scripts for building images
1 parent b767228 commit 918ff3e

7 files changed

+149
-32
lines changed

.gitpod.Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ RUN cd /usr \
1515
&& chmod +x /usr/bin/shfmt \
1616
&& install-packages shellcheck \
1717
&& sudo pip3 install pre-commit \
18-
&& curl -o /usr/bin/yq -L https://github.com/mikefarah/yq/releases/download/v4.22.1/yq_linux_amd64 && chmod +x yq
18+
&& curl -sSL https://github.com/mikefarah/yq/releases/download/v4.22.1/yq_linux_amd64 -o /usr/bin/yq && chmod +x /usr/bin/yq

.gitpod.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ tasks:
2020
command: |
2121
gp await-port 5000
2222
REPO=localhost:5000/dazzle
23-
echo "To build specific chunks and combinations 'time ./dazzle-up.sh -c chunk1 -c chunk2 -n combo'"
24-
echo "To build all the chunks and combinations 'time ./dazzle-up.sh'"
23+
echo "To build specific chunks and combine them 'time ./build-chunk.sh -c chunk1 -c chunk2:variant1.2.3 -n combo'"
24+
echo "To build all the chunks and combinations 'time ./build-all.sh'"
25+
echo "To build a specific combination 'time ./build-combo.sh combo'"
2526
echo "To list image chunks 'dazzle project image-name $REPO'"
2627
echo "To list hashes for image chunks 'dazzle project hash $REPO'"
2728
echo "To print the combined image maniest 'dazzle project manifest $REPO'"

CONTRIBUTING.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Here is a list of dependencies and tools:
3131

3232
## Locally
3333

34-
We ship a shell script [dazzle-up.sh](dazzle-up.sh) that can be used to build the images locally. See the following sub sections for usage.
34+
We ship a shell script [build-all.sh](build-all.sh) that can be used to build the images locally. See the following sub sections for usage.
3535

3636
This script will first build the chunks and run tests followed by creation of container images. It uses `dazzle` to perform these tasks.
3737

@@ -45,20 +45,30 @@ where `combo` is the name of the combination defined in [dazzle.yaml](dazzle.yam
4545

4646
### Build Specific Chunks
4747

48-
Often, you would want to test only the chunks that you modify. You can do that by using the `-c` flag.
48+
Often, you would want to test only the chunks that you modify. You can do that with build-chunk.sh using the `-c` flag and specifying the name for your combination with `-n`.
4949

5050
```console
51-
./dazzle-up.sh -c lang-c -c dep-cacert-update -n mychangecombo
51+
./build-chunk.sh -c lang-c -c dep-cacert-update -c lang-go:1.17.5 -n mychangecombo
5252
```
5353

5454
Above command will build only chunks `lang-c` and `dep-cacert-update` and combine the created chunks (all variants, if any exists) as a combination with name `mychangecombo`.
5555

56+
### Build Specific Combination
57+
58+
Sometimes you only want to build one specific combination e.g. the `postgresql` or the `go` image. You can do that with
59+
60+
```console
61+
./build-combo.sh <comboName> e.g. ./build-combo.sh postgresql
62+
```
63+
64+
This will build all chunks that are referenced by the `go` combination and then combine them to create the `go` image.
65+
5666
### Build All Chunks
5767

5868
Execute the following command to build using the default config `dazzle.yaml` shipped in this repo:
5969

6070
```bash
61-
./dazzle-up.sh
71+
./build-all.sh
6272
```
6373

6474
> **NOTE:** Building images locally consumes a lot of resources and is often slow.

build-all.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
REPO=localhost:5000/dazzle
5+
# First, build chunks without hashes
6+
dazzle build $REPO -v --chunked-without-hash
7+
# Second, build again, but with hashes
8+
dazzle build $REPO -v
9+
# Third, create combinations of chunks
10+
dazzle combine $REPO --all -v

dazzle-up.sh renamed to build-chunk.sh

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
#!/bin/bash
22
set -euo pipefail
3-
trap ctrl_c INT
3+
trap ctrl_c EXIT
44

5-
readonly YELLOW=$(tput setaf 3)
6-
readonly NC=$(tput sgr0)
5+
# shellcheck source=/dev/null
6+
source build-common.sh
77

8-
readonly BACKUP_FILE=".dazzle.yaml.orig"
98
readonly TEMP_FILE=".dazzle.yaml.temp"
10-
readonly ORIGINAL_FILE="dazzle.yaml"
11-
readonly AVAILABLE_CHUNKS=$(ls chunks/)
129
readonly REPO="localhost:5000/dazzle"
1310

1411
function usage() {
1512
cat <<EOF
16-
Usage: ./dazzle-up.sh [OPTION]...
17-
Example: ./dazzle-up.sh -c lang-c -c dep-cacert-update -n mychangecombo
13+
Usage: ./build-chunk.sh [OPTION]...
14+
Example: ./build-chunk.sh -c lang-c -c dep-cacert-update -c lang-go:1.17.5 -n mychangecombo
1815
Options for build:
1916
-h, --help Display this help
2017
-c, --chunk Chunk to build, You can build multiple chunks: -c chunk1 -c chunk2. If no chunks are supplied then build using existing config
2118
-n, --name Combination name, by default a combination name 'default' is created. This flag only works when -c flag is specified
2219
EOF
2320
}
2421

25-
function restore_original() {
26-
echo "${YELLOW}Restoring backup file ${BACKUP_FILE} to original file ${ORIGINAL_FILE}${NC}"
27-
cp ${BACKUP_FILE} ${ORIGINAL_FILE}
28-
}
29-
30-
function ctrl_c() {
31-
echo "${YELLOW}** Trapped CTRL-C${NC}"
32-
restore_original
33-
}
34-
3522
function build_and_combine() {
3623
dazzle build ${REPO} -v --chunked-without-hash && dazzle build ${REPO} -v && dazzle combine ${REPO} --all -v
3724
}
@@ -54,7 +41,7 @@ function extract_variants() {
5441
ARGS=$(getopt -o h,:c:n: --long help:,chunk:,name: -- "$@")
5542
eval set -- "${ARGS}"
5643

57-
[ ! -f ${BACKUP_FILE} ] && echo "${YELLOW}Creating a backup of ${ORIGINAL_FILE} as it does not exist yet...${NC}" && cp ${ORIGINAL_FILE} ${BACKUP_FILE}
44+
save_original
5845

5946
CHUNKS=""
6047
COMBINATION="default"
@@ -85,7 +72,7 @@ while true; do
8572
;;
8673
*)
8774
echo Error: unknown flag "$1"
88-
echo "${YELLOW}Run 'dazzle-up.sh --help' for usage.${NC}"
75+
echo "${YELLOW}Run 'build-chunk.sh --help' for usage.${NC}"
8976
exit 1
9077
;;
9178
esac
@@ -96,9 +83,9 @@ if [[ -z "${CHUNKS[*]}" ]]; then
9683
echo "${YELLOW}No chunks specified, will build using the existing ${ORIGINAL_FILE}${NC}"
9784
else
9885
# else build for supplied arguments
99-
[ -f ${ORIGINAL_FILE} ] && echo "${YELLOW}Deleting ${ORIGINAL_FILE}, will produce a new config with supplied arguments${NC}" && rm ${ORIGINAL_FILE}
86+
[ -f "${ORIGINAL_FILE}" ] && echo "${YELLOW}Deleting ${ORIGINAL_FILE}, will produce a new config with supplied arguments${NC}" && rm "${ORIGINAL_FILE}"
10087

101-
for CN in ${AVAILABLE_CHUNKS}; do
88+
for CN in $(get_available_chunks); do
10289
if [[ ! ${CHUNKS[*]} =~ ${CN} ]]; then
10390
dazzle project ignore "${CN}"
10491
else
@@ -115,6 +102,4 @@ fi
115102
build_and_combine
116103

117104
echo "${YELLOW}Saving dazzle config used to generate build in ${TEMP_FILE}${NC}"
118-
cp ${ORIGINAL_FILE} ${TEMP_FILE}
119-
120-
restore_original
105+
cp "${ORIGINAL_FILE}" ${TEMP_FILE}

build-combo.sh

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#! /bin/bash
2+
set -eo pipefail
3+
trap ctrl_c EXIT
4+
5+
# shellcheck source=/dev/null
6+
source build-common.sh
7+
8+
function build_combination() {
9+
combination=$1
10+
11+
local exists
12+
exists="$(yq e '.combiner.combinations[] | select (.name=="'"$combination"'")' dazzle.yaml)"
13+
if [[ -z "$exists" ]]; then
14+
echo "combination is not defined"
15+
exit 1
16+
fi
17+
18+
refs=$(get_refs "$combination")
19+
required_chunks=$(get_chunks "$refs" | sort | uniq)
20+
available_chunks=$(get_available_chunks)
21+
22+
for ac in $available_chunks; do
23+
if [[ ! "${required_chunks[*]}" =~ ${ac} ]]; then
24+
dazzle project ignore "$ac"
25+
fi
26+
done
27+
}
28+
29+
function get_refs() {
30+
local ref=$1
31+
echo "$ref"
32+
33+
refs="$(yq e '.combiner.combinations[] | select (.name=="'"$ref"'") | .ref[]' dazzle.yaml)"
34+
if [[ -z "$refs" ]]; then
35+
return
36+
fi
37+
38+
for ref in $refs; do
39+
get_refs "$ref"
40+
done
41+
}
42+
43+
function get_chunks() {
44+
# shellcheck disable=SC2068
45+
for ref in $@; do
46+
chunks=$(yq e '.combiner.combinations[] | select (.name=="'"$ref"'") | .chunks[]' dazzle.yaml)
47+
echo "$chunks"
48+
done
49+
}
50+
51+
REPO=localhost:5000/dazzle
52+
53+
save_original
54+
55+
if [ -n "${1}" ]; then
56+
build_combination "$1"
57+
fi
58+
59+
# First, build chunks without hashes
60+
dazzle build $REPO -v --chunked-without-hash
61+
# Second, build again, but with hashes
62+
dazzle build $REPO -v
63+
64+
# Third, create combinations of chunks
65+
if [[ -n "${1}" ]]; then
66+
dazzle combine $REPO --combination "$1" -v
67+
else
68+
dazzle combine $REPO --all -v
69+
fi

build-common.sh

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#! /bin/bash
2+
3+
YELLOW=$(tput setaf 3)
4+
readonly YELLOW
5+
NC=$(tput sgr0)
6+
readonly NC
7+
8+
readonly BACKUP_FILE=".dazzle.yaml.orig"
9+
readonly ORIGINAL_FILE="dazzle.yaml"
10+
11+
function save_original() {
12+
if [ ! -f ${BACKUP_FILE} ]; then
13+
echo "${YELLOW}Creating a backup of ${ORIGINAL_FILE} as it does not exist yet...${NC}" && cp ${ORIGINAL_FILE} ${BACKUP_FILE}
14+
fi
15+
}
16+
17+
function restore_original() {
18+
echo "${YELLOW}Restoring backup file ${BACKUP_FILE} to original file ${ORIGINAL_FILE}${NC}"
19+
cp ${BACKUP_FILE} ${ORIGINAL_FILE}
20+
}
21+
22+
function ctrl_c() {
23+
echo "${YELLOW}** Trapped CTRL-C${NC}"
24+
restore_original
25+
}
26+
27+
function get_available_chunks() {
28+
local chunk_defs
29+
chunk_defs=$(ls chunks)
30+
for chunk in $chunk_defs; do
31+
local chunkYaml="chunks/${chunk}/chunk.yaml"
32+
if [[ -f "$chunkYaml" ]]; then
33+
variants=$(yq e '.variants[].name' "$chunkYaml")
34+
for variant in $variants; do
35+
echo "$chunk:$variant"
36+
done
37+
else
38+
echo "$chunk"
39+
fi
40+
41+
done
42+
}

0 commit comments

Comments
 (0)