Class: ElasticBeans::Command::Create

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

Overview

:nodoc: all

Defined Under Namespace

Classes: UnhealthyEnvironmentError

Constant Summary collapse

USAGE =
"create ENVIRONMENT_TYPE"
DESC =
"Create a new environment in Elastic Beanstalk and attach it to relevant resources"
LONG_DESC =
<<-LONG_DESC
Create a new environment in Elastic Beanstalk and attach it to relevant resources.
The environment type must be one of the recognized types: webserver, worker, exec, or scheduled.
Use `-q` for worker environments to clarify which worker environment to create, e.g. `create worker -q default`.

A new environment will use the same deployed version as all other environments.
If this is the first environment, a new version will be created from the HEAD git commit of the working directory.

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(environment_type:, dns: nil, queue: nil, tags:, application:, ui:, elastic_beanstalk:, route53:, s3:) ⇒ Create

Returns a new instance of Create.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/elastic_beans/command/create.rb', line 24

def initialize(
  environment_type:,
  dns: nil,
  queue: nil,
  tags:,
  application:,
  ui:,
  elastic_beanstalk:,
  route53:,
  s3:
)
  @environment_type = environment_type
  @dns = dns
  @queue = queue
  @tags = tags
  @application = application
  @ui = ui
  @elastic_beanstalk = elastic_beanstalk
  @route53 = route53
  @s3 = s3
end

Instance Method Details

#runObject



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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/elastic_beans/command/create.rb', line 46

def run
  ui.debug { "Creating application version from code at #{Dir.pwd}..." }
  version = application.deployed_version || ElasticBeans::ApplicationVersion.create(
    working_directory: Dir.pwd,
    application: application,
    elastic_beanstalk: elastic_beanstalk,
    s3: s3,
  )
  environment = ElasticBeans::Environment.new_by_type(
    environment_type,
    queue: queue,
    application: application,
    elastic_beanstalk: elastic_beanstalk,
  )

  dns_thread = Thread.new do
    if environment_type == "webserver" && dns_entry
      ui.debug { "Updating DNS entry `#{dns_entry.name}'..." }
      dns_entry.update(dns_value(environment))
    end
  end

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

  ui.info("Creating environment `#{environment.name}'...")
  progressbar = ProgressBar.create(title: "Creating", total: nil, output: ui.stdout)
  environment_thread = Thread.new do
    environment.create(
      tags: tags,
      version: version.version_label,
    )
  end

  loop do
    sleep 0.5
    progressbar.increment
    if !environment_thread.alive? && !dns_thread.alive?
      progressbar.total = progressbar.progress
      break
    end
  end

  environment_thread.join
  dns_thread.join
  ui.info("Created!")
  if environment_type == "webserver"
    if dns_entry
      host = dns_entry.name
    else
      host = environment.cname
    end
    ui.info("#{application.name} reachable at https://#{host}/")
  end
end