This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
templatectl
executable file
·188 lines (158 loc) · 3.45 KB
/
templatectl
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
#!/bin/bash
set -e
usage() {
cat <<-EOF
Usage: $(basename "$0") template-name (test|run)
test
Run the check-environment scripts on the selected workspace.
If specified by 'cloud9/ws-ruby:version' it will use the specified version,
otherwise it will attempt to build and test the one on the filesystem when
used with 'ws-ruby'.
run
Run the workspace with a local instance of cloud9 for testing
EOF
}
err() {
echo "$@" >&2
}
dockerfile_exists() {
[[ -d "$(dirname "$0")/$TEMPLATE" ]]
}
docker_run() {
local IMAGE
IMAGE=$1
shift || :
if [[ $D9 ]]; then
$D9 dry --no-tty "$IMAGE" "$@"
else
docker run --rm -u=ubuntu -v $BINDIR:/mnt/shared/bin -w=/home/ubuntu -e HOME=/home/ubuntu $IMAGE bash -lc "$@"
fi
}
check_run_as_root() {
err "Checking $IMAGE runs as root..."
if ! [[ $(docker run --rm $IMAGE whoami) == root ]]; then
err "Error: d9 check $IMAGE failed - Container is not running as root by default!"
return 1
fi
}
build_template() {
docker build -t cloud9/$TEMPLATE --rm=true ./$TEMPLATE
}
image_exists() {
docker images | grep -q ${IMAGE%%:*}
}
list_check_environment_scripts() {
docker_run $IMAGE "ls /.check-environment"
}
check_environment() {
local check_environment_scripts
check_environment_scripts=$(list_check_environment_scripts)
for script in $check_environment_scripts; do
if ! OUT=$(docker_run $IMAGE /.check-environment/$script 2>&1); then
err "$OUT"
err "Error: templatectl test $IMAGE failed on $NAME checks"
return 1
fi
done
echo "Successfully checked $IMAGE"
echo "------------------------------------------------------"
}
clean_template_variable() {
TEMPLATE=${TEMPLATE#cloud9/}
TEMPLATE=${TEMPLATE%:*}
}
test_template() {
if dockerfile_exists; then
build_template
fi
clean_template_variable
if ! image_exists; then
err "Could not find image '$image'. Aborting"
return 1
fi
check_environment
}
update_sdk() {
cat <<-EOF | docker run --rm -i -v $CACHE/c9sdk:/c9sdk $IMAGE bash
if [[ -d /c9sdk/.git ]]; then
git -C /c9sdk fetch
git -C /c9sdk reset --hard origin/master
else
git clone https://github.com/c9/core.git /c9sdk
fi
chown -R ubuntu. /c9sdk
EOF
}
update_sdk_wrapper() {
cat <<-EOF > $CACHE/c9sdk-wrapper
#!/bin/bash
sudo chown ubuntu. /home/ubuntu/.c9
cd /c9-cache/c9sdk
./scripts/install-sdk.sh
# Run SDK
[[ -f ~/.nvm/nvm.sh ]] && . ~/.nvm/nvm.sh
node server.js -p 8080 -a : -w /home/ubuntu/workspace --listen 0.0.0.0
EOF
}
run_sdk_wrapper() {
docker run -it \
--rm \
-p 8080:8080 \
-v $CACHE:/c9-cache \
-v $CACHE/.c9:/home/ubuntu/.c9 \
--user ubuntu \
$IMAGE bash /c9-cache/c9sdk-wrapper
}
run_template() {
if dockerfile_exists; then
build_template
fi
clean_template_variable
update_sdk
update_sdk_wrapper
run_sdk_wrapper
}
detect_d9() {
D9=${D9:-$(which d9 2>/dev/null)} || :
if [[ $D9 ]]; then
BINDIR=/var/lib/docker/shared-space/bin
SUDO='sudo'
fi
}
cleanup() {
rm -rf $TEMP
}
cd "$(dirname "$0")"
if [[ $1 =~ ^--?h(elp)?$ ]] || [[ ! $2 ]]; then
usage
exit 1
fi
CACHE="$(readlink -f "$(dirname "$0")")/.templatectl-cache"
TEMP=$(mktemp -d)
BINDIR=$TEMP
SUDO=''
CUSTOM_CHECK_ENVIRONMENT=''
TEMPLATE=$1
IMAGE="cloud9/${1#cloud9/}"
shift
detect_d9
for arg in "$@"; do
case $arg in
--check-environment=*)
CUSTOM_CHECK_ENVIRONMENT="${arg#*=}"
shift
;;
test)
test_template
exit
;;
run)
run_template
exit
;;
*)
usage
exit 1
;;
esac
done