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

« back to all changes in this revision

Viewing changes to export.sh

  • Committer: Olivier DOSSMANN
  • Date: 2013-09-17 07:21:50 UTC
  • Revision ID: od@tempo-consulting.fr-20130917072150-gld4t6ckxstgfvvb
[NEW] Scripts that create a synchronization environment, dump files and create an archive with dumps + some scripts to restore DB and launch an OpenERP server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env bash
 
2
#
 
3
# export.sh
 
4
#
 
5
# Create a synchronization environment and create an archive from it
 
6
 
 
7
PROGRAM=`basename $0`
 
8
VERSION="0.0.0"
 
9
current_dir="$PWD"
 
10
 
 
11
#####
 
12
## Variables
 
13
###
 
14
 
 
15
# configuration files
 
16
configfile="exportrc"
 
17
mkdb_configfile="config.py"
 
18
mkdb_tmp_configfile="config_temp.py"
 
19
 
 
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"
 
26
 
 
27
# temporary directory in which we will work
 
28
tmpdir="${HOME}/tmp"
 
29
 
 
30
# default HQ/PROJECT/COORDO counts
 
31
hq_count=1
 
32
coordo_count=2
 
33
project_count=3
 
34
 
 
35
#####
 
36
## FUNCTIONS
 
37
###
 
38
 
 
39
error_and_exit() {
 
40
  echo -e $1
 
41
  exit 1
 
42
}
 
43
 
 
44
check_cmd_presence() {
 
45
  cmd_path=`which $1`
 
46
  if [ -z $cmd_path ]; then
 
47
    error_and_exit "$1 is missing.\nInstall it: sudo apt-get update && sudo apt-get install $2"
 
48
  fi
 
49
  echo "$1: installed."
 
50
}
 
51
 
 
52
stop_server() {
 
53
  start-stop-daemon --stop --quiet --pidfile $1 --oknodo
 
54
}
 
55
 
 
56
stop_server_and_exit() {
 
57
  stop_server "$1" && exit 1
 
58
}
 
59
 
 
60
#####
 
61
## TESTS
 
62
###
 
63
 
 
64
# Configuration file exists and is readable
 
65
if ! [ -r $configfile ] ; then
 
66
  error_and_exit "${configfile}: missing"
 
67
fi
 
68
# Load configuration file
 
69
source $configfile
 
70
echo "${configfile}: imported"
 
71
 
 
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"
 
76
  fi
 
77
  # create mkdb configuration file
 
78
  cp ${mkdb_tmp_configfile} ${mkdb_configfile}
 
79
fi
 
80
 
 
81
# Check some programs presence
 
82
# pip
 
83
# bzr
 
84
# start-stop-daemon
 
85
# tar
 
86
# xz
 
87
# perl
 
88
# psql
 
89
# netstat
 
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
 
94
done
 
95
 
 
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"
 
99
fi
 
100
echo "openerp-client-lib 1.0.1: installed."
 
101
 
 
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."
 
106
  fi
 
107
else
 
108
  mkdir "$tmpdir" || exit 1
 
109
fi
 
110
echo "Temp. dir: $tmpdir"
 
111
 
 
112
#####
 
113
## MAIN
 
114
###
 
115
 
 
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`
 
122
  origin="$inlocal"
 
123
  if ! [ -d "$inlocal" ]; then
 
124
    origin="$remote"
 
125
  fi
 
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}"
 
131
    echo "DONE."
 
132
  # if exists: update branch
 
133
  else
 
134
    cd ${tmpdir}/${copy}
 
135
    echo -n "Updating '$copy': "
 
136
    bzr pull -q
 
137
    cd $current_dir
 
138
    echo "DONE."
 
139
  fi
 
140
done
 
141
 
 
142
## Check which port to use
 
143
read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
 
144
($min, $max) = (8100, 8200);
 
145
while(<>) {
 
146
  $port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
 
147
}
 
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);
 
153
    last;
 
154
  }
 
155
}'`
 
156
echo "xmlrpc port: ${XMLRPCPORT}"
 
157
echo "netrpc port: ${NETRPCPORT}"
 
158
echo "web port: ${WEBPORT}"
 
159
 
 
160
## Check that db is available
 
161
db_exists=`psql -l|grep "${dbname}_*"|wc -l`
 
162
while [ $db_exists -ne 0 ] ; do
 
163
  dbname="${dbname}_1"
 
164
  db_exists=`psql -l|grep "${dbname}_*"|wc -l`
 
165
done
 
166
echo "database: ${dbname}"
 
167
 
 
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}
 
178
 
 
179
## Launch server
 
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
 
187
sleep 15s
 
188
echo "DONE."
 
189
 
 
190
## Launch MKDB script
 
191
cd ${current_dir} && ./mkdb.py || stop_server_and_exit ${pidfile}
 
192
 
 
193
## Stop server
 
194
stop_server ${pidfile}
 
195
 
 
196
## Save databases
 
197
# create a list file containing all files to save
 
198
list="${tmpdir}/${dbname}.list"
 
199
if ! [ -a "$list" ] ; then
 
200
  touch $list
 
201
else
 
202
  echo "" > $list
 
203
fi
 
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."
 
209
  fi
 
210
  echo -e -n "Saving '$db' to $dumpfile: "
 
211
  pg_dump -Fc $db > $dumpfile
 
212
  echo "$dumpfile" >> ${list}
 
213
  dropdb $db
 
214
  echo "DONE."
 
215
done
 
216
 
 
217
## Add some file into list file to archive
 
218
echo "manage_syncdb.sh" >> ${list}
 
219
echo "manage_syncdbrc" >> ${list}
 
220
 
 
221
## Create archive
 
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."
 
226
 
 
227
## Delete all DB dumps
 
228
rm -f *.dump
 
229
 
 
230
########################
 
231
## WORK IN PROGRESS
 
232
#####
 
233
 
 
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
 
237
 
 
238
##############
 
239
 
 
240
exit 0