-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_redmine
executable file
·231 lines (187 loc) · 8.18 KB
/
install_redmine
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/bin/sh
# script to setup a new redmine installation based on instructions from
# http://www.redmine.org/projects/redmine/wiki/RedmineInstall
# Works for Redmine v2.3.0+.
# Redmine v2.x before v2.3 seems to have a hard dependency on the PostgreSQL gem (pg),
# which fails to build if PostgreSQL is not installed.
# DEPENDS: git, ruby, mysql|mariadb
VERSION=0.3.1
# internal functions
# cribbed from http://stackoverflow.com/a/28393320/3366962
# Read secret string
read_secret() {
# Disable echo.
stty -echo
# Set up trap to ensure echo is enabled before exiting if the script
# is terminated while echo is disabled.
trap 'stty echo' EXIT
# Read secret.
read "$@"
# Enable echo.
stty echo
trap - EXIT
# Print a newline because the newline entered by the user after
# entering the passcode is not echoed. This ensures that the
# next line of output begins at a new line.
echo
}
print_bold() {
printf "\n${T_BOLD}%s${T_NORMAL}\n" "$@"
}
# Allow variables to be specified on calling line
# Which Redmine to get
[ -z "$redmine_git" ] && redmine_git='https://github.com/redmine/redmine.git'
# Redmine version (Tag) to checkout.
#redmine_version='3.'
# Where to install Redmine
[ -z "$install_parent_dir" ] && install_parent_dir="$HOME/src"
[ -z "$install_dir" ] && install_dir='redmine'
if [ -n "$redmine_version" ]; then
redmine_branch=$(printf '%s' "$redmine_version" | tr -d '.')
[ -z "$dont_append_branch_to_install_dir" ] && install_dir="${install_dir}_$redmine_branch"
fi
# Redmine system user on the server
[ -z "$install_user" ] && install_user="$USER"
[ -z "$install_group" ] && install_group="$(id -gn "$install_user")"
# Redmine database
[ -z "$db_name" ] && db_name="$install_dir"
[ -z "$db_user" ] && db_user="$db_name"
printf "Enter database password for user '$db_user':"
read_secret db_pswd
# Gems groups to install
[ -z "$bundle_without" ] && bundle_without='--without development test'
[ "$bundle_without" = 'none' ] && bundle_without=''
# 'bundle exec rake' env variables
# export required as bundle apparently uses a subshell
[ -z "$RAILS_ENV" ] && export RAILS_ENV='production'
[ -z "$REDMINE_LANG" ] && export REDMINE_LANG='en'
# internal variables
# used by print_bold
T_NORMAL='\033[0m'
T_BOLD='\033[1m'
# used by 'gem'
[ -n "$http_proxy" ] && HTTP_PROXY="$http_proxy"
[ -n "$http_proxy_user" ] && HTTP_PROXY_USER="$http_proxy_user"
[ -n "$http_proxy_pass" ] && HTTP_PROXY_PASS="$http_proxy_pass"
# init log
log_file="/tmp/$(printf '%s' "$0-$install_parent_dir-$install_dir" | tr '/' '-' | tr ' ' '-').log"
printf "${T_BOLD}%s${T_NORMAL}\n" 'Installion will be logged to'
printf '%s\n' " $log_file"
printf '\n' | tee -a "$log_file"
printf '%s\n' "$(date +"%F %T"): Configuration" | tee -a "$log_file"
printf '%s\n' "redmine_git = $redmine_git" | tee -a "$log_file"
printf '%s\n' "redmine_version = ${redmine_version:-<latest>}" | tee -a "$log_file"
printf '%s\n' "install_parent_dir = $install_parent_dir" | tee -a "$log_file"
printf '%s\n' "install_dir = $install_dir" | tee -a "$log_file"
printf '%s\n' "install_user = $install_user" | tee -a "$log_file"
printf '%s\n' "install_group = $install_group" | tee -a "$log_file"
printf '%s\n' "db_name = $db_name" | tee -a "$log_file"
printf '%s\n' "db_user = $db_user" | tee -a "$log_file"
printf '%s\n' "RAILS_ENV = $RAILS_ENV" | tee -a "$log_file"
printf '%s\n' "REDMINE_LANG = $REDMINE_LANG" | tee -a "$log_file"
# ensure you are in the parent directory of the new redmine install
mkdir -p "$install_parent_dir"
if ! cd "$install_parent_dir"; then
printf '%s\n' "ERROR: Failed to enter parent directory ('$install_parent_dir')."
exit 1
fi
print_bold 'Step 1 - Redmine application'
# clone repo.
# leave git output to act as progress bar.
if git clone "$redmine_git" "$install_dir"; then
printf '%s\n' "Redmine cloned to '$install_parent_dir/$install_dir'."
else
printf '%s\n' "ERROR: Failed to clone Redmine from '$redmine_git' to '$install_parent_dir/$install_dir'."
exit 1
fi
cd "$install_dir"
# checkout version
# allow for versions like '3.' (latest 3-series) and '3.0' (latest 3.0 series)
[ -n "$redmine_version" ] && redmine_version="$(git tag -l --no-column | grep "$redmine_version" | tail -n 1)"
# default to latest version
[ -z "$redmine_version" ] && redmine_version="$(git describe --tags $(git rev-list --tags --max-count=1))"
redmine_branch=$(printf '%s' "$redmine_version" | tr -d '.')
# checkout
if git checkout -b "$redmine_branch" "$redmine_version" >> "$log_file"; then
printf '%s\n' "Version '$redmine_version' checked out as branch '$redmine_branch'"
else
printf '%s\n' "ERROR: Failed to create development branch '$redmine_branch'."
exit 1
fi
print_bold 'Step 2 - Create an empty database and accompanying user'
# create database and user (if user doesn't exist)
printf "%s\n" "We'll try creating the database with ${db_user}.
If that fails, we will retry with 'root'.
So, if you are prompted, please enter the database for 'root'."
if mysql -u $db_user -p"$db_pswd" -e "
DROP DATABASE IF EXISTS $db_name;
CREATE DATABASE $db_name DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
"
then
printf '%s\n' "Database '$db_name' created by user '$db_user'."
elif mysql -u root -p -e "
CHARSET utf8;
SET NAMES utf8 COLLATE utf8_unicode_ci;
SET character_set_server = utf8;
SET collation_server = utf8_unicode_ci;
DROP DATABASE IF EXISTS $db_name;
CREATE DATABASE $db_name DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;
GRANT ALL PRIVILEGES ON $db_name.* TO '$db_user'@'localhost' IDENTIFIED BY '$db_pswd';
"
then
printf '%s\n' "Database '$db_name' created and user '$db_user' given full permission."
else
printf '%s\n' "ERROR: Failed to create database '$db_name' and user '$db_user'."
exit 1
fi
print_bold 'Step 3 - Database connection configuration'
# on branch $redmine_branch
cp config/database.yml.example config/database.yml
sed -i "s|\(database:\) redmine\([a-z_]*\)$|\1 $db_name\2|" config/database.yml
sed -i "s|\(username:\) root$|\1 $db_user|" config/database.yml
sed -i "s|\(password:\) .*$|\1 $db_pswd|" config/database.yml
echo "Database configuration ('config/database.yml') created."
print_bold 'Step 4 - Dependencies installation'
# [ -n "$HTTP_PROXY" ] && PROXY="--http-proxy=$HTTP_PROXY"
#if ! gem install bundler --conservative --minimal-deps; then
if ! gem install bundler --conservative; then
echo 'ERROR: Failed to install "bundler". [install bundler]'
exit 1
fi
if ! bundle install --no-deployment $bundle_without; then
echo 'ERROR: Failed to download dependencies. [bundle install].'
exit 1
fi
print_bold 'Step 5 - Session store secret generation'
if ! bundle exec rake generate_secret_token >> "$log_file"; then
echo 'ERROR: Failed to initialize Redmine. [generate_secret_token]'
exit 1
fi
print_bold 'Step 6 - Database schema objects creation'
if ! bundle exec rake db:migrate >> "$log_file"; then
echo 'ERROR: Failed to initialize Redmine. [db:migrate]'
exit 1
fi
print_bold 'Step 7 - Database default data set'
if ! bundle exec rake redmine:load_default_data >> "$log_file"; then
echo 'ERROR: Failed to initialize Redmine. [redmine:load_default_data]'
exit 1
fi
print_bold 'Step 8 - File system permissions'
mkdir -p tmp/pdf public/plugin_assets
chown -R "$install_user":"$install_group" files log tmp public/plugin_assets
chmod -R 755 files log tmp public/plugin_assets
printf '\n%s\n' "Installation of Redmine v$redmine_version completed."
print_bold 'Step 9 - Test the installation'
# open default browser before starying server
# to allow leaving server in foreground (to allow CTRL+C break)
if which xdg-open >/dev/null 2>&1; then
xdg-open "$log_file" &
xdg-open http://localhost:3000/ &
fi
# test for new location of 'rails' script (Redmine 3.x), falling back to old location.
if [ -e bin/rails ]; then
bundle exec ruby bin/rails server webrick -e $RAILS_ENV
else
bundle exec ruby script/rails server webrick -e $RAILS_ENV
fi