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

« back to all changes in this revision

Viewing changes to manage_syncdb.sh

  • Committer: Samus CTO
  • Date: 2012-08-28 12:38:31 UTC
  • Revision ID: cto@openerp.com-20120828123831-9dhavvjktnrl2p8d
[INIT]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env bash
2
 
#
3
 
# manage_syncdb.sh
4
 
#
5
 
# Synchronization databases management
6
 
#
7
 
 
8
 
#####
9
 
## VARIABLES
10
 
###
11
 
 
12
 
PROGRAM=`basename $0`
13
 
VERSION="0.0.0"
14
 
configdir=`dirname $0`
15
 
config="${configdir}/`basename ${PROGRAM} .sh`rc"
16
 
current="$PWD"
17
 
 
18
 
#####
19
 
## FUNCTIONS
20
 
###
21
 
 
22
 
# display this program's usage
23
 
usage() {
24
 
  cat << EOF
25
 
usage: $PROGRAM [dbname] [command] [branch]
26
 
 
27
 
This script helps to manage synchro databases.
28
 
 
29
 
COMMANDS:
30
 
backup      backup all databases prefixed by <dbname>_
31
 
restore     restore all databases prefixed by <dbname>_ with a '.dump' suffix
32
 
drop        drop all databases prefixed by <dbname>_
33
 
start       launch a openerp server (need a config file '${config}')
34
 
stop        stop a openerp server
35
 
<dbname>    prefix of database to use
36
 
extract     do a drop, restore and start for the given dbname (after an \
37
 
archive extraction, for an example)
38
 
<branch>    path to the WM branch to use (only useful for start command)
39
 
EOF
40
 
}
41
 
 
42
 
# display an error
43
 
error() {
44
 
  echo -e "$1"
45
 
}
46
 
 
47
 
# display an error and quit
48
 
error_and_exit() {
49
 
  error "$1"
50
 
  exit 1
51
 
}
52
 
 
53
 
# check if configuration file exists
54
 
check_config() {
55
 
  if ! [ -f "${config}" ] ; then
56
 
    error_and_exit "Configuration file not found: $config."
57
 
  fi
58
 
}
59
 
 
60
 
# check if some db with this prefix exists
61
 
check_db() {
62
 
db_exists=`psql -l|grep "$1_*"|wc -l`
63
 
if [ $db_exists -eq 0 ] ; then
64
 
  error_and_exit "No database with '$1' prefix found!"
65
 
fi
66
 
}
67
 
 
68
 
# backup all DB that have a prefix "${dbname}_"
69
 
backup() {
70
 
  check_db "$1"
71
 
  for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
72
 
    dumpfile="${db}.dump"
73
 
    if [ -a "$dumpfile" ] ; then
74
 
      error_and_exit "$dumpfile already exists! Aborted."
75
 
    fi
76
 
    echo -e -n "Saving '$db' to $dumpfile: "
77
 
    pg_dump -Fc $db > $dumpfile
78
 
    echo "DONE."
79
 
  done
80
 
}
81
 
 
82
 
# restore all .dump file that have a prefix with "${dbname}_"
83
 
restore() {
84
 
  for file in `ls ${1}_*`; do
85
 
    # do not make process on directories
86
 
    if [ -f $file ] ; then
87
 
      db=`basename $file .dump`
88
 
      exists=`psql template1 -t -c "SELECT COUNT(datname) from pg_database WHERE datname = '$db'"`
89
 
      if [ $exists == 1 ] ; then
90
 
        echo -n "$db exists. Deleting: "
91
 
        dropdb $db || error_and_exit "An error occured @DB deletion: $db"
92
 
        echo "DONE."
93
 
      fi
94
 
      echo -e -n "Restoring $db: "
95
 
      createdb $db && pg_restore -d $db $file >/dev/null 2>&1
96
 
      echo "DONE."
97
 
    fi
98
 
  done
99
 
}
100
 
 
101
 
# drop all DB that begins with the given prefix + _ char
102
 
drop() {
103
 
  check_db "$1"
104
 
  for db in `psql template1 -t -c "SELECT datname from pg_database where datname ilike '${1}_%';"`; do
105
 
    echo -n "Deleting $db: "
106
 
    dropdb $db || error_and_exit "An error occured @DB deletion: $db"
107
 
    echo "DONE."
108
 
  done
109
 
}
110
 
 
111
 
# start a unifield server on free ports (between 8100 and 8200)
112
 
