|
| 1 | +require 'json' |
| 2 | + |
| 3 | +module Wizarddev |
| 4 | + module Heroku |
| 5 | + class Deploy |
| 6 | + attr_reader :target, :heroku |
| 7 | + |
| 8 | + def self.check_auth_config |
| 9 | + unless auth = PlatformClient.load_auth |
| 10 | + puts "Please make sure you have an up to date version of the heroku toolbelt and it's logged in" |
| 11 | + end |
| 12 | + |
| 13 | + unless config = File.exist?('app.json') |
| 14 | + puts "Please include an app.json in the root of this project." |
| 15 | + puts "`rails g wizarddev:app_json` will make one for you." |
| 16 | + end |
| 17 | + |
| 18 | + exit(1) unless auth && config |
| 19 | + end |
| 20 | + |
| 21 | + def initialize(target) |
| 22 | + @target = target.to_s |
| 23 | + @app_config = load_config |
| 24 | + @heroku = PlatformClient.local_auth(app_name) |
| 25 | + end |
| 26 | + |
| 27 | + def deploy! |
| 28 | + force_push? ? force_push : push |
| 29 | + tag_deploy |
| 30 | + run_scripts |
| 31 | + end |
| 32 | + |
| 33 | + def push |
| 34 | + puts "Pushing HEAD to Heroku ..." |
| 35 | + execute "git push [email protected]:#{app_name}.git HEAD:master" |
| 36 | + end |
| 37 | + |
| 38 | + def force_push |
| 39 | + puts "Force pushing HEAD to Heroku ..." |
| 40 | + execute "git push -f [email protected]:#{app_name}.git HEAD:master" |
| 41 | + end |
| 42 | + |
| 43 | + def run_scripts |
| 44 | + scripts.each { |script| run_script(script) } |
| 45 | + end |
| 46 | + |
| 47 | + def tag_deploy |
| 48 | + return unless tag_name |
| 49 | + version = heroku.latest_release['version'] |
| 50 | + release_name = "#{tag_name}/v#{version}" |
| 51 | + puts "Tagging release with #{release_name}" |
| 52 | + execute "git tag -a #{release_name} -m 'Tagged release'" |
| 53 | + execute "git push #{source_repo} #{release_name}" |
| 54 | + end |
| 55 | + |
| 56 | + def execute_remote(cmd) |
| 57 | + print "Executing '#{cmd}' on #{app_name}\n" |
| 58 | + output, code = heroku.run_with_code(cmd) |
| 59 | + puts output.gsub(/^/, "#\t") |
| 60 | + return true if code == 0 |
| 61 | + puts "Failed to Execute #{cmd} with code #{code}" |
| 62 | + exit(1) |
| 63 | + end |
| 64 | + |
| 65 | + def execute(cmd) |
| 66 | + print "Executing '#{cmd}'\n" |
| 67 | + success = system(cmd) |
| 68 | + return true if success |
| 69 | + code = $CHILD_STATUS.to_i |
| 70 | + puts "Failed to Execute #{cmd} with code #{code}" |
| 71 | + exit(1) |
| 72 | + end |
| 73 | + |
| 74 | + private |
| 75 | + |
| 76 | + def run_script(script) |
| 77 | + if script["remote"] |
| 78 | + execute_remote script["cmd"] |
| 79 | + else |
| 80 | + execute script["cmd"] |
| 81 | + end |
| 82 | + restart if script["restart"] |
| 83 | + end |
| 84 | + |
| 85 | + def restart |
| 86 | + puts "Restarting #{app_name}" |
| 87 | + heroku.restart_all |
| 88 | + end |
| 89 | + |
| 90 | + def load_config |
| 91 | + JSON.parse(File.read('app.json')) |
| 92 | + end |
| 93 | + |
| 94 | + def source_repo |
| 95 | + @app_config["source-repo"] |
| 96 | + end |
| 97 | + |
| 98 | + def tag_name |
| 99 | + config["tag-name"] |
| 100 | + end |
| 101 | + |
| 102 | + def force_push? |
| 103 | + !!config["force-push"] |
| 104 | + end |
| 105 | + |
| 106 | + def app_name |
| 107 | + config["app-name"] |
| 108 | + end |
| 109 | + |
| 110 | + def scripts |
| 111 | + config["scripts"] |
| 112 | + end |
| 113 | + |
| 114 | + def config |
| 115 | + c = @app_config["heroku-environments"][target] |
| 116 | + raise "No configuration for #{target} in app.js" unless c |
| 117 | + c |
| 118 | + end |
| 119 | + |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments