Class: Herkko::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/herkko/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



6
7
8
# File 'lib/herkko/runner.rb', line 6

def arguments
  @arguments
end

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/herkko/runner.rb', line 6

def command
  @command
end

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/herkko/runner.rb', line 6

def environment
  @environment
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

#changelogObject



42
43
44
45
# File 'lib/herkko/runner.rb', line 42

def changelog
  fetch_currently_deployed_version
  puts git_changelog
end

#consoleObject



86
87
88
# File 'lib/herkko/runner.rb', line 86

def console
  Herkko.run_with_output "heroku run rails console -r #{environment}"
end

#deployObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/herkko/runner.rb', line 47

def deploy
  Herkko.info "Doing #{forced? ? "forced(!) " : ""}deployment to #{environment}..."
  fetch_currently_deployed_version

  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_modeObject



117
118
119
# File 'lib/herkko/runner.rb', line 117

def disable_maintenance_mode
  Herkko.run_with_output "heroku", "maintenance:off", "-r", environment
end

#enable_maintenance_modeObject



113
114
115
# File 'lib/herkko/runner.rb', line 113

def enable_maintenance_mode
  Herkko.run_with_output "heroku", "maintenance:on", "-r", environment
end

#forced?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/herkko/runner.rb', line 102

def forced?
  arguments && arguments.include?("--force")
end

#migrateObject



94
95
96
97
98
99
100
# File 'lib/herkko/runner.rb', line 94

def migrate
  Herkko.info "Migrating database..."
  Herkko.run_with_output %{
    heroku run rake db:migrate -r #{environment} &&
    heroku restart -r #{environment}
  }
end


38
39
40
# File 'lib/herkko/runner.rb', line 38

def print_usage
  Herkko.puts File.read(File.join(File.dirname(__FILE__), "..", "..", "usage.txt"))
end


34
35
36
# File 'lib/herkko/runner.rb', line 34

def print_version
  Herkko.puts "Herkko #{Herkko::VERSION}"
end

#push_new_codeObject



106
107
108
109
110
111
# File 'lib/herkko/runner.rb', line 106

def push_new_code
  Herkko.info "Pushing commit #{to_be_deployed_sha} from branch #{current_branch} to Heroku..."
  puts
  Herkko.run_with_output("git", "push", environment, "#{to_be_deployed_sha}:master", forced? ? "--force" : nil)
  puts
end

#seedObject



90
91
92
# File 'lib/herkko/runner.rb', line 90

def seed
  Herkko.run_with_output "heroku run rake db:seed -r #{environment}"
end