Top Level Namespace
Defined Under Namespace
Modules: LabdbManager
Classes: ShellCommand, String
Constant Summary
collapse
- COMMAND_PREFIX =
"--> "
- CONFIRM_PREFIX =
"--? "
- BR_DEPLOY_STAGING =
'deploy_staging'
- BR_DEPLOY =
'deploy'
- BR_REMOTE =
'master'
- REM_NAME =
'origin'
- PROJECT_URL =
'https://github.com/cjfuller/labdb.git'
- DEFAULT_REPO_PATH =
'~/labdb'
- DEFAULT_BACKUP_DIR =
'~/backups'
- AUTO_MERGE_MESSAGE =
'"auto merge by manage.py"'
- PG_DUMP_PATH =
`which pg_dump`.strip
- HOSTNAME_CFG_FILE =
'config/full_hostname.txt'
- SECRET_CFG_FILE =
'config/secret_token.txt'
- COLORS =
{
green: "\033[92m",
yellow: "\033[93m",
red: "\033[91m",
off: "\033[0m"
}
- SHELL_COMMAND_LIST =
[]
Instance Method Summary
collapse
Instance Method Details
#backup ⇒ Object
249
250
251
|
# File 'lib/labdb_manager.rb', line 249
def backup
queue_command {create_backup}
end
|
#bundle_install ⇒ Object
158
159
160
|
# File 'lib/labdb_manager.rb', line 158
def bundle_install
"bundle install"
end
|
#bundle_update ⇒ Object
162
163
164
|
# File 'lib/labdb_manager.rb', line 162
def bundle_update
confirm "bundle update"
end
|
#check_for_staging_branch ⇒ Object
107
108
109
110
111
112
113
|
# File 'lib/labdb_manager.rb', line 107
def check_for_staging_branch
"git show-ref --verify --quiet refs/heads/#{BR_DEPLOY_STAGING}"
end
|
#clean_up_staging_branch ⇒ Object
115
116
117
118
|
# File 'lib/labdb_manager.rb', line 115
def clean_up_staging_branch
confirm "git checkout #{BR_DEPLOY} && git branch -d #{BR_DEPLOY_STAGING}"
end
|
#confirm(cmd) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/labdb_manager.rb', line 46
def confirm(cmd)
print (cmd + " (y/N) " + CONFIRM_PREFIX ).yellow
response = gets
unless ['yes', 'y'].include? response.downcase.strip then
exit 0
end
cmd
end
|
#create_backup ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/labdb_manager.rb', line 172
def create_backup
suffix = "_labdb_backup.dump"
backup_timestring = Time.now.strftime("%Y%m%d_%H%M%S")
fn = backup_timestring + suffix
fn_full = File.expand_path(fn, DEFAULT_BACKUP_DIR)
[PG_DUMP_PATH,
"-h localhost labdb > #{fn_full}",
"&&",
"tar cjf #{fn_full}.tar.bz2 -C {DEFAULT_BACKUP_DIR} {fn}",
"&&",
"rm -f #{fn_full}"
].join(" ")
end
|
#create_production_db ⇒ Object
186
187
188
|
# File 'lib/labdb_manager.rb', line 186
def create_production_db
"RAILS_ENV=production bundle exec rake db:setup"
end
|
#create_staging_branch ⇒ Object
120
121
122
123
|
# File 'lib/labdb_manager.rb', line 120
def create_staging_branch
"git checkout #{BR_DEPLOY} && git branch #{BR_DEPLOY_STAGING}"
end
|
#devserver ⇒ Object
277
278
279
|
# File 'lib/labdb_manager.rb', line 277
def devserver
queue_command {run_devserver}
end
|
#fetch_remote_changes ⇒ Object
125
126
127
128
129
130
131
|
# File 'lib/labdb_manager.rb', line 125
def fetch_remote_changes
"git checkout #{BR_REMOTE} && git pull #{REM_NAME} #{BR_REMOTE}"
end
|
#force_update_deps ⇒ Object
253
254
255
|
# File 'lib/labdb_manager.rb', line 253
def force_update_deps
queue_command {bundle_update}
end
|
#generate_application_secret ⇒ Object
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/labdb_manager.rb', line 215
def generate_application_secret
secret = SecureRandom.hex(64)
puts (COMMAND_PREFIX +
"Writing application secret to #{SECRET_CFG_FILE}").green
File.open(SECRET_CFG_FILE, 'w') do |f|
f.write secret
end
File.chmod(0600, SECRET_CFG_FILE)
ok
end
|
#help ⇒ Object
281
282
|
# File 'lib/labdb_manager.rb', line 281
def help
end
|
#hostname(hostname: nil) ⇒ Object
261
262
263
|
# File 'lib/labdb_manager.rb', line 261
def hostname(hostname: nil)
set_hostname(hostname: hostname)
end
|
#install ⇒ Object
265
266
267
|
# File 'lib/labdb_manager.rb', line 265
def install
queue_command {create_production_db}
end
|
#merge_into_production ⇒ Object
145
146
147
148
149
|
# File 'lib/labdb_manager.rb', line 145
def merge_into_production
("git checkout #{BR_DEPLOY} && " +
"git merge -m #{AUTO_MERGE_MESSAGE} #{BR_DEPLOY_STAGING}")
end
|
#ok ⇒ Object
228
229
230
|
# File 'lib/labdb_manager.rb', line 228
def ok
puts "OK".green
end
|
#precompile_assets ⇒ Object
166
167
168
|
# File 'lib/labdb_manager.rb', line 166
def precompile_assets
"bundle exec rake assets:precompile"
end
|
#print_command(cmd) ⇒ Object
41
42
43
44
|
# File 'lib/labdb_manager.rb', line 41
def print_command(cmd)
puts (COMMAND_PREFIX + cmd).green
end
|
#queue_command(**kwargs, &bl) ⇒ Object
61
62
63
|
# File 'lib/labdb_manager.rb', line 61
def queue_command(**kwargs, &bl)
SHELL_COMMAND_LIST << ShellCommand.new(bl, **kwargs)
end
|
#restart ⇒ Object
273
274
275
|
# File 'lib/labdb_manager.rb', line 273
def restart
queue_command {restart_server}
end
|
#restart_server ⇒ Object
192
193
194
|
# File 'lib/labdb_manager.rb', line 192
def restart_server
"supervisorctl restart labdb"
end
|
#revert_failure ⇒ Object
269
270
271
|
# File 'lib/labdb_manager.rb', line 269
def revert_failure
queue_command {revert_merge_failure}
end
|
#revert_merge_failure ⇒ Object
151
152
153
154
|
# File 'lib/labdb_manager.rb', line 151
def revert_merge_failure
"git reset --merge && git checkout #{BR_DEPLOY}"
end
|
#run_devserver ⇒ Object
196
197
198
|
# File 'lib/labdb_manager.rb', line 196
def run_devserver
"bundle exec puma --config config/puma.rb"
end
|
#run_queued_commands(dry_run: false) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/labdb_manager.rb', line 65
def run_queued_commands(dry_run: false)
SHELL_COMMAND_LIST.each do |c|
c.call(dry_run: dry_run)
end
SHELL_COMMAND_LIST.clear
end
|
#run_task(t, args: [], dry_run: false) ⇒ Object
284
285
286
287
288
|
# File 'lib/labdb_manager.rb', line 284
def run_task(t, args: [], dry_run: false)
send t, *args
run_queued_commands dry_run: dry_run
ok
end
|
#secret ⇒ Object
257
258
259
|
# File 'lib/labdb_manager.rb', line 257
def secret
generate_application_secret
end
|
#set_hostname(hostname: nil) ⇒ Object
other commands not using the shell
202
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/labdb_manager.rb', line 202
def set_hostname(hostname: nil)
if hostname.nil?
puts ("Please enter the full hostname of the machine.\n" +
'(i.e. the part that would appear including the https:// ' +
'in a url but before any other slashes):').yellow
print CONFIRM_PREFIX.yellow
hostname = gets
File.open(HOSTNAME_CFG_FILE, 'w') do |f|
f.write hostname
end
end
end
|
#stage_changes ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/labdb_manager.rb', line 133
def stage_changes
("git checkout #{BR_DEPLOY_STAGING} && " +
"git merge -m #{AUTO_MERGE_MESSAGE} #{BR_REMOTE}")
end
|
#update ⇒ Object
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/labdb_manager.rb', line 234
def update
queue_command {create_backup}
if ShellCommand.new(
Proc.new {check_for_staging_branch}, exit_on_fail: false).call == 0 then
queue_command {clean_up_staging_branch}
end
queue_command {create_staging_branch}
queue_command {fetch_remote_changes}
queue_command {stage_changes}
queue_command {bundle_install}
queue_command {precompile_assets}
queue_command {merge_into_production}
queue_command {restart_server}
end
|