Class: Hashicorptools::CodeDeploy

Inherits:
Thor
  • Object
show all
Defined in:
lib/hashicorptools/code_deploy.rb

Constant Summary collapse

AWS_REGION_US_EAST_1 =
'us-east-1'

Instance Method Summary collapse

Instance Method Details

#deployObject



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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hashicorptools/code_deploy.rb', line 58

def deploy
  g = Git.open('..')

  # We set defaults (depending on environment) for aws_regions if not passed in
  aws_regions = options[:aws_regions] || default_regions

  commit = if options[:commit].present?
             g.gcommit(options[:commit])
           else
             branch = options[:branch].nil? ? :main : options[:branch].to_sym
             g.checkout(branch)
             g.log.first
           end

  puts "Deploying to environment #{options[:environment]} - regions: #{aws_regions.join(', ')}
        commit: #{commit.sha}
        message: #{commit.message}"

  puts "Deploying for regions: #{aws_regions}"

  all_succeeded = true
  aws_regions.each_slice(2) do |aws_regions_batch|
    puts "Deploying for batch of regions: #{aws_regions_batch}"

    threads = []
    aws_regions_batch.each do |aws_region|
      thread = Thread.new{ region_deployment(aws_region).create_deployment(commit.sha, commit.message) }
      threads.push(thread)
    end

    threads.each_with_index do |thread, index|
      begin
        # thread.value waits for the thread to finish with #join, then returns the value of the expression
        thread_succeeded = thread.value
        all_succeeded = all_succeeded && thread_succeeded
      rescue StandardError => e
        # Don't quit whole program on exception in thread, just print exception and exit thread
        puts "[#{aws_regions[index]}] EXCEPTION: #{e}"
        all_succeeded = false
      end
    end
  end

  # Return a success or failure status code to be consumed by bash
  exit(all_succeeded)
end