This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.sh
executable file
·164 lines (140 loc) · 5.56 KB
/
build.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! /bin/bash
set -e
#
# A little script I use to run the Packer build because I like to have comments in
# my JSON (documenting what everything is). But, that's not allowed:
#
# https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaGSr
#
# The script prefers to use `strip-json-comments` but will still work if there is a
# JSON artifact from an earlier build still around on the file system.
#
if [ "$DEBUG" = true ]; then
PACKER_LOG_VALUE=true
fi
# Test to make sure we have the external variables we need
if [ ! -f vars.json ]; then
cp example-vars.json vars.json
echo " Please edit the project's vars.json file before running this script"
echo " Leave the _password vars blank if you want them to be autogenerated"
exit 1
fi
function check_builder {
if [ "$1" == "amazon-ebs" ] || [ "$1" == "docker" ] || [ "$1" == "digitalocean" ]; then
if [[ "$BUILDER" != *only* ]]; then
BUILDER="-only=$1"
else
BUILDER="$BUILDER,$1"
fi
else
echo " The requested Packer.io builder \"$1\" is not yet supported"
echo " Supported builders: 'amazon-ebs', 'docker', and 'digitalocean'"
exit 1
fi
}
# Configure a particular Packer.io builder, if desired
if [ ! -z "$1" ]; then
if [ "$1" != "validate" ]; then
OIFS="$IFS"
IFS=','
for BUILDER_VALUE in $1; do
check_builder "$BUILDER_VALUE"
done
IFS="$OIFS"
else
BUILDER="skip-build"
fi
fi
# Turn the URL for this git repository into a URL more fit for human consumption
function humanize-repo-url {
if hash git 2>/dev/null; then
if [ -f ".git/config" ] && [ `cat .git/config |grep -c "\[remote"` -gt 1 ]; then
GIT_REPO_URL=$(git config --get remote.origin.url)
if [[ $GIT_REPO_URL == git@* ]]; then
GIT_REPO_URL="${GIT_REPO_URL/git@/https:\/\/}"
fi
if [[ $GIT_REPO_URL == *.git ]]; then
GIT_REPO_URL="${GIT_REPO_URL/\.git/}"
fi
GIT_REPO_URL="${GIT_REPO_URL/github.com:/github.com/}"
fi
fi
}
# A temporary workaround until I get around to writing a docker-fig post-processor for Packer
function extract_from_json {
export ${1}=`grep -Po "\"${2}\": ?\".*\",?" ${3}.json | sed "s/\"${2}\": \"//" | tr -d "\","`
}
# The main work of the script
function build_graphite {
if [ "$BUILDER" == "skip-build" ]; then
packer validate -var-file=vars.json graphite.json
else
packer validate $BUILDER -var-file=vars.json graphite.json
fi
# Looks to see if the vars file has any empty password variables; creates passwords if needed
while read LINE; do
if [ ! -z "$LINE" ]; then
REPLACEMENT="_password\": \"`openssl rand -base64 12`\""
# If the passwords file doesn't exist, look for empty passwords to fill with new values
if [ ! -f .passwords ]; then
PASSWORD_PATTERN="_password\": \"\""
# If the passwords file exists, we're okay with autogenerating new passwords each time
else
PASSWORD_PATTERN="_password\": \"*\""
fi
NEWLINE="${LINE/$PASSWORD_PATTERN/$REPLACEMENT}"
# If we've autogenerated a password, make a note so we will do it again with each build
if [ "$NEWLINE" != "$LINE" ]; then
touch .passwords
fi
echo $NEWLINE
fi
done <vars.json > vars.json.new
mv vars.json.new vars.json
# If we're not running in CI, use vars file; else, use ENV vars
if [ -z "$CONTINUOUS_INTEGRATION" ]; then
humanize-repo-url
if [ ! -z "$GIT_REPO_URL" ]; then REPO_URL_VAR="--var packer_graphite_repo=$GIT_REPO_URL"; fi
# If we're running in debug mode, inform which repository we're building from
if [ "$DEBUG" = true ] && [ ! -z "$GIT_REPO_URL" ]; then
echo "Running new build checked out from $GIT_REPO_URL"
fi
if [ "$BUILDER" != "skip-build" ]; then
PACKER_LOG=$PACKER_LOG_VALUE packer build $BUILDER $REPO_URL_VAR -var-file=vars.json graphite.json
# If we're running a docker build, create a fig.yml file (this is a workaround until a docker-fig post-processor exists)
if [[ "$BUILDER" == *docker* ]]; then
extract_from_json "DOCKER_USER" "docker_user" "vars"
extract_from_json "GRAPHITE_VERSION" "packer_graphite_version" "graphite"
# Write out the simple fig.yml file
if [ ! -z "$DOCKER_USER" ]; then
echo "graphite:" > fig.yml
echo " image: $DOCKER_USER/packer-graphite:$GRAPHITE_VERSION" >> fig.yml
echo " command: sudo /usr/bin/supervisord -c /etc/supervisor/supervisord.conf" >> fig.yml
echo " ports:" >> fig.yml
echo " - 80:80" >> fig.yml
echo " - 2003:2003" >> fig.yml
else
echo "Fig configuration not auto-generated because docker_user doesn't seem to be configured"
fi
fi
fi
else
echo "Running within Travis, a continuous integration server [Builder: $BUILDER]"
GRAPHITE_ADMIN_PASSWORD=`openssl rand -base64 12`
GRAPHITE_SECRET_KEY=`openssl rand -base64 12`
PACKER_LOG=$PACKER_LOG_VALUE packer -machine-readable build $BUILDER \
-var "graphite_admin_password=${GRAPHITE_ADMIN_PASSWORD}" \
-var "graphite_secret_key_password=${GRAPHITE_SECRET_KEY}" \
graphite.json | tee packer.log
fi
}
# If we have strip-json-comments installed, use JSON source file; else use derivative
if hash strip-json-comments 2>/dev/null; then
strip-json-comments packer-graphite.json > graphite.json
build_graphite
elif [ -f graphite.json ]; then
build_graphite
else
echo " strip-json-comments needs to be installed to generate the graphite.json file"
echo " For installation instructions, see https://github.com/sindresorhus/strip-json-comments"
fi