~jfb-tempo-consulting/unifield-wm/sync-env-py3

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
#!/usr/bin/env bash
#
# export.sh
#
# Create a synchronization environment and create an archive from it

PROGRAM=`basename $0`
VERSION="0.0.0"
current_dir="$PWD"

#####
## Variables
###

# configuration files
configfile="exportrc"
mkdb_configfile="config.py"
mkdb_tmp_configfile="config_temp.py"

# official launchpad branches
branch_server="lp:unifield-server"
branch_addons="lp:unifield-addons"
branch_web="lp:unifield-web"
branch_wm="lp:unifield-wm"
branch_sync="lp:unifield-wm/sync_module_prod"

# temporary directory in which we will work
tmpdir="${HOME}/tmp"

# default HQ/PROJECT/COORDO counts
hq_count=1
coordo_count=2
project_count=3

#####
## FUNCTIONS
###

error_and_exit() {
  echo -e $1
  exit 1
}

check_cmd_presence() {
  cmd_path=`which $1`
  if [ -z $cmd_path ]; then
    error_and_exit "$1 is missing.\nInstall it: sudo apt-get update && sudo apt-get install $2"
  fi
  echo "$1: installed."
}

stop_server() {
  start-stop-daemon --stop --quiet --pidfile $1 --oknodo
}

stop_server_and_exit() {
  stop_server "$1" && exit 1
}

#####
## TESTS
###

# Configuration file exists and is readable
if ! [ -r $configfile ] ; then
  error_and_exit "${configfile}: missing"
fi
# Load configuration file
source $configfile
echo "${configfile}: imported"

# Check configuration file for MKDB script
if ! [ -a $mkdb_configfile ] ; then
  if ! [ -a $mkdb_tmp_configfile ] ; then
    error_and_exit "${mkdb_tmp_configfile}: missing"
  fi
  # create mkdb configuration file
  cp ${mkdb_tmp_configfile} ${mkdb_configfile}
fi

# Check some programs presence
# pip
# bzr
# start-stop-daemon
# tar
# xz
# perl
# psql
# netstat
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
  cmdname=`echo $cmd|cut -d '@' -f 1`
  pkgname=`echo $cmd|cut -d '@' -f 2`
  check_cmd_presence $cmdname $pkgname
done

# openerp-client-lib 1.0.1 presence
if ! [ `pip search openerp-client-lib|grep INSTALLED|cut -d ':' -f 2` == '1.0.1' ] ; then
  error_and_exit "openerp-client-lib 1.0.1 missing.\nInstall it: sudo pip install openerp-client-lib==1.0.1"
fi
echo "openerp-client-lib 1.0.1: installed."

# tmp directory presence. If not create it.
if [ -a "$tmpdir" ] ; then
  if ! [ -d "$tmpdir" ] ; then
    error_and_exit "$tmpdir exists but is not a directory. Change tmpdir variable."
  fi
else
  mkdir "$tmpdir" || exit 1
fi
echo "Temp. dir: $tmpdir"

#####
## MAIN
###

## Fetch all branches (either from local or from remote)
for branch in "web@${branch_web}@${web}" "server@${branch_server}@${server}" "addons@${branch_addons}@${addons}" "wm@${branch_wm}@${wm}" "sync@${branch_sync}@${sync}"; do
  # prepare the transaction
  copy=`echo $branch|cut -d '@' -f 1`
  remote=`echo $branch|cut -d '@' -f 2`
  inlocal=`echo $branch|cut -d '@' -f 3`
  origin="$inlocal"
  if ! [ -d "$inlocal" ]; then
    origin="$remote"
  fi
  # Check if destination directory exists
  # if not: fetch branch content either from local or from remote (regarding local existence)
  if ! [ -d "${tmpdir}/${copy}" ]; then
    echo -n "Fetching '$copy' from '$origin': "
    bzr checkout -q --lightweight "$origin" "${tmpdir}/${copy}"
    echo "DONE."
  # if exists: update branch
  else
    cd ${tmpdir}/${copy}
    echo -n "Updating '$copy': "
    bzr pull -q
    cd $current_dir
    echo "DONE."
  fi
