-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitmanager.sh
executable file
·205 lines (171 loc) · 5.52 KB
/
gitmanager.sh
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
PN=`basename "$0"`
#####################################################################################
# configuration
#####################################################################################
FOLDER_PATH=/var/www
CONF_PATH=/etc/apache2
REP_USER=git
REP_GROUP=www-data
REP_DOMAIN="http://<yourdomain>"
#####################################################################################
# Usage
#####################################################################################
Usage()
{
echo "$PN - Script to simplify the configuration of a git repository."
echo "It adds the apache configuration, password files and create empty"
echo "git repositorie(s)"
echo ""
echo "usage: $PN [-t] <name>"
echo ""
echo "Sample: $PN my-repository"
echo "# this will create an apache entry to access the repository"
}
#####################################################################################
# Create repository
#####################################################################################
reloadConfig()
{
echo ">Reload apache and systemctl"
sudo su - -c "service apache2 reload"
sudo su - -c "systemctl daemon-reload"
}
#####################################################################################
# Get user credentials
#####################################################################################
getUserCredentials()
{
# get credentials
read -p 'Username: ' username
read -sp 'Password: ' password
echo ""
}
#####################################################################################
# create apache access
#####################################################################################
createApacheAccess()
{
if [ -d "$FOLDER_PATH/$1" ]; then
echo "This gitfolder $FOLDER_PATH/$1 alredy exist!"
return
fi
echo "The url is $REP_DOMAIN/$1/"
getUserCredentials
echo ""
if ! [ -n "$username" ]; then
echo "Invalid empty username!"
exit 1
fi
if ! [ -n "$password" ]; then
echo "Invalid empty password!"
exit 1
fi
sudo su - -c "echo \>Create folter"
sudo su - -c "mkdir -p $FOLDER_PATH/$1"
sudo su - -c "chown -R ${REP_USER}:${REP_GROUP} $FOLDER_PATH/$1"
# be sure folders existing (also in test)
sudo su - -c "mkdir -p $CONF_PATH/conf-available" >/dev/null 2>&1
sudo su - -c "mkdir -p $CONF_PATH/conf-enabled" >/dev/null 2>&1
sudo su - -c "mkdir -p $CONF_PATH/gitpasswd" >/dev/null 2>&1
echo ">Create new Virtual Host"
cat <<< "<Location \"/$1\">
DAV on
AuthType Basic
AuthName \"Git repositories for $1\"
AuthUserFile $CONF_PATH/gitpasswd/$1.passwd
Require valid-user
</Location>" > /tmp/$1.conf$$
sudo su - -c "cat /tmp/$1.conf$$ > $CONF_PATH/conf-available/$1.conf && rm /tmp/$1.conf$$"
sudo su -c "ln -s $CONF_PATH/conf-available/$1.conf $CONF_PATH/conf-enabled/$1.conf"
echo ">Create htpsswd file"
echo -n ">"
sudo su - -c "htpasswd -c -b $CONF_PATH/gitpasswd/$1.passwd $username $password"
reloadConfig
}
#####################################################################################
# Create repository
#####################################################################################
createRepo()
{
if [ -d "$FOLDER_PATH/$1/$2.git" ]; then
echo "This repository $FOLDER_PATH/$1/$2.git alredy exist!"
return
fi
echo "The url is $REP_DOMAIN/$1/$2.git"
echo ">Creating repositroy $2.git .."
sudo su $REP_USER -c "mkdir -p $FOLDER_PATH/$1/$2.git"
# init git repository
echo -n ">"
sudo su $REP_USER -c "cd $FOLDER_PATH/$1/$2.git && git init --bare --shared"
sudo su $REP_USER -c "cd $FOLDER_PATH/$1/$2.git && git update-server-info"
sudo su $REP_USER -c "cd $FOLDER_PATH/$1/$2.git && chmod 775 -R *"
reloadConfig
}
#####################################################################################
#
#####################################################################################
askContinue()
{
read -p "Continue (y/n)? " choice
# change to lower case
choice=$(echo $choice | tr '[:upper:]' '[:lower:]')
}
#####################################################################################
# main
#####################################################################################
NAME=""
ADD_USER="false"
setup_git=0
while [ $# -gt 0 ]
do
case "$1" in
-s) setup_git=1 ;;
-t) FOLDER_PATH=/tmp/repo/www && CONF_PATH=/tmp/repo/apache2 ;;
-u) ADD_USER="true" ;;
-h) Usage; exit 1 ;;
-*) Usage; exit 1 ;;
*) NAME="$NAME$1"; break ;;
esac
shift
done
if ! [ -n "$NAME" ]; then
echo "Invalid parameter, missing name!"
exit 1
fi
if [ $setup_git -eq 1 ]; then
sudo su - -c "cat <<< \"LoadModule dav_module libexec/httpd/libdav.so
AddModule mod_dav.c
DAVLockDB \"/usr/local/apache2/temp/DAV.lock\"\" > /etc/apache2/httpd.conf"
sudo a2enmod dav_fs
sudo systemctl restart apache2
sudo useradd $REP_USER
sudo chown -R $REP_USER:$REP_GROUP $FOLDER_PATH
fi
createApacheAccess $NAME
while true
do
echo ""
echo "Do you want to create an additional repository?"
askContinue
if [ "$choice" == "y" ]; then
read -p 'Reponame: ' reponame
if ! [ -n "$reponame" ]; then
echo "Invalid empty reponame!"
else
echo ""
createRepo $NAME $reponame
fi
else
break;
fi
done
if [ "$ADD_USER" == "true" ]; then
echo ""
getUserCredentials
echo -n ">"
sudo su - -c "htpasswd -b $CONF_PATH/gitpasswd/$NAME.passwd $username $password"
fi
#####################################################################################
# EOF
#####################################################################################