From 02cf4e5fd780e42a5cf542a67f83b960ba183979 Mon Sep 17 00:00:00 2001
From: Tyler Lemburg <trlemburg@gmail.com>
Date: Tue, 28 Jun 2016 11:03:44 -0500
Subject: [PATCH] Deploy script

---
 deploy.rb  | 45 +++++++++++++++++++++++++++++++++++++++++++++
 unicorn.rb |  3 +++
 2 files changed, 48 insertions(+)
 create mode 100644 deploy.rb
 create mode 100644 unicorn.rb

diff --git a/deploy.rb b/deploy.rb
new file mode 100644
index 0000000..b9d1303
--- /dev/null
+++ b/deploy.rb
@@ -0,0 +1,45 @@
+pid_file = '/var/www/html/ucommscheduler.unl.edu/ucommscheduler.pid'
+
+def get_exit
+  $?.exitstatus
+end
+
+pid = (File.read pid_file).to_i
+
+puts 'Starting new unicorn'
+puts "Sending USR2 to #{pid}"
+# send USR2 to master unicorn process; this will spawn a new unicorn master
+Process.kill('USR2', pid)
+
+puts 'Waiting for new unicorn to spawn'
+sleep 3
+new_pid = (File.read pid_file).to_i
+
+puts 'Gracefully stopping old workers'
+puts "Sending WINCH to #{pid}"
+# send WINCH to old master process. this gracefully stops workers
+Process.kill('WINCH', pid)
+
+# decide if things are going well. 
+puts 'Did it.'
+puts 'Check on the website. Are new things enabled and everything looking good?'
+puts '(Y) for yes, anything else for no'
+
+response = STDIN.gets
+
+if response.capitalize[0] == 'Y'
+  # if so, send QUIT to old master. It shuts down, and finished.
+  puts "Sending QUIT to old master at #{pid}"
+  Process.kill('QUIT', pid)
+  puts 'QUIT sent'
+  puts 'We should be good! Nice job.'
+  puts 'New code deployed'
+else
+  # if not, send HUP to old master to reload and restart workers.
+  # then send QUIT to new master to stop it and workers.
+  puts "Dang. Sending HUP to old master at #{pid} to restart workers."
+  Process.kill('HUP', pid)
+  puts "Sending QUIT to new master at #{new_pid} to shut that all down."
+  Process.kill('QUIT', new_pid)
+  puts 'job is done. Better luck next time!'
+end
diff --git a/unicorn.rb b/unicorn.rb
new file mode 100644
index 0000000..0305bd6
--- /dev/null
+++ b/unicorn.rb
@@ -0,0 +1,3 @@
+worker_processes 1
+working_directory '/var/www/html/ucommscheduler.unl.edu'
+pid '/var/www/html/ucommscheduler.unl.edu/ucommscheduler.pid'
-- 
GitLab