81873de4 |
1 | #! /bin/bash |
2 | |
3 | # usage: create <PROJECT> - create a git bare repository named PROJECT.git |
4 | # this command will setup the repo and send a mail for confirmation. |
5 | |
6 | GITDIR="/var/git" |
7 | MULTIMAIL="/usr/doc/git-2.14.5/contrib/hooks/multimail/git_multimail.py" |
8 | GITUSER="git" |
9 | GITGRP="git" |
10 | VENVSDIR="/usr/local/venvs" |
11 | USEVENV=false |
12 | |
13 | function is_bare() { |
14 | repodir=$1 |
15 | if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true |
16 | then |
17 | true |
18 | else |
19 | false |
20 | fi |
21 | } |
22 | |
23 | function git_init() { |
24 | PROJECT=$1 |
25 | echo "creating project \"${PROJECT}.git\"" |
13686e1e |
26 | echo -n "Describe your project in one line: " |
27 | read DESCRIPTION |
81873de4 |
28 | if [ ! -d ${GITDIR}/${PROJECT}.git ]; then |
29 | mkdir ${GITDIR}/${PROJECT}.git |
30 | fi |
31 | cd ${GITDIR}/${PROJECT}.git |
32 | git init --bare |
33 | echo "Remember to insert a README.md file to explain what your project is about." |
34 | mkdir custom-hooks |
35 | ln -s $MULTIMAIL custom-hooks/git_multimail.py |
36 | touch hooks/post-receive |
37 | cat > hooks/post-receive <<EOPR |
38 | #!/bin/sh |
39 | /usr/bin/pee ${GITDIR}/${PROJECT}.git/custom-hooks/deploy.sh \ |
40 | ${GITDIR}/${PROJECT}.git/custom-hooks/pip_install \ |
41 | ${GITDIR}/${PROJECT}.git/custom-hooks/git_multimail.py |
42 | |
43 | EOPR |
44 | if [ $USEVENV == true ]; then |
45 | cat > custom-hooks/pip_install << EOPIP |
46 | #!/bin/sh |
47 | while read oldrev newrev refname; do |
48 | if [[ \$refname =~ .*/master$ ]]; then |
49 | # definitely updating master; |
50 | CHECKFILE=\$(git ls-tree --full-tree -r HEAD |grep requirements.txt |awk '{print \$3}') |
51 | TMPREQ=\$(git cat-file -p \$CHECKFILE > /tmp/${PROJECT}-req.txt) |
52 | if [ \$CHECKFILE ]; then |
53 | ${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt |
54 | fi |
55 | if git diff-tree --name-only -r -z \$oldrev \$newrev \$CHECKFILE; then |
56 | ${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt |
57 | fi |
58 | fi |
59 | done |
60 | EOPIP |
61 | fi |
62 | cat >> config <<EOT |
63 | |
64 | [multimailhook] |
65 | mailer = "sendmail" |
66 | refchangeShowGraph = true |
67 | mailingList = "danixland@gmail.com" |
68 | commitEmailFormat = "html" |
69 | htmlInIntro = true |
70 | htmlInFooter = true |
71 | from = "danix@danix.xyz" |
72 | administrator = "danix@danix.xyz" |
73 | quiet = true |
74 | logFile = "/var/log/multimail.log" |
75 | errorLogFile = "/var/log/multimail_err.log" |
76 | EOT |
77 | touch custom-hooks/deploy.sh |
78 | cat > custom-hooks/deploy.sh <<EODP |
79 | #!/bin/bash |
80 | # Directory where to deploy files from repository |
81 | DPTARGET="" |
82 | # Directory containing repository |
83 | DPGIT_DIR="${GITDIR}/${PROJECT}.git" |
84 | # Branch that is going to be deployed to server |
85 | DPBRANCH="master" |
86 | |
87 | while read oldrev newrev ref |
88 | do |
89 | # if DPTARGET is empty we don't want deploy for this project |
90 | if [[ ! "" == \$DPTARGET ]]; then |
91 | # let's check that we are deploying to the correct branch |
92 | if [[ \$ref = refs/heads/\${DPBRANCH} ]]; then |
93 | echo "Ref \$ref received. Deploying \${DPBRANCH} branch to production..." |
94 | git --work-tree=\$DPTARGET --git-dir=\$DPGIT_DIR checkout -f \$DPBRANCH |
95 | NOW=\$(date +"%d%m%Y-%H%M") |
96 | git tag release_\$NOW \$DPBRANCH |
97 | echo " /===============================" |
98 | echo " | DEPLOYMENT COMPLETED" |
99 | echo " | Target branch: \$DPTARGET" |
100 | echo " | Target folder: \$DPGIT_DIR" |
101 | echo " | Tag name : release_\$NOW" |
102 | echo " \==============================" |
103 | else |
104 | echo "Ref \$ref received. Doing nothing: only the \${DPBRANCH} branch may be deployed on this server." |
105 | fi |
106 | else |
107 | echo "Target directory not declared. Skipping deploy to server." |
108 | fi |
109 | done |
110 | |
111 | EODP |
112 | chmod 0755 hooks/post-receive custom-hooks/deploy.sh |
113 | echo $DESCRIPTION > description |
114 | cd ${GITDIR}/ |
115 | chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git |
116 | echo "All done, you can now work on \"${PROJECT}.git\"" |
117 | exit 0 |
118 | } |
119 | |
120 | if [ ! -z $1 ]; then |
121 | if [ "python" == $1 ]; then |
122 | USEVENV=true |
123 | # this is a python project. Let's create a virtualenv |
124 | if [ ! -z $2 ]; then |
125 | PROJECT=$2 |
126 | else |
127 | read -p 'Python project name: ' PROJECT |
128 | fi |
129 | virtualenv ${VENVSDIR}/${PROJECT} |
130 | echo "virtual environment created inside ${VENVSDIR}/${PROJECT}" |
131 | else |
132 | PROJECT=$1 |
133 | fi |
134 | else |
135 | read -p 'Project name: ' PROJECT |
136 | fi |
137 | |
138 | if [ ! -d ${GITDIR}/${PROJECT}.git ]; then |
139 | git_init $PROJECT |
140 | else |
141 | echo "Project directory ${PROJECT}.git already exists." |
142 | if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then |
143 | if is_bare ${GITDIR}/${PROJECT}.git |
144 | then |
145 | echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name." |
146 | exit 171 |
147 | else |
148 | echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name." |
149 | exit 172 |
150 | fi |
151 | else |
152 | echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]" |
153 | read answer |
154 | case $answer in |
155 | Y|y) |
156 | git_init $PROJECT |
157 | ;; |
158 | N|n) |
159 | echo "Aborting due to user request." |
160 | exit 173 |
161 | ;; |
162 | *) |
163 | # we assume no as default answer. |
164 | echo "you said \"$answer\" which I don't understand, so to me is no. Aborting." |
165 | exit 177 |
166 | ;; |
167 | esac |
168 | fi |
169 | fi |
170 | |
171 | |