This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
octo
executable file
·926 lines (829 loc) · 26 KB
/
octo
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
#!/bin/bash
if [ -f /etc/default/octohost ]; then
. /etc/default/octohost
fi
log () {
local message="$1"
#local level="${2:-info}"
logger -p local4.info -t octohost "$message"
}
log "Command='$0 $*'"
show_break ()
{
echo ""
echo ""
echo "################################"
}
start_logspout ()
{
if [ -z "$LOGSPOUT_TARGET" ]; then
echo "No LOGSPOUT_TARGET in /etc/default/octohost - not starting."
exit
fi
if [ -e "/var/run/docker.sock" ]; then
$LOGSPOUT
else
sudo service docker start
sleep 5
$LOGSPOUT
fi
}
stop_logspout ()
{
sudo docker ps | grep "$LOGSPOUT_IMAGE" | awk '{ print $1 }' | xargs sudo docker kill
}
pull_logspout ()
{
sudo docker pull $LOGSPOUT_IMAGE
}
start_logs ()
{
check_syslog_port
if [ -e "/var/run/docker.sock" ]; then
LOG_COMMAND="sudo docker run -d --restart always -v=$1:$2 $REMOTE_SYSLOG_COMMAND $2"
$LOG_COMMAND
else
sudo service docker start
sleep 5
$LOG_COMMAND
fi
}
stop_octologs ()
{
sudo docker ps | grep "$OCTOLOGS_IMAGE" | awk '{ print $1 }' | xargs sudo docker kill
}
check_syslog_port ()
{
if [ -z "$REMOTE_SYSLOG_PORT" ]; then
echo "Need to define REMOTE_SYSLOG_PORT in /etc/default/octohost"
exit 1
fi
}
inspect_to_get_external_port()
{
PORT=$(sudo docker -H $DOCKER_HOST inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostPort}} {{end}}' $ID)
echo $PORT
}
inspect_to_get_ip_address()
{
IP_ADDRESS=$(sudo docker -H $DOCKER_HOST inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{(index $conf 0).HostIp}} {{end}}' $ID)
echo $IP_ADDRESS
}
inspect_to_get_container_name()
{
for tag in $(sudo docker -H $DOCKER_HOST inspect -f '{{json .Config.Env}}' $ID | jq '.[]' ); do
if echo $tag | grep -q "CONTAINER_NAME"
then
CONTAINER_NAME=$(echo $tag | cut -d '"' -f 2 | cut -d '=' -f 2-10)
fi
done
echo $CONTAINER_NAME
}
launch_container_return_id ()
{
if [ -n "$PRIVATE_REGISTRY" ]; then
ID=$(echo "$RUN_OPTIONS $PRIVATE_REGISTRY/$BASE" | xargs sudo docker -H $DOCKER_HOST run)
else
ID=$(echo "$RUN_OPTIONS $BUILD_ORG_NAME/$BASE" | xargs sudo docker -H $DOCKER_HOST run)
fi
echo $ID
}
set_src_variables ()
{
BASE="$1"
REPOSITORY="$BASE.git"
REPO_PATH="$SRC_DIR/$REPOSITORY"
DOCKERFILE="$REPO_PATH/Dockerfile"
log "Base: $BASE, Repo: $REPOSITORY, Path: $REPO_PATH, Dockerfile: $DOCKERFILE"
}
get_unique_domains ()
{
SCRATCH=`mktemp`
DOMAINS="$1"
for domain in ${DOMAINS//,/ } ; do
echo $domain
done | sort | uniq > $SCRATCH
FINAL_DOMAINS=""
for line in $( cat $SCRATCH ); do
FINAL_DOMAINS="$FINAL_DOMAINS $line"
done
rm -f $SCRATCH
# Trim the first space.
echo "${FINAL_DOMAINS/ /}"
}
case "$1" in
logs)
if [ "$2" == "octostart" ]; then
check_syslog_port
$OCTOLOGS
elif [ "$2" == "octostop" ]; then
stop_octologs
elif [ "$2" == "start" ]; then
if [[ $3 && $4 ]]; then
check_syslog_port
start_logs "$3" "$4"
else
echo "Usage: octo logs start {full-log-path} {mount}"
fi
else
echo "Usage: octo logs {octostart|start [full-log-path] [mount]}"
fi
;;
logspout)
if [ "$2" == "start" ]; then
check_syslog_port
start_logspout
elif [ "$2" == "stop" ]; then
stop_logspout
elif [ "$2" == "pull" ]; then
pull_logspout
else
echo "Usage: octo logspout {start|stop|pull}"
fi
;;
start)
if [ $2 ]; then
set_src_variables "$2"
NUMBER=${3:-1}
for (( i=1; i <= $NUMBER; i++ ))
do
if [ -e "$DOCKERFILE" ]; then
RUN_OPTIONS=$(/usr/bin/octo config:options "$BASE" "$DOCKERFILE")
else
RUN_OPTIONS="-P -d -e CONTAINER_NAME=$BASE"
ENV=$(/usr/bin/octo config:env $BASE)
RUN_OPTIONS="$RUN_OPTIONS $ENV"
fi
ID=$(launch_container_return_id)
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
TAGS=$(/usr/bin/octo service:tags $ID)
/usr/bin/octo service:set $BASE $CONTAINER_IP $PORT $TAGS
/usr/bin/octo add:upstart $BASE
done
else
echo "Usage: octo start {container}"
fi
;;
stop)
if [ "$2" ]; then
BASE="$2"
if [ -n $PRIVATE_REGISTRY ]; then
IMAGE_PREFIX="$PRIVATE_REGISTRY/"
else
IMAGE_PREFIX=""
fi
CONTAINERS=$(sudo docker -H $DOCKER_HOST ps | grep "${IMAGE_PREFIX}$BASE" | cut -d ' ' -f 1)
for container in $CONTAINERS
do
octo kill "$BASE" "$container"
done
else
echo "Usage: octo stop {container_name}"
fi
;;
kill)
if [[ "$2" && "$3" ]]; then
BASE="$2"
ID="$3"
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
sudo docker -H $DOCKER_HOST kill "$ID" > /dev/null
octo service:rm "$BASE" "$CONTAINER_IP" "$PORT"
else
echo "Usage: octo kill {container} {container_id}"
fi
;;
config:set)
if [[ $2 && "$3" ]]; then
/usr/bin/consulkv set $CONSUL_PREFIX/$2 "$3"
else
echo "Usage: octo config:set {container/key} {var}"
fi
;;
config:get)
if [ $2 ]; then
/usr/bin/consulkv get $CONSUL_PREFIX/$2
else
echo "Usage: octo config:get {container/key}"
fi
;;
config:rm)
if [ $2 ]; then
/usr/bin/consulkv del $CONSUL_PREFIX/$2
else
echo "Usage: octo config:rm {container/key}"
fi
;;
config:export)
for key in `/usr/bin/consulkv ls $CONSUL_PREFIX`
do
if [ "$key" != "null" ]; then
VALUE=$(octo config:get $key)
echo "octo config:set $key '$VALUE'"
fi
done
;;
config)
if [ $2 ]; then
for conf in `/usr/bin/consulkv ls $CONSUL_PREFIX/$2`;
do
CONF_VALUE=$(/usr/bin/octo config:get $2/$conf)
echo "/$2/$conf:$CONF_VALUE";
done
else
echo "Usage: octo config {container}"
fi
;;
config:env)
if [ $2 ]; then
ENV=""
while read conf
do
KEY=$(echo $conf | cut -d '/' -f 3 | cut -d ':' -f 1)
VAR=$(echo $conf | cut -d ':' -f 2-10)
ENV="$ENV -e $KEY=\"$VAR\""
done < <(/usr/bin/octo config $2)
echo "$ENV"
else
echo "octo config:env {container}"
fi
;;
config:options)
if [[ $2 && $3 ]]; then
BASE="$2"
DOCKERFILE="$3"
RUN_OPTIONS=" -d -e CONTAINER_NAME=$BASE"
ADD_NAME=$(grep -i "^# ADD_NAME" $DOCKERFILE)
if [ -n "$ADD_NAME" ]
then
RUN_OPTIONS="$RUN_OPTIONS --name $BASE"
fi
VOLUMES_FROM=$(grep -i "^# VOLUMES_FROM" $DOCKERFILE)
if [ -n "$VOLUMES_FROM" ]
then
VOLUME_NAME="${BASE}_data"
RUN_OPTIONS="$RUN_OPTIONS --volumes-from $VOLUME_NAME"
fi
while read -r MOUNT_FROM_HOST ; do
if [ -n "$MOUNT_FROM_HOST" ]
then
HOST_PATH=$(echo $MOUNT_FROM_HOST | awk '{ print $3 }')
CONTAINER_PATH=$(echo $MOUNT_FROM_HOST | awk '{ print $4 }')
if [ -n "$CONTAINER_PATH" ]
then
MOUNT_PATH="$HOST_PATH:$CONTAINER_PATH"
else
MOUNT_PATH="$HOST_PATH:$HOST_PATH"
fi
RUN_OPTIONS=" -v $MOUNT_PATH $RUN_OPTIONS"
fi
done < <(grep -i '^# MOUNT_FROM_HOST' $DOCKERFILE )
while read -r PORTS_FROM_HOST ; do
if [ -n "$PORTS_FROM_HOST" ]
then
HOST_PORT=$(echo $PORTS_FROM_HOST | awk '{ print $3 }')
CONTAINER_PORT=$(echo $PORTS_FROM_HOST | awk '{ print $4 }')
CONTAINER_ADDRESS=$(echo $PORTS_FROM_HOST | awk '{ print $5 }')
if [ -n "$CONTAINER_ADDRESS" ]
then
NEW_PORT="$CONTAINER_ADDRESS:$HOST_PORT:$CONTAINER_PORT"
else
NEW_PORT="0.0.0.0:$HOST_PORT:$CONTAINER_PORT"
fi
PORT_OPTIONS=" -p $NEW_PORT $PORT_OPTIONS"
fi
done < <(grep -i '^# PORTS_FROM_HOST' $DOCKERFILE )
if [ -n "$PORT_OPTIONS" ]
then
RUN_OPTIONS=" $PORT_OPTIONS $RUN_OPTIONS"
else
RUN_OPTIONS=" -P $RUN_OPTIONS"
fi
# Support a CNAME file in repo src
# and if there supply the -h option to docker run
CNAME="/home/git/src/${BASE}.git/CNAME"
if [ -f $CNAME ]
then
# Add a new line at end if it does not exist to ensure the loop gets last line
# sed -i -e '$a\' $CNAME
while read DOMAIN
do
CONTAINER_HOSTNAME="$DOMAIN"
done < $CNAME
fi
if [ -n "$CONTAINER_HOSTNAME" ]
then
RUN_OPTIONS=" -h $CONTAINER_HOSTNAME $RUN_OPTIONS"
fi
FOUND_LINKS=$(grep -i "^# LINK_SERVICE" $DOCKERFILE)
if [ -n "$FOUND_LINKS" ]; then
while read LINK_LINE
do
SOURCE=$(echo $LINK_LINE | awk '{ print $3 }')
LINK_NAME=$(echo $LINK_LINE | awk '{ print $4 }')
if [ -n "$LINK_NAME" ]
then
LINK_NAME="$LINK_NAME:$SOURCE"
else
LINK_NAME="${BASE}_${SOURCE}:${SOURCE}"
fi
RUN_OPTIONS="$RUN_OPTIONS --link $LINK_NAME"
done <<<"$FOUND_LINKS"
fi
MEMORY_LIMIT=$(/usr/bin/octo config:get $BASE/MEMORY_LIMIT)
if [ -n "$MEMORY_LIMIT" ]
then
RUN_OPTIONS="$RUN_OPTIONS --memory='$MEMORY_LIMIT'"
fi
ENV_VARS=$(/usr/bin/octo config $BASE | grep -v "Error: 100: Key not found")
if [ -n "$ENV_VARS" ]
then
ENV=$(/usr/bin/octo config:env $BASE)
RUN_OPTIONS="$RUN_OPTIONS $ENV"
fi
echo "$RUN_OPTIONS"
log "Run Options: $RUN_OPTIONS"
else
echo "Something went wrong."
fi
;;
config:check)
if [[ $2 && $3 ]]; then
set_src_variables "$2"
CONFIG_TO_CHECK="$3"
VALUE=$(grep -i "^# $CONFIG_TO_CHECK" $DOCKERFILE)
if [ -z "$VALUE" ]; then
VALUE=$(/usr/bin/consulkv get $CONSUL_PREFIX/$2/$CONFIG_TO_CHECK)
fi
echo $VALUE
log "config:check '$CONFIG_TO_CHECK=$VALUE'"
else
echo "octo config:check {container} {var_to_check}"
fi
;;
service:set)
if [[ $2 ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$2" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
if [[ $2 && $3 && $4 ]]; then
CONTAINER="$2"
CONTAINER_IP="$3"
PORT="$4"
TAGS=${5:-http}
CUSTOM_CHECK=$(/usr/bin/octo config:get $CONTAINER/CUSTOM_CHECK)
CHECK_COMMAND=${CUSTOM_CHECK:-http://$CONTAINER_IP:$PORT}
CUSTOM_CHECK_INTERVAL=$(/usr/bin/octo config:get $CONTAINER/CUSTOM_CHECK_INTERVAL)
CHECK_INTERVAL=${CUSTOM_CHECK_INTERVAL:-15s}
if echo $TAGS | grep -q 'http'
then
CHECK=",\"check\": {\"http\": \"$CHECK_COMMAND\",\"interval\": \"$CHECK_INTERVAL\"}"
else
CHECK=""
fi
PAYLOAD="{\"ID\": \"$CONTAINER-$CONTAINER_IP-$PORT\",\"Name\": \"$CONTAINER\",\"Address\": \"$CONTAINER_IP\",\"Port\": $PORT,\"Tags\": [\"$TAGS\"]$CHECK}"
/usr/bin/consulkv service:set "$PAYLOAD"
log "service:set '$PAYLOAD'"
else
echo "Usage: octo service:set {container} {ip_address} {port} {tags}"
fi
;;
service:rm)
if [[ $2 ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$2" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
if [[ "$2" && "$3" && "$4" ]]; then
CONTAINER="$2"
CONTAINER_IP="$3"
PORT="$4"
/usr/bin/consulkv service:del "$CONTAINER-$CONTAINER_IP-$PORT"
else
echo "Usage: octo service:rm {container} {ip} {port}"
fi
;;
service:tags)
if [ $2 ]; then
for tag in $(sudo docker -H $DOCKER_HOST inspect -f '{{json .Config.Env}}' $2 | jq '.[]' ); do
if echo $tag | grep -q "CONSUL_TAGS"
then
TAG=$(echo $tag | cut -d '"' -f 2 | cut -d '=' -f 2-10)
fi
done
echo $TAG
log "service:tags '$TAGS'"
fi
;;
services)
/usr/bin/consulkv services
;;
services:catalog)
/usr/bin/consulkv services:catalog
;;
services:register)
for container in $(sudo docker -H $DOCKER_HOST ps | grep $BUILD_ORG_NAME | awk '{ print $1 }'); do
ID=$container
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
BASE=$(inspect_to_get_container_name)
TAGS=$(/usr/bin/octo service:tags $ID)
if [[ $BASE && $PORT ]]; then
/usr/bin/octo service:set $BASE $CONTAINER_IP $PORT $TAGS
fi
done
;;
services:clear)
for container in $(sudo docker -H $DOCKER_HOST ps -q ); do
ID=$container
PORT=$(inspect_to_get_external_port)
CONTAINER_IP=$(inspect_to_get_ip_address)
BASE=$(inspect_to_get_container_name)
if [[ "$BASE" && "$CONTAINER_IP" && "$PORT" ]]; then
/usr/bin/octo service:rm "$BASE" "$CONTAINER_IP" "$PORT"
fi
done
# Remove HTTP tagged orphans from crashed containers.
/usr/bin/octo services:remove_all
;;
services:remove_all)
# Remove HTTP tagged orphans from crashed containers.
for service in $(/usr/bin/octo services); do
if echo $service | grep -q 'http'
then
/usr/bin/octo service:remove "$service"
fi
done
;;
services:remove)
if [ $2 ]; then
for service in $(/usr/bin/octo services | grep "\"Service\":\"$2\""); do
if echo $service | grep -q 'http'
then
/usr/bin/octo service:remove "$service"
fi
done
else
echo "Usage: octo services:remove {container}"
fi
;;
service:remove)
if [ $2 ]; then
ID=$(echo $2 | jq .ID | cut -d '"' -f 2)
PORT=$(echo $ID | grep -o [0-9]*$)
BASE=${ID%-$PORT}
/usr/bin/octo service:rm $BASE $PORT
fi
;;
domains:set)
if [[ $2 && $3 ]]; then
DOMAINS=$(get_unique_domains $3)
/usr/bin/consulkv set $CONSUL_PREFIX/$2/DOMAINS "$DOMAINS"
log "domains:set '$DOMAINS'"
else
echo "Usage: octo domains:set {container} {domains}"
echo "Domains are space separated."
fi
;;
domains:get)
if [ $2 ]; then
/usr/bin/consulkv get $CONSUL_PREFIX/$2/DOMAINS
else
echo "Usage: octo domains:get {container}"
fi
;;
config:consul_template)
BASE="$2"
if [[ $BASE ]]; then
NO_HTTP_PROXY=$(/usr/bin/octo config:check "$BASE" "NO_HTTP_PROXY")
if [ -n "$NO_HTTP_PROXY" ]; then
exit 0
fi
fi
# If there isn't a template already, copy the template and set the name.
NGINX_TEMPLATE="/etc/nginx/templates/$BASE.ctmpl"
NGINX_TEMPLATE_ALT=$(/usr/bin/octo config:get $BASE/NGINX_ALT)
NGINX_TEMPLATE_SSL=$(/usr/bin/octo config:get $BASE/SSL_FORCED)
if [ ! -f $NGINX_TEMPLATE ]; then
if [ ! -z "$NGINX_TEMPLATE_ALT" ]; then
sudo cp $NGINX_TEMPLATE_ALT $NGINX_TEMPLATE
elif [ ! -z "$NGINX_TEMPLATE_SSL" ]; then
sudo cp /etc/octohost/templates/nginx-ssl.ctmpl $NGINX_TEMPLATE
else
sudo cp /etc/octohost/templates/nginx.ctmpl $NGINX_TEMPLATE
fi
sudo sed -i "s/REPLACEME/$BASE/" $NGINX_TEMPLATE
fi
# If there isn't a Consul template config already, copy and update.
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
if [ ! -f $CONSUL_CONFIG ]; then
sudo cp /etc/octohost/templates/consul-template.cfg $CONSUL_CONFIG
sudo sed -i "s/REPLACEME/$BASE/" $CONSUL_CONFIG
fi
# If there isn't a Consul service watch, copy, update and reload consul.
CONSUL_WATCH_JSON="/etc/consul.d/watch-$BASE.json"
if [ ! -f $CONSUL_WATCH_JSON ]; then
sudo cp /etc/octohost/templates/consul-service-watch.json $CONSUL_WATCH_JSON
sudo sed -i "s/REPLACEME/$BASE/" $CONSUL_WATCH_JSON
sudo service consul reload
fi
# Render the proper template and restart the proxy service.
/usr/bin/octo config:consul_template_render $BASE
;;
config:consul_template_render)
BASE="$2"
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
sudo consul-template -config $CONSUL_CONFIG -once
;;
add:upstart)
if [ $2 ]; then
BASE="$2"
UPSTART_CONFIG="/etc/init/octohost-$BASE.conf"
if [ ! -f $UPSTART_CONFIG ]; then
sudo cp /etc/octohost/templates/upstart.conf $UPSTART_CONFIG
sudo sed -i "s/REPLACEME/$BASE/" $UPSTART_CONFIG
fi
else
echo "Usage: octo add:upstart {container}"
fi
;;
remove:upstart)
if [ $2 ]; then
BASE="$2"
UPSTART_CONFIG="/etc/init/octohost-$BASE.conf"
if [ -f $UPSTART_CONFIG ]; then
sudo rm -f $UPSTART_CONFIG
fi
else
echo "Usage: octo remove:upstart {container}"
fi
;;
status)
cd $SRC_DIR
for i in *.git; do
site=`basename $i .git`
echo -n "$site: "
if (/usr/bin/consulkv service:health $site | grep -q "passing"); then
echo -e "\e[32mOK\e[0m"
else
echo -e "\e[31mDOWN\e[0m"
fi
done
;;
clean)
CONTAINERS_TO_DELETE=$(sudo docker -H $DOCKER_HOST ps -a | grep -v "$BUILD_ORG_NAME\/.*\_data*" | grep "Exit" | cut -d ' ' -f 1)
if [ "$CONTAINERS_TO_DELETE" != '' ] ; then
echo "Cleaning old exited containers."
echo $CONTAINERS_TO_DELETE | xargs sudo docker -H $DOCKER_HOST rm
else
echo "No exited containers to delete."
fi
echo "Showing remaining containers."
sudo docker -H $DOCKER_HOST ps -a
IMAGES_TO_DELETE=$(sudo docker -H $DOCKER_HOST images | grep "\<none\>" | awk '{ print $3 }')
if [ "$IMAGES_TO_DELETE" != '' ] ; then
echo "Cleaning out all unused images."
echo $IMAGES_TO_DELETE | xargs sudo docker -H $DOCKER_HOST rmi
else
echo "No unused images to delete."
fi
echo "Showing remaining images."
sudo docker -H $DOCKER_HOST images
;;
remove)
if [ $2 ]; then
BASE="$2"
# Remove the Consul watch.
/usr/bin/octo remove:consul_watch $BASE
/usr/bin/octo remove:upstart $BASE
/usr/bin/octo stop $BASE
if [ -d /home/git/$BASE.git ]; then
sudo rm -rf /home/git/$BASE.git
else
echo "There is no source with that name."
exit
fi
if [ -d $SRC_DIR/$BASE.git ]; then sudo rm -rf $SRC_DIR/$BASE.git; fi
echo "Removed the source for $BASE."
/usr/bin/octo remove:nginx_config $BASE
else
echo "Usage: octo remove {container}"
fi
;;
remove:config)
if [ $2 ]; then
BASE="$2"
# Remove the nginx config.
/usr/bin/octo remove:nginx_config $BASE
# Remove the nginx template.
NGINX_TEMPLATE="/etc/nginx/templates/$BASE.ctmpl"
if [ -f $NGINX_TEMPLATE ]; then
sudo rm -f $NGINX_TEMPLATE
fi
# Remove the Consul template config.
CONSUL_CONFIG="/etc/nginx/templates/$BASE.cfg"
if [ -f $CONSUL_CONFIG ]; then
sudo rm -f $CONSUL_CONFIG
fi
# Remove the Consul Watches.
/usr/bin/octo remove:consul_watch $BASE
# Remove the kv data.
/usr/bin/octo config:rm $BASE
# Remove the Upstart config.
/usr/bin/octo remove:upstart $BASE
else
echo "Usage: octo remove:config {container}"
fi
;;
remove:consul_watch)
if [ $2 ]; then
BASE="$2"
CONSUL_WATCH_JSON="/etc/consul.d/watch-$BASE.json"
CONSUL_WATCH_KV_JSON="/etc/consul.d/watch-kv-$BASE.json"
if [ -f $CONSUL_WATCH_JSON ]; then
sudo rm -f $CONSUL_WATCH_JSON
fi
if [ -f $CONSUL_WATCH_KV_JSON ]; then
sudo rm -f $CONSUL_WATCH_KV_JSON
fi
sudo service consul reload
echo "Removed any watches and reloaded Consul."
else
echo "Usage: octo remove:consul_watch {container}"
fi
;;
remove:nginx_config)
if [ $2 ]; then
BASE="$2"
# Remove the nginx config.
NGINX_CONFIG="/etc/nginx/containers/$BASE.conf"
if [ -f $NGINX_CONFIG ]; then
sudo rm -f $NGINX_CONFIG
sudo service proxy reload
fi
fi
;;
move:sites)
if [ $2 ]; then
# Grab all of the sites.
RSYNC_USERNAME=${3:-ubuntu}
sudo /usr/bin/rsync -avz $RSYNC_USERNAME@$2:/home/git/*.git /home/git/
cd /home/git
for i in *.git; do
site=`basename $i .git`
# Need a checkout and not a bare repo to build with later.
sudo git clone /home/git/$site.git $SRC_DIR/$site.git
/usr/bin/octo restart $site
done
sudo chown -R git.git $SRC_DIR
else
echo "Usage: octo move:sites {ip-address}'"
fi
;;
move:config)
if [ $2 ]; then
SSH_USERNAME=${3:-ubuntu}
ssh $SSH_USERNAME@$2 "octo config:export" | bash
echo "## Done moving the config from $2. ##"
octo config:export
else
echo "Usage: octo move:config {ip-address}"
fi
;;
move:redis)
if [[ $2 && $3 ]]; then
WHOAMI=`whoami`
ssh -f -N -L 9999:127.0.01:$3 $WHOAMI@$2
python /usr/local/bin/redis-copy.py -s 127.0.0.1:9999 -t 127.0.0.1:$3 -d 0 --clean
python /usr/local/bin/redis-copy.py -s 127.0.0.1:9999 -t 127.0.0.1:$3 -d 0
ps auwx | grep "$WHOAMI@$2" | awk '{ print $2 }' | xargs kill
else
echo "Usage: octo move:redis {ip-address} {port}"
fi
;;
restart)
if [ $2 ]; then
show_break
echo "Restarting $2."
newrev=`cd /home/git/$2.git; git log . | head -1 | cut -d ' ' -f 2`
branch=`cd /home/git/$2.git; git branch | grep "*" | cut -d ' ' -f 2`
cd /home/git/$2.git; sudo git archive $newrev | /home/git/receiver "$2" "$newrev" "git" "NOTHING HERE" "$branch"
sudo rm -rf $SRC_DIR/$2
else
echo "Usage: octo restart {container}"
fi
;;
reload)
if [ $2 ]; then
BASE="$2"
NUM_CONTAINERS=$(/usr/bin/octo config:get $BASE/CONTAINERS)
NUM_CONTAINERS=${NUM_CONTAINERS:-1}
/usr/bin/octo stop $BASE
/usr/bin/octo start $BASE $NUM_CONTAINERS
else
echo "Usage: octo reload {container}"
fi
;;
push)
if [ $2 ]; then
BASE="$2"
if [ -n "$PRIVATE_REGISTRY" ]; then
sudo docker tag -f $BUILD_ORG_NAME/$BASE $PRIVATE_REGISTRY/$BASE
sudo docker push $PRIVATE_REGISTRY/$BASE
else
echo "This feature requires a private registry."
fi
else
echo "Usage: octo push {container}"
fi
;;
pull)
if [ $2 ]; then
BASE="$2"
if [ -n "$PRIVATE_REGISTRY" ]; then
sudo docker -H $DOCKER_HOST pull $PRIVATE_REGISTRY/$BASE
sudo docker -H $DOCKER_HOST tag -f $PRIVATE_REGISTRY/$BASE $BUILD_ORG_NAME/$BASE
else
echo "This feature requires a private registry."
fi
else
echo "Usage: octo pull {container}"
fi
;;
update)
echo "Updating your /usr/bin/octo from $OCTO_BIN"
cd /usr/bin/ && rm -f octo && wget -q $OCTO_BIN && chmod 755 octo
;;
version)
if [ -f '/var/archive/version' ]; then
echo "==== octohost version ===="
sudo cat /var/archive/version
fi
echo "==== Docker Version ===="
sudo docker version
echo "==== Consul Version ===="
sudo consul version
if [ -d '/var/archive' ]; then
echo "All source code for this installation is in /var/archive/"
fi
;;
consul:reset)
sudo service consul stop
rm -rf /var/cache/octohost/*
sudo service consul start
;;
docker:reset_key)
sudo service docker stop
rm -f /etc/docker/key.json
sudo service docker start
;;
help|-h|--help)
echo "Usage: octo status|clean|restart|reload|remove|move|config|logspout|logs|update"
cat<<EOF
octo status Show basic app status: OK or DOWN.
octo clean Remove unused and stopped containers.
octo start {image} Start a {container} from an already built image.
octo stop {container} Stop all {containers} that match.
octo restart {container} Rebuild and restart {container} from src stored in $SRC_DIR.
octo reload {container} Reload {container} without a rebuild.
octo remove {container} Remove {container}, the src and the service entry.
octo move:sites {ip-address} Move sites from targeted octohost, rebuilding all containers from src.
octo move:config {ip-address} Move config from targeted octohost.
octo move:redis {ip-address} {port} Move all keys from Redis from {ip-address}:{port} to 127.0.0.1:{port}
octo push {image} Tag and push {image} to a private registry.
octo pull {image} Pull and re-tag {image} from a private registry.
octo config {container} Show all ENV variables for {container}.
octo config:set {container/key} {var} Set an ENV variable for {container}.
octo config:get {container/key} Get an ENV variable for {container}.
octo config:rm {container/key} Remove an ENV variable for {container}.
octo config:export Get all config variables for all containers.
octo domains:set {container} {domains} Put list of comma separated domains into Consul for a {container}
octo domains:get {container} Get list of domains from Consul for a {container}
octo service:set {container} {ip} {port} Add service to Consul.
octo service:rm {container} {ip} {port} Remove a service from Consul.
octo service:tags {container} Get service tags from CONSUL_TAGS ENV in a container.
octo services List all services that the local Consul agent knows about.
octo services:catalog List all services that the Consul cluster knows about.
octo services:clear Remove all services from Consul.
octo services:register Register all running containers with Consul.
octo consul:reset Remove all data from Consul and restart fresh.
octo logspout {pull|start|stop} Pull/start/stop progrium/logspout image.
octo logs octostart Use remote_syslog to send Docker and nginx logs to remote syslog.
octo logs octostop Stop sending Docker and nginx logs to remote syslog.
octo logs start {full-log-path} {mount} Use remote_syslog to send {full-log-path} to remote syslog.
octo update Update octo command from $OCTO_BIN.
EOF
for script in $(ls -d /usr/local/octohost/plugins/*/commands); do
$script "$@"
done
;;
*)
for script in $(ls -d /usr/local/octohost/plugins/*/commands); do
$script "$@"
done
;;
esac