Module: OpsWorks::CLI::Subcommands::Deployments

Included in:
Agent
Defined in:
lib/opsworks/cli/subcommands/deployments.rb

Class Method Summary collapse

Class Method Details

.included(thor) ⇒ Object



5
6
7
8
9
10
11
12
13
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/opsworks/cli/subcommands/deployments.rb', line 5

def self.included(thor)
  thor.class_eval do
    desc 'deployments:retry [--stack STACK]', 'Retry last deployment'
    option :stack, type: :array
    option :timeout, type: :numeric, default: 300
    define_method 'deployments:retry' do
      stacks = parse_stacks(options.merge(active: true))

      last_deployment = stacks.map do |stack|
        say "Loading last deployment for #{stack.name}"
        [stack, stack.deployments.max_by(&:created_at)]
      end

      will_retry = last_deployment.map do |stack, deployment|
        if deployment.nil?
          say "Skipping stack #{stack.name}: no deployment"
          next
        end

        if deployment.status == 'successful'
          say "Skipping stack #{stack.name}: last deployment is " \
              "#{deployment.status}"
          next
        end

        say "Loading instance status for #{stack.name}"

        res = deployment.client.describe_commands(
          deployment_id: deployment.id
        )

        retry_commands = res.commands.select do |command|
          command.status != 'successful'
        end

        instance_ids = retry_commands.map(&:instance_id).to_a

        if instance_ids.empty?
          say "Skipping #{stack.name}: no instances failed"
          next
        end

        [stack, deployment, instance_ids]
      end.compact

      deployments = will_retry.map do |stack, dep, instance_ids|
        say "Retrying #{dep.command[:name]} in #{stack.name} " \
            "on #{instance_ids.join ' '}"

        opts = {
          command: dep.command,
          instance_ids: instance_ids
        }
        opts[:app_id] = dep.app_id if dep.app_id
        opts[:custom_json] = dep.custom_json if dep.custom_json

        new_deployment = stack.create_deployment(opts)

        [stack, new_deployment]
      end

      say "Waiting #{options[:timeout]}s for deployments to finish"

      OpsWorks::Deployment.wait(deployments.map(&:last),
                                options[:timeout])

      failures = deployments.map do |stack, deployment|
        next if deployment.success?
        stack
      end.compact

      unless failures.empty?
        raise "Deploy failed on #{failures.map(&:name).join(' ')}"
      end

      say "All #{deployments.size} deployments suceeded"
    end
  end
end