Class: Ufo::Stop

Inherits:
Base
  • Object
show all
Defined in:
lib/ufo/stop.rb

Instance Method Summary collapse

Methods inherited from Base

#full_service, #info, #initialize, #no_service_message, #switch_current

Methods included from Ufo::Stack::Helper

#adjust_stack_name, #cfn, #find_stack, #status

Methods included from Util

#default_cluster, #display_params, #execute, #pretty_time, #settings, #task_definition_arns, #user_params

Methods included from AwsService

#cloudformation, #cloudwatchlogs, #ec2, #ecr, #ecs, #elb

Constructor Details

This class inherits a constructor from Ufo::Base

Instance Method Details

#latest_deployed_arnObject

latest deployment task definition arn



27
28
29
30
31
32
# File 'lib/ufo/stop.rb', line 27

def latest_deployed_arn
  latest = @deployments.sort_by do |deployment|
    Time.parse(deployment["created_at"].to_s)
  end.last
  latest["task_definition"]
end

#log(text) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/ufo/stop.rb', line 40

def log(text)
  path = "#{Ufo.root}/.ufo/log/stop.log"
  FileUtils.mkdir_p(File.dirname(path))
  File.open(path, 'a') do |f|
    f.puts("#{Time.now} #{text}")
  end
  puts text unless @options[:mute]
end

#runObject



3
4
5
6
7
8
9
10
11
12
# File 'lib/ufo/stop.rb', line 3

def run
  info = Info.new(@service, @options)
  service = info.service
  return unless service # brand new deploy

  @deployments = service.deployments
  if @deployments.size > 1
    stop_old_tasks(service.service_name)
  end
end

#service_tasks(cluster, service_name) ⇒ Object



34
35
36
37
38
# File 'lib/ufo/stop.rb', line 34

def service_tasks(cluster, service_name)
  all_task_arns = ecs.list_tasks(cluster: cluster, service_name: service_name).task_arns
  return [] if all_task_arns.empty?
  ecs.describe_tasks(cluster: cluster, tasks: all_task_arns).tasks
end

#stop_old_tasks(service_name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ufo/stop.rb', line 14

def stop_old_tasks(service_name)
  # json = JSON.pretty_generate(deployments.map(&:to_h))
  # IO.write("/tmp/deployments.json", json)
  tasks = service_tasks(@cluster, service_name)
  reason = "Ufo #{Ufo::VERSION} has deployed new code and stopping old tasks."
  tasks.each do |task|
    next if task["task_definition_arn"] == latest_deployed_arn
    log "Stopping task #{task["task_arn"]}"
    ecs.stop_task(cluster: @cluster, task: task["task_arn"], reason: reason)
  end
end