Class: Shipitron::Server::RunPostBuild

Inherits:
Object
  • Object
show all
Includes:
Metaractor, EcsClient
Defined in:
lib/shipitron/server/run_post_build.rb

Instance Method Summary collapse

Methods included from EcsClient

#ecs_client, #generate_ecs_client

Instance Method Details

#callObject



14
15
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
61
62
63
64
# File 'lib/shipitron/server/run_post_build.rb', line 14

def call
  return if post_builds.nil? || post_builds.empty?

  Logger.info 'Running post build commands'

  begin
    post_builds.each do |post_build|
      Logger.info "Running #{post_build.command}"
      response = ecs_client(region: region).run_task(
        cluster: cluster_name,
        task_definition: post_build.ecs_task,
        overrides: {
          container_overrides: [
            {
              name: post_build.container_name,
              command: post_build.command_ary
            }
          ]
        },
        count: 1,
        started_by: 'shipitron'
      )

      if !response.failures.empty?
        response.failures.each do |failure|
          fail_with_error! message: "ECS run_task failure: #{failure.arn}: #{failure.reason}"
        end
      end

      task_arn = response.tasks.first.task_arn

      Logger.info 'Waiting for task to finish'
      loop do
        response = ecs_client(region: region).describe_tasks(
          cluster: cluster_name,
          tasks: [task_arn]
        )
        next if response.tasks.empty?
        Logger.info "Task status: #{response.tasks.first.last_status}"
        break if response.tasks.first.last_status == 'STOPPED'.freeze
        sleep 1
      end
    end

  rescue Aws::ECS::Errors::ServiceError => e
    fail_with_errors!(messages: [
      "Error: #{e.message}",
      e.backtrace.join("\n")
    ])
  end
end