forked from adwid/db_manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
b
executable file
·241 lines (204 loc) · 5.7 KB
/
b
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
#!/bin/bash
function show_help {
echo "Usage: b [OPTIONS] NAME"
echo ""
echo "Based on NAME, the script sets up the Odoo environment. It sets:"
echo "- The OC/OE branch"
echo "- The PY environment"
echo "- The DB name"
echo ""
echo "Options:"
echo " -h, --help"
echo " Show help"
echo " -o VERSION, --odoo VERSION"
echo " Set Odoo version"
echo " -d DB, --database DB"
echo " Set database name"
echo " -f, --fetch"
echo " If NAME is considered as a branch and if that branch is not found "
echo " locally, try to find it on remote repositories"
echo " -R, --reset-branch"
echo " If the script finds the branch NAME, reset it with the remote one"
echo " -n, --no-init"
echo " Avoid DB initialization"
echo " -r, --restore"
echo " Force DB initialization (i.e., if the DB already exists, it will "
echo " restore it to its initialization savepoint"
echo " -i MODULES, --install-modules MODULES"
echo " Install the list of modules at the end of the configuration"
echo " -I MODULES, --init-modules MODULES"
echo " Same as -i but delete the existing DB first"
echo ""
echo "If NAME starts with '<version>-', the script will consider NAME as a potential"
echo "branch name. It will therefore check in the local repositories to find and use"
echo "it. Options -f and -R are only relevant in such situation."
echo ""
echo "If -d is not defined, the script will define the DB name itself:"
echo "- If NAME is a branch and contains an OPW number, the DB name will be:"
echo " <opw number>-<version>[-fw]"
echo " (The part '-fw' is defined when the branch's author is fw-bot)"
echo "- Else, NAME will be used as DB name"
echo ""
echo "If -o is not specified, the script will try to extract the odoo version from"
echo "NAME. Without any identified version, the execution is interrupted."
echo ""
echo "Notes: "
echo " - If Odoo version is master, the DB initialization is automatically skipped."
}
odoo=""
database=""
name=""
fetch=false
reset_branch=false
init=true
restore_init=false
modules_to_init=""
modules_to_install=""
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-o|--odoo)
odoo="$2"
shift
;;
-d|--database)
database="$2"
shift
;;
-f|--fetch)
fetch=true
;;
-R|--reset-branch)
reset_branch=true
;;
-n|--no-init)
init=false
;;
-r|--restore)
restore_init=true
;;
-i|--install-modules)
modules_to_install="$2"
shift
;;
-I|--init-modules)
modules_to_init="$2"
shift
;;
*)
if [ -z "$name" ]
then
name="$1"
else
>&2 echo "Incorrect options/arguments"
>&2 echo ""
>&2 show_help
exit 1
fi
;;
esac
shift
done
if [[ -z "$name" ]]; then
>&2 echo "Missing argument."
exit 1
fi
odoo=${odoo:-$name}
odoo=$(echo "$odoo" | grep -oE 'master|(saas-)?[0-9]{2}\.[0-9]' | head -1)
if [ -z "$odoo" ]
then
>&2 echo "Missing version (or incorrect version format)."
exit 1
fi
switch_branch() {
repo="$1"
branch_name="$2"
fetch="$3"
must_reset="$4"
cd "$HOME"/src/"$repo" || (>&2 echo "Repo $repo not found"; exit 1)
branch=$(git branch | grep -E "$branch_name\$" >/dev/null && echo "$branch_name")
if [ "$fetch" = true ] && [ -z "$branch" ]
then
echo "[$repo side] Nothing found. Looking on remote repo..."
branch=$(git ls-remote --exit-code --heads "$repo"-dev "$branch_name" >/dev/null 2>&1 && git fetch "$repo"-dev "$branch_name" && echo "$branch_name")
fi
if [ "$branch" ]
then
git checkout -q "$branch"
if [ "$must_reset" = true ]
then
echo "[$repo side] Resetting branch..."
git fetch "$repo"-dev "$branch_name" >/dev/null 2>&1 && git reset_branch --hard "$repo"-dev/"$branch_name" >/dev/null 2>&1 || echo "[$repo side] Reset failed."
fi
return 0
fi
return 1
}
ostate "Previous configuration"
echo
echo "Switching Odoo version..."
cv "$odoo" > /dev/null
if echo "$name" | grep -oEq '^master|(saas-)?[0-9]{2}\.[0-9]-.'
then
echo
echo "Looking for specific branch..."
switch_branch odoo "$name" "$fetch" "$reset_branch" &
oc_process=$!
switch_branch enterprise "$name" "$fetch" "$reset_branch" &
oe_process=$!
wait $oc_process
is_oc_branch=$?
wait $oe_process
is_oe_branch=$?
if [ $is_oc_branch = 0 ] || [ $is_oe_branch = 0 ]
then
is_branch=true
fi
fi
echo
echo "Specifying DB..."
if [ -z "$database" ]
then
opw=$(echo "$name" | grep -oE '([0-9]{3,})' | head -1)
if [ "$is_branch" = true ] && [ "$opw" ]
then
fw=$(echo "$name" | grep -Eo '\-fw$' || echo "")
database=$opw-$odoo$fw
else
database=$name
fi
fi
setdb "$database"
echo
ostate "New configuration"
if [ -n "$modules_to_init" ]; then
odoo-install "$modules_to_init"
elif [ "$odoo" != "master" ] && [ "$init" = true ] && ! ldb | grep -Eq "^$database$"
then
template="${odoo}__init"
if ldb -a | grep -Eq "^$template$"
then
echo
echo "Initializing DB & Filestore..."
createdb -T "$template" "$database"
mkdir -p ~/.local/share/Odoo/filestore/"$database"
cp -rf ~/.local/share/Odoo/filestore/"$odoo"/* ~/.local/share/Odoo/filestore/"$database"/
savedb init
fi
elif [ "$restore_init" = true ] && ldb -a | grep -Eq "^${database}__init$"
then
echo
echo "Restoring DB to initialization savepoint..."
restoredb init
fi
if [ -n "$modules_to_install" ]; then
echo
read -r -p "Install $modules_to_install ? (Y/n): " res
res=$(echo "$res" | tr '[:upper:]' '[:lower:]')
if [[ $res =~ ^(yes|y|)$ ]]; then
odoo-install -n "$modules_to_install"
fi
fi