daily pastebin goal
14%
SHARE
TWEET

build-myapp.sh

a guest Aug 4th, 2014 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #!/usr/bin/env bash
  3. #
  4. # Build, assemble, and prepare for deployment
  5. #
  6. #
  7.  
  8.  
  9. #-----------------------------------------------------------------------------
  10. # setup and validation
  11. #-----------------------------------------------------------------------------
  12. dname="myapp"
  13. virtname="myapp"
  14. code_path="trunk"
  15. rebuild_virt=0
  16. target_environment="dev"
  17.  
  18. #-----------------------------------------------------------------------------
  19. # Parse options and overrides
  20. #-----------------------------------------------------------------------------
  21. usage () {
  22.     echo
  23.     echo "$0"
  24.     echo "Build application and prepare for deployment"
  25.     echo
  26.     echo "Usage:"
  27.     echo "   [-r]           # rebuild virtual environment"
  28.     echo "   [-c CODEPATH]  # alternate code export path"
  29.     echo "   [-e]           # target environment (dev|prod)"
  30.     echo
  31.     echo "Examples:"
  32.     echo "$0  # defaults: no virt rebuild. export from trunk."
  33.     echo "$0 -r"
  34.     echo "$0 -c 'trunk'"
  35.     echo "$0 -c 'tags/REL-0.1'"
  36.     echo "$0 -e 'prod'"
  37.     echo "$0 -e 'dev'"
  38.     echo
  39. }
  40.  
  41. while getopts ":rc:e:" Option
  42. do
  43.     case $Option in
  44.         r ) rebuild_virt=1;;
  45.         c ) code_path="$OPTARG";;
  46.         e ) target_environment="$OPTARG";;
  47.         * ) usage; exit 1; ;;
  48.     esac
  49. done
  50. shift $(($OPTIND - 1))
  51.  
  52.  
  53. #------------------------------------------------------------------------------
  54. # Setup / Validate
  55. #------------------------------------------------------------------------------
  56.  
  57. approot="/opt/company/$virtname"
  58. exportroot="$approot/$dname"
  59. repo_uri="svn+ssh://somerepo.company.com/var/svn/repos/$dname/${code_path}/"
  60.  
  61.  
  62. result=$(svn ls "$repo_uri")
  63. retval="$?"
  64. if [ $retval -ne 0 ]; then
  65.     echo "Not a valid code branch or tag"
  66.     exit 2
  67. fi
  68.  
  69.  
  70. if [[ $target_environment != "prod" && $target_environment != "dev" ]]; then
  71.     echo "Not a valid target_environment"
  72.     exit 3
  73. fi
  74.  
  75.  
  76. #-----------------------------------------------------------------------------
  77. # start
  78. #-----------------------------------------------------------------------------
  79.  
  80. echo "::> starting build process"
  81. echo -n "::> "; date
  82.  
  83. echo "::> deleting pre-existing work directories"
  84. rm -rf "$exportroot"
  85.  
  86.  
  87. #-----------------------------------------------------------------------------
  88. # virtualenv
  89. #-----------------------------------------------------------------------------
  90.  
  91. if [ $rebuild_virt -eq 1 ]; then
  92.  
  93.     echo "::> re-creating new python virtual environment"
  94.     rm -rf "$approot"
  95.     mkdir "$approot"
  96.     virtualenv --no-site-packages $approot
  97.  
  98.     echo "::> entering new virtual environment"
  99.     cd $approot
  100.     source ./bin/activate
  101.  
  102. else
  103.  
  104.     echo "::> using existing python virtual environment"
  105.     echo "::> entering virtual environment"
  106.     cd $approot
  107.     source ./bin/activate
  108.  
  109. fi
  110.  
  111.  
  112. #-----------------------------------------------------------------------------
  113. # application
  114. #-----------------------------------------------------------------------------
  115.  
  116. # leave note indicating automated build
  117. touch "$approot/BUILT-BY-$(basename $0)"
  118.  
  119. echo "::> exporting a copy of code from svn repo"
  120. svn -q export $repo_uri $exportroot
  121.  
  122. echo "::> installing/updating dependencies (using pip inside virtualenv)"
  123. pip install -r $exportroot/requirements.txt
  124.  
  125. echo "::> app root: $approot"
  126. echo "::> app root contents:"
  127. cd
  128. ls -la $approot
  129.  
  130.  
  131. #-----------------------------------------------------------------------------
  132. # finish
  133. #-----------------------------------------------------------------------------
  134.  
  135. echo "::> finished"
  136. echo -n "::> "; date
RAW Paste Data
Top