forked from solo-io/packer-plugin-arm-image
-
Notifications
You must be signed in to change notification settings - Fork 2
/
provision-build-image.sh
51 lines (46 loc) · 1.72 KB
/
provision-build-image.sh
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
#!/bin/bash
#
# @script provision.sh
# @description provisioning script that builds environment for
# https://github.com/solo-io/packer-builder-arm-image
#
# By default, sets up environment, builds the plugin, and image
##
set -x
set -e
# Set to false to disable auto building
export PACKERFILE=${PACKERFILE:-samples/raspbian_golang.json}
PLUGIN_DIR=${PLUGIN_DIR:-/root/.packer.d/plugins}
sudo mkdir -p $PLUGIN_DIR
sudo cp /vagrant/packer-builder-arm-image "$PLUGIN_DIR/"
# Now build the image
if sudo [[ ! -f "$PLUGIN_DIR/packer-builder-arm-image" ]]; then {
echo "Error: Plugin not found. Retry build."
exit
} else {
echo "Attempting to build image"
PACKER_LOG=$(mktemp)
# If there is a custom json, try that one
# otherwise go with the default
if [[ -f /vagrant/${PACKERFILE} ]]; then {
sudo packer build /vagrant/${PACKERFILE} | tee ${PACKER_LOG}
} else {
if [[ -f $GOPATH/src/github.com/solo-io/packer-builder-arm-image/${PACKERFILE} ]]; then {
sudo packer build $GOPATH/src/github.com/solo-io/packer-builder-arm-image/${PACKERFILE} | tee ${PACKER_LOG}
} else {
echo "Error: packer build definition ${PACKERFILE} not found."
exit
}; fi
}; fi
BUILD_NAME=$(grep -Po "(?<=Build ').*(?=' finished.)" ${PACKER_LOG})
IMAGE_PATH=$(grep -Po "(?<=--> ${BUILD_NAME}: ).*" ${PACKER_LOG})
rm -f ${PACKER_LOG}
# If the new image is there, copy it out or throw an error
if [[ -f ${HOME}/${IMAGE_PATH} ]]; then {
sudo cp ${HOME}/${IMAGE_PATH} \
/vagrant/${IMAGE_PATH%/image}.img
} else {
echo "Error: Unable to find build artifact."
exit
}; fi
}; fi