|
1 #!/bin/bash |
|
2 # hg.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 hg operations required by puppi::project::hg" |
|
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 "-t <tag> (Optional) Tag to deploy" |
|
20 echo "-b <branch> (Optional) Branch to deploy" |
|
21 echo "-c <commit> (Optional) Commit to deploy" |
|
22 echo "-v <true|false> (Optional) If verbose" |
|
23 echo "-k <true|false> (Optional) If .hg dir is kept on deploy_root" |
|
24 echo |
|
25 echo "Examples:" |
|
26 echo "hg.sh -a deploy -s $source -d $deploy_root -u $user -t $tag -b $branch -c $commit -v $bool_verbose -k $bool_keep_hgdata" |
|
27 } |
|
28 |
|
29 verbose="true" |
|
30 |
|
31 # Check Arguments |
|
32 while [ $# -gt 0 ]; do |
|
33 case "$1" in |
|
34 -a) |
|
35 case $2 in |
|
36 rollback) |
|
37 action="rollback" |
|
38 ;; |
|
39 *) |
|
40 action="install" |
|
41 ;; |
|
42 esac |
|
43 shift 2 ;; |
|
44 -s) |
|
45 if [ $source ] ; then |
|
46 source=$source |
|
47 else |
|
48 source=$2 |
|
49 fi |
|
50 shift 2 ;; |
|
51 -d) |
|
52 if [ $deploy_root ] ; then |
|
53 deploy_root=$deploy_root |
|
54 else |
|
55 deploy_root=$2 |
|
56 fi |
|
57 shift 2 ;; |
|
58 -u) |
|
59 if [ $user ] ; then |
|
60 deploy_user=$user |
|
61 else |
|
62 deploy_user=$2 |
|
63 fi |
|
64 shift 2 ;; |
|
65 -t) |
|
66 if [ $hg_tag ] ; then |
|
67 hg_tag=$hg_tag |
|
68 else |
|
69 hg_tag=$2 |
|
70 fi |
|
71 shift 2 ;; |
|
72 -b) |
|
73 if [ $branch ] ; then |
|
74 branch=$branch |
|
75 else |
|
76 branch=$2 |
|
77 fi |
|
78 shift 2 ;; |
|
79 -c) |
|
80 if [ $commit ] ; then |
|
81 commit=$commit |
|
82 else |
|
83 commit=$2 |
|
84 fi |
|
85 shift 2 ;; |
|
86 -v) |
|
87 if [ $verbose ] ; then |
|
88 verbose=$verbose |
|
89 else |
|
90 verbose=$2 |
|
91 fi |
|
92 shift 2 ;; |
|
93 -k) |
|
94 if [ $keep_hgdata ] ; then |
|
95 keep_hgdata=$keep_hgdata |
|
96 else |
|
97 keep_hgdata=$2 |
|
98 fi |
|
99 shift 2 ;; |
|
100 *) |
|
101 showhelp |
|
102 exit ;; |
|
103 esac |
|
104 done |
|
105 |
|
106 if [ "x$verbose" == "xtrue" ] ; then |
|
107 verbosity="-v" |
|
108 else |
|
109 verbosity="" |
|
110 fi |
|
111 |
|
112 cd / |
|
113 |
|
114 hgdir=$deploy_root |
|
115 if [ "x$keep_hgdata" != "xtrue" ] ; then |
|
116 if [ ! -d $archivedir/$project-hg ] ; then |
|
117 mkdir $archivedir/$project-hg |
|
118 chown -R $deploy_user:$deploy_user $archivedir/$project-hg |
|
119 fi |
|
120 hgdir=$archivedir/$project-hg/hgrepo |
|
121 fi |
|
122 |
|
123 do_install () { |
|
124 if [ -d $hgdir/.hg ] ; then |
|
125 cd $hgdir |
|
126 hg pull $verbosity origin $branch |
|
127 hg update $verbosity $branch |
|
128 if [ "x$?" != "x0" ] ; then |
|
129 hg update $verbosity $branch |
|
130 fi |
|
131 else |
|
132 hg clone $verbosity --branch $branch $source $hgdir |
|
133 cd $hgdir |
|
134 fi |
|
135 |
|
136 if [ "x$hg_tag" != "xundefined" ] ; then |
|
137 hg update $verbosity $hg_tag |
|
138 fi |
|
139 |
|
140 if [ "x$commit" != "xundefined" ] ; then |
|
141 hg update $verbosity $commit |
|
142 fi |
|
143 |
|
144 if [ "x$hgdir" == "x$archivedir/$project-hg" ] ; then |
|
145 rsync -a --exclude=".hg" $hgdir/$hgsubdir $deploy_root/ |
|
146 fi |
|
147 |
|
148 } |
|
149 |
|
150 do_rollback () { |
|
151 |
|
152 echo "Rollback not yet supported" |
|
153 } |
|
154 |
|
155 # Action! |
|
156 case "$action" in |
|
157 install) export -f do_install ; su $deploy_user -c do_install ;; |
|
158 rollback) do_rollback ;; |
|
159 esac |