Guest User

Untitled

a guest
Aug 25th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.06 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. . $HOME/.automafield.config.sh
  4. SCRIPTS=$HOME/automafield/
  5.  
  6. _contains_element () {
  7. local e
  8. for e in ${@:2};
  9. do
  10. # matching 1:1
  11. [[ "$e" == "$1" ]] && return 0;
  12.  
  13. # regex?
  14. REGEX=`echo $e | sed s/%/\.\*/g`
  15.  
  16. if echo $1 | grep ^$REGEX$ > /dev/null;
  17. then
  18. return 0
  19. fi
  20. done
  21.  
  22. return 1
  23. }
  24.  
  25. _convert_name() {
  26.  
  27. if echo $2 | grep '%' > /dev/null;
  28. then
  29. for db in `pct_all_instances $1 | sort`;
  30. do
  31. # regex?
  32. REGEX=`echo $2 | sed s/%/\.\*/g`
  33.  
  34. if echo $db | grep ^$REGEX$ > /dev/null;
  35. then
  36. RESULT=$db
  37. return
  38. fi
  39. done
  40.  
  41. RESULT=$2
  42. else
  43. RESULT=$2
  44. return
  45. fi
  46.  
  47. }
  48.  
  49. _prepare_config() {
  50. echo "OK"
  51. }
  52.  
  53. pct_get_hwid(){
  54.  
  55. case $1 in
  56. 0)
  57. HWID=$MYHWID
  58. ;;
  59.  
  60. 1)
  61. HWID='c11d5c86cc46381b41321813708f3ed1'
  62. ;;
  63. 2)
  64. HWID='3ca384248ffd038c9646f721cba31500'
  65. ;;
  66.  
  67. 3)
  68. HWID='95f83b7f704bf10d59d80f1be66f80b0'
  69. ;;
  70.  
  71.  
  72. 4)
  73. HWID='2f496f630213c07ca3f7adfe28405403'
  74. ;;
  75.  
  76. 5)
  77. HWID='57b6a1a8095421135960c719269d85bf'
  78. ;;
  79.  
  80. 6)
  81. HWID='dc929db5d5c7b28164d173006a7161d6'
  82. ;;
  83.  
  84. *)
  85. HWID='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
  86. esac
  87.  
  88. echo $HWID
  89. }
  90.  
  91. pct_download() {
  92.  
  93. if [[ $# < 2 ]];
  94. then
  95. echo "Usage: pct_download ct# [OC] [ instance1 instance2 ...] "
  96. echo " Description: load all the databases coming from an OC (OCG and OCB available now)"
  97. return 1
  98. fi
  99.  
  100. HWID=$(pct_get_hwid $1)
  101.  
  102. case $2 in
  103. OCB)
  104. BACKUPDIR=OCB_Backups
  105. LOGIN=$MY_LOGIN_OWNCLOUD
  106. PASSWORD=$MY_PASSWORD_OWNCLOUD
  107. ;;
  108.  
  109. OCG)
  110. BACKUPDIR=UNIFIELD-BACKUP
  111. LOGIN=$MY_LOGIN_OWNCLOUD
  112. PASSWORD=$MY_PASSWORD_OWNCLOUD
  113. ;;
  114.  
  115. OCA)
  116. BACKUPDIR=OCA_Backups
  117. LOGIN=$MY_LOGIN_OWNCLOUD
  118. PASSWORD=$MY_PASSWORD_OWNCLOUD
  119. ;;
  120.  
  121. OCP|OCBA)
  122. echo OCP and OCBA are not available right now
  123. return 1
  124. ;;
  125.  
  126. *)
  127. echo Unkown OC $2
  128. return 1
  129. ;;
  130. esac
  131.  
  132. PORT=$NORMALPORT
  133. if [[ "0" == $1 ]];
  134. then
  135. PORT=$MYPORT
  136. fi
  137.  
  138. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  139.  
  140. then
  141. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  142. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  143. else
  144. LOGIN_POSTGRES=$POSTGRES_USERNAME
  145. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  146. fi
  147.  
  148. export PGPASSWORD=$PASSWORD_POSTGRES
  149.  
  150. source $ENV4SYNC
  151.  
  152. LOCALENV=$SCRIPTS/import-$$
  153. mkdir $LOCALENV
  154. cp -R $SCRIPTS/import/* $LOCALENV/
  155. PREVIOUS=$PWD
  156. cd $LOCALENV
  157.  
  158. python insert_db.py "$HWID" "$LOGIN_POSTGRES" "$PASSWORD_POSTGRES" "$PORT" "$1" "$BACKUPDIR" "$LOGIN" "$PASSWORD" "$LOGIN_BACKUPS" "$PASSWORD_BACKUPS" "$URL_BACKUPS" ${@:3}
  159.  
  160. cd $PREVIOUS
  161.  
  162. rm -rf $LOCALENV
  163. deactivate
  164.  
  165. return 0
  166. }
  167.  
  168. setup_lettuce()
  169. {
  170. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  171. then
  172. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  173. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  174. else
  175. LOGIN_POSTGRES=$POSTGRES_USERNAME
  176. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  177. fi
  178.  
  179. PORT=$NORMALPORT
  180. if [[ "0" == $1 ]];
  181. then
  182. PORT=$MYPORT
  183. fi
  184.  
  185. PATH_TESTFIELD=$SCRIPTS/testfield-$$
  186. if [[ -e $PATH_TESTFIELD ]]
  187. then
  188. rm -rf $PATH_TESTFIELD
  189. fi
  190.  
  191. cp -R $SCRIPTS/testfield $PATH_TESTFIELD
  192.  
  193. if [[ -e $PATH_TESTFIELD/files/file ]]
  194. then
  195. rm -rf $PATH_TESTFIELD/files/file
  196. fi
  197.  
  198. if [[ $# == 4 ]]
  199. then
  200. cp "$4" $PATH_TESTFIELD/files/file
  201. fi
  202.  
  203. if [[ -e $PATH_TESTFIELD/config.sh ]]
  204. then
  205. rm $PATH_TESTFIELD/config.sh
  206. fi
  207.  
  208. ADMINPASSWORD=`pct $1 $2 -c 'SELECT password FROM res_users WHERE id = 1' -t | tr -d '[[:space:]]'`
  209. ADMINUSERNAME=`pct $1 $2 -c 'SELECT login FROM res_users WHERE id = 1' -t | tr -d '[[:space:]]'`
  210.  
  211. cat <<EOF > $PATH_TESTFIELD/config.sh
  212. #!/bin/bash
  213.  
  214. SERVER_HOST=ct$1
  215.  
  216. NETRPC_PORT=8070
  217. WEB_PORT=8061
  218. XMLRPC_PORT=8069
  219. XMLRPCS_PORT=8071
  220.  
  221. UNIFIELDADMIN=$ADMINUSERNAME
  222. UNIFIELDPASSWORD=$ADMINPASSWORD
  223.  
  224. DBPASSWORD=$LOGIN_POSTGRES
  225. DBADDR=ct$1
  226. DBUSERNAME=$PASSWORD_POSTGRES
  227. DBPORT=$PORT
  228. EOF
  229.  
  230. PREVIOUS=$PWD
  231. cp $SCRIPTS/$3 $PATH_TESTFIELD/
  232. cd $PATH_TESTFIELD
  233. ./generate_credentials.sh
  234.  
  235. source $ENV4SYNC
  236.  
  237. OLDDISPLAY=$DISPLAY
  238. tmux new -d -s X_$$ "Xvfb :$$"
  239. export DISPLAY=:$$
  240. }
  241.  
  242. clean_lettuce(){
  243.  
  244. cd $PREVIOUS
  245. deactivate
  246.  
  247. rm -rf $SCRIPTS/testfield-$$
  248.  
  249. export DISPLAY=$OLDDISPLAY
  250. tmux kill-session -t X_$$ 2> /dev/null
  251. }
  252.  
  253.  
  254. pct_upgrade() {
  255.  
  256. if [[ $# != 4 ]];
  257. then
  258. echo "Usage: pct_upgrade ct# SYNC_SERVER ONE_INSTANCE patch.zip"
  259. echo " Description: Upgrade database SYNC_SERVER and ONE_INSTANCE on ct# with the given patch.zip"
  260. return
  261. fi
  262.  
  263. _convert_name $1 $2
  264. export LETTUCE_SYNC_SERVER=$RESULT
  265. export LETTUCE_REVISION=$4
  266. _convert_name $1 $3
  267. export LETTUCE_DATABASE=$RESULT
  268.  
  269. setup_lettuce $1 $LETTUCE_SYNC_SERVER patching.meta_feature "$4"
  270.  
  271.  
  272. BEFORE_TIME=$TIME_BEFORE_FAILURE
  273. export TIME_BEFORE_FAILURE=
  274.  
  275. if ./runtests_local.sh patching.meta_feature 1>&2 > /dev/null;
  276. then
  277. echo "Upgrade $LETTUCE_SYNC_SERVER $LETTUCE_DATABASE: OK"
  278. RET=0
  279. else
  280. echo "Upgrade $LETTUCE_SYNC_SERVER $LETTUCE_DATABASE: FAILURE"
  281. RET=1
  282. fi
  283.  
  284. export TIME_BEFORE_FAILURE=$BEFORE_TIME
  285.  
  286. clean_lettuce
  287.  
  288. return $RET
  289. }
  290.  
  291. pct_sync() {
  292.  
  293. if [[ $# != 2 ]];
  294. then
  295. echo "Usage: pct_sync ct# instance"
  296. echo " Description: synchronize the instance on ct# to its SYNC_SERVER"
  297. echo "WARNING: We use the SAME credentials to connect to the instance and to connect to the"
  298. echo " sync server. You have to ensure that these credentials are identical with pct_password"
  299.  
  300. return
  301. fi
  302.  
  303. _convert_name $1 $2
  304. export LETTUCE_DATABASE=$RESULT
  305.  
  306. setup_lettuce $1 $LETTUCE_DATABASE synchronize.feature
  307.  
  308. BEFORE_TIME=$TIME_BEFORE_FAILURE
  309. export TIME_BEFORE_FAILURE=
  310.  
  311. # remove the backups before/after sync
  312. pct $1 $LETTUCE_DATABASE -c "UPDATE backup_config SET beforemanualsync='f', beforepatching='f', aftermanualsync='f'"
  313.  
  314. if ./runtests_local.sh synchronize.feature 1>&2 > /dev/null;
  315. then
  316. echo "Sync $LETTUCE_DATABASE: OK"
  317. RET=0
  318. else
  319. echo "Sync $LETTUCE_DATABASE: FAILURE"
  320. RET=1
  321. fi
  322.  
  323. export TIME_BEFORE_FAILURE=$BEFORE_TIME
  324.  
  325. clean_lettuce
  326.  
  327. return $RET
  328. }
  329.  
  330. pct_syncall() {
  331.  
  332. if [[ $# < 1 ]];
  333. then
  334. echo "Usage: pct_syncall ct# [ instance1 instance2 ... ]"
  335. echo " Description: synchronize all the instances (or the given one) on ct#"
  336. echo "WARNING: We use the SAME credentials to connect to the instance and to connect to the"
  337. echo " sync server. You have to ensure that these credentials are identical with pct_password"
  338. return
  339. fi
  340.  
  341. for db in `pct_other_instances $1`;
  342. do
  343. if [[ $# == 1 ]] || _contains_element $db ${@:2};
  344. then
  345. pct_sync $1 $db
  346. fi
  347. done
  348. }
  349.  
  350. pct_link() {
  351.  
  352. if [[ $# != 3 ]]
  353. then
  354. echo "Usage: pct_link ct# SYNC_SERVER instance"
  355. echo " Description: link the instance to SYNC_SERVER on ct#"
  356. return
  357. fi
  358.  
  359. _convert_name $1 $2
  360. SYNC_SERVER_NAME=$RESULT
  361. _convert_name $1 $3
  362. INSTANCE_NAME=$RESULT
  363.  
  364. ADMINUSERNAME=`pct $1 $SYNC_SERVER_NAME -c 'SELECT login FROM res_users WHERE id = 1' -t | tr -d '[[:space:]]'`
  365. INSTANCENAME=`pct $1 $INSTANCE_NAME -c "SELECT name FROM sync_client_entity" -t`
  366. HWID=$(pct_get_hwid $1)
  367.  
  368. echo $INSTANCE_NAME '=>' $SYNC_SERVER_NAME
  369.  
  370. pct $1 $INSTANCE_NAME -c "UPDATE sync_client_sync_server_connection SET protocol = 'xmlrpc', login = '$ADMINUSERNAME', database = '$SYNC_SERVER_NAME', host='127.0.0.1', port=8069"
  371. pct $1 $SYNC_SERVER_NAME -c "UPDATE sync_server_entity SET hardware_id = '$HWID' WHERE name = trim('$INSTANCENAME')"
  372. }
  373.  
  374. pct_linkall() {
  375.  
  376. if [[ $# < 2 ]]
  377. then
  378. echo "Usage: pct_linkall ct# SYNC_SERVER [ instance1 instance2 ... ]"
  379. echo " Description: link all instances on ct# (or the given ones) to the SYNC_SERVER"
  380. return
  381. fi
  382.  
  383. LOGIN=`pct $1 $2 -c "SELECT login FROM res_users WHERE id = 1" -t`
  384.  
  385. for db in `pct_other_instances $1`;
  386. do
  387. if [[ $# == 2 ]] || _contains_element $db ${@:3};
  388. then
  389. pct_link $1 $2 $db
  390. fi
  391. done
  392. }
  393.  
  394. pct_password()
  395. {
  396. if [[ $# != 3 ]]
  397. then
  398. echo "Usage: pct_password ct# instance password"
  399. echo " Description: reset the admin password on ct# for the given instance"
  400. return
  401. fi
  402.  
  403. _convert_name $1 $2
  404. INSTANCENAME=$RESULT
  405.  
  406. echo "$INSTANCENAME => $3"
  407. pct $1 $INSTANCENAME -c "UPDATE res_users SET login = '$3', password = '$3' WHERE id = 1" > /dev/null
  408. }
  409.  
  410. pct_passwordall()
  411. {
  412. if [[ $# < 2 ]]
  413. then
  414. echo "Usage: pct_passwordall ct# password [ instance1 instance2 ...] "
  415. echo " Description: reset the admin password on ct# for all the instances (or the given ones)"
  416. return
  417. fi
  418.  
  419. for db in `pct_all_instances $1`;
  420. do
  421. if [[ $# == 2 ]] || _contains_element $db ${@:3};
  422. then
  423. pct_password $1 $db $2
  424. fi
  425. done
  426. }
  427.  
  428.  
  429. pct_sqldump()
  430. {
  431. if [[ $# < 2 ]]
  432. then
  433. echo "Usage: pct_sqldump ct# instance [ file ]"
  434. echo " Description: dump the instance on ct# and save it in file.sql"
  435. return
  436. fi
  437.  
  438. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  439. then
  440. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  441. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  442. else
  443. LOGIN_POSTGRES=$POSTGRES_USERNAME
  444. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  445. fi
  446.  
  447. export PGPASSWORD=$PASSWORD_POSTGRES
  448.  
  449. _convert_name $1 $2
  450. INSTANCENAME=$RESULT
  451.  
  452. echo "Dumping $INSTANCENAME"
  453.  
  454. if [[ "" == $3 ]];
  455. then
  456. pg_dump --no-owner -U $LOGIN_POSTGRES -h ct$1 $INSTANCENAME -f $INSTANCENAME.sql
  457. else
  458. pg_dump --no-owner -U $LOGIN_POSTGRES -h ct$1 $INSTANCENAME -f $3.sql
  459. fi
  460. }
  461.  
  462. pct_sqldumpall()
  463. {
  464. if [[ $# < 1 ]]
  465. then
  466. echo "Usage: pct_sqldump ct# [ instance1 instance2 ... ]"
  467. echo " Description: dump all the instances of ct# (or the given subset) in SQL files"
  468. echo " you can use % as a wildcard character"
  469. return 1
  470. fi
  471.  
  472. for db in `pct_all_instances $1`;
  473. do
  474. if [[ $# == 1 ]] || _contains_element $db ${@:2};
  475. then
  476. pct_sqldump $1 $db
  477. fi
  478. done
  479. }
  480.  
  481. pct()
  482. {
  483. if [[ $# == 0 ]];
  484. then
  485. echo "Usage: pct ct# [ instance ] [ args ]"
  486. echo " Description: launch psql on ct# on the given instance with the given arguments"
  487. return 1
  488. fi
  489.  
  490. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  491. then
  492. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  493. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  494. else
  495. LOGIN_POSTGRES=$POSTGRES_USERNAME
  496. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  497. fi
  498.  
  499. export PGPASSWORD=$PASSWORD_POSTGRES
  500.  
  501. PORT=$NORMALPORT
  502.  
  503. if [[ "0" == $1 ]];
  504. then
  505. PORT=$MYPORT
  506. fi
  507.  
  508. if [[ "" == $2 ]];
  509. then
  510. psql -p $PORT -U $LOGIN_POSTGRES -h ct$1 -d postgres "${@:3}"
  511. else
  512. _convert_name $1 $2
  513. psql -p $PORT -U $LOGIN_POSTGRES -h ct$1 -d $RESULT "${@:3}"
  514. fi
  515. }
  516.  
  517.  
  518. pct_all_instances()
  519. {
  520. if [[ $# != 1 ]];
  521. then
  522. echo "Usage: pct_all_instances ct#"
  523. echo " Description: list all the instances servers on ct#"
  524. return 1
  525. fi
  526.  
  527. for instance in `pct $1 postgres -c "SELECT datname FROM pg_database WHERE datistemplate = 'n' AND datname != 'postgres'" -t`;
  528. do
  529. echo $instance
  530. done
  531. }
  532.  
  533. pct_sync_servers()
  534. {
  535. if [[ $# != 1 ]];
  536. then
  537. echo "Usage: pct_sync_servers ct#"
  538. echo " Description: list all the sync servers on ct#"
  539. return 1
  540. fi
  541.  
  542. for instance in `pct_all_instances $1`;
  543. do
  544. for yes in `pct $1 $instance -c "SELECT 1 FROM information_schema.tables WHERE table_catalog = '$instance' AND table_name = 'sync_server_entity'" -t`;
  545. do
  546. echo $instance
  547. done
  548. done
  549. }
  550.  
  551. pct_other_instances()
  552. {
  553. if [[ $# != 1 ]];
  554. then
  555. echo "Usage: pct_other_instances ct#"
  556. echo " Description: list all the instances used by users on ct#"
  557. return 1
  558. fi
  559.  
  560. for instance in `pct_all_instances $1`;
  561. do
  562. for yes in `pct $1 $instance -c "SELECT 1 FROM information_schema.tables WHERE table_catalog = '$instance' AND table_name = 'sync_client_version'" -t`;
  563. do
  564. echo $instance
  565. done
  566. done
  567. }
  568.  
  569. pct_restore()
  570. {
  571. if [[ $# < 2 ]]
  572. then
  573. echo "Usage: pct_restore ct# instance [ filename ]"
  574. echo " Description: restore in ct# the instance with the given filename"
  575. echo " if the filename is not provided, we will use instance.dump or instance.sql"
  576. echo " sql et dump files are used accoding to their definition"
  577. return 1
  578. fi
  579.  
  580. _convert_name $1 $2
  581. DBNAME=$RESULT
  582.  
  583. pct_drop $1 $DBNAME
  584. pct $1 postgres -c "CREATE DATABASE \"$DBNAME\""
  585.  
  586. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  587. then
  588. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  589. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  590. else
  591. LOGIN_POSTGRES=$POSTGRES_USERNAME
  592. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  593. fi
  594.  
  595. PORT=$NORMALPORT
  596.  
  597. if [[ "0" == $1 ]];
  598. then
  599. PORT=$MYPORT
  600. fi
  601.  
  602. export PGPASSWORD=$PASSWORD_POSTGRES
  603.  
  604. if [[ "" == $3 ]];
  605. then
  606. if [[ -f "${DBNAME}.dump" ]];
  607. then
  608. echo "Restore $DBNAME (${DBNAME}.dump)"
  609. pg_restore -p $PORT -n public -U $LOGIN_POSTGRES --no-acl --no-owner -h ct$1 -d $DBNAME ${DBNAME}.dump
  610. else
  611. if [[ ! -f "${DBNAME}.sql" ]]
  612. then
  613. echo "No file $DBNAME found (sql/dump)"
  614. return 1
  615. fi
  616.  
  617. echo "Restore $DBNAME (${DBNAME}.sql)"
  618. pct $1 $DBNAME < "${DBNAME}.sql"
  619. fi
  620. else
  621.  
  622. if [[ ! -f "${3}" ]]
  623. then
  624. echo "No file $3 found"
  625. return 1
  626. fi
  627.  
  628. if echo $3 | grep -i '.sql$' > /dev/null;
  629. then
  630. echo "Restore $DBNAME ($3)"
  631. pct $1 $DBNAME < $3
  632. else
  633. echo "Restore $DBNAME ($3)"
  634. pg_restore -p $PORT -n public -U $LOGIN_POSTGRES --no-acl --no-owner -h ct$1 -d $DBNAME $3
  635. fi
  636. fi
  637.  
  638. # remove all the automatic tasks
  639. pct $1 $DBNAME -c "UPDATE ir_cron SET active = 'f' WHERE model = 'backup.config'"
  640. pct $1 $DBNAME -c "UPDATE ir_cron SET active = 'f' WHERE model = 'sync.client.entity'"
  641. }
  642.  
  643. pct_restoreall()
  644. {
  645. if [[ $# < 1 ]]
  646. then
  647. echo "Usage: pct_restoreall ct# [ instance1 instance2 ... ] "
  648. echo " Description: restore all the instances (*.dump and *.sql) in the current directory (or the given subset)"
  649. return 1
  650. fi
  651.  
  652. for DBFILE in `ls *.dump 2> /dev/null` `ls *.sql 2> /dev/null`;
  653. do
  654. DATABASE=${DBFILE%%.dump}
  655. DATABASE=${DATABASE%%.sql}
  656.  
  657. FOUND=n
  658.  
  659. if [[ $# == 1 ]];
  660. then
  661. FOUND=y
  662. else
  663.  
  664. for arg in ${@:2};
  665. do
  666.  
  667. REGEX=`echo $arg | sed s/%/\.\*/g`
  668.  
  669. if echo $DATABASE | grep ^$REGEX$ > /dev/null;
  670. then
  671. FOUND=y
  672. fi
  673. done
  674. fi
  675.  
  676. if [[ $FOUND == y ]];
  677. then
  678. echo "Restore $DATABASE (dump/sql)"
  679. pct_restore $1 $DATABASE $DBFILE
  680. fi
  681. done
  682. }
  683.  
  684. pct_drop()
  685. {
  686. if [[ $# != 2 ]]
  687. then
  688. echo "Usage: pct_drop ct# instance"
  689. echo " Description: drop the instance in ct#"
  690. return 1
  691. fi
  692.  
  693. INSTANCES=(`pct_all_instances $1`)
  694.  
  695. _convert_name $1 $2
  696. INSTANCE=$RESULT
  697.  
  698. VERSION=$(pct $1 postgres -t -c "SHOW SERVER_VERSION")
  699. MAJOR_VERSION=${VERSION:1:1}
  700.  
  701. if [[ $MAJOR_VERSION == 8 ]]
  702. then
  703. DB_PROCID=procpid
  704. else
  705. DB_PROCID=pid
  706. fi
  707.  
  708. if _contains_element $INSTANCE ${INSTANCES[@]};
  709. then
  710. echo Drop $INSTANCE
  711. BEFORE_IFS=$IFS
  712. IFS=' '
  713. E="$(pct $1 postgres -t -c "SELECT 'select pg_terminate_backend(' || $DB_PROCID || ');' FROM pg_stat_activity WHERE datname = '$INSTANCE'")"
  714. IFS=';'
  715. for line in $(echo -e "$E");
  716. do
  717. pct $1 postgres -c $line > /dev/null 2>&1
  718. done
  719.  
  720. IFS=$BEFORE_IFS
  721.  
  722. pct $1 postgres -c "DROP DATABASE \"$INSTANCE\""
  723. return 0
  724. else
  725. return 1
  726. fi
  727. }
  728.  
  729. pct_dropall()
  730. {
  731. if [[ $# < 1 ]]
  732. then
  733. echo "Usage: pct_dropall ct# [ instance1 instance2 ... ] "
  734. echo " Description: drop all the instances of ct# (or the given subset)"
  735. echo " you can use % as a wildcard character"
  736. return 1
  737. fi
  738.  
  739. for db in `pct_all_instances $1`;
  740. do
  741. if [[ $# == 1 ]] || _contains_element $db ${@:2};
  742. then
  743. # if [[ $# ==1 ]] ||
  744. echo Dropping $db
  745. pct_drop $1 $db
  746. fi
  747. done
  748. }
  749.  
  750. pct_dump()
  751. {
  752. if [[ $# < 2 ]]
  753. then
  754. echo "Usage: pct_dump ct# instance [ file ]"
  755. echo " Description: dump the instance on ct# and save it in file.dump"
  756. return
  757. fi
  758.  
  759. if [[ "0" == $1 ]] || [[ "10" == $1 ]];
  760. then
  761. LOGIN_POSTGRES=$MY_POSTGRES_USERNAME
  762. PASSWORD_POSTGRES=$MY_POSTGRES_PASSWORD
  763. else
  764. LOGIN_POSTGRES=$POSTGRES_USERNAME
  765. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  766. fi
  767.  
  768. PORT=$NORMALPORT
  769.  
  770. if [[ "0" == $1 ]];
  771. then
  772. PORT=$MYPORT
  773. fi
  774.  
  775. export PGPASSWORD=$PASSWORD_POSTGRES
  776.  
  777. _convert_name $1 $2
  778. INSTANCE=$RESULT
  779.  
  780. if [[ "" == $3 ]];
  781. then
  782. pg_dump -p $PORT --no-owner -Fc -U $LOGIN_POSTGRES -h ct$1 $INSTANCE -f ${INSTANCE}.dump
  783. else
  784. pg_dump -p $PORT --no-owner -Fc -U $LOGIN_POSTGRES -h ct$1 $INSTANCE -f $3.dump
  785. fi
  786. }
  787.  
  788. pct_dumpall()
  789. {
  790. if [[ $# < 1 ]]
  791. then
  792. echo "Usage: pct_dumpall ct# [ instance1 instance2 ... ]"
  793. echo " Description: dump all the instances of ct# (or the given subset)"
  794. echo " you can use % as a wildcard character"
  795. return 1
  796. fi
  797.  
  798. for db in `pct_all_instances $1`;
  799. do
  800. if [[ $# == 1 ]] || _contains_element $db ${@:2};
  801. then
  802. echo "Dumping $db"
  803. pct_dump $1 $db
  804. fi
  805. done
  806. }
  807.  
  808. pct_login()
  809. {
  810. local VAL OPTIND OPTAR
  811.  
  812. TIMEOUT=
  813. while getopts ':t:' VAL;
  814. do
  815. case $VAL in
  816. t)
  817. TIMEOUT=$OPTARG
  818. ;;
  819. *)
  820. echo "Unkown argument"
  821. exit
  822. esac
  823. done
  824.  
  825. ARGS=("${@:OPTIND}")
  826.  
  827. if [[ ${#ARGS[*]} != 2 ]]
  828. then
  829. echo "Usage: pct_login [-t TIMEOUT] ct# instance"
  830. echo " Description: log into the instance in ct#. Wait for the upgrade to process if necessary"
  831. return
  832. fi
  833.  
  834. _convert_name ${ARGS[0]} ${ARGS[1]}
  835. export LETTUCE_DATABASE=$RESULT
  836.  
  837. setup_lettuce ${ARGS[0]} $LETTUCE_DATABASE login.feature
  838.  
  839. BEFORE_TIME=$TIME_BEFORE_FAILURE
  840. export TIME_BEFORE_FAILURE=$TIMEOUT
  841.  
  842. if ./runtests_local.sh login.feature 1>&2 > /dev/null;
  843. then
  844. echo "Login $LETTUCE_DATABASE: OK"
  845. RET=0
  846. else
  847. echo "Login $LETTUCE_DATABASE: FAILURE"
  848. RET=1
  849. fi
  850.  
  851. export TIME_BEFORE_FAILURE=$BEFORE_TIME
  852.  
  853. clean_lettuce
  854.  
  855. return $RET
  856. }
  857.  
  858. pct_loginall()
  859. {
  860. local VAL OPTIND OPTAR
  861.  
  862. ARGSPCT_LOGIN=
  863. while getopts ':t:' VAL;
  864. do
  865. case $VAL in
  866. t)
  867. ARGSPCT_LOGIN="-t $OPTARG"
  868. ;;
  869. *)
  870. echo "Unkown argument"
  871. exit
  872. esac
  873. done
  874.  
  875. ARGS_LOGINALL=("${@:OPTIND}")
  876.  
  877. if [[ ${#ARGS_LOGINALL[*]} < 1 ]]
  878. then
  879. echo "Usage: pct_loginall [-t TIMEOUT] ct# [ instance1 instance2 ... ]"
  880. echo " Description: log into all the instances (or the given one) in ct#"
  881. return 1
  882. fi
  883.  
  884. for db in `pct_all_instances ${ARGS_LOGINALL[0]}`;
  885. do
  886. RET=${ARGS_LOGINALL:0}
  887.  
  888. INSTANCES=("${@:OPTIND+1}")
  889.  
  890. if [[ ${#ARGS_LOGINALL[*]} == 1 ]] || _contains_element $db ${INSTANCES[@]};
  891. then
  892. echo "Login $db"
  893. pct_login $ARGSPCT_LOGIN ${ARGS_LOGINALL[0]} $db
  894. fi
  895. done
  896. }
  897.  
  898. pct_import()
  899. {
  900. if [[ $# != 4 ]]
  901. then
  902. echo "Usage: pct_import ct# instance [sync_rule|message_rule|BAR|ACL|FAR|FAR_lines|user_rights|record_rules|window_actions] file"
  903. echo " Description: import file (type sync_...) in instance on ct#"
  904. return 1
  905. fi
  906.  
  907. FEATURE_NAME=
  908.  
  909. case $3 in
  910. sync_rule)
  911. export FEATURE_NAME=import_sync_data.feature
  912. ;;
  913.  
  914. message_rule)
  915. export FEATURE_NAME=import_sync_message.feature
  916. ;;
  917.  
  918. BAR)
  919. export FEATURE_NAME=BAR.feature
  920. ;;
  921.  
  922. ACL)
  923. export FEATURE_NAME=ACL.feature
  924. ;;
  925.  
  926. FAR)
  927. export FEATURE_NAME=FAR.feature
  928. ;;
  929.  
  930. FAR_lines)
  931. export FEATURE_NAME=FAR_lines.feature
  932. ;;
  933.  
  934. user_rights)
  935. export FEATURE_NAME=user_rights.feature
  936. ;;
  937.  
  938. record_rules)
  939. export FEATURE_NAME=record_rules.feature
  940. ;;
  941.  
  942. window_actions)
  943. export FEATURE_NAME=window_actions.feature
  944. ;;
  945.  
  946. *)
  947. echo "I don't know file type $3"
  948. return 1
  949.  
  950. esac
  951.  
  952. if [[ -e "$4" ]]
  953. then
  954.  
  955. _convert_name $1 $2
  956. export LETTUCE_DATABASE=$RESULT
  957.  
  958. setup_lettuce $1 $LETTUCE_DATABASE $FEATURE_NAME "$4"
  959. else
  960. echo "File $4 does not exist"
  961. return 1
  962. fi
  963.  
  964. BEFORE_TIME=$TIME_BEFORE_FAILURE
  965. export TIME_BEFORE_FAILURE=
  966.  
  967. if ./runtests_local.sh $FEATURE_NAME ;
  968. then
  969. echo "Import $LETTUCE_DATABASE: OK"
  970. RET=0
  971. else
  972. echo "Import $LETTUCE_DATABASE: FAILURE"
  973. RET=1
  974. fi
  975.  
  976. export TIME_BEFORE_FAILURE=$BEFORE_TIME
  977.  
  978. clean_lettuce
  979.  
  980. return $RET
  981.  
  982. echo "OK"
  983. }
  984.  
  985. pct_update()
  986. {
  987. pct_drop 0 OCG_Jasper
  988.  
  989. echo "pct_dropall 0 OCG%"
  990. echo "pct_download 0 OCG OCG%"
  991. echo "pct_passwordall 0 admin"
  992. echo "pct_linkall 0 SYNC_SERVER_XXX"
  993. echo "pct_loginall -t 1500 0"
  994.  
  995. DBNAME="OCG_Jasper"
  996. pct 0 postgres -c "CREATE DATABASE \"$DBNAME\""
  997.  
  998. LOGIN_POSTGRES=$POSTGRES_USERNAME
  999. PASSWORD_POSTGRES=$POSTGRES_PASSWORD
  1000. PORT=$MYPORT
  1001. export PGPASSWORD=$PASSWORD_POSTGRES
  1002.  
  1003.  
  1004.  
  1005. pg_dump -h ct0 -s -U $LOGIN_POSTGRES -t purchase_order OCG_UG2_SUKA | psql -h ct0 -U $LOGIN_POSTGRES -d OCG_Jasper
  1006.  
  1007. echo ""
  1008. echo "ADD instance column"
  1009. pct 0 postgres -c "ALTER TABLE \"purchase_order\" ADD COLUMN \"uf_instance\" text " $DBNAME
  1010.  
  1011. }
  1012.  
  1013. pct_help()
  1014. {
  1015. echo "List the databases:"
  1016. echo " pct_all_instances: returns all the databases"
  1017. echo " pct_other_instances: returns all the databases used for an instance that is not a sync server"
  1018. echo " pct_sync_servers: returns all the databases used for a sync server"
  1019. echo
  1020. echo " pct_upgrade: apply a patch on a computer using the updater"
  1021. echo " pct_download: downloads dump files and restore them"
  1022. echo " pct_import: import CSV files in a database"
  1023. echo
  1024. echo "Manage them:"
  1025. echo " pct_drop: drops a database"
  1026. echo " pct_dropall: drops all the databases (or a part of them)"
  1027. echo " pct_dump: backs up a database (.dump)"
  1028. echo " pct_dumpall: backs up all the databases (or a part of them)"
  1029. echo " pct_sqldump: back up a database (.sql)"
  1030. echo " pct_sqldumpall: back up all the databases (in the current directory)"
  1031. echo " pct_restore: restore a database"
  1032. echo " pct_restoreall: restore all the databases (in the current directory)"
  1033. echo
  1034. echo "Make them available:"
  1035. echo " pct_link: link two database (used for synchronization)"
  1036. echo " pct_linkall: link all the databases (used for synchronization)"
  1037. echo " pct_password: reset the admin password"
  1038. echo " pct_passwordall: reset the admin password in all the databases"
  1039. echo
  1040. echo "Update them:"
  1041. echo " pct_login: log in a database (used to upgrade it)"
  1042. echo " pct_loginall: log in all the databases (used to upgrade them)"
  1043. echo " pct_sync: synchronize an instance"
  1044. echo " pct_syncall: synchronize all the instances (according to their link)"
  1045.  
  1046. echo
  1047. echo "Automatic OCG restore"
  1048. echo " pct_update: automatic upgrade of the latest OC dbs"
  1049. }
Add Comment
Please, Sign In to add comment