Sha256: 4773c5f20c2bc2bc487c4e9df0edd2e64f433a5bea60160403f4906921a8dd0d

Contents?: true

Size: 1.52 KB

Versions: 31

Compression:

Stored size: 1.52 KB

Contents

module Ufo
  class Stop < Base
    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

    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

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

    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

    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
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ufo-5.0.7 lib/ufo/stop.rb
ufo-5.0.6 lib/ufo/stop.rb
ufo-5.0.5 lib/ufo/stop.rb
ufo-5.0.4 lib/ufo/stop.rb
ufo-5.0.3 lib/ufo/stop.rb
ufo-5.0.2 lib/ufo/stop.rb
ufo-5.0.1 lib/ufo/stop.rb
ufo-5.0.0 lib/ufo/stop.rb
ufo-4.6.3 lib/ufo/stop.rb
ufo-4.6.2 lib/ufo/stop.rb
ufo-4.6.1 lib/ufo/stop.rb
ufo-4.6.0 lib/ufo/stop.rb
ufo-4.5.11 lib/ufo/stop.rb
ufo-4.5.10 lib/ufo/stop.rb
ufo-4.5.9 lib/ufo/stop.rb
ufo-4.5.8 lib/ufo/stop.rb
ufo-4.5.7 lib/ufo/stop.rb
ufo-4.5.6 lib/ufo/stop.rb
ufo-4.5.5 lib/ufo/stop.rb
ufo-4.5.4 lib/ufo/stop.rb