-
Notifications
You must be signed in to change notification settings - Fork 0
/
swapd
executable file
·211 lines (166 loc) · 5.95 KB
/
swapd
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
#!/usr/bin/env bash
# CUSTOM VARS: HOST_DB_DATA_DIR (default: "${HOME}/devbox-mongodb-data-volume")
IMAGE_FROM="node:latest"
IMAGE_NAME="devbox-generate-swap-rest-api"
IMAGE_TAG="latest"
CONTAINER_NAME="${IMAGE_NAME}"
while getopts 'hr' flag; do
case "${flag}" in
h) helpCommand="true" ;;
r) rootCommand="true" ;;
*) echo "ERROR: Unexpected option ${flag}" ;;
esac
done
if [[ -z $1 || $1 == "help" || $helpCommand == "true" ]]; then
echo "SWAP Daemon Controller (swapd)
----------------------------
Available commands:
build build the swapd container (rebuild it when upgraded)
Usage:
./swapd build
start start the swapd container (start it once to use it)
Usage:
./swapd start
status display the daemon status (use it to know if the daemon is running for example)
Usage:
./swapd status
stop stop the swapd container (stop it when you are done with development)
Usage:
./swapd stop
exec execute a non-interactive command with an arbitrary number of arguments.
Usage:
./swapd exec [-r] <exec_command> [<command args>...]
Options:
-r start the bash session as root (use it to do admin
tasks like installing new software)
bash start a bash session (start as many session as you need),
or pass an additionnal command with an arbitrary number of
arguments to execute that command in a subprocess of the
new bash session.
Usage:
./swapd bash [-r] [<exec_command> [<command args>...]]
Options:
-r start the bash session as root (use it to do admin tasks like
installing new software)
ungit start the ungit server listenning on port 8448
Usage:
./swapd ungit
:start run 'yarn start' inside the devbox container
:debug run 'yarn debug' inside the devbox container
:dev run 'yarn dev' inside the devbox container
:serve run 'yarn serve' inside the devbox container
:build run 'yarn build' inside the devbox container
"
exit 0
elif [[ $1 == "build" ]]; then
docker build \
-t ${IMAGE_NAME}:${IMAGE_TAG} \
--build-arg username=$(echo $USERNAME) \
--build-arg password=$(echo $USERNAME) \
--build-arg uid=$(echo $UID) \
-f devbox.Dockerfile \
.
elif [[ $1 == "start" ]]; then
docker run --detach -t --rm \
--name "${CONTAINER_NAME}" \
--publish 9000:9000 \
--publish 3000:8100 \
--publish 8080:8080 \
--publish 8448:8448 \
--publish 35729:35729 \
--publish 53703:53703 \
--volume "/home/${USERNAME}/.gitconfig:/home/${USERNAME}/.gitconfig" \
--volume "/home/${USERNAME}/.gnupg:/home/${USERNAME}/.gnupg" \
--volume "/home/${USERNAME}/.ssh:/home/${USERNAME}/.ssh" \
--volume "$(pwd):/home/${USERNAME}/app" \
"${IMAGE_NAME}:${IMAGE_TAG}" \
&& echo "${CONTAINER_NAME} has started successfully."
elif [[ $1 == "bash" ]]; then
shift
while getopts 'r' flag; do
case "${flag}" in
r) rootCommand="true" ;;
*) echo "ERROR: Unexpected option ${flag}" ;;
esac
done
if [[ $rootCommand == "true" ]]; then
shift
if [[ $# > 0 ]] ; then
cmdstr="$@"
else
cmdstr="bash"
fi;
echo "Run a bash session as root"
docker exec -itu 0 -w "/root" ${CONTAINER_NAME} $cmdstr
else
if [[ $# > 0 ]] ; then
cmdstr="$@"
else
cmdstr="bash"
fi;
echo "Run a bash session as ${USERNAME}"
docker exec -itu 1000 ${CONTAINER_NAME} $cmdstr
fi
elif [[ $1 == "exec" ]]; then
shift
while getopts 'r' flag; do
case "${flag}" in
r) rootCommand="true" ;;
*) echo "ERROR: Unexpected option ${flag}" ;;
esac
done
if [[ $rootCommand == "true" ]]; then
shift
if [[ $# == 0 ]] ; then
echo "ERROR: Missing command to exec"
fi;
echo "Exec a command as root"
docker exec -u 0 -w "/root" ${CONTAINER_NAME} $@
else
if [[ $# == 0 ]] ; then
echo "ERROR: Missing command to exec"
fi;
echo "Exec a command as ${USERNAME}"
docker exec -u 1000 ${CONTAINER_NAME} $@
fi
elif [[ $1 == "status" ]]; then
echo "Running Devboxes"
echo "----------------"
docker ps --filter name=devbox* --format 'table {{.Names}}\t{{.Image}}\t{{.Ports}}\t{{.Mounts}}'
# docker ps --filter name=devbox* --format '{{.Names}}'
for box in $(docker ps --filter name=devbox* --format '{{.Names}}')
do
# echo "Name: " $(docker container inspect ${box} --format '{{.Name}}')
# echo "ID: " $(docker container inspect ${box} --format '{{.ID}}')
# echo "Image: " $(docker container inspect ${box} --format '{{.Config.Image}}')
# echo "Ports: " $(docker container inspect ${box} --format '{{json .HostConfig.PortBindings}}')
# echo "Mounts: " $(docker container inspect ${box} --format '{{join .HostConfig.Binds ", "}}')
# echo ""
echo ""
echo "Running processes for ${box}"
echo "----------------------------"
docker top ${box}
echo ""
done
elif [[ $1 == "stop" ]]; then
docker stop ${CONTAINER_NAME} || echo "Skipping."
elif [[ $1 == "update" ]]; then
./swapd stop \
&& docker pull ${IMAGE_FROM} || echo "Skipping." \
&& ./swapd build \
&& ./swapd start
elif [[ $1 == "ungit" ]]; then
docker exec -du 1000 ${CONTAINER_NAME} ungit
elif [[ $1 == ":start" ]]; then
docker exec -itu 1000 ${CONTAINER_NAME} yarn start
elif [[ $1 == ":debug" ]]; then
docker exec -itu 1000 ${CONTAINER_NAME} yarn debug
elif [[ $1 == ":dev" ]]; then
docker exec -itu 1000 ${CONTAINER_NAME} yarn dev
elif [[ $1 == ":serve" ]]; then
docker exec -itu 1000 ${CONTAINER_NAME} yarn serve
elif [[ $1 == ":build" ]]; then
docker exec -itu 1000 ${CONTAINER_NAME} yarn build
else
echo "ERROR: \"$1\": invalid subcomand"
fi