-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.sh
executable file
·183 lines (160 loc) · 4.43 KB
/
bootstrap.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
#!/bin/bash
set -e
SELF_UPDATE_GIT_REMOTE='git://github.com/swingdev/bootstrap-bootstrap.git'
SELF_UPDATE_STAMP_FILE='./._last_self_updated'
COMMAND_STATUS='status'
COMMAND_BRANCH='branch'
COMMAND_ADD='add-module'
COMMAND_DOWN='down'
COMMAND_UP='up'
COMMAND_BUILD='build'
COMMAND_LOGS='logs'
COMMAND_EXEC='exec'
DOCKER_COMPOSE_FILE='docker-compose.yml'
# Resolving Arguments
while getopts c: name
do
case $name in
c) DOCKER_COMPOSE_FILE="$OPTARG";;
?) printf "Usage: %s: [-c configuration-file] command\n" $0
exit 2;;
esac
done
shift $(($OPTIND - 1))
# -- End - Resolving Arguments
function indent_string() {
sed -e 's/^/ /'
}
function self_heal() {
git remote rm upstream 2>/dev/null || true
}
function self_update() {
git fetch ${SELF_UPDATE_GIT_REMOTE} master
git merge FETCH_HEAD
}
function self_update_if_its_time() {
stamp_file_content="1970-01-01T00:00:00Z"
if [ -f $SELF_UPDATE_STAMP_FILE ]; then
stamp_file_content=$(cat $SELF_UPDATE_STAMP_FILE)
fi
now=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" $(date -u +"%Y-%m-%dT%H:%M:%SZ") +%s 2>/dev/null)
stamp=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" ${stamp_file_content} +%s 2>/dev/null)
delta=$(($now-$stamp))
update_if_n_sec_passed=$((60*60*24*1))
if [ $delta -ge $update_if_n_sec_passed ]; then
echo "Performing self update..."
self_update
echo "Done."
echo ""
echo $(date -u +"%Y-%m-%dT%H:%M:%SZ") > $SELF_UPDATE_STAMP_FILE
fi
}
function iterate_over_modules() {
fn=$1;
while IFS='' read -r line; do
el=($line)
repo="${el[0]}"
dir="${el[1]}"
($fn $repo $dir)
done < "modules.txt"
}
function fetch_or_update() {
repo=$1;
dir=$2;
if [ -e "./$dir" ]; then
echo "${dir}: Updating"
cd ${dir}
git checkout -q master
git pull | indent_string
cd ..
else
echo "${dir}: Fetching from ${repo}"
git clone ${repo} ${dir} 2>&1 | indent_string
fi
}
function status() {
repo=$1;
dir=$2;
if [ -e "./$dir" ]; then
echo "${dir}"
cd ${dir}
git status | indent_string
cd ..
else
echo "{$dir} does not exist"
fi
}
function branch() {
repo=$1;
dir=$2;
if [ -e "./$dir" ]; then
cd ${dir}
branch=`git rev-parse --abbrev-ref HEAD`
printf "%-15s %s\n" "${dir}:" $branch
# echo -e "${dir}: \t\t"
cd ..
else
echo "${dir} does not exist"
fi
}
function add_module() {
url=$1;
folder=$2;
echo "Adding module $url $folder";
echo "$url $folder" >> modules.txt
(fetch_or_update $url $folder);
if ! grep -q "^${folder}\$" .gitignore; then echo "${folder}" >> .gitignore ; fi
# Updating docker compose
if [ -f "${folder}/.docker-compose.template.yml" ]; then
echo "Updating $DOCKER_COMPOSE_FILE"
TAB=$'\t'
echo -e "${TAB}${folder}:" >> docker-compose.yml
sed \
-e "s/^/${TAB}${TAB}/" \
-e "s/\<PROJECT_DIR\>/\.\/$folder/" \
"./${folder}/.docker-compose.template.yml" >> $DOCKER_COMPOSE_FILE
else
echo "No template for docker compose module, you have to setup the module manually.";
fi
}
function docker_compose_down() {
docker-compose -f $DOCKER_COMPOSE_FILE down $@
}
function docker_compose_up() {
docker-compose -f $DOCKER_COMPOSE_FILE up -d --remove-orphans --build $@
}
function docker_compose_build() {
docker-compose -f $DOCKER_COMPOSE_FILE build --force-rm $@
}
function docker_compose_exec() {
docker-compose -f $DOCKER_COMPOSE_FILE exec $@
}
function docker_compose_logs() {
echo "Attaching logs. Press Ctrl+C twice to exit."
while sleep .5; do
echo "Reattaching logs. Press Ctrl+C twice to exit."
docker-compose -f $DOCKER_COMPOSE_FILE logs --tail 100 --follow $@
done
}
self_heal
self_update_if_its_time
case "$1" in
"$COMMAND_STATUS")
(iterate_over_modules status);;
"$COMMAND_BRANCH")
(iterate_over_modules branch);;
"$COMMAND_ADD")
(add_module $2 $3);;
"$COMMAND_DOWN")
(docker_compose_down "${@:2}");;
"$COMMAND_UP")
(docker_compose_up "${@:2}");;
"$COMMAND_BUILD")
(docker_compose_build "${@:2}");;
"$COMMAND_EXEC")
(docker_compose_exec "${@:2}");;
"$COMMAND_LOGS")
(docker_compose_logs "${@:2}");;
"")
(iterate_over_modules fetch_or_update);;
esac