From e443a3addb5c0a1e4d5316cdd41fac052e9eac73 Mon Sep 17 00:00:00 2001 From: Adam Cooke Date: Thu, 1 Jun 2017 14:07:25 +0100 Subject: [PATCH] improvements to the auto upgrade script * change how the channel is passed * restart rather than stop/start when upgrading * add safe mode for forcing a stop/start on upgrade --- bin/postal | 6 +++--- script/auto_upgrade.rb | 44 +++++++++++++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/bin/postal b/bin/postal index 5fff7e3..039f7c7 100755 --- a/bin/postal +++ b/bin/postal @@ -16,11 +16,11 @@ fi if [ $SHOULD_SUDO == "yes" ] && [ $USER != "postal" ]; then run() { HOME=/opt/postal - eval "sudo -u postal $1" + eval "sudo -u postal $@" } else run() { - eval $1 + eval $@ } fi @@ -87,7 +87,7 @@ case "$1" in ;; auto-upgrade) - run "ruby script/auto_upgrade.rb $2" + run "ruby script/auto_upgrade.rb $@" ;; make-user) diff --git a/script/auto_upgrade.rb b/script/auto_upgrade.rb index fc26ef2..c8e9296 100644 --- a/script/auto_upgrade.rb +++ b/script/auto_upgrade.rb @@ -1,19 +1,38 @@ #!/usr/bin/env ruby # This script will attempt to upgrade a Postal installation automatically. -# It is always recommended to upgrade Postal manaually and this script should only -# be used for development or for pro-users. # # It can be run as any user that has access to /opt/postal and that can run # commands as postal. -CHANNEL = ARGV[0] || "stable" +channel = 'stable' +safe_mode = false -unless ['beta', 'stable'].include?(CHANNEL) +begin + require 'optparse' + OptionParser.new do |opts| + opts.banner = "Usage: postal auto-upgrade [options]" + + opts.on("-c", "--channel CHANNEL", "The channel to pull the latest version from") do |v| + channel = v + end + + opts.on("--safe", "Stop postal before running the upgrade") do |v| + safe_mode = true + end + end.parse! +rescue OptionParser::InvalidOption => e + puts e.message + exit 1 +end + +unless ['beta', 'stable'].include?(channel) puts "Channel must be either 'stable' or 'beta'" exit 1 end +puts "Upgrading from the \e[32m#{channel}\e[0m channel" + def run(command, options = {}) if system(command) # Good. @@ -23,8 +42,10 @@ def run(command, options = {}) end end -puts "Stopping current Postal instance" -run "postal stop", :exit_on_failure => false +if safe_mode + puts "Stopping current Postal instance" + run "postal stop", :exit_on_failure => false +end if File.exist?("/opt/postal/app/.git") puts "Getting latest version of repository" @@ -34,7 +55,7 @@ else run "rm -Rf /opt/postal/app.backup" run "cp -R /opt/postal/app /opt/postal/app.backup" puts "Downloading latest version of application" - run "wget https://postal.atech.media/packages/#{CHANNEL}/latest.tgz -O - | tar zxpv -C /opt/postal/app" + run "wget https://postal.atech.media/packages/#{channel}/latest.tgz -O - | tar zxpv -C /opt/postal/app" end puts "Installing dependencies" @@ -43,7 +64,12 @@ run "postal bundle /opt/postal/vendor/bundle" puts "Upgrading database & assets" run "postal upgrade" -puts "Starting Postal" -run "postal start" +if safe_mode + puts "Starting Postal" + run "postal start" +else + puts "Restarting Postal" + run "postal restart" +end puts "\e[32mUpgrade complete\e[0m"