Class: Aerosol::Runner

Inherits:
Object
  • Object
show all
Extended by:
Dockly::Util::Delegate
Includes:
Dockly::Util::Logger::Mixin
Defined in:
lib/aerosol/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



12
13
14
# File 'lib/aerosol/runner.rb', line 12

def initialize
  @log_pids = {}
end

Instance Attribute Details

#deployObject (readonly)

Returns the value of attribute deploy.



10
11
12
# File 'lib/aerosol/runner.rb', line 10

def deploy
  @deploy
end

#log_pidsObject (readonly)

Returns the value of attribute log_pids.



10
11
12
# File 'lib/aerosol/runner.rb', line 10

def log_pids
  @log_pids
end

Instance Method Details

#check_live_with(session, command) ⇒ Object



146
147
148
149
150
151
# File 'lib/aerosol/runner.rb', line 146

def check_live_with(session, command)
  debug "running #{command}"
  ret = ssh_exec!(session, command)
  debug "finished running #{command}"
  ret[:exit_status].zero?
end

#check_site_live(session) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/aerosol/runner.rb', line 133

def check_site_live(session)
  command = [
    'wget',
    '-q',
    # Since we're hitting localhost, the cert will always be invalid, so don't try to check it.
    deploy.ssl ? '--no-check-certificate' : nil,
    "'#{deploy.live_check_url}'",
    '-O',
    '/dev/null'
  ].compact.join(' ')
  check_live_with(session, command)
end

#destroy_new_auto_scaling_groupsObject



223
224
225
226
227
228
# File 'lib/aerosol/runner.rb', line 223

def destroy_new_auto_scaling_groups
  require_deploy!
  info "destroying autoscaling groups created for this sha"
  new_auto_scaling_groups.map(&:destroy)
  info "destroyed new autoscaling groups"
end

#destroy_old_auto_scaling_groupsObject



215
216
217
218
219
220
221
# File 'lib/aerosol/runner.rb', line 215

def destroy_old_auto_scaling_groups
  require_deploy!
  info "destroying old autoscaling groups"
  sleep deploy.sleep_before_termination
  old_auto_scaling_groups.map(&:destroy)
  info "destroyed old autoscaling groups"
end

#git_shaObject



275
276
277
# File 'lib/aerosol/runner.rb', line 275

def git_sha
  @git_sha ||= Aerosol::Util.git_sha
end

#healthy?(instance) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/aerosol/runner.rb', line 95

def healthy?(instance)
  debug "Checking if #{instance.instance_id} is healthy"

  unless instance.live?
    debug "#{instance.instance_id} is not live"
    return false
  end

  debug "trying to SSH to #{instance.instance_id}"
  success = false
  ssh.with_connection(instance) do |session|
    start_tailing_logs(ssh, instance) if log_pids[instance.instance_id].nil?
    debug "checking if #{instance.instance_id} is healthy"
    success =
      case is_alive?
      when Proc
        debug 'Using custom site live check'
        is_alive?.call(session, self)
      when String
        debug "Using custom site live check: #{is_alive?}"
        check_live_with(session, is_alive?)
      else
        debug 'Using default site live check'
        check_site_live(session)
      end
  end

  if success
    debug "#{instance.instance_id} is healthy"
  else
    debug "#{instance.instance_id} is not healthy"
  end
  success
rescue => ex
  debug "#{instance.instance_id} is not healthy: #{ex.message}"
  false
end

#new_auto_scaling_groupsObject



239
240
241
# File 'lib/aerosol/runner.rb', line 239

def new_auto_scaling_groups
  select_auto_scaling_groups { |asg| asg.tags['GitSha'] == auto_scaling.tags['GitSha'] }
end

#new_instancesObject



252
253
254
255
256
257
258
259
# File 'lib/aerosol/runner.rb', line 252

def new_instances
  require_deploy!
  while launch_configuration.all_instances.length < auto_scaling.min_size
    info "Waiting for instances to come up"
    sleep 10
  end
  launch_configuration.all_instances
end

#old_auto_scaling_groupsObject



235
236
237
# File 'lib/aerosol/runner.rb', line 235

def old_auto_scaling_groups
  select_auto_scaling_groups { |asg| asg.tags['GitSha'] != auto_scaling.tags['GitSha'] }
end

#old_instancesObject



230
231
232
233
# File 'lib/aerosol/runner.rb', line 230

def old_instances
  require_deploy!
  old_auto_scaling_groups.map(&:launch_configuration).compact.map(&:all_instances).flatten.compact
end

#require_deploy!Object



271
272
273
# File 'lib/aerosol/runner.rb', line 271

def require_deploy!
  raise "@deploy must be present" if deploy.nil?
end

#run_migrationObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aerosol/runner.rb', line 16

def run_migration
  require_deploy!
  return unless deploy.migrate?
  raise 'To run a migration, $RAILS_ENV must be set.' if ENV['RAILS_ENV'].nil?

  info "running migration"
  begin
    info "loading config for env #{ENV['RAILS_ENV']}"
    original_config = YAML.load(ERB.new(File.read(db_config_path)).result)[ENV['RAILS_ENV']]
    debug "creating ssh tunnel"
    migration_ssh.with_connection do |session|
      # session.logger.sev_threshold=Logger::Severity::DEBUG
      debug "finding free port"
      port = random_open_port
      db_port = original_config['port'] || 3306 # TODO: get default port from DB driver
      host = original_config['host']
      info "forwarding 127.0.0.1:#{port} --> #{host}:#{db_port}"
      session.forward.local(port, host, db_port)
      child = fork do
        GC.disable
        with_prefix('child:') do |logger|
          logger.debug "establishing connection"
          ActiveRecord::Base.establish_connection(original_config.merge(
                                                    'host' => '127.0.0.1',
                                                    'port' => port
                                                  ))
          logger.info "running migration"
          ActiveRecord::Migrator.migrate(%w[db/migrate])
        end
      end
      debug "waiting for child"
      exitstatus = nil
      session.loop(0.1) do
        pid = Process.waitpid(child, Process::WNOHANG)
        exitstatus = $?.exitstatus if !pid.nil?
        pid.nil?
      end
      raise "migration failed: #{exitstatus}" unless exitstatus == 0
    end
    info "complete"
  ensure
    ActiveRecord::Base.clear_all_connections!
  end
  info "migration ran"
end

#select_auto_scaling_groups(&block) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/aerosol/runner.rb', line 243

def select_auto_scaling_groups(&block)
  require_deploy!
  Aerosol::LaunchConfiguration.all # load all of the launch configurations first
  Aerosol::AutoScaling.all.select { |asg|
    (asg.tags['Deploy'].to_s == auto_scaling.tags['Deploy']) &&
      (block.nil? ? true : block.call(asg))
  }
end

#ssh_fork(command, ssh, instance) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/aerosol/runner.rb', line 163

def ssh_fork(command, ssh, instance)
  debug 'starting ssh fork'
  fork do
    Signal.trap('HUP') do
      debug 'Killing tailing session'
      Process.exit!
    end
    debug 'starting tail'
    begin
      ssh.with_connection(instance) do |session|
        debug 'tailing session connected'
        buffer = ''
        ssh_exec!(session, command) do |stream, data|
          data.lines.each do |line|
            if line.end_with?($/)
              debug "[#{instance.instance_id}] #{stream}: #{buffer + line}"
              buffer = ''
            else
              buffer = line
            end
          end
        end
      end
    rescue => ex
      error "#{ex.class}: #{ex.message}"
      error "#{ex.backtrace.join("\n")}"
    ensure
      debug 'finished'
    end
  end
end

#start_tailing_logs(ssh, instance) ⇒ Object



153
154
155
156
157
158
159
160
161
# File 'lib/aerosol/runner.rb', line 153

def start_tailing_logs(ssh, instance)
  if tail_logs && log_files.length > 0
    command = [
      'sudo', 'tail', '-f', *log_files
    ].join(' ')

    log_pids[instance.instance_id] ||= ssh_fork(command, ssh, instance)
  end
end

#stop_appObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/aerosol/runner.rb', line 195

def stop_app
  info "stopping old app"
  to_stop = old_instances

  info "starting with #{to_stop.length} instances to stop"

  stop_app_retries.succ.times do |n|
    break if to_stop.empty?
    debug "stop app: #{to_stop.length} instances remaining"
    to_stop.reject! { |instance| stop_one_app(instance) }
  end

  if to_stop.length.zero?
    info "successfully stopped the app on each old instance"
  elsif !continue_if_stop_app_fails
    raise "Failed to stop app on #{to_stop.length} instances"
  end
  info "stopped old app"
end

#wait_for_new_instancesObject



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
91
92
93
# File 'lib/aerosol/runner.rb', line 62

def wait_for_new_instances
  require_deploy!
  info "waiting for new instances"

  live_instances = []
  Timeout.timeout(instance_live_grace_period) do
    loop do
      current_instances = new_instances
      remaining_instances = current_instances - live_instances
      info "waiting for instances to be live (#{remaining_instances.count} remaining)"
      debug "current instances: #{current_instances.map(&:instance_id)}"
      debug "live instances: #{live_instances.map(&:instance_id)}"
      live_instances.concat(remaining_instances.select { |instance| healthy?(instance) })
      break if (current_instances - live_instances).empty?
      debug 'sleeping for 10 seconds'
      sleep(10)
    end
  end

  info 'new instances are up'
rescue Timeout::Error
  raise "[aerosol runner] site live check timed out after #{instance_live_grace_period} seconds"
ensure
  log_pids.each do |instance_id, fork|
    debug "Killing tailing for #{instance_id}: #{Time.now}"
    Process.kill('HUP', fork)
    debug "Killed process for #{instance_id}: #{Time.now}"
    debug "Waiting for process to die"
    Process.wait(fork)
    debug "Process ended for #{instance_id}: #{Time.now}"
  end
end

#with_deploy(name) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



261
262
263
264
265
266
267
268
269
# File 'lib/aerosol/runner.rb', line 261

def with_deploy(name)
  unless dep = Aerosol::Deploy[name]
    raise "No deploy named '#{name}'"
  end
  original = @deploy
  @deploy = dep
  yield self
  @deploy = original
end