Class: Beanstalkify::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/beanstalkify/environment.rb

Constant Summary collapse

POLL_INTERVAL =
5
STATUS_CHANGE_TIMEOUT =
1200
HEALTHY_TIMEOUT =
120

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive, name) ⇒ Environment

Returns a new instance of Environment.



11
12
13
14
# File 'lib/beanstalkify/environment.rb', line 11

def initialize(archive, name)
  @name = [archive.app_name, name].join("-")
  @beanstalk = Beanstalk.api
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/beanstalkify/environment.rb', line 9

def name
  @name
end

Instance Method Details

#create!(archive, stack, cnames, settings = []) ⇒ Object

Assuming the archive has already been uploaded, create a new environment with the app deployed onto the provided stack. Attempts to use the first available cname in the cnames array.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/beanstalkify/environment.rb', line 30

def create!(archive, stack, cnames, settings=[])
  params = {
    application_name: archive.app_name,
    version_label: archive.version,
    environment_name: self.name,
    solution_stack_name: stack,
    option_settings: settings
  }
  cnames.each do |c|
    if dns_available(c)
      params[:cname_prefix] = c
      break
    else
      puts "CNAME #{c} is unavailable."
    end
  end
  @beanstalk.create_environment(params)
end

#deploy!(app, settings = []) ⇒ Object

Assuming the provided app has already been uploaded, update this environment to the app’s version Optionally pass in a bunch of settings to override



19
20
21
22
23
24
25
# File 'lib/beanstalkify/environment.rb', line 19

def deploy!(app, settings=[])
  @beanstalk.update_environment({
    version_label: app.version,
    environment_name: self.name,
    option_settings: settings
  })
end

#dns_available(cname) ⇒ Object



49
50
51
52
53
# File 'lib/beanstalkify/environment.rb', line 49

def dns_available(cname)
  @beanstalk.check_dns_availability({
    cname_prefix: cname
  })[:available]
end

#healthy?Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/beanstalkify/environment.rb', line 60

def healthy?
  e = describe_environment
  e ? e[:health] == 'Green' : false
end

#statusObject



65
66
67
68
# File 'lib/beanstalkify/environment.rb', line 65

def status
  e = describe_environment
  e ? e[:status] : ""
end

#urlObject



55
56
57
58
# File 'lib/beanstalkify/environment.rb', line 55

def url
  e = describe_environment
  e ? e[:cname] : ""
end

#wait_until_healthyObject



75
76
77
78
# File 'lib/beanstalkify/environment.rb', line 75

def wait_until_healthy
  puts "Waiting until #{self.name} is healthy..."
  wait_until -> { healthy? }, HEALTHY_TIMEOUT
end

#wait_until_status_is_not(old_status) ⇒ Object



70
71
72
73
# File 'lib/beanstalkify/environment.rb', line 70

def wait_until_status_is_not(old_status)
  puts "Waiting for #{self.name} to finish #{old_status.downcase}..."      
  wait_until -> { status != old_status }, STATUS_CHANGE_TIMEOUT
end