Class: ElasticBeans::Command::Deploy

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_beans/command/deploy.rb

Overview

:nodoc: all

Constant Summary collapse

USAGE =
"deploy"
DESC =
"Deploy the HEAD git commit to all environments in Elastic Beanstalk"
LONG_DESC =
<<-LONG_DESC
Deploy the HEAD git commit of the working directory to all environments in Elastic Beanstalk.
Cleans up old application versions in parallel to avoid reaching the 1,000 application version limit.

Requires AWS credentials to be set in the environment, i.e. AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
LONG_DESC
APPLICATION_VERSION_MAX_AGE =

Maximum age of application versions that are left behind and not cleaned up. 1 week.

604_800
APPLICATION_VERSION_MIN_COUNT =

Minimum number of application versions to leave around.

5

Instance Method Summary collapse

Constructor Details

#initialize(application:, elastic_beanstalk:, s3:, ui:) ⇒ Deploy

Returns a new instance of Deploy.



21
22
23
24
25
26
# File 'lib/elastic_beans/command/deploy.rb', line 21

def initialize(application:, elastic_beanstalk:, s3:, ui:)
  @application = application
  @elastic_beanstalk = elastic_beanstalk
  @s3 = s3
  @ui = ui
end

Instance Method Details

#runObject



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
84
85
86
87
88
89
90
91
92
# File 'lib/elastic_beans/command/deploy.rb', line 28

def run
  environments = application.environments
  unready_environments = environments.select { |environment| environment.status != "Ready" }
  if unready_environments.any?
    raise EnvironmentsNotReady.new(environments: unready_environments)
  end

  ui.debug { "Creating application version from code at #{Dir.pwd}..." }
  progressbar = ProgressBar.create(title: "Uploading", total: nil, output: ui.stdout)
  version = nil
  version_thread = Thread.new do
    version = ElasticBeans::ApplicationVersion.create(
      working_directory: Dir.pwd,
      application: application,
      elastic_beanstalk: elastic_beanstalk,
      s3: s3,
    ).version_label
  end
  cleanup_thread = Thread.new do
    remove_old_versions
  end

  loop do
    sleep 0.5
    progressbar.increment
    if !version_thread.alive? && !cleanup_thread.alive?
      break
    end
  end
  version_thread.join
  cleanup_thread.join

  Signal.trap("INT") do
    puts "\nInterrupting beans"
    puts "Deployment still in progress, follow it in the AWS console"
    raise Interrupt
  end
  Signal.trap("TERM") do
    puts "Terminating beans"
    puts "Deployment still in progress, follow it in the AWS console"
    raise SignalException.new("TERM")
  end

  progressbar.log("Deploying version '#{version}' to #{application.name}...")
  progressbar.title = "Deploying"
  deploy_threads = environments.map { |environment|
    progressbar.log("Deploying version '#{version}' to environment `#{environment.name}'...")
    thread = Thread.new do
      environment.deploy_version(version)
    end
    progressbar.increment
    thread
  }

  loop do
    sleep 0.5
    progressbar.increment
    if deploy_threads.none?(&:alive?)
      progressbar.total = progressbar.progress
      break
    end
  end
  deploy_threads.each(&:join)
  ui.info("Deployed!")
end