-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrun
More file actions
executable file
·286 lines (231 loc) · 8.28 KB
/
Copy pathrun
File metadata and controls
executable file
·286 lines (231 loc) · 8.28 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env bash
set -euo pipefail
export TERRAFORM_VERSION='0.13.5'
export GORELEASER_BIN="${GORELEASER_BIN:-goreleaser}"
declare -r plugins_dir=".terraform/plugins"
declare -r dist_dir="${PWD:?}/dist"
declare -r examples_dir="${PWD:?}/examples"
declare -r test_dir="${PWD:?}/test"
function log() {
local -r level="${1}"
local -r message="$2"
local -r timestamp=$(date +"%Y-%m-%d %H:%M:%S")
local -r script_name="$(basename "$0")"
echo >&2 -e "${timestamp} [\033[1;94m${level}\033[0m] [$script_name] ${message}"
}
run_setup() {
install_goreleaser
}
install_goreleaser() {
export TMPDIR
export VERSION
TMPDIR=$(mktemp -d)
VERSION=v2.7.0
if [[ -f "${GORELEASER_BIN:?}" ]]; then
if ${GORELEASER_BIN:?} --version | grep -q "${VERSION:?}"; then
log INF "Goreleaser already installed"
return 0
else
log INF "Goreleaser already installed but version mismatch"
fi
fi
echo "Using temporary directory: ${TMPDIR:?}"
curl -sL https://git.io/goreleaser | bash -s -- --version
mkdir -p "./bin" &>/dev/null || true
mv "${TMPDIR:?}/goreleaser" "${GORELEASER_BIN:?}"
}
install_plugin() {
local destination_dir="${1:?'A destination directory is required'}"
local full_plugin_dir="${destination_dir:?}/${plugins_dir:?}/registry.terraform.io/kristofferahl/healthchecksio/${PROVIDER_VERSION:?}"
log DEB "Installing plugin (src=${dist_dir:?} dest=${destination_dir:?})"
[[ ! -d "${dist_dir:?}" ]] && log ERR "Source directory not found (${dist_dir:?})! Please run build and try again..." && exit 1
rm -rf "${destination_dir:?}/${plugins_dir:?}" &>/dev/null || true
mkdir -p "${full_plugin_dir:?}" &>/dev/null || true
cp -R "${dist_dir:?}/." "${full_plugin_dir:?}"
# Temporary fix. Issue: https://github.com/goreleaser/goreleaser/issues/1059
local dir_name
for dir in ${full_plugin_dir:?}/*/; do
dir_name="$(basename "${dir:?}")"
dir_name="${dir_name/dist_/}"
mv "${dir}" "${full_plugin_dir:?}/${dir_name:?}"
done
}
generate_provider_config_file() {
[ "${PROVIDER_VERSION:?'Environment variable "PROVIDER_VERSION" is required'}" ]
echo "terraform {
required_providers {
healthchecksio = {
source = \"kristofferahl/healthchecksio\"
version = \"${PROVIDER_VERSION:?}\"
}
}
}
provider \"healthchecksio\" {
# Configuration options
}" >generated.tf
}
export GPG_FINGERPRINT="${GPG_FINGERPRINT:-}"
ensure_gpg_fingerprint() {
if [[ "${GPG_FINGERPRINT}" == '' && ${CI:-false} == false ]]; then
log INF 'GPG_FINGERPRINT is not set in your environment. Please provide your gpg email adress to have it set for you!'
echo -n ' GPG Email: '
read -r GPG_FINGERPRINT
fi
[[ "${GPG_FINGERPRINT}" == '' ]] && log ERR 'Running this command requires the GPG_FINGERPRINT environment variable to be set. Exiting!' && exit 1
return 0
}
run_help() {
echo "usage: ./run <command> [<arg1> <arg2> ...]
commands:
build Build the provider
dev Run a command in dev mode (example: ./run dev examples full)
docker Run a command in docker (example: ./run docker test)
examples Run examples
test Run tests
test-integration Run integration tests
prepare-release Prepare for a new release (local only)
release Create a new release of the provider"
}
run_create_provider_version() {
local version="${1:?'A version is required'}"
local snapshot="${2:-false}"
local provider_version="${version:?}"
[[ "${snapshot}" == 'true' && ${provider_version} != *-next ]] && provider_version+='-next'
echo "${provider_version:?}" >./provider-version
}
run_build() {
log INF 'Cross-compiling using goreleaser...'
[[ "${1:-}" == '--sign' ]] && ensure_gpg_fingerprint
if [[ "${GPG_FINGERPRINT}" == '' ]]; then
${GORELEASER_BIN:?} release --snapshot --skip publish --skip sign --clean
else
${GORELEASER_BIN:?} release --snapshot --skip publish --skip validate --clean
fi
}
run_prepare_release() {
log INF 'Listing remote tags'
git ls-remote --tags --sort=version:refname origin 'v*'
declare next_version
echo -n 'Bump version to (semver prefixed with v): '
read -r next_version
if [[ "${next_version}" != '' ]]; then
if [[ "${next_version:?}" == v* ]]; then
log INF "Creating tag ${next_version:?}"
git tag "${next_version:?}" -a -s -m "Release ${next_version:?}"
else
log ERR 'Invalid version (must be prefixed with v)'
fi
else
log ERR 'No version provided (skipping)'
fi
git tag --list 'v*' -n --sort=version:refname | cat
log INF "When you're ready to release, simply use the following commands:"
log INF "git push --follow-tags"
}
run_release() {
log INF 'Creating a release using goreleaser...'
ensure_gpg_fingerprint
${GORELEASER_BIN:?} release --clean
}
run_dev() {
log WAR 'Running in dev mode...'
log INF "Building provider..."
go build .
log INF "Generating provider install instructions..."
echo "provider_installation {
dev_overrides {
\"registry.terraform.io/kristofferahl/healthchecksio\" = \"${PWD:?}\"
}
direct {}
}" >dev.tfrc
log INF "Setting up environment..."
export TF_CLI_CONFIG_FILE=${PWD:?}/dev.tfrc
export SKIP_INIT=true
local command="${1:-}"
shift || true
case "${command}" in
examples) run_examples "$@" ;;
test-integration) run_test_integration "$@" ;;
*) log ERR "Unknown dev command: ${command}" && exit 1 ;;
esac
}
run_examples() {
[ "${HEALTHCHECKSIO_API_KEY:?'Environment variable "HEALTHCHECKSIO_API_KEY" is required'}" ]
local example="${1:-}"
shift
tf_args=("$@")
if [[ ${#tf_args[@]} -lt 1 ]]; then
tf_args+=('apply')
fi
log INF "Running example(s)... terraform ${tf_args[*]}"
for d in ${examples_dir:?}/**; do
if [[ -d "${d}" ]]; then
[[ "$(basename "${d:?}")" != "${example}" && "${example}" != "" ]] && continue
[[ "${example}" == "" && "$(basename "${d:?}")" == "self-hosted" ]] && continue
log INF "Example: $(basename "${d:?}") (${d:?})"
cd "${d:?}" || exit 1
[[ "$(basename "${d:?}")" != "self-hosted" ]] && generate_provider_config_file
[[ ${SKIP_INIT:-false} == false ]] && terraform init -upgrade
terraform "${tf_args[@]:?}"
fi
done
}
run_test() {
log INF 'Running unit tests...'
go test healthchecksio/**
}
run_test_integration() {
[ "${HEALTHCHECKSIO_API_KEY:?'Environment variable "HEALTHCHECKSIO_API_KEY" is required'}" ]
log INF "Running integration tests ($(terraform version | head -n 1))..."
for d in ${test_dir:?}/**; do
if [[ -d "${d}" ]]; then
log DEB "Test: $(basename "${d:?}") (${d:?})"
rm -rf "${d:?}/terraform.tfstate" || true
rm -rf "${d:?}/terraform.tfstate.backup" || true
cd "${d:?}" || exit 1
generate_provider_config_file
[[ ${SKIP_INIT:-false} == false ]] && terraform init -upgrade
terraform apply -auto-approve
if [[ ${CI:-false} != true ]]; then
echo "PRESS ENTER TO CONTINUE..."
read -r
fi
terraform destroy -auto-approve
fi
done
}
run_docker() {
local v="${1:?'A terraform version is required'}"
shift
log INF "Building test image for terraform v${v:?}"
docker build -t "terraform-provider-healthchecksio:${v:?}-test" --build-arg "TERRAFORM_VERSION=${v:?}" -f "${PWD:?}/test/Dockerfile.test" .
[[ "${1:-}" == 'docker' ]] && log ERR 'Unable to run docker in docker!' && exit 1
log INF "Running in docker... (terraform v${v:?})"
local tty='-it'
[[ ${CI:-false} == true ]] && tty=''
docker run --rm ${tty:-} -e CI -e HEALTHCHECKSIO_API_KEY -e GITHUB_TOKEN -e GPG_FINGERPRINT -v "${PWD:?}:/work/" "terraform-provider-healthchecksio:${v:?}-test" -c "./run $*"
}
main() {
local command="${1:-}"
shift || true
if [[ ${CI:-false} != true ]]; then
git config core.hooksPath .githooks
fi
export PROVIDER_VERSION
[[ -f ./provider-version ]] && PROVIDER_VERSION="$(cat ./provider-version)"
case "${command}" in
create-provider-version) run_create_provider_version "$@" ;;
build) run_build "$@" ;;
docker) run_docker "${TERRAFORM_VERSION:?}" "$@" ;;
dev) run_dev "$@" ;;
examples) run_examples "$@" ;;
test) run_test "$@" ;;
test-integration) run_test_integration "$@" ;;
prepare-release) run_prepare_release "$@" ;;
release) run_release "$@" ;;
setup) run_setup "$@" ;;
help) run_help ;;
*) run_help ;;
esac
}
main "$@"