Skip to content

Commit 69431c6

Browse files
Furistokylos101
andauthored
Add new build scripts (#769)
* Update yq * Add new scripts for building images * Remove combo from build-chunk.sh * Update docs for build-chunk Co-authored-by: Kyle Brennan <[email protected]>
1 parent a9c5ded commit 69431c6

7 files changed

+159
-46
lines changed

.gitpod.Dockerfile

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ ENV BUILDKIT_FILENAME=buildkit-v${BUILDKIT_VERSION}.linux-amd64.tar.gz
88
USER root
99

1010
# Install dazzle, buildkit and pre-commit
11-
RUN cd /usr \
12-
&& curl -sSL https://github.com/moby/buildkit/releases/download/v${BUILDKIT_VERSION}/${BUILDKIT_FILENAME} | tar -xvz \
13-
&& curl -sSL https://github.com/gitpod-io/dazzle/releases/download/v0.1.8/dazzle_0.1.8_Linux_x86_64.tar.gz | tar -xvz dazzle \
11+
RUN curl -sSL https://github.com/moby/buildkit/releases/download/v${BUILDKIT_VERSION}/${BUILDKIT_FILENAME} | tar -xvz -C /usr \
12+
&& curl -sSL https://github.com/gitpod-io/dazzle/releases/download/v0.1.8/dazzle_0.1.8_Linux_x86_64.tar.gz | tar -xvz -C /usr/local/bin \
1413
&& curl -sSL https://github.com/mvdan/sh/releases/download/v3.4.2/shfmt_v3.4.2_linux_amd64 -o /usr/bin/shfmt \
1514
&& chmod +x /usr/bin/shfmt \
1615
&& install-packages shellcheck \
17-
&& sudo pip3 install pre-commit
16+
&& sudo pip3 install pre-commit \
17+
&& 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

+17-5
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,32 @@ 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.
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
5252
```
5353

54-
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`.
54+
Above command will build only chunks `lang-c` and `dep-cacert-update`.
55+
56+
The next step, is to test your changes with [./build-combo](#build-specific-combination).
57+
58+
### Build Specific Combination
59+
60+
Sometimes you only want to build one specific combination e.g. the `postgresql` or the `go` image. You can do that with
61+
62+
```console
63+
./build-combo.sh <comboName> e.g. ./build-combo.sh postgresql
64+
```
65+
66+
This will build all chunks that are referenced by the `go` combination and then combine them to create the `go` image.
5567

5668
### Build All Chunks
5769

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

6072
```bash
61-
./dazzle-up.sh
73+
./build-all.sh
6274
```
6375

6476
> **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
+14-35
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,25 @@
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
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
21-
-n, --name Combination name, by default a combination name 'default' is created. This flag only works when -c flag is specified
2218
EOF
2319
}
2420

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-
35-
function build_and_combine() {
36-
dazzle build ${REPO} -v --chunked-without-hash && dazzle build ${REPO} -v && dazzle combine ${REPO} --all -v
21+
function build_chunks() {
22+
dazzle build ${REPO} -v --chunked-without-hash && dazzle build ${REPO} -v
3723
}
3824

3925
function extract_variants() {
@@ -54,10 +40,9 @@ function extract_variants() {
5440
ARGS=$(getopt -o h,:c:n: --long help:,chunk:,name: -- "$@")
5541
eval set -- "${ARGS}"
5642

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

5945
CHUNKS=""
60-
COMBINATION="default"
6146

6247
while true; do
6348
case "$1" in
@@ -75,17 +60,13 @@ while true; do
7560
fi
7661
shift 2
7762
;;
78-
-n | --name)
79-
COMBINATION="$2"
80-
shift 2
81-
;;
8263
--)
8364
shift
8465
break
8566
;;
8667
*)
8768
echo Error: unknown flag "$1"
88-
echo "${YELLOW}Run 'dazzle-up.sh --help' for usage.${NC}"
69+
echo "${YELLOW}Run 'build-chunk.sh --help' for usage.${NC}"
8970
exit 1
9071
;;
9172
esac
@@ -96,9 +77,9 @@ if [[ -z "${CHUNKS[*]}" ]]; then
9677
echo "${YELLOW}No chunks specified, will build using the existing ${ORIGINAL_FILE}${NC}"
9778
else
9879
# 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}
80+
[ -f "${ORIGINAL_FILE}" ] && echo "${YELLOW}Deleting ${ORIGINAL_FILE}, will produce a new config with supplied arguments${NC}" && rm "${ORIGINAL_FILE}"
10081

101-
for CN in ${AVAILABLE_CHUNKS}; do
82+
for CN in $(get_available_chunks); do
10283
if [[ ! ${CHUNKS[*]} =~ ${CN} ]]; then
10384
dazzle project ignore "${CN}"
10485
else
@@ -109,12 +90,10 @@ else
10990
fi
11091
done
11192

112-
dazzle project add-combination "${COMBINATION}" "${CHUNKS_TO_COMBINE[@]}"
93+
# dazzle project add-combination "${COMBINATION}" "${CHUNKS_TO_COMBINE[@]}"
11394
fi
11495

115-
build_and_combine
96+
build_chunks
11697

11798
echo "${YELLOW}Saving dazzle config used to generate build in ${TEMP_FILE}${NC}"
118-
cp ${ORIGINAL_FILE} ${TEMP_FILE}
119-
120-
restore_original
99+
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)