Skip to content

Commit

Permalink
Add new scripts for building images
Browse files Browse the repository at this point in the history
  • Loading branch information
Furisto committed Mar 15, 2022
1 parent 6ca514b commit 0d44757
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ tasks:
command: |
gp await-port 5000
REPO=localhost:5000/dazzle
echo "To build specific chunks and combinations 'time ./dazzle-up.sh -c chunk1 -c chunk2 -n combo'"
echo "To build all the chunks and combinations 'time ./dazzle-up.sh'"
echo "To build specific chunks and combine them 'time ./build-chunk.sh -c chunk1 -c chunk2 -n combo'"
echo "To build all the chunks and combinations 'time ./build-all.sh'"
echo "To build a specific combination 'time ./build-combo.sh combo'"
echo "To list image chunks 'dazzle project image-name $REPO'"
echo "To list hashes for image chunks 'dazzle project hash $REPO'"
echo "To print the combined image maniest 'dazzle project manifest $REPO'"
Expand Down
16 changes: 12 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Here is a list of dependencies and tools:

## Locally

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.
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.

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

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

### Build Specific Chunks

Often, you would want to test only the chunks that you modify. You can do that by using the `-c` flag.
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`.

```console
./dazzle-up.sh -c lang-c -c dep-cacert-update -n mychangecombo
./build-chunk.sh -c lang-c -c dep-cacert-update -n mychangecombo
```

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`.

### Build Specific Combination
Sometimes you only want to build one specification combination e.g. the postgresql or the go image. You can do that with

```console
./build-combo.sh <comboName e.g. go>
```
This will build all chunks that are referenced by the go combination and then combine them to create the go image.

### Build All Chunks

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

```bash
./dazzle-up.sh
./build-all.sh
```

> **NOTE:** Building images locally consumes a lot of resources and is often slow.
Expand Down
10 changes: 10 additions & 0 deletions build-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -euo pipefail

REPO=localhost:5000/dazzle
# First, build chunks without hashes
dazzle build $REPO -v --chunked-without-hash
# Second, build again, but with hashes
dazzle build $REPO -v
# Third, create combinations of chunks
dazzle combine $REPO --all -v
6 changes: 3 additions & 3 deletions dazzle-up.sh → build-chunk.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ readonly REPO="localhost:5000/dazzle"

function usage() {
cat <<EOF
Usage: ./dazzle-up.sh [OPTION]...
Example: ./dazzle-up.sh -c lang-c -c dep-cacert-update -n mychangecombo
Usage: ./build-chunk.sh [OPTION]...
Example: ./build-chunk.sh -c lang-c -c dep-cacert-update -n mychangecombo
Options for build:
-h, --help Display this help
-c, --chunk Chunk to build, You can build multiple chunks: -c chunk1 -c chunk2. If no chunks are supplied then build using existing config
Expand Down Expand Up @@ -85,7 +85,7 @@ while true; do
;;
*)
echo Error: unknown flag "$1"
echo "${YELLOW}Run 'dazzle-up.sh --help' for usage.${NC}"
echo "${YELLOW}Run 'build-chunk.sh --help' for usage.${NC}"
exit 1
;;
esac
Expand Down
104 changes: 104 additions & 0 deletions build-combo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#! /bin/bash
set -eo pipefail
trap ctrl_c INT

readonly YELLOW=$(tput setaf 3)
readonly NC=$(tput sgr0)

readonly BACKUP_FILE=".dazzle.yaml.orig"
readonly ORIGINAL_FILE="dazzle.yaml"

function save_original {
if [ ! -f ${BACKUP_FILE} ]; then
echo "${YELLOW}Creating a backup of ${ORIGINAL_FILE} as it does not exist yet...${NC}" && cp ${ORIGINAL_FILE} ${BACKUP_FILE}
fi
}

function restore_original() {
echo "${YELLOW}Restoring backup file ${BACKUP_FILE} to original file ${ORIGINAL_FILE}${NC}"
cp ${BACKUP_FILE} ${ORIGINAL_FILE}
}

function ctrl_c() {
echo "${YELLOW}** Trapped CTRL-C${NC}"
restore_original
}

function build_combination {
combination=$1

local exists="$(yq e '.combiner.combinations[] | select (.name=="'"$combination"'")' dazzle.yaml)"
if [[ -z "$exists" ]]; then
echo "combination is not defined"
exit 1
fi

refs=$(get_refs "$combination")
requiredChunks=$(get_chunks "$refs" | sort | uniq)
availableChunks=$(get_available_chunks)

for ac in $availableChunks; do
if [[ ! "${requiredChunks[*]}" =~ "${ac}" ]]; then
dazzle project ignore "$ac"
fi
done
}

function get_refs {
local ref=$1
echo "$ref"

refs="$(yq e '.combiner.combinations[] | select (.name=="'"$ref"'") | .ref[]' dazzle.yaml)"
if [[ -z "$refs" ]]; then
return
fi

for ref in $refs; do
get_refs "$ref"
done
}

function get_chunks {
for ref in $@; do
chunks=$(yq e '.combiner.combinations[] | select (.name=="'"$ref"'") | .chunks[]' dazzle.yaml)
echo "$chunks"
done
}

function get_available_chunks {
local chunk_defs=$(ls chunks)
for chunk in $chunk_defs;do
local chunkYaml="chunks/${chunk}/chunk.yaml"
if [[ -f "$chunkYaml" ]]; then
variants=$(yq e '.variants[].name' "$chunkYaml" )
for variant in $variants; do
echo "$chunk:$variant"
done
else
echo "$chunk"
fi

done
}

REPO=localhost:5000/dazzle

save_original

if [ -n "${1}" ]; then
build_combination "$1"
fi

# First, build chunks without hashes
dazzle build $REPO -v --chunked-without-hash
# Second, build again, but with hashes
dazzle build $REPO -v

# Third, create combinations of chunks
if [[ -n "${1}" ]]; then
dazzle combine $REPO --combination "$1" -v
else
dazzle combine $REPO --all -v
fi

restore_original

0 comments on commit 0d44757

Please sign in to comment.