class Herkko::Runner

Attributes

arguments[R]
command[R]
environment[R]

Public Class Methods

new(argv) click to toggle source
# File lib/herkko/runner.rb, line 8
def initialize(argv)
  argv.dup.tap do |arguments|
    @environment = arguments.shift
    @command = arguments.shift
    @arguments = arguments unless arguments.empty?
  end
end

Public Instance Methods

call() click to toggle source
# File lib/herkko/runner.rb, line 16
def call
  if ["version", "--version"].include?(environment)
    return print_version
  end

  if ["help", "--help", "usage"].include?(environment)
    return print_usage
  end

  return print_usage if environment.nil? || command.nil?

  if respond_to?(command)
    public_send(command)
  else
    Herkko.run_with_output("heroku", command, arguments, "-r#{environment}")
  end
end
changelog() click to toggle source
# File lib/herkko/runner.rb, line 45
def changelog
  fetch_currently_deployed_version
  puts git_changelog
end
console() click to toggle source
# File lib/herkko/runner.rb, line 92
def console
  Herkko.run_with_output "heroku run rails console -r #{environment}"
end
deploy() click to toggle source
# File lib/herkko/runner.rb, line 50
def deploy
  Herkko.info "Doing #{forced? ? "forced(!) " : ""}deployment to #{environment}..."
  fetch_currently_deployed_version

  Herkko.info "Pushing commit #{to_be_deployed_sha} from branch #{current_branch} to Heroku..."
  puts

  Herkko.info("Deploying changes:")

  puts
  puts git_changelog
  puts

  ci_state = if skip_ci_check?
    :skip
  else
    check_ci
  end

  if ci_state == :green
    Herkko.info "CI is green. Deploying..."

    deploy!
  elsif ci_state == :skip
    Herkko.info "Skipping CI. Deploying..."

    deploy!
  elsif ci_state == :not_used
    Herkko.info "CI not in use for this project. Deploying..."

    deploy!
  elsif ci_state == :yellow
    Herkko.info "CI is running. Wait a while."
  elsif ci_state == :queued
    Herkko.info "Build is queued in CI. Wait a while."
  elsif ci_state == :red
    Herkko.info "CI is red. Not deploying."
  else
    Herkko.info "CI is in unknown state (#{ci_state}). Can't continue."
  end
end
disable_maintenance_mode() click to toggle source
# File lib/herkko/runner.rb, line 127
def disable_maintenance_mode
  Herkko.run_with_output "heroku", "maintenance:off", "-r", environment
end
enable_maintenance_mode() click to toggle source
# File lib/herkko/runner.rb, line 123
def enable_maintenance_mode
  Herkko.run_with_output "heroku", "maintenance:on", "-r", environment
end
forced?() click to toggle source
# File lib/herkko/runner.rb, line 108
def forced?
  arguments && arguments.include?("--force")
end
migrate() click to toggle source
# File lib/herkko/runner.rb, line 100
def migrate
  Herkko.info "Migrating database..."
  Herkko.run_with_output %{
    heroku run rake db:migrate -r #{environment} &&
    heroku restart -r #{environment}
  }
end
print_usage() click to toggle source
print_version() click to toggle source
push_new_code() click to toggle source
# File lib/herkko/runner.rb, line 112
def push_new_code
  Herkko.run_with_output(
    "git",
    "push",
    environment,
    "#{to_be_deployed_sha}:master",
    forced? ? "--force" : nil
  )
  puts
end
seed() click to toggle source
# File lib/herkko/runner.rb, line 96
def seed
  Herkko.run_with_output "heroku run rake db:seed -r #{environment}"
end

Private Instance Methods

check_ci() click to toggle source
# File lib/herkko/runner.rb, line 133
def check_ci
  Herkko.info "Checking CI..."

  travis_client.status_for(current_branch)
end
current_branch() click to toggle source
# File lib/herkko/runner.rb, line 194
def current_branch
  Herkko.run("git", "rev-parse", "--abbrev-ref", "HEAD")[0].strip
end
currently_deployed_to(environment) click to toggle source
# File lib/herkko/runner.rb, line 198
def currently_deployed_to(environment)
  Herkko.run("git", "rev-parse", "#{environment}/master")[0].strip
end
deploy!() click to toggle source
# File lib/herkko/runner.rb, line 139
def deploy!
  run_migrations = migrations_needed?

  in_maintenance_mode do
    push_new_code

    if run_migrations
      if procfile_release_defined?
        Herkko.info "This release has new migrations but because project has Release Phase defined in Procfile, the migrations are not automatically run."
      else
        migrate
      end
    else
      Herkko.info "No need to migrate."
    end

    if seed_file_changed?
      Herkko.info "NOTE: Seed file seem the have changed. Make sure to run it if needed."
    end
  end

  print_after_deployment_instructions
end
fetch_currently_deployed_version() click to toggle source
# File lib/herkko/runner.rb, line 218
def fetch_currently_deployed_version
  Herkko.run_with_output("git", "fetch", environment)
end
file_changed?(file_path) click to toggle source
# File lib/herkko/runner.rb, line 206
def file_changed?(file_path)
  files = Herkko.run(
    "git",
    "diff",
    "--name-only",
    currently_deployed_to(environment),
    to_be_deployed_sha
  )[0]

  files.split("\n").any? {|filename| filename.match(Regexp.new(file_path)) }
end
git_changelog() click to toggle source
# File lib/herkko/runner.rb, line 222
def git_changelog
  Herkko.run(
    "git",
    "log",
    "--pretty=format:%C(yellow)%h %Cblue%ad%Creset %an %Cgreen%s%Creset",
    "--date=short",
    "#{currently_deployed_to(environment)}..#{to_be_deployed_sha}"
  )[0]
end
in_maintenance_mode() { || ... } click to toggle source
# File lib/herkko/runner.rb, line 163
def in_maintenance_mode
  enable_maintenance_mode if use_maintenace_mode?

  yield

  disable_maintenance_mode if use_maintenace_mode?
end
migrations_needed?() click to toggle source
# File lib/herkko/runner.rb, line 179
def migrations_needed?
  file_changed?("db/migrate")
end
print_after_deployment_instructions() click to toggle source
procfile_release_defined?() click to toggle source
# File lib/herkko/runner.rb, line 187
def procfile_release_defined?
  return false unless File.exist?("Procfile")

  procfile_contents = File.read("Procfile")
  !!procfile_contents.match(/^release:/)
end
seed_file_changed?() click to toggle source
# File lib/herkko/runner.rb, line 183
def seed_file_changed?
  file_changed?("db/seeds.rb")
end
skip_ci_check?() click to toggle source
# File lib/herkko/runner.rb, line 171
def skip_ci_check?
  arguments && arguments.include?("--skip-ci-check")
end
to_be_deployed_sha() click to toggle source
# File lib/herkko/runner.rb, line 202
def to_be_deployed_sha
  Herkko.run("git", "rev-parse", "HEAD")[0].strip
end
travis_client() click to toggle source
# File lib/herkko/runner.rb, line 246
def travis_client
  @travis_client ||= Herkko::Travis.new
end
use_maintenace_mode?() click to toggle source
# File lib/herkko/runner.rb, line 175
def use_maintenace_mode?
  arguments && arguments.include?("--maintenance-mode")
end