dev/provisioning/modules/puppi/files/scripts/git.sh
changeset 28 b0b56e0f8c7f
equal deleted inserted replaced
27:a2342f26c9de 28:b0b56e0f8c7f
       
     1 #!/bin/bash
       
     2 # git.sh - Made for Puppi
       
     3 
       
     4 # All variables are exported
       
     5 set -a 
       
     6 
       
     7 # Sources common header for Puppi scripts
       
     8 . $(dirname $0)/header || exit 10
       
     9 
       
    10 # Show help
       
    11 showhelp () {
       
    12     echo "This script performs the git operations required by puppi::project::git"
       
    13     echo "It integrates and uses variables provided by other core Puppi scripts"
       
    14     echo "It has the following options:"
       
    15     echo "-a <action> (Optional) What action to perform. Available options: deploy (default), rollback"
       
    16     echo "-s <source> (Required) Git source repo to use"
       
    17     echo "-d <destination> (Required) Directory where files are deployed"
       
    18     echo "-u <user> (Optional) User that performs the deploy operations. Default root"
       
    19     echo "-gs <git_subdir> (Optional) If only a specific subdir of the gitrepo has to be copied to the install destination"
       
    20     echo "-t <tag> (Optional) Tag to deploy"
       
    21     echo "-b <branch> (Optional) Branch to deploy"
       
    22     echo "-c <commit> (Optional) Commit to deploy"
       
    23     echo "-v <true|false> (Optional) If verbose"
       
    24     echo "-k <true|false> (Optional) If .git dir is kept on deploy_root"
       
    25     echo 
       
    26     echo "Examples:"
       
    27     echo "git.sh -a deploy -s $source -d $deploy_root -u $user -gs $git_subdir -t $tag -b $branch -c $commit -v $bool_verbose -k $bool_keep_gitdata"
       
    28 }
       
    29 
       
    30 verbose="true"
       
    31 
       
    32 # Check Arguments
       
    33 while [ $# -gt 0 ]; do
       
    34   case "$1" in
       
    35     -a)
       
    36       case $2 in
       
    37           rollback)
       
    38           action="rollback"
       
    39           ;;
       
    40           *)
       
    41           action="install"
       
    42           ;;
       
    43       esac 
       
    44       shift 2 ;;
       
    45     -s)
       
    46       if [ $source ] ; then
       
    47         source=$source
       
    48       else
       
    49         source=$2
       
    50       fi
       
    51       shift 2 ;;
       
    52     -d)
       
    53       if [ $deploy_root ] ; then
       
    54         deploy_root=$deploy_root
       
    55       else
       
    56         deploy_root=$2
       
    57       fi
       
    58       shift 2 ;;
       
    59     -u)
       
    60       if [ $user ] ; then
       
    61         deploy_user=$user
       
    62       else
       
    63         deploy_user=$2
       
    64       fi
       
    65       shift 2 ;;
       
    66     -gs)
       
    67       if [ $git_subdir ] ; then
       
    68         git_subdir=$git_subdir
       
    69       else
       
    70         git_subdir=$2
       
    71       fi
       
    72       shift 2 ;;
       
    73     -t)
       
    74       if [ $git_tag ] ; then
       
    75         git_tag=$git_tag
       
    76       else
       
    77         git_tag=$2
       
    78       fi
       
    79       shift 2 ;;
       
    80     -b)
       
    81       if [ $branch ] ; then
       
    82         branch=$branch
       
    83       else
       
    84         branch=$2
       
    85       fi
       
    86       shift 2 ;;
       
    87     -c)
       
    88       if [ $commit ] ; then
       
    89         commit=$commit
       
    90       else
       
    91         commit=$2
       
    92       fi
       
    93       shift 2 ;;
       
    94     -v)
       
    95       if [ $verbose ] ; then
       
    96         verbose=$verbose
       
    97       else
       
    98         verbose=$2
       
    99       fi
       
   100       shift 2 ;;
       
   101     -k)
       
   102       if [ $keep_gitdata ] ; then
       
   103         keep_gitdata=$keep_gitdata
       
   104       else
       
   105         keep_gitdata=$2
       
   106       fi
       
   107       shift 2 ;;
       
   108     *)
       
   109       showhelp
       
   110       exit ;;
       
   111   esac
       
   112 done
       
   113 
       
   114 if [ "x$verbose" == "xtrue" ] ; then
       
   115   verbosity=""
       
   116 else
       
   117   verbosity="--quiet"
       
   118 fi
       
   119 
       
   120 cd /
       
   121 
       
   122 gitsubdir=""
       
   123 gitdir=$deploy_root
       
   124 if [ "x$keep_gitdata" != "xtrue" ] ; then
       
   125   if [ ! -d $archivedir/$project-git ] ; then
       
   126     mkdir $archivedir/$project-git
       
   127     chown -R $deploy_user:$deploy_user $archivedir/$project-git
       
   128   fi
       
   129   gitdir=$archivedir/$project-git/gitrepo
       
   130 fi
       
   131 if [ "x$git_subdir" != "xundefined" ] ; then
       
   132   if [ ! -d $archivedir/$project-git ] ; then
       
   133     mkdir $archivedir/$project-git
       
   134     chown -R $deploy_user:$deploy_user $archivedir/$project-git
       
   135   fi
       
   136   gitdir=$archivedir/$project-git
       
   137   gitsubdir="$git_subdir/"
       
   138 fi
       
   139 
       
   140 do_install () {
       
   141   if [ -d $gitdir/.git ] ; then
       
   142     cd $gitdir
       
   143     git pull $verbosity origin $branch
       
   144     git checkout $verbosity $branch
       
   145     if [ "x$?" != "x0" ] ; then
       
   146       git checkout -b $verbosity $branch
       
   147     fi
       
   148   else
       
   149     git clone $verbosity --branch $branch --recursive $source $gitdir
       
   150     cd $gitdir
       
   151   fi
       
   152 
       
   153   if [ "x$git_tag" != "xundefined" ] ; then
       
   154     git checkout $verbosity $git_tag
       
   155   fi
       
   156 
       
   157   if [ "x$commit" != "xundefined" ] ; then
       
   158     git checkout $verbosity $commit
       
   159   fi
       
   160 
       
   161   if [ "x$gitdir" == "x$archivedir/$project-git/gitrepo" ] ; then
       
   162     rsync -a --exclude=".git" $gitdir/$gitsubdir $deploy_root/
       
   163   fi
       
   164 
       
   165 }
       
   166 
       
   167 do_rollback () {
       
   168 
       
   169   echo "Rollback not yet supported"
       
   170 }
       
   171 
       
   172 # Action!
       
   173 case "$action" in
       
   174     install) export -f do_install ; su $deploy_user -c do_install ;;
       
   175     rollback) do_rollback ;;
       
   176 esac