forked from benjaminoakes/roboconf
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathroboconf.sh
More file actions
301 lines (254 loc) · 7.67 KB
/
roboconf.sh
File metadata and controls
301 lines (254 loc) · 7.67 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env bash
echo "Begin loading roboconf functions..."
function term-width() {
if [ -t 1 ]; then
tput cols 2>/dev/null
elif [ -n "$COLUMNS" ]; then
echo $COLUMNS
else
echo 80
fi
}
function banner() {
# Example use: banner "Flibberty-gibbiting..."
tput cols 1>/dev/null 2>/dev/null # ensure the shell is in a state to use tput
echo "" # blank line before banner
local text="$1"
local bg=${2:-2}
local fg=${3:-0}
local term_width=$(term-width)
local target_width
target_width=$(( term_width < 80 ? term_width : 80 ))
if [ "$ROB_CONF_DEBUG" == "true" ]; then
echo "Debug: term_width=$term_width, target_width=$target_width, text_length=${#text}"
fi
# Set colors
tput setab "$bg"
tput setaf "$fg"
# Split text into words and build lines
local words=($text)
local line=""
local lines=()
for word in "${words[@]}"; do
if [[ ${#line} -eq 0 ]]; then
line="$word"
elif (( ${#line} + 1 + ${#word} <= target_width )); then
line="$line $word"
else
lines+=("$line")
line="$word"
fi
done
[[ -n "$line" ]] && lines+=("$line")
for l in "${lines[@]}"; do
local pad=$(( (target_width - ${#l}) / 2 ))
printf "%*s%s%*s" $pad "" "$l" $((target_width - pad - ${#l})) ""
tput sgr0
tput el
echo
tput setab "$bg"
tput setaf "$fg"
done
tput sgr0
tput el
}
function roboconf-check {
echo -n "checking for $1... "
hash "$1" 2>&- || {
echo 'no'
exit 1
}
echo 'yes'
}
# Since the developer may not want to merge the latest SHAs during his development,
# this function only checks out what the current parent project "thinks" are the
# current submodule SHAs.
function check_out_current_project_shas {
banner "Checking out current project SHAs..."
roboconf-check git
git submodule init
git submodule sync
git submodule update
}
# Only used for Ruby 1.x environments
function roboconf-bundler1 {
roboconf-check ruby
roboconf-check gem
gem install bundler -v 1.17.3 --no-rdoc --no-ri
# shellcheck disable=SC2086
roboconf-bundler-install $*
}
function roboconf-bundler {
roboconf-bundler-prepare
# shellcheck disable=SC2086
roboconf-bundler-install $*
}
function roboconf-bundler-dev {
banner "Bundling dev environment..."
roboconf-bundler-prepare
roboconf-bundler-without
# shellcheck disable=SC2086
roboconf-bundler-install $*
}
function roboconf-bundler-prepare {
roboconf-check ruby
roboconf-check gem
gem install bundler --no-document
}
function roboconf-bundler-install {
# shellcheck disable=SC2086
bundle install $*
}
function roboconf-bundler-without {
bundle config set --local without 'production staging'
}
function roboconf-npm {
banner "Installing node/npm deps..."
roboconf-check node
roboconf-check npm
npm install
}
function roboconf-rails-activerecord {
banner "Migrating..."
bundle exec rake db:create
bundle exec rake db:migrate
}
function roboconf-padrino-activerecord {
banner "Migrating..."
bundle exec padrino rake ar:create
bundle exec padrino rake ar:create -e test
bundle exec padrino rake ar:migrate
bundle exec padrino rake ar:migrate -e test
bundle exec padrino rake seed
}
function roboconf-rails-schema-updates {
banner "Migrating..."
bin/rails db:migrate db:test:prepare
}
function roboconf-passenger {
banner "Restarting server..."
mkdir -p tmp
touch tmp/restart.txt
}
function echo_cmd {
echo "\$ $*"
$*
}
function fire_up_heroku_app {
curl "http://${app}.herokuapp.com/" &> /dev/null
}
# Loads $HEROKU_CONSTANTS or exits
function load_heroku_constants {
if [ -z "$HEROKU_CONSTANTS" ]; then
echo "Error: \$HEROKU_CONSTANTS is undefined"
echo "\$HEROKU_CONSTANTS should reference a file holding key=value pairs of Heroku configuration settings"
exit 1
elif [[ -f $HEROKU_CONSTANTS ]]; then
echo "Sourcing $HEROKU_CONSTANTS"
source $HEROKU_CONSTANTS
else
echo "Error: Failed to find $HEROKU_CONSTANTS"
exit 1
fi
}
# "Private" function called by run_heroku_config_if_settings_changed.
# Sets the heroku_vars_changed variable to true/false depending on whether
# the Heroku environment variables in $HEROKU_CONSTANTS differ from what
# is currently configured for the Heroku app.
function detect_heroku_vars_changed {
echo "Starting detect_heroku_vars_changed"
# current app settings on Heroku
current_configs=$(heroku config --app "$app")
# desired new Heroku settings
load_heroku_constants
# test whether desired Heroku settings equal current Heroku settings
heroku_vars_changed=false
while read line
do
new_key="$( cut -d '=' -f 1 <<< $line )"
new_value="$( cut -d '=' -f 2- <<< $line )"
new_value=`sed -E -e "s/(^'|'$)//g" <<< $new_value` # strip leading/trailing 's
new_value=`sed -E -e "s/(^\"|\"$)//g" <<< $new_value` # strip leading/trailing "s
if [[ $new_key =~ 'HEROKU_CONFIG_ADD_CONSTANTS' # ignore this script-only variable; it is not a Heroku config setting
|| $new_key == \#* # ignore lines that start with '#' (comments)
|| $new_key == '' # ignore blank lines
]]; then
continue
fi
if [[ "$current_configs" == *"$new_key"* && "$current_configs" == *"$new_value"* ]]; then
echo "Key '$new_key' already has value '$new_value'"
else
heroku_vars_changed=true
echo "Key '$new_key' will be set to value '$new_value'"
fi
done < $HEROKU_CONSTANTS
}
# runs 'heroku config:set' only if the Heroku configuration settings have changed
function run_heroku_config_if_settings_changed {
echo "Starting run_heroku_config_if_settings_changed..."
detect_heroku_vars_changed
if ! $heroku_vars_changed; then
echo "Skipping 'heroku config:add' because Heroku variables unchanged"
else
echo "Running 'heroku config:add' because Heroku variables have changed"
heroku config:set $HEROKU_CONFIG_ADD_CONSTANTS --app "$app"
fi
}
function show_prep_release_usage_and_exit {
cat <<EOF
Usage: $0 heroku-app-name
Function: Release app to heroku-app-name.herokuapp.com as staging/testing environment before a release.
Example: $0 hedgeye-cedar-smarketing
Example: $0 hedgeye-smailroom
EOF
exit 1
}
function show_release_usage_and_exit {
cat <<EOF
Usage: $0 heroku-app-name
Function: Release app to heroku-app-name.herokuapp.com as production environment.
Example: $0 hedgeye-cedar-marketing
Example: $0 hedgeye-reader
EOF
exit 1
}
function show_config_usage_and_exit {
cat <<EOF
Usage: $0 heroku-app-name
Function: Update Heroku configuration.
Example: $0 hedgeye-labs
EOF
exit 1
}
function heroku_addon {
name="$1"
match=$(ruby -e "puts %x(heroku addons --app "$app").match(/$name/).to_s")
if [ '' == "$match" ]; then
echo "Installing '$name' because it's not yet installed"
echo_cmd heroku addons:add $name --app "$app"
else
echo "Not installing '$name' because it's already installed"
fi
}
# Since database migration is performed in CMS, the db/schema.rb file
# for non-CMS applications can get out of sync. The ./configure file
# in such projects should invoke this method to make sure that the
# db/schema.rb file is updated periodically.
#
# The 'cruise' check is to allow Jenkins to not do this, since
# non-CMS applications _do_ have their own databases and therefore
# handle db/schema.rb differently.
function dump_schema {
if ! [[ "$TEST_ENVIRONMENT" == 'cruise' ]]; then
bundle exec rake db:schema:dump
fi
}
# The following two routines are needed by a script/release script:
function get_current_git_branch_name {
git rev-parse --abbrev-ref HEAD
}
function set_current_git_branch_name {
current_git_branch_name=$(get_current_git_branch_name)
echo "The current branch is '$current_git_branch_name'"
}
echo "Finished loading roboconf functions"