forked from kristofferahl/terraform-provider-healthchecksio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run
executable file
·224 lines (179 loc) · 6.59 KB
/
run
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
#!/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}"
}
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
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'
echo "${provider_version:?}" >./provider-version
}
run_build() {
log INF 'Cross-compiling using goreleaser...'
[[ "${1:-}" == '--sign' ]] && ensure_gpg_fingerprint
if [[ "${GPG_FINGERPRINT}" == '' ]]; then
${GORELEASER_BIN:?} --snapshot --skip-publish --skip-sign --rm-dist
else
${GORELEASER_BIN:?} --snapshot --skip-publish --skip-validate --rm-dist
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:?} --rm-dist
}
run_examples() {
[ "${HEALTHCHECKSIO_API_KEY:?'Environment variable "HEALTHCHECKSIO_API_KEY" is required'}" ]
local example="${1:-}"
log INF "Running example(s)..."
for d in ${examples_dir:?}/**; do
if [[ -d "${d}" ]]; then
[[ "$(basename "${d:?}")" != "${example}" && "${example}" != "" ]] && continue
log INF "Example: $(basename "${d:?}") (${d:?})"
install_plugin "${d:?}"
cd "${d:?}" || exit 1
generate_provider_config_file
terraform init
terraform apply
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
install_plugin "${d:?}"
cd "${d:?}" || exit 1
generate_provider_config_file
terraform init
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
git config core.hooksPath .githooks
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:?}" "$@" ;;
examples) run_examples "$@" ;;
test) run_test "$@" ;;
test-integration) run_test_integration "$@" ;;
prepare-release) run_prepare_release "$@" ;;
release) run_release "$@" ;;
help) run_help ;;
*) run_help ;;
esac
}
main "$@"