forked from Danie/gitlab-dpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitlab.postinst
198 lines (166 loc) · 6 KB
/
gitlab.postinst
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
#!/bin/sh
set -e
#
# Function: add_user
#
# Description: add gitlab user "git"
#
add_user() {
# check if git user already exists and if not create it
id -u git > /dev/null 2>&1 || {
adduser --quiet --system --group --disabled-login \
--home /var/lib/gitlab --shell /bin/sh --gecos 'GitLab User' git
}
}
#
# Function: create_dirs
#
# Description: creates directory structure for gitlab
#
create_dirs() {
# create gitlab home directory
mkdir -p /var/lib/gitlab
chown git:git /var/lib/gitlab
# create gitlab satellites directory
mkdir -p /var/lib/gitlab/gitlab-satellites
chown git:git /var/lib/gitlab/gitlab-satellites
# create gitlab cache directory
mkdir -p /var/cache/gitlab
chown git:git /var/cache/gitlab
chmod 0750 /var/cache/gitlab
# create gitlab log directory
mkdir -p /var/log/gitlab
chown git:git /var/log/gitlab
chmod 0750 /var/log/gitlab
# create gitlab run directory
mkdir -p /var/run/gitlab
chown git:git /var/run/gitlab
chmod 0750 /var/run/gitlab
# link python2.[6|7] to python2
which python2 > /dev/null 2>&1 || {
which python2.6 > /dev/null 2>&1 || {
which python2.7 > /dev/null 2>&1 || {
echo "python2.6 or python2.7 are not available."
exit 1
}
ln -s /usr/bin/python2.7 /usr/bin/python2
PYTHON27="true"
}
if [ -z $PYTHON27 ]
then
ln -s /usr/bin/python2.6 /usr/bin/python2
fi
}
}
#
# Function: gitlab_shell_install
#
# Description: creates gitlab-shell directory and initialize it
#
# Parameters: $FQDN
#
gitlab_shell_install() {
# create gitlab-shell directory
cp -a /usr/share/gitlab-shell /var/lib/gitlab
chown -R git:git /var/lib/gitlab/gitlab-shell
# edit gitlab-shell configuration
sed -i "s/localhost/$1/" /etc/gitlab/gitlab-shell.yml
# initialize gitlab-shell
cd /var/lib/gitlab/gitlab-shell
sudo -u git ./bin/install
}
#
# Function: gitlab_db_initialize
#
# Description: edit the db configuration and initialize the database
#
gitlab_db_initialize() {
# determine needed database values
DB_ADAPTER=$(dialog --radiolist "Choose Database-Adapter for GitLab" 0 0 0 "mysql" "MySQL-Database Adapter" on "postgresql" "PostgreSQL-Database Adapter" off 3>&1 1>&2 2>&3 3>&-)
DB_SERVER=$(dialog --inputbox "Please enter the Database-Servername for GitLab." 0 0 "localhost" 3>&1 1>&2 2>&3 3>&-)
DB_DATABASE=$(dialog --inputbox "Please enter the Database for GitLab." 0 0 "gitlab" 3>&1 1>&2 2>&3 3>&-)
DB_USER=$(dialog --inputbox "Please enter the Database-User for GitLab." 0 0 "gitlab" 3>&1 1>&2 2>&3 3>&-)
DB_PASS=$(dialog --inputbox "Please enter the Database-Password for GitLab." 0 0 "gitlab" 3>&1 1>&2 2>&3 3>&-)
# show how to create database for gitlab
if [ "$DB_ADAPTER" = "mysql" ]
then
dialog --msgbox "
# Login to MySQL
mysql -u root -p
# Create a user for GitLab. (change gitlab to a real password)
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';
# Create the GitLab production database
mysql> CREATE DATABASE IF NOT EXISTS gitlab DEFAULT CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci';
# Grant the GitLab user necessary permissopns on the table.
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON gitlab.* TO 'gitlab'@'localhost';
# Quit the database session
mysql> \q
# Try connecting to the new database with the new user
mysql -u gitlab -p -D gitlab" 0 0
else
dialog --msgbox "
# Login to PostgreSQL
sudo -u postgres psql -d template1
# Create a user for GitLab. (change gitlab to a real password)
template1=# CREATE USER gitlab WITH PASSWORD 'gitlab';
# Create the GitLab production database & grant all privileges on database
template1=# CREATE DATABASE gitlab OWNER gitlab;
# Quit the database session
template1=# \q
# Try connecting to the new database with the new user
psql -d gitlab" 0 0
fi
# place values into databse configuration
sed -i "s/host: localhost/host: $DB_SERVER/g" /etc/gitlab/database.yml.$DB_ADAPTER
sed -i "s/database: gitlab/database: $DB_DATABASE/g" /etc/gitlab/database.yml.$DB_ADAPTER
sed -i "s/username: gitlab/username: $DB_USER/g" /etc/gitlab/database.yml.$DB_ADAPTER
sed -i "s/password: 'gitlab'/password: '$DB_PASS'/g" /etc/gitlab/database.yml.$DB_ADAPTER
# link database configuration in gitlab base directory
cd /usr/share/gitlab/config
ln -s /etc/gitlab/database.yml.$DB_ADAPTER database.yml
# initialize the database structure
cd /usr/share/gitlab
sudo -u git -H bundle exec rake db:setup RAILS_ENV=production
sudo -u git -H bundle exec rake db:seed_fu RAILS_ENV=production
}
#
# Function: gitlab_app_initialize
#
# Description: edit gitlab configuration and initialize the gitlab application
#
# Parameters: $FQDN
#
gitlab_app_initialize() {
# place values into gitlab configuration
sed -i "s/host: localhost/host: $1/" /etc/gitlab/gitlab.yml
sed -i "s/email_from: gitlab@localhost/email_from: gitlab@$1/" /etc/gitlab/gitlab.yml
sed -i "s/support_email: support@localhost/support_email: support@$1/" /etc/gitlab/gitlab.yml
# initialize the gitlab application
cd /usr/share/gitlab
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
}
#
# Description: running configuration tasks
#
case "$1" in
configure)
if [ -z "$2" ]
then
# determine the FQDN of the gitlab server
FQDN=$(dialog --inputbox "Please enter FQDN of GitLab-Server." 0 0 "$(hostname -f)" 3>&1 1>&2 2>&3 3>&-)
# running the tasks
add_user
create_dirs
gitlab_shell_install $FQDN
gitlab_db_initialize
gitlab_app_initialize $FQDN
else
# in case of upgrade. but at the moment no upgrade needed.
echo "Upgrading not implementated yet"
fi
;;
esac
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.
#DEBHELPER#
exit 0