This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
/
octo
executable file
·124 lines (111 loc) · 3.06 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
#!/bin/bash
# Directories and domain names.
SRC_DIR="/home/git/src/"
PUBLIC_IP=$(curl -s http://ipv4.icanhazip.com)
DOMAIN_SUFFIX="$PUBLIC_IP.xip.io"
get_short_container_id ()
{
docker ps | grep "octohost/$1" | cut -d ' ' -f 1
}
get_long_container_id ()
{
docker inspect $1 | jq '.[0].ID' | cut -d '"' -f 2
}
get_memory_usage ()
{
cat /sys/fs/cgroup/memory/lxc/$1/memory.usage_in_bytes
}
reformat_bytes ()
{
(( size = $1/1024/1024 ))
echo "($size MB)"
}
show_memory_usage ()
{
ID=$(get_short_container_id $1)
LONG=$(get_long_container_id $ID)
MEMORY=$(get_memory_usage $LONG)
MB=$(reformat_bytes $MEMORY)
echo -n "$MB: "
}
case "$1" in
status)
cd $SRC_DIR
for i in *.git; do
site=`basename $i .git`
echo -n "$site "
show_memory_usage $site
if (wget -S -t 1 -T 2 --delete-after -qO- http://$site.$DOMAIN_SUFFIX 2>&1 | grep -q "HTTP/1.1 200 OK"); then
echo -e "\e[32mOK\e[0m"
else
echo -e "\e[31mDOWN\e[0m"
fi
done
;;
clean)
echo "Cleaning old exited containers."
# TODO: Errors if there's no containers - annoying.
docker ps -a | grep "Exit" | cut -d ' ' -f 1 | xargs docker rm
echo "Showing all remaining containers."
docker ps -a
echo "Cleaning out all unused images."
docker images | grep "\<none\>" | awk '{ print $3 }' | xargs docker rmi
echo "Showing all remaining images."
docker images
;;
remove)
if [ $2 ]; then
if [ -d /home/git/$2.git ]; then
rm -rf /home/git/$2.git
else
echo "There is no source with that name."
exit
fi
if [ -d /home/git/src/$2.git ]; then rm -rf /home/git/src/$2.git; fi
echo "Removed the source for $2."
OLD_ID=$(docker ps | grep "$2:latest" | cut -d ' ' -f 1)
if [ $OLD_ID ]; then
docker kill $OLD_ID
echo "Removed running conainter."
fi
/usr/bin/redis-cli del frontend:$2.$DOMAIN_SUFFIX >/dev/null
else
echo "Need the name to remove."
fi
;;
move)
if [ $2 ]; then
if [ "$2" == 'sites' ]; then
if [ $3 ]; then
# Grab all of the sites.
/usr/bin/rsync -avz ubuntu@$3:/home/git/*.git /home/git/
cd /home/git
for i in *.git; do
site=`basename $i .git`
/usr/bin/octo restart $site
# Need a checkout and not a bare repo to build with later.
git clone /home/git/$site.git /home/git/src/$site.git
done
chown -R git.git /home/git/src
else
echo "Need a source."
fi
fi
else
echo "Please enter '/usr/bin/octo move sites source.ip.address'"
fi
;;
restart)
if [ $2 ]; then
echo "Restarting $2."
newrev=`cd /home/git/$2.git; git log . | head -1 | cut -d ' ' -f 2`
cd /home/git/$2.git; git archive $newrev | /home/git/receiver "$2" "$newrev" "git" "NOTHING HERE"
rm -rf /home/git/src/$2
else
echo "Need a site to restart."
fi
;;
*)
echo "Usage: octo status|clean|restart \$site|remove \$site|move"
;;
esac