done

## Check which port to use
read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
($min, $max) = (8100, 8200);
while(<>) {
  $port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
}
for my $i ($min..$max) {
  if( not exists $port{$i} and
    not exists $port{$i+1} and
    not exists $port{$i+2}) {
    print join(" ", $i, $i+1, $i+2);
    last;
  }
}'`
echo "xmlrpc port: ${XMLRPCPORT}"
echo "netrpc port: ${NETRPCPORT}"
echo "web port: ${WEBPORT}"

## Check that db is available
db_exists=`psql -l|grep "${dbname}_*"|wc -l`
while [ $db_exists -ne 0 ] ; do
  dbname="${dbname}_1"
  db_exists=`psql -l|grep "${dbname}_*"|wc -l`
done
echo "database: ${dbname}"

## Change MKDB configuration file 'prefix' variable
sed -i "s#\(prefix =\).*#\1 '${dbname}'#g" ${mkdb_configfile}
## Same thing with different ports for server and web
sed -i "s#\(client_port =\).*#\1 ${XMLRPCPORT}#g" ${mkdb_configfile}
sed -i "s#\(server_port =\).*#\1 ${XMLRPCPORT}#g" ${mkdb_configfile}
sed -i "s#\(netrpc_port =\).*#\1 ${NETRPCPORT}#g" ${mkdb_configfile}
## Set HQ to 1, COORDO to 2 and PROJECT to 3
sed -i "s#\(hq_count =\).*#\1 ${hq_count}#g" ${mkdb_configfile}
sed -i "s#\(coordo_count =\).*#\1 ${coordo_count}#g" ${mkdb_configfile}
sed -i "s#\(project_count =\).*#\1 ${project_count}#g" ${mkdb_configfile}

## Launch server
pidfile="${tmpdir}/${dbname}.pid"
echo -n "Launching openerp-server: "
start-stop-daemon --start --quiet --pidfile ${pidfile} \
  --background --make-pidfile --exec ${tmpdir}/server/bin/openerp-server.py -- \
  --no-xmlrpcs --netrpc-port=${NETRPCPORT} --xmlrpc-port=${XMLRPCPORT} \
  --addons-path=${tmpdir}/addons,${tmpdir}/wm,${tmpdir}/sync
# Wait 15 second before launching MKDB script
sleep 15s
echo "DONE."

## Launch MKDB script
cd ${current_dir} && ./mkdb.py || stop_server_and_exit ${pidfile}

## Stop server
stop_server ${pidfile}

## Save databases
# create a list file containing all files to save
list="${tmpdir}/${dbname}.list"
if ! [ -a "$list" ] ; then
  touch $list
else
  echo "" > $list
fi
# save each DB in a dump file, add it to a list to save and drop DB
for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${dbname}_%';"`; do
  dumpfile="${db}.dump"
  if [ -a "$dumpfile" ] ; then
    error_and_exit "$dumpfile already exists! Aborted."
  fi
  echo -e -n "Saving '$db' to $dumpfile: "
  pg_dump -Fc $db > $dumpfile
  echo "$dumpfile" >> ${list}
  dropdb $db
  echo "DONE."
done

## Add some file into list file to archive
echo "manage_syncdb.sh" >> ${list}
echo "manage_syncdbrc" >> ${list}

## Create archive
echo -n "Creating archive: "
archive="`date +'%Y%m%d'`_${dbname}.tar.xz"
tar cfJ $archive `cat $list|tr -s '\n' ' '` || error_and_exit "An error occured to create archive: $archive."
echo "$archive DONE."

## Delete all DB dumps
rm -f *.dump

########################
## WORK IN PROGRESS
#####

## TODO: Update CSV files in sync_module_prod for data to be correct
## TODO: update all accounts on all databases
## TODO: open all periods from january to today

##############

exit 0