-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.sh
executable file
·358 lines (319 loc) · 8.93 KB
/
run.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
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
#!/usr/bin/env bash
#### Written by: Catalin Airimitoaie - [email protected]
#### Description: ODM main executable file
me=$(basename "$0")
usage="Usage: $me [CONTAINER] [OPTION]...
This script provides two modes of functionality, via options or positional arguments of a given container.
If the first argument given to this script does not start with a hyphen (-), it will assume you're trying to launch it with the latter functionality
POSITIONAL ARGUMENTS:
Usage: $me new|list|<container name>
ARGUMENTS:
n|ne|new <container name> <odoo version> <port> [postgres docker version]
create and build a new container with the given options, the postgres docker version is optional since it defaults to latest
l|li|lis|list option [--all]
list status of containers built by this script.
list all docker containers if --all is specified
<container name> argument
b|boo|boot boots container
c|cd path cds into given path starting at the <container name>
e|en|ent|ente|enter opens an interactive bash shell inside <container name>
--as user
--shell shell
k|kil|kill stops <container name>
s|st|sta|star|start
u|up|upd|upda|updat|update updates the odoo version from upstream - git pull
OPTIONS:
-n, --new <container name> <odoo version> <port> [postgres docker version]
create and builds a new container with the given options, the postgres docker version is optional since it defaults to latest
-b, --boot [<container name1> <container name2>...]
start containers
-d, --delete [<container name1> <container name2>...]
kill and removes containers
-l, --list
list the status of the current built containers
-s, --start <container name> [<odoo server option 1> <odoo server option 2>...]
start [CONTAINER]'s Odoo server with [OPTIONS]
example: $me -s odoo12 -u sale -d base
-k, --kill <container name>
stop the container
-e, --enter <container name>
log into <container name> with a bash shell session
-r, --run <container name> <user> <command>
run <command> inside <container name>
-c, --clone <container> <script> <version>
launch a predefined clone script. see repos/README.md
-h, --help
display this message
"
#### DEFINED PATHS
ROOT=$(dirname "$(readlink -f "$0")")
REPOS=${ROOT}/repos
CONTAINERS=${ROOT}/containers
cd "$ROOT" || exit
#### GLOBALS
ID=$(id -u)
### colors
declare -A color_codes=(["command"]='\e[34m' ["container"]='\e[96m' ["start"]='\e[32m' ["stop"]='\e[31m' ['good']='\e[92m' ['bad']='\e[91m')
wipe='\033[1m\033[0m'
#### PARSER
function parse_positional_arguments() {
case "$1" in
n | ne | new)
shift
new "$1" "$2" "$3" "$4"
;;
l | li | lis | list)
all=0
for arg in "$@"; do
if [[ "$arg" == "--all" ]]; then
all=1
fi
done
if [[ "$all" == 1 ]]; then
docker ps -a
else
containers=$(ls containers)
docker ps -a | grep "CONTAINER ID"
for container in ${containers}; do
docker ps -a | grep "$container"
done
fi
;;
*)
if container_exists "$1" 1; then
parse_container_args "$@"
else
echo "container ""$1"" does not exist"
fi
;;
esac
}
function parse_container_args() {
container="$1"
shift
first_level_arg="$1"
case "$first_level_arg" in
c | cd)
change_directory "$container" "$@"
;;
e | en | ent | ente | enter)
user=
shell=
i=0
# shellcheck disable=SC2124
args="$@"
args_list=("$@")
for arg in ${args}; do
if [[ "$arg" == "--as" ]]; then
user=${args_list[$((i + 1))]}
elif [[ "$arg" == "--shell" ]]; then
shell=${args_list[$((i + 1))]}
fi
i=$((i + 1))
done
if [[ -n "$user" ]]; then
if [[ -n "$shell" ]]; then
docker exec -it "$container" su -s "$shell" - "$user"
else
docker exec -it "$container" su - "$user"
fi
else
docker exec -it "$container" su - odoo
fi
;;
b | bo | boo | boot)
boot "$container"
;;
s | st | sta | star | start)
shift
start "$container" "$@"
;;
k | ki | kil | kill)
shift
kill "$container" "$@"
;;
u | up | upd | upda | updat | update)
shift
docker exec -it "$container" git -C /opt/odoo/sources/odoo pull
;;
*)
help
;;
esac
}
#### FUNCTIONS
function container_exists() {
if [[ -z "$1" ]]; then
return 1
else
return 0
fi
}
function help() {
echo -e "$usage"
}
function change_directory() {
container="$1"
shift
dir="$2"
cd "$CONTAINERS/$container/custom/$dir" || exit
exec "$SHELL"
}
function boot() {
for container in "$@"; do
echo -e "${color_codes["stop"]}stopping $wipe eventual instance of ${color_codes[container]}$container"
echo -e "${color_codes["start"]}starting $wipe ${color_codes['container']}$container $wipe"
cd "$CONTAINERS"/"$container" || exit
docker-compose up -d
done
}
function delete() {
for container in "$@"; do
echo "This will delete all containers associated with this instance, including the postgres, and its files."
echo "Are you sure you want to proceed [y/N]?"
local delete='n'
read -r delete
case "$delete" in
y | Y)
cd "$CONTAINERS"/"$container" 2>/dev/null || echo -e "${color_codes[container]} ${container} $wipe does not exist" || exit
echo -e "${color_codes["stop"]}stopping $wipe ${color_codes[container]}$container"
docker-compose down
rm -rf "${CONTAINERS:?}/${container:?}"
;;
*)
echo "${container} was not deleted"
;;
esac
done
}
function start() {
container=$1
shift
echo -e "starting ${color_codes[container]}$container's Odoo server $wipe"
docker exec -it "$container" runuser odoo -c 'cd; cd custom/etc; kill -9 $(cat run.pid)'
local ARGS="$*"
local ETC="/opt/odoo/custom/etc"
docker exec -it "$container" runuser odoo -c "cd; $ETC/generate-conf.sh; $ETC/run-odoo.sh $ARGS"
}
function run() {
container=$1
shift
user=$1
echo -e "running ${color_codes[command]}$* $wipe into ${color_codes[container]}$container $wipe via $user"
shift
local array=("$@")
local len=${#array[@]}
args=${array[*]:0:${len}}
docker exec -it "$container" runuser "$user" -c "$args"
}
function clone() {
container=$1
repo=$2
branch=$3
if [[ -f "$REPOS/$repo.sh" ]]; then
echo -e "launching clone script ${color_codes["good"]} $repo.sh $wipe"
"$ROOT"/repos/"$repo".sh "$CONTAINERS"/"$container" "$branch"
else
echo -e "repo script ${color_codes["bad"]} $repo.sh $wipe does not exist"
fi
}
function kill() {
echo -e "stopping $1"
cd "$CONTAINERS"/"$1" || exit
docker-compose stop
echo -e "$1 stopped"
}
function update() {
echo -e "stopping $1"
echo -e "starting $1"
echo -e "starting the odoo server and updating $2"
start "$1" "$2"
}
function new() {
CONTAINER="$CONTAINERS/$1"
if [[ ! -d "$CONTAINER" ]]; then
ODOO_VERSION="$2"
PORT="$3"
POSTGRESQL_VERSION="${4:-latest}"
env_template="odoo_version=$ODOO_VERSION\nid=$ID\ncontainer_name=$1\nport=$PORT\naddons_path=$CONTAINER/custom/addons\ndata_path=$CONTAINER/custom/data\netc_path=$CONTAINER/custom/etc\nsources_path=$CONTAINER/custom/sources\npostgres_version=$POSTGRESQL_VERSION"
mkdir -p "$CONTAINER"/custom/{sources,etc,addons,data}
cp templates/template.conf "$CONTAINER"/custom/etc/odoo.conf
echo -e "$env_template" >"$CONTAINER"/.env
cp templates/docker-compose.yml "$CONTAINER"/docker-compose.yml
cp dockerfiles/custom/Dockerfile "$CONTAINER"/Dockerfile
./build.sh "$CONTAINER"
git clone --depth=1 --branch="$ODOO_VERSION" https://github.com/odoo/odoo.git "$CONTAINER"/custom/sources/odoo || exit 1
boot "$1"
run "$1" root chown -R odoo:odoo /opt/odoo/sources/odoo || exit 1
run "$1" odoo virtualenv -p python3.6 /opt/odoo/sources/odoo/env || exit 1
PIPYENV="/opt/odoo/sources/odoo/env/bin/pip"
run "$1" odoo "$PIPYENV" install -r /opt/odoo/sources/odoo/doc/requirements.txt || exit 1
run "$1" odoo "$PIPYENV" install -r /opt/odoo/sources/odoo/requirements.txt || exit 1
run "$1" odoo "$PIPYENV" install phonenumbers xmlsig workdays numpy unidecode zeep pandas || exit 1
echo -e "
You can now ${color_codes['good']} boot $wipe the newly created container by typing:\n
$0 -b $1\n
And ${color_codes['good']} start $wipe the Odoo server with:\n
$0 -s $1\n"
else
echo "The container already exists"
fi
}
##### ARGUMENTS PARSER
case "$1" in
-n | --new)
shift
if [[ -z "$1" ]]; then
help
fi
new "$@"
;;
-b | --boot)
shift
boot "$@"
;;
-c | --clone)
shift
clone "$@"
;;
-d | --delete)
shift
delete "$@"
;;
-s | --start)
shift
start "$@"
;;
-r | --run)
shift
run "$@"
;;
-u | --update)
shift
update "$@"
;;
-k | --kill)
kill "$2"
;;
-e | --enter)
shift
user="odoo"
if [[ -n "$2" ]]; then
user=$2
fi
docker exec -it "$1" su - "$user"
;;
-l | --list)
docker ps -a
;;
-h | --help)
help
;;
*)
if [[ -z "$1" ]]; then
help
else
parse_positional_arguments "$@"
fi
;;
esac