5
# Create a synchronization environment and create an archive from it
17
mkdb_configfile="config.py"
18
mkdb_tmp_configfile="config_temp.py"
20
# official launchpad branches
21
branch_server="lp:unifield-server"
22
branch_addons="lp:unifield-addons"
23
branch_web="lp:unifield-web"
24
branch_wm="lp:unifield-wm"
25
branch_sync="lp:unifield-wm/sync_module_prod"
27
# temporary directory in which we will work
30
# default HQ/PROJECT/COORDO counts
44
check_cmd_presence() {
46
if [ -z $cmd_path ]; then
47
error_and_exit "$1 is missing.\nInstall it: sudo apt-get update && sudo apt-get install $2"
53
start-stop-daemon --stop --quiet --pidfile $1 --oknodo
56
stop_server_and_exit() {
57
stop_server "$1" && exit 1
64
# Configuration file exists and is readable
65
if ! [ -r $configfile ] ; then
66
error_and_exit "${configfile}: missing"
68
# Load configuration file
70
echo "${configfile}: imported"
72
# Check configuration file for MKDB script
73
if ! [ -a $mkdb_configfile ] ; then
74
if ! [ -a $mkdb_tmp_configfile ] ; then
75
error_and_exit "${mkdb_tmp_configfile}: missing"
77
# create mkdb configuration file
78
cp ${mkdb_tmp_configfile} ${mkdb_configfile}
81
# Check some programs presence
90
for cmd in "pip@pip" "bzr@bzr" "start-stop-daemon@dpkg" "tar@tar" "xz@xz-utils" "perl@perl-base" "psql@postgresql-client-common" "netstat@net-tools"; do
91
cmdname=`echo $cmd|cut -d '@' -f 1`
92
pkgname=`echo $cmd|cut -d '@' -f 2`
93
check_cmd_presence $cmdname $pkgname
96
# openerp-client-lib 1.0.1 presence
97
if ! [ `pip search openerp-client-lib|grep INSTALLED|cut -d ':' -f 2` == '1.0.1' ] ; then
98
error_and_exit "openerp-client-lib 1.0.1 missing.\nInstall it: sudo pip install openerp-client-lib==1.0.1"
100
echo "openerp-client-lib 1.0.1: installed."
102
# tmp directory presence. If not create it.
103
if [ -a "$tmpdir" ] ; then
104
if ! [ -d "$tmpdir" ] ; then
105
error_and_exit "$tmpdir exists but is not a directory. Change tmpdir variable."
108
mkdir "$tmpdir" || exit 1
110
echo "Temp. dir: $tmpdir"
116
## Fetch all branches (either from local or from remote)
117
for branch in "web@${branch_web}@${web}" "server@${branch_server}@${server}" "addons@${branch_addons}@${addons}" "wm@${branch_wm}@${wm}" "sync@${branch_sync}@${sync}"; do
118
# prepare the transaction
119
copy=`echo $branch|cut -d '@' -f 1`
120
remote=`echo $branch|cut -d '@' -f 2`
121
inlocal=`echo $branch|cut -d '@' -f 3`
123
if ! [ -d "$inlocal" ]; then
126
# Check if destination directory exists
127
# if not: fetch branch content either from local or from remote (regarding local existence)
128
if ! [ -d "${tmpdir}/${copy}" ]; then
129
echo -n "Fetching '$copy' from '$origin': "
130
bzr checkout -q --lightweight "$origin" "${tmpdir}/${copy}"
132
# if exists: update branch
135
echo -n "Updating '$copy': "
142
## Check which port to use
143
read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
144
($min, $max) = (8100, 8200);
146
$port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
148
for my $i ($min..$max) {
149
if( not exists $port{$i} and
150
not exists $port{$i+1} and
151
not exists $port{$i+2}) {
152
print join(" ", $i, $i+1, $i+2);
156
echo "xmlrpc port: ${XMLRPCPORT}"
157
echo "netrpc port: ${NETRPCPORT}"
158
echo "web port: ${WEBPORT}"
160
## Check that db is available
161
db_exists=`psql -l|grep "${dbname}_*"|wc -l`
162
while [ $db_exists -ne 0 ] ; do
164
db_exists=`psql -l|grep "${dbname}_*"|wc -l`
166
echo "database: ${dbname}"
168
## Change MKDB configuration file 'prefix' variable
169
sed -i "s#\(prefix =\).*#\1 '${dbname}'#g" ${mkdb_configfile}
170
## Same thing with different ports for server and web
171
sed -i "s#\(client_port =\).*#\1 ${XMLRPCPORT}#g" ${mkdb_configfile}
172
sed -i "s#\(server_port =\).*#\1 ${XMLRPCPORT}#g" ${mkdb_configfile}
173
sed -i "s#\(netrpc_port =\).*#\1 ${NETRPCPORT}#g" ${mkdb_configfile}
174
## Set HQ to 1, COORDO to 2 and PROJECT to 3
175
sed -i "s#\(hq_count =\).*#\1 ${hq_count}#g" ${mkdb_configfile}
176
sed -i "s#\(coordo_count =\).*#\1 ${coordo_count}#g" ${mkdb_configfile}
177
sed -i "s#\(project_count =\).*#\1 ${project_count}#g" ${mkdb_configfile}
180
pidfile="${tmpdir}/${dbname}.pid"
181
echo -n "Launching openerp-server: "
182
start-stop-daemon --start --quiet --pidfile ${pidfile} \
183
--background --make-pidfile --exec ${tmpdir}/server/bin/openerp-server.py -- \
184
--no-xmlrpcs --netrpc-port=${NETRPCPORT} --xmlrpc-port=${XMLRPCPORT} \
185
--addons-path=${tmpdir}/addons,${tmpdir}/wm,${tmpdir}/sync
186
# Wait 15 second before launching MKDB script
190
## Launch MKDB script
191
cd ${current_dir} && ./mkdb.py || stop_server_and_exit ${pidfile}
194
stop_server ${pidfile}
197
# create a list file containing all files to save
198
list="${tmpdir}/${dbname}.list"
199
if ! [ -a "$list" ] ; then
204
# save each DB in a dump file, add it to a list to save and drop DB
205
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${dbname}_%';"`; do
206
dumpfile="${db}.dump"
207
if [ -a "$dumpfile" ] ; then
208
error_and_exit "$dumpfile already exists! Aborted."
210
echo -e -n "Saving '$db' to $dumpfile: "
211
pg_dump -Fc $db > $dumpfile
212
echo "$dumpfile" >> ${list}
217
## Add some file into list file to archive
218
echo "manage_syncdb.sh" >> ${list}
219
echo "manage_syncdbrc" >> ${list}
222
echo -n "Creating archive: "
223
archive="`date +'%Y%m%d'`_${dbname}.tar.xz"
224
tar cfJ $archive `cat $list|tr -s '\n' ' '` || error_and_exit "An error occured to create archive: $archive."
225
echo "$archive DONE."
227
## Delete all DB dumps
230
########################
234
## TODO: Update CSV files in sync_module_prod for data to be correct
235
## TODO: update all accounts on all databases
236
## TODO: open all periods from january to today