-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcoog
executable file
·293 lines (267 loc) · 9.01 KB
/
coog
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
#!/bin/bash
if [ -z "$COOG_CODE_DIR" ] || [ ! -d "$COOG_CODE_DIR" ] || [ -z "$COOG_DATA_DIR" ]
then
{
echo "Please make sure that these two env vars are set:"
echo " COOG_CODE_DIR: your coog-admin install folder"
echo " COOG_DATA_DIR: the folder where to keep your custom config"
} >&2 && exit 1
fi
########
# args #
########
session_args() {
[ ! -z "$COOG_SESSION_SUPERPWD" ] && echo "-e COOG_SESSION_SUPERPWD=$COOG_SESSION_SUPERPWD"
}
postgres_args() {
local args
if [ ! -z "$COOG_POSTGRES_HOST" ]
then
args="-e COOG_POSTGRES_HOST=$COOG_POSTGRES_HOST"
[ ! -z "$COOG_POSTGRES_PORT" ] && args="$args -e COOG_POSTGRES_PORT=$COOG_POSTGRES_PORT"
else
args="$args -e COOG_POSTGRES_HOST=$NETWORK_NAME-postgres"
fi
echo "$args"
}
redis_args() {
local args
if [ ! -z "$COOG_REDIS_HOST" ]
then
args="-e COOG_REDIS_HOST=$COOG_REDIS_HOST"
[ ! -z "$COOG_REDIS_PORT" ] && args="$args -e COOG_REDIS_PORT=$COOG_REDIS_PORT"
else
args="$args -e COOG_REDIS_HOST=$NETWORK_NAME-redis"
fi
echo "$args"
}
report_args() {
[ -z "$COOG_REPORT_API" ] \
&& COOG_REPORT_API="http://$NETWORK_NAME-unoconv:5000/unoconv/{oext}/"
echo "-e COOG_REPORT_API=$COOG_REPORT_API"
}
db_args() {
local args
[ ! -z "$COOG_DB_LANG" ] && args="$args -e COOG_DB_LANG=$COOG_DB_LANG"
[ ! -z "$COOG_DB_USER" ] && args="$args -e COOG_DB_USER=$COOG_DB_USER"
[ ! -z "$COOG_DB_PASSWORD" ] && args="$args -e COOG_DB_PASSWORD=$COOG_DB_PASSWORD"
[ ! -z "$COOG_DB_NAME" ] && args="$args -e COOG_DB_NAME=$COOG_DB_NAME"
echo "$args"
}
cache_args() {
local args
[ ! -z "$COOG_CACHE_MODEL" ] && args="$args -e COOG_CACHE_MODEL=$COOG_CACHE_MODEL"
[ ! -z "$COOG_CACHE_RECORD" ] && args="$args -e COOG_CACHE_RECORD=$COOG_CACHE_RECORD"
[ ! -z "$COOG_CACHE_FIELD" ] && args="$args -e COOG_CACHE_FIELD=$COOG_CACHE_FIELD"
[ ! -z "$COOG_CACHE_COOG" ] && args="$args -e COOG_CACHE_COOG=$COOG_CACHE_COOG"
[ ! -z "$COOG_CACHE_DB" ] && args="$args -e COOG_CACHE_DB=$COOG_CACHE_DB"
echo "$args"
}
celery_args() {
[ ! -z "$COOG_CELERY_DB" ] && echo "-e COOG_CELERY_DB=$COOG_CELERY_DB"
}
rq_args() {
[ ! -z "$COOG_RQ_DB" ] && echo "-e COOG_RQ_DB=$COOG_RQ_DB"
}
sentry_args() {
local args
if [ -z "$COOG_SENTRY_PROTOCOL" ] || [ -z "$COOG_SENTRY_HOST" ] || [ -z "$COOG_SENTRY_PORT" ]
then
COOG_SENTRY_PROTOCOL=http
COOG_SENTRY_HOST="$NETWORK_NAME-sentry"
COOG_SENTRY_PORT=9000
fi
if [ ! -z "$COOG_SENTRY_PUB" ] && [ ! -z "$COOG_SENTRY_SEC" ] && [ ! -z "$COOG_SENTRY_PROJECT" ]
then
args="$args -e COOG_SENTRY=$COOG_SENTRY_PROTOCOL://$COOG_SENTRY_PUB:$COOG_SENTRY_SEC@$COOG_SENTRY_HOST:$COOG_SENTRY_PORT/$COOG_SENTRY_PROJECT"
echo "$args"
fi
}
_args() {
local args
args="-v $COOG_VOLUME:/workspace/io"
[ ! -z "$COOG_LOG_LEVEL" ] && args="$args -e LOG_LEVEL=$COOG_LOG_LEVEL"
[ ! -z "$COOG_TIMEOUT" ] && args="$args -e COOG_TIMEOUT=$COOG_TIMEOUT"
[ ! -z "$COOG_ADMIN_EMAIL" ] && args="$args -e COOG_ADMIN_EMAIL=$COOG_ADMIN_EMAIL"
[ ! -z "$COOG_SERVER_WORKERS" ] && args="$args -e COOG_SERVER_WORKERS=$COOG_SERVER_WORKERS"
[ ! -z "$COOG_CELERY_WORKERS" ] && args="$args -e COOG_CELERY_WORKERS=$COOG_CELERY_WORKERS"
[ ! -z "$COOG_TEST" ] && args="$args -e COOG_TEST=$COOG_TEST"
echo "$args $(session_args) $(postgres_args) $(redis_args) $(report_args) $(db_args) $(cache_args) $(celery_args) $(rq_args) $(sentry_args)"
}
_build_args() {
local VERSION;
VERSION=$1
export COOG_VERSION=$1
export COOG_STATIC_VERSION=$1
export NETWORK_NAME
if [ ! -z "$CUSTOMER" ]
then
echo $CUSTOMER
export CUSTOMER="-$CUSTOMER"
fi
}
###############
# entrypoints #
###############
_build() {
_build_args $1
cd "$COOG_CODE_DIR/images/coog" && ./build "$@"
}
_edit_ls() {
echo
( cd "$COOG_DATA_DIR/coog/conf" && find . -type f | cut -c 3- )
}
_edit_commit() {
config_data_path_changed "coog*" \
&& git add coog* \
&& config_data_commit -m "update coog - $1"
}
_edit() {
[ -z "$1" ] && echo "choose file to edit:" \
&& _edit_ls \
&& return 1
local f; f="$COOG_DATA_DIR/coog/conf/$1"
[ ! -f "$f" ] \
&& echo "bad file name, choose within:" \
&& _edit_ls \
&& return 1
"$EDITOR" "$COOG_DATA_DIR/coog/conf/$1"
( cd "$COOG_DATA_DIR" && _edit_commit "$1" )
}
_docker() {
[ -z "$1" ] && echo missing container && return 1
local cont; cont=$1; shift
docker "$@" "$NETWORK_NAME-coog-$cont"
}
_image() {
[ -z "$1" ] && echo missing command && return 1
local cmd; cmd=$(echo "$1" | cut -d "-" -f "1")
local args
args="--name $NETWORK_NAME-coog-$1 --entrypoint legacy.ep"
if [ "$cmd" = "server" ] || [ "$cmd" = "celery" ]
then
args="$args $DOCKER_DAEMON_OPTS"
elif [ "$cmd" = "admin" ]
then
args="$args $DOCKER_INTERACTIVE_OPTS"
elif [ "$cmd" = "rq" ]
then
args="$args $DOCKER_BURST_OPTS"
else
args="$args $DOCKER_PIPED_OPTS"
fi
shift
docker run \
--network "$NETWORK_NAME" \
$args \
$(_args) \
"$COOG_IMAGE" "$cmd" "$@"
}
_patch() {
if [[ "$1" = "help" ]]; then
echo "Usage: coog patch <repo> <file>"
echo ""
echo "Will apply <file> as a patch on <repo>"
echo "For instance,"
echo " coog patch coog patch_1234.diff"
echo "will try to apply the patch_1234.diff file as a git patch on "
echo "the coog repo inside the image"
echo ""
echo "This requires a running container to work. By default it will "
echo "be applied on the $NETWORK_NAME-coog-server container, but this "
echo "can be changed by setting the \$CONTAINER_NAME env variable"
echo ""
echo "Applied patches will be stored in the 'patches' folder in the "
echo "root folder of the container filesystem"
echo ""
echo "It will still be your responsibility to commit the container "
echo "in order to make the changes permanent"
return 0
fi
if [[ "$#" != 2 ]]; then
echo "Usage: coog patch <repo> <file>"
return 1
fi
local repo_path
local image_name
repo_path="/workspace/$1"
container=${CONTAINER_NAME:-$NETWORK_NAME-coog-server}
docker exec "$container" \
bash -c "git -C $repo_path init"
docker exec "$container" \
bash -c "git config --global user.email \"[email protected]\""
docker exec "$container" \
bash -c "git config --global user.name \"Admin\""
docker exec "$container" mkdir -p /patches
docker exec "$container" mkdir -p "/patches/$1"
docker cp "$2" "$container:/patches/$1/$(basename $2)"
docker exec "$container" \
bash -c "git config --global user.name \"Admin\""
docker exec "$container" \
bash -c "git -C $repo_path add . && git -C $repo_path commit -m Initial"
if [[ "$?" != 0 ]]; then
echo "Could not initialize git repo, aborting"
return 1
fi
docker exec -it "$container" \
bash -c "git -C $repo_path apply /patches/$1/$(basename $2)"
if [[ "$?" = 0 ]]; then
docker exec -it "$container" \
bash -c "git -C $repo_path add ."
docker exec -it "$container" \
bash -c "git -C $repo_path commit -m \"$(basename $2)\""
else
echo ""
echo "Patch application failed, you can try it manually by running the "
echo "following command from inside the container:"
echo " "
echo " git -C $repo_path apply /patches/$1/$(basename $2)"
return 1
fi
}
########
# main #
########
usage() {
echo
echo Admin commands
echo
echo " build -> build coog image: <tag> <repo:branch>+"
echo " reset -> reset coog configuration"
echo " edit -> edit coog configuration"
echo " patch -> apply a git patch on a part of the image"
echo
echo Docker commands
echo
echo " -- -> calls docker action on container - <cont> <act>"
docker run $DOCKER_PIPED_OPTS "$COOG_IMAGE"
return 0
}
set_runtime_vars() {
[ ! -z "$IMAGE" ] && COOG_IMAGE="$IMAGE"
[ ! -z "$LOG_LEVEL" ] && COOG_LOG_LEVEL="$LOG_LEVEL"
[ ! -z "$TEST" ] && COOG_TEST="$TEST"
[ ! -z "$DB_NAME" ] && COOG_DB_NAME="$DB_NAME"
if [ ! -z "$CACHE_DB" ]
then
if [ "$CACHE_DB" = "x" ]
then
unset COOG_CACHE_DB
else
COOG_CACHE_DB="$CACHE_DB"
fi
fi
}
main() {
source "$COOG_CODE_DIR/config"
set_runtime_vars
[ -z "$1" ] && usage && return 1
local cmd; cmd="$1"; shift
[ "$cmd" = "build" ] && { (_build "$@"); return $?; }
[ "$cmd" = "reset" ] && { ( cd "$COOG_DATA_DIR" && config_data_reset_coog "$@" ); return $?; }
[ "$cmd" = "edit" ] && { _edit "$@"; return $?; }
[ "$cmd" = "patch" ] && { _patch "$@"; return $?; }
[ "$cmd" = "--" ] && { _docker "$@"; return $?; }
_image "$cmd" "$@"
}
main "$@"