[rt18107] Deploy from git
[sitka/sitka-tools.git] / deployment / git-deploy.sh
CommitLineData
2385238b
LW
1#!/bin/bash
2
3#TODO decide about sql files and other odd files
4#TODO make sure access_pathmap.ini is fixed so Open-ILS/xul/staff_client/ files map properly
5
6PROD_SERVERS=" app1-1 app1-2 app2-1 app2-2 app3-1 app3-2 stanns bibc-prod "
7REPO='.'
8
9function usage()
10{
11 cat <<EOF
12USAGE:
13./git-deploy.sh -g ~/evergreen/.git -r origin -b HEAD host1 [ host2 ... ]
14./git-deploy.sh -r origin -b user/lwhalen/rt15288 -P
15
16This script is currently in testing. By default it will echo the commands it would run to the screen.
17If you wan to deploy something you need to use the -d option.
18
19e.g. ./git-deploy.sh -r origin -b HEAD -d host1
20
21As well, this script will not deploy to our production servers currently. It will print out a message
22saying so if you try to use a production server as a host option or if you use the -P option to deploy
23to all production servers.
24
25OPTIONS:
26 -g The location of the git repo
27 -r The remote to deploy from
28 -b The branch on the remote to deploy from
29 -P Deploy to all production servers
30 -d Actually deploy. While testing this option is needed to force the commands to execute
31EOF
32}
33
34function create_tmp_file() {
35 TMP_DIR=$1
36 FILENAME=$2
37 touch $TMP_DIR/$FILENAME
38 echo "$TMP_DIR/$FILENAME"
39}
40
41function get_full_path() {
42 RELATIVE_PATH=$1
43
44 for COMPONENT in `access_pathmap.pl --config $PATH_MAP_FILE`
45 do
46 BASE_PATH=`access_pathmap.pl --config $PATH_MAP_FILE --component $COMPONENT --srcpath $RELATIVE_PATH`
47 if [[ $BASE_PATH ]]
48 then
49 #We extract the last directory in the base path so we can
50 #use parameter substitution to remove everything up to the
51 #last directory in the BASE_PATH from the RELATIVE_PATH
52 #this leaves us with the part of the git source tree path
53 #that needs to be appended to the base path in order to locate
54 #the file on the server
55 LAST_DIR_IN_BASE_PATH=`basename $BASE_PATH`
56 RELATIVE_PART=${RELATIVE_PATH/*$LAST_DIR_IN_BASE_PATH/}
57 echo $BASE_PATH$RELATIVE_PART
58 return;
59 fi
60 done
61}
62
63function deployfile() {
64 REMOTE=$1
65 BRANCH=$2
66 RELATIVE_PATH=$3
67 FILENAME=$4
68 TMP_DIR=$5
69 HOSTS=$6
70
71 PERL_FILE_RE='^.*.pm$'
72 SQL_FILE_RE='^.*.sql$'
73 TT2_FILE_RE='^Open-ILS/src/(templates)/(.*)$'
74 USER='opensrf'
75
76 if [[ $FILENAME =~ $PERL_FILE_RE ]]
77 then
78 USER='root'
79 elif [[ $FILENAME =~ $SQL_FILE_RE ]]
80 then
81 echo 'SQL file -- TAKE THE TIME TO LOAD THIS PROPERLY INTO THE DATABASE. DO NOT FORGET TO WRAP IT IN BEGIN AND COMMIT'
82 return
83 else
84 USER='opensrf'
85 fi
86
87 FULL_PATH=`get_full_path $RELATIVE_PATH`
88
89 TEMPFILE=`create_tmp_file $TMP_DIR $FILENAME`
90 git --git-dir $REPO show $REMOTE/$BRANCH:$RELATIVE_PATH/$FILENAME > $TEMPFILE
91 for HOST in $HOSTS
92 do
93 if [[ $PROD_SERVERS =~ " $HOST " ]]
94 then
95 echo "Cannot deploy `basename $TEMPFILE` to $HOST while testing. Please use another server"
96 else
97 COMMAND="deployfile.sh -f $TEMPFILE -p $FULL_PATH -u $USER -b $HOST"
98 if [[ -z "$DEPLOY" ]]
99 then
100 echo $COMMAND
101 else
102 $COMMAND
103 fi
104
105 fi
106 done
107}
108
109while getopts ":g:r:b:c:Pdh" opt
110do
111 case $opt in
112 g ) REPO=$OPTARG;;
113 r ) REMOTE=$OPTARG;;
114 b ) BRANCH=$OPTARG;;
115 c ) PATH_MAP_FILE=$OPTARG;;
116 P ) PROD="P";;
117 d ) DEPLOY="d";;
118 h ) usage && exit 0;;
119 esac
120done
121
122if [[ ! "$REPO" =~ .git$ ]]
123then
124 REPO=$REPO"/.git"
125fi
126
127shift $(($OPTIND - 1))
128
129if [[ -z "$PROD" ]]
130then
131 HOST_LIST=$@
132else
133 HOST_LIST=$@$PROD_SERVERS
134fi
135
136if [[ -z $HOST_LIST ]]
137then
138 echo 'You must specify a host to deploy to in order to use this script'
139 exit 1
140fi
141
142TMP_DIR=`mktemp -d`
143git --git-dir $REPO show $REMOTE/$BRANCH --name-only --oneline | awk '{if(NR!=1) {print}}' | \
144while read PATH_AND_FILENAME
145do
146 FILENAME=`basename $PATH_AND_FILENAME`
147 RELATIVE_PATH=`dirname $PATH_AND_FILENAME`
148 deployfile $REMOTE $BRANCH $RELATIVE_PATH $FILENAME $TMP_DIR "$HOST_LIST"
149done