-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathconfig.fn
188 lines (172 loc) · 5.27 KB
/
config.fn
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
# vim: set ft=sh:
config_code_version() {
local version
version=$(git describe --tags --exact-match 2> /dev/null | grep "^coog-" | head -1)
if [ -z "$version" ]
then
version=$(git rev-parse --abbrev-ref HEAD)
else
version=$(echo "$version" | grep -oP "coog-\d+\.\d+")
fi
if [ "$version" != HEAD ]
then
echo "$version"
fi
}
config_version() {
( cd "$COOG_CODE_DIR" && config_code_version )
}
config_data_branch_exists() {
local l
l=$(git show-branch --list | grep -c "\[$1\]")
[ "$l" -eq 0 ] && return 1
return 0
}
config_data_path_changed() {
local n; n=$(git status -s --porcelain -- "$1" | wc -l)
[ "$n" -ne 0 ] && return 0
return 1
}
config_data_commit() {
git -c user.name="$USER" -c user.email="$USER@$HOSTNAME.io" commit -q --allow-empty "$@"
}
config_data_check() {
local n; n=$(git status -s --porcelain | wc -l)
if [ "$n" -eq 0 ]
then
return 0
else
echo "data repo is not clean ($COOG_DATA_DIR)" \
&& git status \
&& return 1
fi
}
config_data_gitignore() {
{
echo "/redis"
echo "/postgres"
echo "/dwh"
echo "/coog/edm"
echo "/coog/batch"
echo "/mongo"
} >> .gitignore
}
config_data_bootstrap() {
git init -q \
&& git checkout -q -b init \
&& config_data_gitignore \
&& git add .gitignore \
&& config_data_commit -m "bootstrap"
}
config_data_migrate() {
git checkout -q init \
&& git checkout -q -b "$1" \
&& git add . \
&& config_data_commit -m "migrate"
}
config_data_reset_base() {
git rm -q --ignore-unmatch config \
&& cp "$COOG_CODE_DIR/defaults/config" "$COOG_DATA_DIR/config" \
&& git add config \
&& config_data_commit -m "reset base"
}
config_data_reset_coog() {
git rm -q --ignore-unmatch -r coog* \
&& mkdir -p coog \
&& mkdir -p coog/conf \
&& mkdir -p coog/edm \
&& mkdir -p coog/batch \
&& touch coog/conf/coog.conf \
&& touch coog/conf/batch.conf \
&& git add coog* \
&& config_data_commit -m "reset coog"
}
config_data_reset_nginx_cp() {
local def; def="$COOG_CODE_DIR/defaults/nginx"
cp -f "$def/nginx.conf" "nginx/nginx.conf"
if [ -z "$COOG_IMAGE" ]
then
touch "nginx/http-coog.conf"
touch "nginx/server-coog.conf"
touch "nginx/server-coog-ssl.conf"
else
cp -f "$def/http-coog.conf" "nginx/http-coog.conf"
cp -f "$def/server-coog.conf" "nginx/server-coog.conf"
cp -f "$def/server-coog-ssl.conf" "nginx/server-coog-ssl.conf"
fi
if [ -z "$WEB_IMAGE" ]
then
touch "nginx/http-web.conf"
touch "nginx/server-web.conf"
else
cp -f "$def/http-web.conf" "nginx/http-web.conf"
cp -f "$def/server-web.conf" "nginx/server-web.conf"
fi
touch "nginx/main-custom.conf"
touch "nginx/events-custom.conf"
touch "nginx/http-custom.conf"
touch "nginx/server-custom.conf"
if [ -z "$NGINX_SSL_METHOD" ]
then
NGINX_SERVER_CONF=nginx_server.conf
else
source "$COOG_CODE_DIR/ssl/$NGINX_SSL_METHOD/init"
fi
cp -f "$def/$NGINX_SERVER_CONF" "nginx/$NGINX_SERVER_CONF"
}
config_data_reset_nginx_sedf() {
sed -i "s/NETWORK_NAME/$NETWORK_NAME/g" "$1" \
&& sed -i "s/NGINX_SERVER_CONF/$NGINX_SERVER_CONF/g" "$1" \
&& sed -i "s/NGINX_SSL_SERVER_NAME/$NGINX_SSL_SERVER_NAME/g" "$1" \
&& sed -i "s/COOG_DB_NAME/$COOG_DB_NAME/g" "$1"
}
config_data_reset_nginx_sed() {
find nginx -maxdepth 1 -type f | while read f
do
config_data_reset_nginx_sedf "$f" || return 1
done
}
config_data_reset_nginx() {
git rm -q --ignore-unmatch -r nginx* \
&& mkdir -p "nginx" \
&& config_data_reset_nginx_cp \
&& config_data_reset_nginx_sed \
&& git add nginx* \
&& config_data_commit -m "reset nginx"
}
config_check() {
[ ! -d "$COOG_DATA_DIR" ] || [ ! -d "$COOG_DATA_DIR/.git" ] \
&& echo "execute init to setup coog-admin" >&2 \
&& return 1
( cd "$COOG_DATA_DIR" && config_data_check ) || return $?
local code; code="$(config_version)"
[ -z "$code" ] \
&& echo "code version is empty" >&2 \
&& return 1
( cd "$COOG_DATA_DIR" && config_data_branch_exists "$code" )
if [ $? -eq 0 ]
then
( cd "$COOG_DATA_DIR" && git checkout -q "$code" )
else
echo
echo "You are switching to a new branch: $code"
echo " - This will **reset** your configuration (based on defaults)"
echo " - Nothing will be lost (your configuration is **archived**)"
echo
read -p "Confirm switching? [y/N] " ok
case "$ok" in
y|Y) ( cd "$COOG_DATA_DIR" \
&& git checkout -q init \
&& git checkout -q -b "$code" \
&& config_data_reset_base \
&& config_data_reset_coog \
&& config_data_reset_nginx \
&& git tag "init-$code" )
;;
n|N) echo "leaving, please switch branch on coog-admin and retry" && return 1
;;
*) echo "leaving, please switch branch on coog-admin and retry" && return 1
;;
esac
fi
}