-
Notifications
You must be signed in to change notification settings - Fork 152
/
herokuish.bash
152 lines (132 loc) · 3.77 KB
/
herokuish.bash
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
if [[ "${BASH_VERSINFO[0]}" -lt "4" ]]; then
echo "!! Your system Bash is out of date: $BASH_VERSION"
echo "!! Please upgrade to Bash 4 or greater."
exit 2
fi
readonly app_path="${APP_PATH:-/app}"
readonly env_path="${ENV_PATH:-/tmp/env}"
readonly build_path="${BUILD_PATH:-/tmp/build}"
readonly cache_path="${CACHE_PATH:-/tmp/cache}"
readonly import_path="${IMPORT_PATH:-/tmp/app}"
readonly buildpack_path="${BUILDPACK_PATH:-/tmp/buildpacks}"
declare unprivileged_user="$USER"
declare unprivileged_group="${USER/nobody/nogroup}"
export PS1='\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]'
ensure-paths() {
mkdir -p \
"$app_path" \
"$env_path" \
"$build_path" \
"$cache_path" \
"$buildpack_path"
}
paths() {
declare desc="Shows path settings"
printf "%-32s # %s\n" \
"APP_PATH=$app_path" "Application path during runtime" \
"ENV_PATH=$env_path" "Path to files for defining base environment" \
"BUILD_PATH=$build_path" "Working directory during builds" \
"CACHE_PATH=$cache_path" "Buildpack cache location" \
"IMPORT_PATH=$import_path" "Mounted path to copy to app path" \
"BUILDPACK_PATH=$buildpack_path" "Path to installed buildpacks"
}
version() {
declare desc="Show version and supported version info"
echo "herokuish: ${HEROKUISH_VERSION:-dev}"
echo "buildpacks:"
asset-cat include/buildpacks.txt | sed -e 's/.*heroku\///' -e 's/.*dokku\///' | xargs printf " %-26s %s\n"
}
title() {
echo $'\e[1G----->' "$@"
}
indent() {
while read -r line; do
if [[ "$line" == --* ]] || [[ "$line" == ==* ]]; then
# shellcheck disable=SC2086
echo $'\e[1G'$line
else
echo $'\e[1G ' "$line"
fi
done
}
unprivileged() {
setuidgid "$unprivileged_user" "$@"
}
detect-unprivileged() {
unprivileged_user="$(stat -c %U "$app_path")"
unprivileged_group="${unprivileged_user/nobody/nogroup}"
}
randomize-unprivileged() {
local userid="$((RANDOM + 1000))"
local username="u${userid}"
addgroup --quiet --gid "$userid" "$username"
adduser \
--shell /bin/bash \
--disabled-password \
--force-badname \
--no-create-home \
--uid "$userid" \
--gid "$userid" \
--gecos '' \
--quiet \
--home "$app_path" \
"$username"
unprivileged_user="$username"
unprivileged_group="$username"
}
herokuish-test() {
declare desc="Test running an app through Herokuish"
declare path="${1:-/}" expected="$2"
export PORT=5678
echo "::: BUILDING APP :::"
buildpack-build
echo "::: STARTING WEB :::"
procfile-start web &
for retry in $(seq 1 30); do
sleep 1
if ! nc -z -w 5 localhost "$PORT"; then
echo "::: RETRYING LISTENER ($retry) :::"
else
echo "::: FOUND LISTENER :::" && break
fi
done
echo "::: CHECKING APP :::"
local output
output="$(curl --fail --retry 10 --retry-delay 2 -v -s "localhost:${PORT}${path}")"
if [[ "$expected" ]]; then
sleep 1
echo "::: APP OUTPUT :::"
echo -e "$output"
if [[ "$output" != "$expected" ]]; then
echo "::: TEST FAILED :::"
exit 2
fi
fi
echo "::: TEST FINISHED :::"
}
main() {
set -eo pipefail
[[ "$TRACE" ]] && set -x
cmd-export paths
cmd-export version
cmd-export herokuish-test test
cmd-export-ns buildpack "Use and install buildpacks"
cmd-export buildpack-build
cmd-export buildpack-install
cmd-export buildpack-list
cmd-export buildpack-test
cmd-export-ns slug "Manage application slugs"
cmd-export slug-import
cmd-export slug-generate
cmd-export slug-export
cmd-export-ns procfile "Use Procfiles and run app commands"
cmd-export procfile-start
cmd-export procfile-exec
cmd-export procfile-parse
case "$SELF" in
/start) procfile-start "$@" ;;
/exec) procfile-exec "$@" ;;
/build) buildpack-build ;;
*) cmd-ns "" "$@" ;;
esac
}