aboutsummaryrefslogtreecommitdiffstats
path: root/create
blob: 04d299a8b4c0195f06cccfcf4ce45ded698db907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#! /bin/bash

# usage:	create <PROJECT> - create a git bare repository named PROJECT.git
#		this command will setup the repo and send a mail for confirmation.

GITDIR="/var/git"
MULTIMAIL="/usr/doc/git-2.14.5/contrib/hooks/multimail/git_multimail.py"
GITUSER="git"
GITGRP="git"
VENVSDIR="/usr/local/venvs"
USEVENV=false

function is_bare() {
	repodir=$1
	if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true
	then
		true
	else
		false
	fi
}

function git_init() {
	PROJECT=$1
	echo "creating project \"${PROJECT}.git\""
	read -p "Describe your project in one line: " DESCRIPTION
	if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
		mkdir ${GITDIR}/${PROJECT}.git
	fi
	cd ${GITDIR}/${PROJECT}.git
	git init --bare
	echo "Remember to insert a README.md file to explain what your project is about."
	mkdir custom-hooks
	ln -s $MULTIMAIL custom-hooks/git_multimail.py
	touch hooks/post-receive
	cat > hooks/post-receive <<EOPR
#!/bin/sh
/usr/bin/pee ${GITDIR}/${PROJECT}.git/custom-hooks/deploy.sh \
	${GITDIR}/${PROJECT}.git/custom-hooks/pip_install \
	${GITDIR}/${PROJECT}.git/custom-hooks/git_multimail.py

EOPR
	if [ $USEVENV == true ]; then
		cat > custom-hooks/pip_install << EOPIP
#!/bin/sh
while read oldrev newrev refname; do
	if [[ \$refname =~ .*/master$ ]]; then
		# definitely updating master;
		CHECKFILE=\$(git ls-tree --full-tree -r HEAD |grep requirements.txt |awk '{print \$3}')
		TMPREQ=\$(git cat-file -p \$CHECKFILE > /tmp/${PROJECT}-req.txt)
		if [ \$CHECKFILE ]; then
			${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt
		fi
		if git diff-tree --name-only -r -z \$oldrev \$newrev \$CHECKFILE; then
			${VENVSDIR}/${PROJECT}/bin/pip install -r /tmp/${PROJECT}-req.txt
		fi
	fi
done
EOPIP
	fi
	cat >> config <<EOT

[multimailhook]
	mailer = "sendmail"
	refchangeShowGraph = true
	mailingList = "danixland@gmail.com"
	commitEmailFormat = "html"
	htmlInIntro = true
	htmlInFooter = true
	from = "danix@danix.xyz"
	administrator = "danix@danix.xyz"
	quiet = true
	logFile = "/var/log/multimail.log"
	errorLogFile = "/var/log/multimail_err.log"
EOT
	touch custom-hooks/deploy.sh
	cat > custom-hooks/deploy.sh <<EODP
#!/bin/bash
# Directory where to deploy files from repository
DPTARGET=""
# Directory containing repository
DPGIT_DIR="${GITDIR}/${PROJECT}.git"
# Branch that is going to be deployed to server
DPBRANCH="master"

while read oldrev newrev ref
do
	# if DPTARGET is empty we don't want deploy for this project
	if [[ ! "" == \$DPTARGET ]]; then
		# let's check that we are deploying to the correct branch
		if [[ \$ref = refs/heads/\${DPBRANCH} ]]; then
			echo "Ref \$ref received. Deploying \${DPBRANCH} branch to production..."
			git --work-tree=\$DPTARGET --git-dir=\$DPGIT_DIR checkout -f \$DPBRANCH
			NOW=\$(date +"%d%m%Y-%H%M")
			git tag release_\$NOW \$DPBRANCH
			echo "   /==============================="
			echo "   | DEPLOYMENT COMPLETED"
			echo "   | Target branch: \$DPTARGET"
			echo "   | Target folder: \$DPGIT_DIR"
			echo "   | Tag name     : release_\$NOW"
			echo "   \=============================="
		else
			echo "Ref \$ref received. Doing nothing: only the \${DPBRANCH} branch may be deployed on this server."
		fi
	else
		echo "Target directory not declared. Skipping deploy to server."
	fi
done

EODP
	chmod 0755 hooks/post-receive custom-hooks/deploy.sh
	echo $DESCRIPTION > description
	cd ${GITDIR}/
	chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git
	echo "All done, you can now work on \"${PROJECT}.git\""
	exit 0
}

if [ ! -z $1 ]; then
	if [ "python" == $1 ]; then
		USEVENV=true
		# this is a python project. Let's create a virtualenv
		if [ ! -z $2 ]; then
			PROJECT=$2
		else
			read -p 'Python project name: ' PROJECT
		fi
		virtualenv ${VENVSDIR}/${PROJECT}
		echo "virtual environment created inside ${VENVSDIR}/${PROJECT}"
	else
		PROJECT=$1
	fi
else
	read -p 'Project name: ' PROJECT
fi

if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
	git_init $PROJECT
else
	echo "Project directory ${PROJECT}.git already exists."
	if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then
		if is_bare ${GITDIR}/${PROJECT}.git
		then
			echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name."
			exit 171
		else
			echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name."
			exit 172
		fi
	else
		echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]"
		read answer
		case $answer in
			Y|y)
				git_init $PROJECT	
				;;
			N|n)
				echo "Aborting due to user request."
				exit 173
				;;
			*)
				# we assume no as default answer.
				echo "you said \"$answer\" which I don't understand, so to me is no. Aborting."
				exit 177
				;;
		esac
	fi
fi