class Deploy::Runner
Constants
- BLACK
- BLUE
Public Class Methods
new(spec)
click to toggle source
# File lib/deploy/runner.rb, line 14 def initialize(spec) @spec = spec end
run(spec)
click to toggle source
# File lib/deploy/runner.rb, line 10 def self.run(spec) self.new(spec).deploy end
Public Instance Methods
deploy()
click to toggle source
# File lib/deploy/runner.rb, line 18 def deploy @before = @spec["before"] @after = @spec["after"] @cleanup = @spec["cleanup"] ok = true tasks "BEFORE", @before cmd "ssh-add #{@spec[:key]} 2>/dev/null" if @spec[:key] @spec["servers"].each do |server| log "[Server #{server}]", BLUE ok = ok && justdoit(server) @commit = ssh(server, "git log -1 --pretty=%s") @author = ssh(server, "git log -1 --pretty=%an") @hash = ssh(server, "git log -1 --pretty=%h") @date = ssh(server, "git log -1 --pretty=%ad") end tasks "AFTER", @after if ok tasks "CLEANUP", @cleanup notify end
justdoit(server)
click to toggle source
# File lib/deploy/runner.rb, line 64 def justdoit(server) log "CURRENT STATUS" status = ssh(server, "git status") puts status puts # return unless status.include? "working directory clean" log "CURRENT RELEASE" puts ssh(server, "git log -1") puts # TODO Figure this out log "PULLING FROM ORIGIN" fetched = ssh(server, "git fetch #{@spec["remote"]}") puts fetched == "" ? "(none)" : fetched puts # return unless fetched.size > 0 log "REBASING" rebased = ssh(server, "git rebase #{@spec["remote"]}/#{@spec["branch"]}") puts rebased puts # return if rebased.include? "is up to date" log "NEW STATUS" status = ssh(server, "git status") puts status puts log "NEW RELEASE" puts ssh(server, "git log -1") puts true end
notify()
click to toggle source
# File lib/deploy/runner.rb, line 100 def notify return unless @spec["notifications"] return unless @spec["notifications"]["slack"] site = @spec["site"] service = @spec["notifications"]["slack"]["service"] channel = @spec["notifications"]["slack"]["channel"] url = "https://hooks.slack.com/services/#{service}" payload = { "channel" => channel, "username" => "Deployment #{DateTime.now}", "text" => "New deployment to <#{site}>", "icon_emoji" => ":dotser:", "attachments" => [ { # "fallback" => "[fallback] Required text summary of the attachment that is shown by clients that understand attachments but choose not to show them.", # "pretext" => "[pretext] Optional text that should appear above the formatted data", # "text" => "[text] Optional text that should appear within the attachment", "_color" => "warning", "fields" => [ { "title" => "Commit", "value" => @commit, "short" => true, }, { "title" => "Date", "value" => @date, "short" => true, }, { "title" => "Author", "value" => @author, "short" => true, }, { "title" => "Hash", "value" => @hash, "short" => true, }, { "title" => "Site", "value" => site, "short" => true, }, { "title" => "Branch", "value" => @spec["branch"], "short" => true, }, ] } ] }.to_json `curl -s -X POST --data-urlencode 'payload=#{payload}' #{url}` end
task(cmd)
click to toggle source
# File lib/deploy/runner.rb, line 58 def task(cmd) puts "$ #{cmd}" system(cmd) puts end
tasks(type, tasks)
click to toggle source
# File lib/deploy/runner.rb, line 46 def tasks(type, tasks) return unless tasks log "#{type} ACTIONS" if tasks.kind_of? Array tasks.each do |t| task t end else task tasks end end
Private Instance Methods
cmd(cmd)
click to toggle source
# File lib/deploy/runner.rb, line 172 def cmd(cmd) output = `#{cmd}`.chomp code = $?.exitstatus abort "Command <#{full}> failed with status #{code}." if code != 0 output end
log(message, color = BLACK)
click to toggle source
# File lib/deploy/runner.rb, line 179 def log(message, color = BLACK) puts "\033[#{color}m#{message}\033[0m" end
ssh(server, cmd)
click to toggle source
# File lib/deploy/runner.rb, line 160 def ssh(server, cmd) full = "ssh -A #{@spec['user']}@#{server}" full << " -o StrictHostKeyChecking=no" full << " -o UserKnownHostsFile=/dev/null" full << " -i #{@spec['key']}" if @spec['key'] full << " 'cd #{@spec['path']} && #{cmd}'" output = `#{full}`.chomp code = $?.exitstatus abort "Command <#{full}> failed with status #{code}." if code != 0 output end