start_serv() {
113
 
  # check if configuration is here and right completed
114
 
  check_config
115
 
  source "$config"
116
 
  for var in server addons web sync ; do
117
 
    if [ -z ${!var} ] ; then
118
 
      error_and_exit "Variable not found in configuration file ($config): ${var}."
119
 
    fi
120
 
    # check directories existence
121
 
    if ! [ -d ${!var} ] ; then
122
 
      error_and_exit "Directory not found (${var} variable in $config file): ${!var}"
123
 
    fi
124
 
  done
125
 
  # check branch existence
126
 
  if ! [ -d "$2" ] ; then
127
 
    error_and_exit "Directory not found: $2"
128
 
  fi
129
 
  # search free ports
130
 
  echo -n "Searching free ports to launch server: "
131
 
  read XMLRPCPORT NETRPCPORT WEBPORT <<<`netstat -anltp 2> /dev/null | perl -e '%port = ();
132
 
  ($min, $max) = (8100, 8200);
133
 
  while(<>) {
134
 
    $port{$&} = 1 if m/:\K\d+\b/ and $& >= $min and $& <= $max;
135
 
  }
136
 
  for my $i ($min..$max) {
137
 
    if( not exists $port{$i} and
138
 
      not exists $port{$i+1} and
139
 
      not exists $port{$i+2}) {
140
 
      print join(" ", $i, $i+1, $i+2);
141
 
      last;
142
 
    }
143
 
  }'`
144
 
  echo "DONE."
145
 
  # Prepare files and directories
146
 
  pidfile="${configdir}/tmp/$1.pid"
147
 
  logfile="${configdir}/tmp/$1.log"
148
 
  if [ -a "${configdir}/tmp" ] ; then
149
 
    if ! [ -d "${configdir}/tmp" ] ; then
150
 
      error_and_exit "Directory ${configdir}/tmp not found! Is that a file?"
151
 
    fi
152
 
  else
153
 
    echo -n "Creating ${configdir}/tmp directory: "
154
 
    mkdir ${configdir}/tmp
155
 
    echo "DONE."
156
 
  fi
157
 
  touch ${logfile}
158
 
  echo -n "Launching openerp-server: "
159
 
  LANG=C start-stop-daemon --start --pidfile ${pidfile} \
160
 
  --background --make-pidfile --exec ${server}/bin/openerp-server.py -- \
161
 
  --addons-path=${addons},${wm},${sync} --additional-xml --without-demo=all \
162
 
  --logfile=${logfile} \
163
 
  --no-xmlrpcs --no-xmlrpc --netrpc-port=${NETRPCPORT}
164
 
  # Wait 15 second before launching MKDB script
165
 
  sleep 15s
166
 
  echo "DONE."
167
 
  echo -e "Ports used:\nxmlrpc: ${XMLRPCPORT}\nnetrpc: ${NETRPCPORT}"
168
 
  echo "You can launch a web server using this command:"
169
 
  echo "${web}/openerp-web.py --openerp-port=${NETRPCPORT}"
170
 
}
171
 
 
172
 
stop_serv() {
173
 
  pidfile="${configdir}/tmp/$1.pid"
174
 
  logfile="${configdir}/tmp/$1.log"
175
 
  if ! [ -a "$pidfile" ] ; then
176
 
    error_and_exit "PID file $pidfile not found!\nThis probably means that no server is working."
177
 
  fi
178
 
  echo "Stopping server for $1"
179
 
  start-stop-daemon --stop --quiet --pidfile $pidfile --oknodo
180
 
  # Delete some files (PID and log)
181
 
  rm -f $pidfile $logfile
182
 
}
183
 
 
184
 
#####
185
 
## TESTS
186
 
###
187
 
 
188
 
# We need 2 params at least
189
 
if [ $# -lt 2 ] ; then
190
 
  error "Need at least 2 parameters."
191
 
  usage
192
 
  exit 1
193
 
fi
194
 
 
195
 
prefix=$1
196
 
command=$2
197
 
branch=$3
198
 
 
199
 
if [ -z "$branch" ] ; then
200
 
  check_config
201
 
  source "$config"
202
 
  if [ -z ${wm} ] ; then
203
 
    error_and_exit "Variable not found in configuration file ($config): wm."
204
 
  fi
205
 
 
206
 
  branch="${wm}"
207
 
fi
208
 
 
209
 
#####
210
 
## MAIN
211
 
###
212
 
 
213
 
# Check command
214
 
case $command in 
215
 
  backup)
216
 
  backup "$prefix"
217
 
  ;;
218
 
  restore)
219
 
  restore "$prefix"
220
 
  ;;
221
 
  drop)
222
 
  drop "$prefix"
223
 
  ;;
224
 
  start)
225
 
  start_serv "$prefix" "$branch"
226
 
  ;;
227
 
  stop)
228
 
  stop_serv "$prefix"
229
 
  ;;
230
 
  extract)
231
 
  drop "$prefix" && restore "$prefix" && start_serv "$prefix" "$branch"
232
 
  ;;
233
 
  help)
234
 
  usage
235
 
  ;;
236
 
  *)
237
 
  error "Command $command not found!"
238
 
  usage
239
 
  exit 1
240
 
  ;;
241
 
esac
242
 
 
243
 
exit 0