forked from ToyoDAdoubiBackup/WebIDE
-
Notifications
You must be signed in to change notification settings - Fork 40
/
ide.sh
executable file
·299 lines (254 loc) · 5.57 KB
/
ide.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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/bin/bash
BASEDIR=$(cd "$(dirname "$0")"; pwd)
PROG_NAME=$(basename $0)
BACKEND=$BASEDIR/backend
FRONTEND=$BASEDIR/frontend
FRONTEND_WEBJARS=$BASEDIR/frontend-webjars
CONTAINER=webide
valid_last_cmd() {
if [ $? -ne 0 ]; then
exit 1
fi
}
sub_help(){
echo "Usage: $PROG_NAME <subcommand>"
echo ""
echo "Subcommands:"
echo " build [-t tag]"
echo " run [-p port]"
echo " docker [build|run|attach|stop|logs|exec]"
echo ""
echo "For help with each subcommand run:"
echo "$PROG_NAME <subcommand> -h | --help"
echo ""
}
sub_backend() {
bakend_usage() {
echo "Usage: $PROG_NAME backend [clean]"
}
case $1 in
"-h" | "--help")
backend_usage
;;
"clean")
cd $BACKEND
mvn clean
cd $BASEDIR
;;
esac
}
do_build_frontend() {
cd $FRONTEND
echo "building frontend..."
yarn install
yarn run build
valid_last_cmd
cd $FRONTEND_WEBJARS
echo "packing frontend...."
mvn clean install
valid_last_cmd
cd $BASEDIR
}
do_build_backend() {
cd $BACKEND
echo "mvn clean and packaging..."
mvn clean package -Dmaven.test.skip=true
valid_last_cmd
cd $BASEDIR
}
sub_build() {
build_usage() {
echo "Usage: $PROG_NAME build [frontend | backend | run]"
}
case $1 in
"-h" | "--help")
build_usage
;;
"")
do_build_frontend
do_build_backend
;;
"backend")
do_build_backend
;;
"frontend")
do_build_frontend
;;
"run") # build and run
do_build_frontend
do_build_backend
do_run_backend
;;
esac
}
error() {
printf "${RED}ERROR:${NC} %s\n" "${1}"
}
error_exit() {
# echo "---------------------------------------"
# error "!!!"
error "${1}"
# error "!!!"
# echo "---------------------------------------"
exit 1
}
valid_container_exist() {
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if [ $? -eq 1 ]; then
echo "UNKNOWN - container $CONTAINER does not exist."
exit 3
fi
}
assert_container_is_running() {
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if [ $? -eq 1 ]; then
echo "UNKNOWN - $CONTAINER does not exist."
exit 3
fi
if [ "$RUNNING" == "false" ]; then
echo "CRITICAL - $CONTAINER is not running."
exit 2
fi
}
container_exist() {
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
container_is_running() {
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if [ $? -eq 1 ]; then
return 1
fi
if [ "$RUNNING" == "true" ]; then
return 0
else
return 1
fi
}
sub_docker() {
local OPTIND opt
check_docker() {
if ! docker ps > /dev/null 2>&1; then
output=$(docker ps)
error_exit "Error - Docker not installed properly: \n${output}"
fi
}
docker_usage() {
echo "Usage: $PROG_NAME docker [build|run|attach|stop|logs|exec]"
}
check_docker
# process options
while getopts ":t:" opt; do
case $opt in
t)
EXTRA_VARS="-t ${OPTARG}"
;;
\?)
docker_usage
exit 1
;;
esac
done
case $1 in
"-h" | "--help")
docker_usage
;;
"build")
docker build $EXTRA_VARS -t webide/webide .
;;
"run")
RUNNING=$(docker inspect --format="{{ .State.Running }}" $CONTAINER 2> /dev/null)
if ! container_exist ; then
echo "creating container $CONTAINER"
docker create -p 8080:8080 --env-file config -v coding-ide-home:/root/.coding-ide --name webide -h webide webide/webide
valid_last_cmd
elif [ "$RUNNING" == "true" ]; then
echo "CRITICAL - $CONTAINER is running."
exit 2
fi
echo "starting container $CONTAINER"
docker start webide
valid_last_cmd
docker attach --sig-proxy=false webide
;;
"stop")
assert_container_is_running
docker stop webide
;;
"attach")
assert_container_is_running
docker attach --sig-proxy=false webide
;;
"logs")
assert_container_is_running
docker logs -f webide
;;
"remove")
if container_is_running ; then
echo "stoping webide..."
docker stop webide
fi
echo "removing webide..."
docker rm webide
echo "done..."
;;
"exec")
assert_container_is_running
docker exec -it webide bash
;;
esac
}
do_run_backend() {
if [ ! -f $BACKEND/target/ide-backend.jar ]; then
sub_build
fi
. $BASEDIR/config
java -jar $BACKEND/target/ide-backend.jar --PTY_LIB_FOLDER=$BACKEND/src/main/resources/lib ${1}
}
sub_run() {
local OPTIND opt
run_usage() {
echo "Usage: $PROG_NAME run [-p port]"
}
# process options
while getopts ":p:" opt; do
case $opt in
p)
EXTRA_VARS="--server.port=${OPTARG}"
;;
\?)
run_usage
exit 1
;;
esac
done
shift $((OPTIND-1))
case $1 in
"-h" | "--help")
run_usage
;;
"")
do_run_backend $EXTRA_VARS
;;
esac
}
# process subcommands
subcommand=$1
case $subcommand in
"" | "-h" | "--help")
sub_help
;;
*)
shift
sub_${subcommand} $@
if [ $? = 127 ]; then
echo "Error: '$subcommand' is not a known subcommand." >&2
echo " Run '$PROG_NAME --help' for a list of known subcommands." >&2
exit 1
fi
;;
esac