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



45
46
47
48
# File 'lib/herkko/runner.rb', line 45

def changelog
  fetch_currently_deployed_version
  puts git_changelog
end

#consoleObject



92
93
94
# File 'lib/herkko/runner.rb', line 92

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

#deployObject



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
85
86
87
88
89
90
# 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_modeObject



127
128
129
# File 'lib/herkko/runner.rb', line 127

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

#enable_maintenance_modeObject



123
124
125
# File 'lib/herkko/runner.rb', line 123

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

#forced?Boolean

Returns:

  • (Boolean)


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

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

#migrateObject



100
101
102
103
104
105
106
# 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


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

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


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

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

#push_new_codeObject



112
113
114
115
116
117
118
119
120
121
# 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

#seedObject



96
97
98
# File 'lib/herkko/runner.rb', line 96

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