Guest User

Untitled

a guest
Oct 22nd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # SETUP
  5. #
  6.  
  7. if [ "$#" -lt 1 ] ; then
  8. echo "Called without parameters, exiting"
  9. echo "Usage: $0 <AGAVI ENVIROMENT NAME>"
  10. exit 1
  11. fi
  12. no_childs=0
  13. while (($#)) ; do
  14. case "$1" in
  15. "--no-childs") no_childs=1 ;;
  16. *) env_arg="$1" ;;
  17. esac
  18. shift
  19. done
  20. if [ $env_arg == "" ] ; then
  21. echo "Called without enviroment"
  22. exit 2
  23. fi
  24. if [ `echo $env_arg | grep -c '/'` -eq 1 ] ; then
  25. env_name=`echo $env_arg | cut -d'/' -f1`
  26. else
  27. env_name="$env_arg"
  28. fi
  29.  
  30. #
  31. # CONFIGURATION
  32. #
  33. timestamp=`date +%Y%m%d-%H%M%S`
  34. hostname=`cat /etc/hostname`
  35. pidfile="/tmp/agavi-deploy.pid"
  36.  
  37. webroot="HTDOCROOT"
  38. agavi_env="${webroot}/${env_name}"
  39. checkout="${agavi_env}/checkout"
  40. svn_export="${agavi_env}/export-${timestamp}"
  41. svn_bin="${svn_export}/dev/bin"
  42. construction_page_src="${checkout}/dev/pub/index-deploy.php"
  43. construction_page_dest="${svn_export}/pub"
  44. html="${agavi_env}/html"
  45.  
  46. # SVN CONFIGURATION
  47. svn_user="USER"
  48. svn_pass="PASS"
  49.  
  50. #
  51. # GLOBAL DECLARATIONS
  52. #
  53. rollback=0
  54. #
  55. # FUNCTIONS
  56. #
  57.  
  58. # Exit with a message
  59. e() {
  60. if [ "$1" -gt 19 ] ; then
  61. rm $pidfile
  62. fi
  63. case "$1" in
  64. # Pidfile does NOT get removed if
  65. # errorcode is lower than 20
  66. "0") echo "deploy successful." && rm $pidfile ;;
  67. "10") echo "${pidfile} found, Process already running with pid: `cat $pidfile`" ;;
  68. "20") echo "Folder $2 does not exist, aborting" ;;
  69. "25") echo "Symlink or file $3 exists in $2 " ;;
  70. "26") echo "Cannot create Symlink, target $3 not found in $2 " ;;
  71. "27") echo "Cannot create Symlink to export, rolling back to $2 " ;;
  72. "30") echo "User exit" ;;
  73. "50") echo "Something went terribly wrong. Command $3 returned $2 exitstatus. Aborting" ;;
  74. "60") echo "Variable for $2 not set, aborting" ;;
  75. *) echo "Something wicked happened... o.O " ;;
  76. esac
  77. exit $1
  78. }
  79.  
  80. check_pidfile() {
  81. [ -f "$pidfile" ] && e 10
  82. echo "$$" > $pidfile
  83. }
  84.  
  85. check_folder() {
  86. [ ! -d "$1" ] && e 20 "$1"
  87. }
  88.  
  89. check_status() {
  90. if [ $rollback -ne 1 ] ; then
  91. [ $1 -ne 0 ] && e 50 "$1" "$2"
  92. else
  93. if [ $1 -ne 0 ] ; then
  94. cd $agavi_env
  95. [ -s html ] && rm html
  96. ln -s $prev html
  97. echo "Errors occured, rolling back to $prev"
  98. e 50 "$1" "$2"
  99. fi
  100. fi
  101. }
  102.  
  103. create_symlink() {
  104. # Usage create_symlink A B C
  105. # A: Working Directory
  106. # B: Target
  107. # C: Link Name
  108. check_folder $1
  109. current_dir=`pwd`
  110. cd $1
  111. check_status $? "cd"
  112. if [ -e ${1}/$3 ] ; then
  113. # Symlink or File exists
  114. e 25 "$1" "$3"
  115. fi
  116. if [ ! -e ${1}/$2 ] ; then
  117. # No Target for Link
  118. e 26 "$1" "$2"
  119. fi
  120. ln -s ${2} ${3}
  121. check_status $? "ln"
  122. cd $current_dir
  123. }
  124.  
  125. #
  126. # MAIN
  127. #
  128.  
  129. check_pidfile
  130. check_folder "$agavi_env"
  131. check_folder "$checkout"
  132.  
  133.  
  134. # SVN Update
  135. cd $checkout
  136. svn --username $svn_user --password $svn_pass up
  137. check_status "$?" "svn update"
  138.  
  139. # SVN Export
  140. cd $checkout
  141. svn export . $svn_export
  142. check_status "$?" "svn export"
  143.  
  144. # Creating Symlinks
  145. create_symlink "$svn_bin" "agavi_${hostname}_${env_name}" "agavi"
  146.  
  147. # Set permissions
  148. chmod +x ${svn_bin}/agavi
  149.  
  150. # html Symlink
  151. cd $agavi_env
  152. # This is critical, we need to roll back from here if anything unexpected happens
  153. rollback=1
  154. prev=`ls -l html | awk '{ print $11 }'`
  155. [ -s "html" ] && rm html
  156. create_symlink "$agavi_env" "export-${timestamp}" "html"
  157. check_status $? "ln"
  158.  
  159. # Set construction page
  160. cp $construction_page_src $construction_page_dest
  161. cd $construction_page_dest
  162. mv index-deploy.php index.php
  163.  
  164. # Set fs permissions
  165. chgrp -R www-data ${html}/app/cache
  166. chgrp -R www-data ${html}/app/log
  167. chmod -R 770 ${html}/app/cache
  168. chmod -R 770 ${html}/app/log
  169.  
  170. # Agavi create
  171. rm ${construction_page_dest}/index.php
  172. cd ${html}/dev/bin
  173. check_status $? "cd"
  174. ./agavi -D public.environment $env_name -D target.public-create.create-htaccess n public-create
  175. check_status $? "agavi public-create"
  176.  
  177. # Show Symlink
  178. ls -l ${html}
  179.  
  180. e 0
Add Comment
Please, Sign In to add comment