-
Notifications
You must be signed in to change notification settings - Fork 31
/
coreos
executable file
·316 lines (277 loc) · 9.14 KB
/
coreos
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/bash
CWD=`pwd`
ENV="$CWD/.coreosenv"
source $ENV
function operateDray {
sudo systemctl $1 panamax-redis.service
sudo systemctl $1 panamax-dray.service
}
function operatePanamax {
operateDray $1
sudo systemctl $1 panamax-metrics.service
sudo systemctl $1 panamax-api.service
sudo systemctl $1 panamax-ui.service
}
function cleanupCoreOS {
if [[ "fleetctl list-units | grep service " != "" ]]; then
echo Destroying all fleet units
units=( `fleetctl list-units | grep service | gawk '{print $1 }'` )
for i in "${units[@]}"
do
fleetctl destroy $i
done
#fleetctl destroy `fleetctl list-units | grep service | gawk '{print $1 }'`
fi
if [[ "`docker ps -a | grep ago`" != "" ]]; then
echo Destroying remaining docker containers
docker stop `docker ps -a -q` && \
docker rm `docker ps -a -q`
fi
}
function uninstallPanamax {
echo "Uninstalling Panamax."
operatePanamax stop
operatePanamax disable
sudo rm -f /etc/systemd/system/panamax*.service
#sudo rm -f /etc/systemd/user/*
sudo rm -f units/panamax*.service
if [[ "$CLEANUP_MODE" == "all" ]]; then
cleanupCoreOS
fi
}
function downloadImage {
echo ""
echo "docker pull $1"
/usr/bin/docker pull $1 >/dev/null &
PID=$!
while $(kill -n 0 $PID 2> /dev/null)
do
echo -n '.'
sleep 2
done
}
function downloadPmxImages {
downloadImage $REPO_URL_NAMESPACE/$IMAGE_API:$IMAGE_TAG
downloadImage $REPO_URL_NAMESPACE/$IMAGE_UI:$IMAGE_TAG
downloadImage $CADVISOR_IMAGE
downloadImage $DRAY_REDIS_IMAGE
downloadImage $DRAY_IMAGE
}
function installPanamax {
echo "Installing Panamax..."
startCoreOSServices
if [[ -z "$PANAMAX_ID" ]]; then
PANAMAX_ID=`uuidgen`
echo "" >> $ENV
echo "PANAMAX_ID=\"$PANAMAX_ID\"" >> $ENV
fi
sudo mkdir -p units
sudo rm -f units/*
writeUnitFiles
sudo cp units/* /etc/systemd/system/
sudo cp -f scripts/* /etc/systemd/user 2>/dev/null
downloadPmxImages
operatePanamax enable
operatePanamax start
waitUntilStarted
echo "Panamax install complete"
}
function writeUnitFiles {
echo "[Unit]
Description=Panamax API
After=docker.service fleet.service fleet.socket
Requires=docker.service fleet.service fleet.socket
[Service]
ExecStartPre=-/usr/bin/docker rm -f $CONTAINER_NAME_API
ExecStart=$(getRunCmdAPI)
ExecStop=/usr/bin/docker stop $CONTAINER_NAME_API
Restart=always
[Install]
WantedBy=multi-user.target" > units/panamax-api.service
echo "[Unit]
Description=Panamax UI
After=panamax-api.service panamax-metrics.service
Requires=panamax-api.service panamax-metrics.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f $CONTAINER_NAME_UI
ExecStart=$(getRunCmdUI)
ExecStop=/usr/bin/docker stop $CONTAINER_NAME_UI
Restart=always
[Install]
WantedBy=multi-user.target" > units/panamax-ui.service
echo "[Unit]
Description=Panamax Metrics
[Service]
ExecStartPre=-/usr/bin/docker rm -f $CONTAINER_NAME_CADVISOR
ExecStart=/usr/bin/docker run --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=3002:8080 --name=$CONTAINER_NAME_CADVISOR $CADVISOR_IMAGE
ExecStop=/usr/bin/docker stop $CONTAINER_NAME_CADVISOR
Restart=always
[Install]
WantedBy=multi-user.target" > units/panamax-metrics.service
echo "[Unit]
Description=Panamax Redis
Before=panamax-dray.service
After=docker.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f $CONTAINER_NAME_DRAY_REDIS
ExecStart=/usr/bin/docker run --expose=6379 --name=$CONTAINER_NAME_DRAY_REDIS $DRAY_REDIS_IMAGE
ExecStop=/usr/bin/docker stop $CONTAINER_NAME_DRAY_REDIS
Restart=always
[Install]
WantedBy=multi-user.target" > units/panamax-redis.service
echo "[Unit]
Description=Panamax Dray
Before=panamax-api.service
After=docker.service panamax-redis.service
Requires=panamax-redis.service
[Service]
ExecStartPre=-/usr/bin/docker rm -f $CONTAINER_NAME_DRAY
ExecStart=/usr/bin/docker run --link $CONTAINER_NAME_DRAY_REDIS:REDIS --volume=/var/run/docker.sock:/var/run/docker.sock:rw --publish=3003:3000 --name=$CONTAINER_NAME_DRAY $DRAY_IMAGE
ExecStop=/usr/bin/docker stop $CONTAINER_NAME_DRAY
Restart=always
[Install]
WantedBy=multi-user.target" > units/panamax-dray.service
}
function getRunCmdAPI {
local dbMount=""
if [[ "$PERSIST_DB" == "true" ]]; then
dbMount="-v /var/panamax-data:/usr/src/app/db/mnt"
fi
docker_ip="`ifconfig docker0 2>/dev/null | grep 'inet '| awk '{print $2}'`"
if [[ "$docker_ip" != "" ]]; then
COREOS_ENDPOINT="http://$docker_ip"
fi
echo "/usr/bin/docker run --name $CONTAINER_NAME_API $dbMount -m=1g -c=10 -v /var/run/docker.sock:/var/run/docker.sock:rw -v /var/run/fleet.sock:/var/run/fleet.sock -e DRAY_PORT=$COREOS_ENDPOINT:3003 -e PANAMAX_ID=$PANAMAX_ID -e INSECURE_REGISTRY=$INSECURE_REGISTRY -e JOURNAL_ENDPOINT=$COREOS_ENDPOINT:19531 -t -p 3001:3000 $REPO_URL_NAMESPACE/$IMAGE_API:$IMAGE_TAG"
}
function getRunCmdUI {
echo "/usr/bin/docker run --name $CONTAINER_NAME_UI -m=1g -c=10 -v /var/run/docker.sock:/var/run/docker.sock:rw --link $CONTAINER_NAME_API:PMX_API --link $CONTAINER_NAME_CADVISOR:CADVISOR -e INSECURE_REGISTRY=$INSECURE_REGISTRY -p 3000:3000 $REPO_URL_NAMESPACE/$IMAGE_UI:$IMAGE_TAG"
}
function startCoreOSServices {
sudo systemctl stop update-engine-reboot-manager.service
sudo systemctl mask update-engine-reboot-manager.service
sudo systemctl stop update-engine.service
sudo systemctl mask update-engine.service
sudo systemctl enable etcd fleet systemd-journal-gatewayd.socket
sudo systemctl restart etcd systemd-journal-gatewayd.socket
sudo systemctl stop ntpd.service
sudo systemctl start ntpd.service
sudo systemctl stop fleet.service;
sudo systemctl restart fleet.socket;
sudo systemctl start fleet.service
}
function restartPanamax {
echo "Restarting Panamax"
startCoreOSServices
operatePanamax restart
waitUntilStarted
echo "Panamax restarted"
}
function stopPanamax {
echo Stopping Panamax
operatePanamax stop
startCoreOSServices
echo Panamax Stopped.
}
function updatePanamax {
echo Updating Panamax
uninstallPanamax
installPanamax
echo Panamax Updated
echo "***PLEASE NOTE: If running Panamax on a dedicated CoreOS VM from a cloud provider, please ensure you are running the latest version of CoreOS from the stable branch, to ensure compatibility with Panamax.***"
}
function openPanamax {
echo "waiting for panamax to start....."
until [ `curl -sL -w "%{http_code}" "http://localhost:$PMX_PORT_UI" -o /dev/null` == "200" ];
do
printf .
sleep 2
done
echo ""
open "http://localhost:$PMX_PORT_UI" || { echo "Please goto http://localhost:$PMX_PORT_UI"; }
echo ""
echo ""
}
function waitUntilStarted {
logStartTime="`date +'%Y-%m-%d %H:%M:%S'`"
until [ `curl -sL -w "%{http_code}" "http://localhost:3000" -o /dev/null` == "200" ];
do
journalctl --since="$logStartTime" | grep 'PMX\|Panamax'
sleep 1
logStartTime="`date +'%Y-%m-%d %H:%M:%S'`"
done
}
function showHelp {
echo ""
echo "Usage:
./setup.sh <install/reinstall/restart/stop>
./setup.sh "
echo ""
}
function readParams {
for i in "$@"
do
case $i in
--dev)
IMAGE_TAG=dev
;;
--stable)
IMAGE_TAG=latest
;;
install)
operation=install
;;
reinstall)
operation=reinstall
;;
uninstall)
operation=uninstall
;;
stop)
operation=stop
;;
start)
operation=restart
;;
restart)
operation=restart
;;
update)
operation=update
;;
-pid=*)
PANAMAX_ID="${i#*=}"
;;
-insecureregistry=*)
INSECURE_REGISTRY="${i#*=}"
;;
*)
exit 1;
;;
esac
done
}
function main {
readParams "$@"
if [[ $# -gt 0 ]]; then
case $operation in
install) installPanamax && exit 0 || { showHelp; exit 1; } ;;
reinstall) installPanamax && exit 0 || { showHelp; exit 1; } ;;
restart) restartPanamax || { showHelp; exit 1; } ;;
stop) stopPanamax || { showHelp; exit 1; } ;;
update) updatePanamax || { showHelp; exit 1; } ;;
*) showHelp;;
esac
else
echo "Please select one of the following options: "
select operation in "install" "restart" "reinstall" "update"; do
case $operation in
install) installPanamax && exit 0; break;;
reinstall ) installPanamax && exit 0; break;;
restart) restartPanamax; break;;
stop) stopPanamax; break;;
update) updatePanamax; break;;
esac
done
fi
}
main "$@";