Class: ElasticBeans::Command::SetEnv

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

Overview

:nodoc: all

Defined Under Namespace

Classes: MissingValueError

Constant Summary collapse

USAGE =
"setenv KEY=[value] [KEY=[value]]..."
DESC =
"Update environment variables in the Elastic Beanstalk application"
LONG_DESC =
<<-LONG_DESC
Update environment variables in the Elastic Beanstalk application.
These are stored in S3 to avoid the 4096-byte limit on environment variables in Elastic Beanstalk.
Restarts all running environments so they load the updated environment vars.

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

Instance Method Summary collapse

Constructor Details

#initialize(application:, ui:) ⇒ SetEnv

Returns a new instance of SetEnv.



19
20
21
22
# File 'lib/elastic_beans/command/set_env.rb', line 19

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

Instance Method Details

#run(*env_pairs) ⇒ Object



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
# File 'lib/elastic_beans/command/set_env.rb', line 24

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

  env_vars = pairs_as_hash(env_pairs)
  threads = []

  progressbar = ProgressBar.create(title: "Updating", total: nil, output: ui.stdout)

  Signal.trap("INT") do
    puts "\nInterrupting beans"
    puts "WARNING: Configuration update is partially applied, you should run this command again"
    raise Interrupt
  end
  Signal.trap("TERM") do
    puts "Terminating beans"
    puts "WARNING: Configuration update is partially applied, you should run this command again"
    raise SignalException.new("TERM")
  end

  progressbar.log("Updating configuration in #{application.name}...")
  application.env_vars.upsert(env_vars)
  progressbar.increment

  threads += environments.map { |environment|
    progressbar.log("Updating `#{environment.name}'...")
    thread = Thread.new do
      environment.restart
    end
    progressbar.increment
    thread
  }

  loop do
    sleep 0.5
    progressbar.increment
    if threads.none?(&:alive?)
      progressbar.total = progressbar.progress
      break
    end
  end

  threads.each(&:join)
end