-
Notifications
You must be signed in to change notification settings - Fork 0
/
processes
254 lines (215 loc) · 8.89 KB
/
processes
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Include common processes
source $(_decompose-project-root)/.decompose/environment/lib/web/processes
source $(_decompose-project-root)/.decompose/environment/lib/common/processes
source $(_decompose-project-root)/.decompose/environment/lib/backup/processes
source $(_decompose-project-root)/.decompose/environment/lib/docker/processes
# Declare processes
DECOMPOSE_PROCESSES=( 'build' 'up' 'explore_db' 'explore_passenger' 'initialize_redmine'
'remove_generated_files' 'update_db' 'bundle_install' 'cron' 'backup-site-data' 'export_db' 'import_db'
"${DECOMPOSE_WEB_PROCESSES[@]}" "${DECOMPOSE_COMMON_PROCESSES[@]}"
"${DECOMPOSE_BACKUP_PROCESSES[@]}" "${DECOMPOSE_DOCKER_PROCESSES[@]}" )
# The purpose of this process is to start Redmine for the first time which
# requires several configurations. This script handles the following:
# - Prompting for a database password
# - Creating and configuring the database.yml file
# - Building and running docker containers
# - Creating Redmine user with proper permissions
# - Creating Redmine database and add default data
#
# For documentation on configuring Redmine:
# http://www.redmine.org/projects/redmine/wiki/redmineinstall
_decompose-process-initialize_redmine() {
local project_root="$(_decompose-project-root)"
local redmine_dir="$project_root/redmine"
echo "Starting Initilize Redmine environment script..."
echo "Building and then running containers."
decompose build
decompose up
# Get mariadb container information
mariadb_container_id=$(docker-compose ps -q mariadb)
echo "Allow MySQL to start up (wait 20 seconds)"
for i in {1..20}; do printf "$((21-i)) "; sleep 1; done; echo ""
echo "Creating Redmine user"
docker exec $mariadb_container_id mysql -u root -p$PROJECT_DB_ROOT_PASSWORD -e 'CREATE USER '"'"$PROJECT_DB_USER"'"'@'"'"'172.%'"'"' IDENTIFIED BY '"'"$PROJECT_DB_PASSWORD"'"''
docker exec $mariadb_container_id mysql -u root -p$PROJECT_DB_ROOT_PASSWORD -e 'GRANT ALL PRIVILEGES ON redmine_production.* TO '"'"$PROJECT_DB_USER"'"'@'"'"'172.%'"'"''
docker exec $mariadb_container_id mysql -u root -p$PROJECT_DB_ROOT_PASSWORD -e 'GRANT ALL PRIVILEGES ON redmine_test.* TO '"'"$PROJECT_DB_USER"'"'@'"'"'172.%'"'"''
# Get passenger container information
passenger_container_id=$(docker-compose ps -q passenger)
# Copy database.yml again even though Dockerfile does because in development
# it might not be in the source
echo "Copy 'database.yml' into passenger container"
docker cp containers/passenger/database.yml $passenger_container_id:/home/app/redmine/config
echo "Copy 'secrets.yml' into passenger container"
docker cp containers/passenger/secrets.yml $passenger_container_id:/home/app/redmine/config
echo "Building Gemfile.lock"
docker exec $passenger_container_id bash -c 'cd /home/app/redmine; bundle install --without development test'
echo "Creating Redmine database"
docker exec $passenger_container_id bash -c 'cd /home/app/redmine; (export RAILS_ENV=production && rake db:create)'
echo "Migrating database"
docker exec $passenger_container_id bash -c 'cd /home/app/redmine; (export RAILS_ENV=production && rake db:migrate)'
echo "Loading default data"
docker exec -it $passenger_container_id bash -c "cd /home/app/redmine; (export RAILS_ENV=production && \
export REDMINE_LANG=$PROJECT_REDMINE_LANG && \
rake redmine:load_default_data)"
# Create file directories
docker exec -it $passenger_container_id bash -c 'cd /home/app/redmine && \
mkdir -p tmp/cache tmp/pdf tmp/test tmp/thumbnails && \
chmod 777 -R files log tmp public/plugin_assets'
# Generate secret if needed
if [ ! -f "$project_root/.decompose/secret.txt" ]; then
docker exec -it $passenger_container_id bash -c 'cd /home/app/redmine && \
RAILS_ENV=production rake secret > /tmp/secret.txt'
docker cp $passenger_container_id:/tmp/secret.txt "$project_root/.decompose/secret.txt"
decompose build
fi
# Restart passenger
decompose up
}
_decompose-process-initialize_redmine_help() {
echo " Start Redmine for the first time and initialize it"
}
_decompose-process-remove_generated_files() {
local redmine_dir="$(_decompose-project-root)/redmine"
# Delete files generated from initilization process
rm "$redmine_dir/Gemfile.lock"
rm "$redmine_dir/config/initializers/secret_token.rb"
rm "$redmine_dir/config/database.yml"
mv "$redmine_dir/tmp/cache" /tmp/cache-$(uuidgen)
}
_decompose-process-remove_generated_files_help() {
echo " Delete files generated from initilization process"
}
_decompose-process-explore_db() {
# Get the name of the database container
local cid=$(docker-compose ps -q mariadb)
# Enter mysql
local password_param=""
if [ "$PROJECT_DB_PASSWORD" ]; then
local password_param="-p$PROJECT_DB_PASSWORD"
fi
docker exec -it $cid bash -c "mysql -u $PROJECT_DB_USER $password_param -D $PROJECT_DB_DATABASE"
}
_decompose-process-explore_db_help() {
echo " Explore database"
}
_decompose-process-explore_passenger() {
local cid=$(docker-compose ps -q passenger)
docker exec -it $cid bash
}
_decompose-process-explore_passenger_help() {
echo " Explore passenger"
}
_decompose-process-build() {
_decompose-process-common-build
_decompose-process-build_version_file
_decompose-process_nginx_proxy_build
_decompose-process-docker-build
}
_decompose-process-build_help() {
echo " Build the project"
}
_decompose-process-up() {
_decompose-process-docker-up
_decompose-process_nginx_proxy_up
}
_decompose-process-up_help() {
echo " Start project locally"
}
_decompose-process-update_db() {
local cid=$(docker-compose ps -q passenger)
docker exec -it $cid bash -c "cd /home/app/redmine && \
bundle exec rake db:migrate RAILS_ENV=$PROJECT_ENVIRONMENT && \
bundle exec rake redmine:plugins:migrate RAILS_ENV=$PROJECT_ENVIRONMENT"
}
_decompose-process-update_db_help() {
echo " Run database update as define here:"
echo " http://www.redmine.org/projects/redmine/wiki/RedmineUpgrade"
}
_decompose-process-bundle_install() {
local cid=$(docker-compose ps -q passenger)
docker exec -it $cid bash -c "cd /home/app/redmine && \
bundle install --without development test"
}
_decompose-process-bundle_install_help() {
echo " Run bundle install. Useful for regenerating Gemfile.lock in development."
}
_decompose-process-backup-site-data() {
# Get the name of the database container
local cid=$(docker-compose ps -q backup)
# Run backup
docker exec $cid duply site_data backup
}
_decompose-process-backup-db_help() {
echo " Manually start backup process"
}
_decompose-process-cron() {
echo "Rebuilding the project ..."
decompose build
return_code=$((return_code + $?))
if [ $return_code -ne 0 ]; then
echo "Return code of '$return_code' detected. Stopping Cron process."
return $return_code
fi
echo "Restarting services ..."
decompose up
return_code=$((return_code + $?))
if [ $return_code -ne 0 ]; then
echo "Return code of '$return_code' detected. Stopping Cron process."
return $return_code
fi
echo "Running update database scripts ..."
decompose update_db
return_code=$((return_code + $?))
echo "Removing dangling Docker images ..."
decompose remove-dangling-docker-images
return_code=$((return_code + $?))
echo "Backing up config ..."
decompose backup_config
return_code=$((return_code + $?))
return $return_code
}
_decompose-process-cron_help() {
echo " Run cron tasks"
}
_decompose-process-export_db() {
# Verify the the first parameter is a file
local filename="$1"
if [ -z "$filename" ]; then
echo "Please specify name of export"
exit 1
fi
# Get the name of the database container
local cid=$(docker-compose ps -q mariadb)
# Export file
#docker exec $cid bash -c "drush sql-dump --gzip > /tmp/${filename##*/}.sql.gz"
docker exec $cid bash -c "mysqldump -u$PROJECT_DB_USER -p$PROJECT_DB_PASSWORD $PROJECT_DB_DATABASE | gzip > /tmp/${filename##*/}.sql.gz"
# Copy file to container /tmp
docker cp $cid:/tmp/${filename##*/}.sql.gz $filename.sql.gz
# Delete /tmp file
docker exec $cid rm /tmp/${filename##*/}.sql.gz
}
_decompose-process-export_db_help() {
echo " Export DB to the specified filename"
echo " Export will append '.sql.gz' to the argument provided."
}
_decompose-process-import_db() {
# Verify the the first parameter is a file
local sql_file=$1
if [ ! -e "$sql_file" ]; then
echo "File '$sql_file' does not exist"
exit 1
fi
# Get the name of the database container
local cid=$(docker-compose ps -q mariadb)
# Copy file to container /tmp
docker cp $sql_file $cid:/tmp
# Import file
docker exec $cid bash -c "gzip -d < /tmp/${sql_file##*/} | mysql -u$PROJECT_DB_USER -p$PROJECT_DB_PASSWORD $PROJECT_DB_DATABASE"
# Delete /tmp file
docker exec $cid rm /tmp/${sql_file##*/}
}
_decompose-process-import_db_help() {
echo " Import DB passed in as a parameter 1"
echo " Warning: This will drop current database."
}
# vim:syntax=sh tabstop=2 shiftwidth=2 expandtab