Class: Environment

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/dew/models/environment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, servers = [], database = nil) ⇒ Environment

Returns a new instance of Environment.



8
9
10
11
12
13
# File 'lib/dew/models/environment.rb', line 8

def initialize(name, servers=[], database=nil)
  @name = name
  @servers = servers
  @database = database
  valid?
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/dew/models/environment.rb', line 6

def database
  @database
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/dew/models/environment.rb', line 6

def name
  @name
end

#serversObject (readonly)

Returns the value of attribute servers.



6
7
8
# File 'lib/dew/models/environment.rb', line 6

def servers
  @servers
end

Class Method Details

.create(name, profile) ⇒ Object



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
# File 'lib/dew/models/environment.rb', line 25

def self.create(name, profile)
  raise "Keypair '#{profile.keypair}' is not available in AWS #{Cloud.region}." unless Cloud.keypair_exists?(profile.keypair)
  raise "AMI for '#{Cloud.region}' is not setup in the '#{profile.profile_name}' profile." unless profile.ami

  environment = new(name)
  Inform.info "Creating new environment %{name}", :name => name

  # Creating the database is the longest running task, so do that first.
  password = Password.random if profile.has_rds?
  environment.add_database(profile.rds_size, profile.rds_storage_size, password) if profile.has_rds?

  (1..profile.count).each do
    environment.add_server(
      profile.username,
      :ami => profile.ami,
      :size => profile.size,
      :keypair => profile.keypair,
      :groups => profile.security_groups,
      :disk_size => profile.instance_disk_size
    )
  end

  environment.add_elb(profile.elb_listener_ports) if profile.has_elb?

  environment.wait_until_ready
  
  environment.resize_disks if profile.instance_disk_size?
  environment.configure_servers_for_database password if profile.has_rds?
  
  Inform.info "Environment %{name} ready!", :name => name
  environment
end

.get(name) ⇒ Object



19
20
21
22
23
# File 'lib/dew/models/environment.rb', line 19

def self.get name
  servers = Server.find('Environment', name)
  database = Database.get(name)
  servers.length > 0 || database ? new(name, servers, database) : nil
end

.indexObject



62
63
64
65
66
67
# File 'lib/dew/models/environment.rb', line 62

def self.index
  #environments = rows.inject({}) { |h, (name, owner)| h[name] = Environment.get(name); h }
  #items = rows.collect { |name, owner| [name, owner, environments[name].servers.size, environments[name].database] }
  #table(%w(name owner instance_count database), *rows).to_s.indent
  Inform.info View.new('Environments', owners, %w(name owner)).index
end

.ownersObject



58
59
60
# File 'lib/dew/models/environment.rb', line 58

def self.owners
  Cloud.valid_servers.collect(&:tags).collect { |h| {:name => h['Environment'], :owner => h['Creator']} if h['Environment'] }.uniq.compact
end

Instance Method Details

#add_database(size, storage_size, password) ⇒ Object



161
162
163
164
165
166
# File 'lib/dew/models/environment.rb', line 161

def add_database(size, storage_size, password)
  Inform.info "Adding Database %{name} of size %{size} (storage = #{storage_size}Gb) with master password %{password}",
    :name => name, :size => size, :password => password do
    @database = Database.create!(name, size, storage_size, password)
  end
end

#add_elb(listener_ports = []) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/dew/models/environment.rb', line 152

def add_elb listener_ports=[]
  zones = servers.map {|s| s.availability_zone}.uniq
  Inform.info "Adding Load Balancer for availability zone(s) %{zones}", :zones => zones.join(', ') do
    Cloud.elb.create_load_balancer(zones, name, listeners_from_ports(listener_ports))
    Inform.debug("ELB created, adding instances...")
    Cloud.elb.register_instances_with_load_balancer(servers.map {|s| s.id}, name)
  end
end

#add_server(username, create_options) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dew/models/environment.rb', line 129

def add_server(username, create_options)
  server_name = "#{name} #{servers.count + 1}"

  server = Server.create!(create_options)
  server.add_tag('Environment', name)
  server.add_tag('Creator', ENV['USER'])
  server.add_tag('Name', server_name)
  server.add_tag('Username', username) # Needed for SSH
  Inform.debug("%{name} ID: %{id} AZ: %{az}", :name => server_name, :id =>server.id, :az => server.availability_zone)

  servers << server
end

#add_server_to_elb(server) ⇒ Object



142
143
144
145
# File 'lib/dew/models/environment.rb', line 142

def add_server_to_elb server
  Inform.debug("Adding %{id} to ELB", :id => server.id)
  Cloud.elb.register_instances_with_load_balancer([server.id], name)
end

#configure_servers_for_database(password) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/dew/models/environment.rb', line 187

def configure_servers_for_database password
  Inform.info "Configuring servers for database" do
    servers.each do |server|
      Inform.debug "%{id}", :id => server.id
      server.configure_for_database(database, password)
    end
  end
end

#destroyObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dew/models/environment.rb', line 111

def destroy
  servers.each do |server|
    Inform.info("Destroying server %{serverid}", :serverid => server.id) do
      server.destroy
    end
  end
  if database
    Inform.info("Destroying database") do
      database.destroy(nil)
    end
  end
  if has_elb?
    Inform.info("Destroying load balancer") do
      Cloud.elb.delete_load_balancer(name)
    end
  end
end

#elbObject



202
203
204
# File 'lib/dew/models/environment.rb', line 202

def elb
  @elb ||= Cloud.elb.load_balancers.detect { |elb| elb.id == name }  # XXX - need to refactor
end

#has_elb?Boolean

Returns:

  • (Boolean)


206
207
208
209
210
# File 'lib/dew/models/environment.rb', line 206

def has_elb?
  elb_descriptions.select { |elb|
    elb['LoadBalancerName'] == name
  }.length > 0
end

#remove_server_from_elb(server) ⇒ Object



147
148
149
150
# File 'lib/dew/models/environment.rb', line 147

def remove_server_from_elb server
  Inform.debug("Removing %{id} from ELB", :id => server.id)
  Cloud.elb.deregister_instances_from_load_balancer([server.id], name)
end

#resize_disksObject



196
197
198
199
200
# File 'lib/dew/models/environment.rb', line 196

def resize_disks
  Inform.info "Resizing disks on servers" do
    servers.each { |s| s.resize_disk }
  end
end

#show(options = {}) ⇒ Object



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
# File 'lib/dew/models/environment.rb', line 69

def show(options = {})
  return show_json if options[:json]

  result = ""

  result << "ENVIRONMENT:\n"

  result << "NAME: #{name}\n"
  result << "REGION: #{Cloud.region}\n"
  result << "ACCOUNT: #{Cloud..aws_user_id}\n"

  unless servers.empty?
    #keys = %w(id flavor_id public_ip_address state created_at key_name tags)
    keys = %w(id flavor_id public_ip_address state created_at groups key_name availability_zone creator)
    servers_view = View.new('SERVERS', servers, keys)
    result << servers_view.index
  end

  if elb
    elbs_view = View.new('ELB', [elb], %w(created_at dns_name instances availability_zones))
    result << elbs_view.show(0)
  end

  if database
    databases_view = View.new('DATABASE', [database], %w(flavor_id state created_at availability_zone db_security_groups))
    result << databases_view.show(0)
  end

  Inform.info result
end

#show_jsonObject



100
101
102
103
104
105
106
107
108
109
# File 'lib/dew/models/environment.rb', line 100

def show_json
  json = servers.map do |s|
    {
      'public_dns' => s.dns_name,
      'public_ip' => s.public_ip_address
    }
  end
  
  puts JSON.pretty_generate(json)
end

#valid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/dew/models/environment.rb', line 15

def valid?
  Validation::validates_format_of @name, /^[a-zA-Z0-9-]+$/
end

#wait_until_readyObject



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/dew/models/environment.rb', line 168

def wait_until_ready
  Inform.info "Waiting for servers to become ready" do
    servers.each do |server|
      ip_permissions = server.groups.map { |group_name| Cloud.security_groups[group_name] }.compact.collect(&:ip_permissions)
      if ip_permissions.flatten.empty?
        Inform.warning "Server %{id} has no ip_permissions in its security groups %{security_group_names}", :id => server.id, :security_group_names => server.groups.inspect
      else
        Inform.debug "Trying to connect to %{id}", :id => server.id
        server.wait_until_ready
      end
    end
  end
  if database
    Inform.info "Waiting for database to become ready" do
      database.wait_until_ready if database
    end
  end
end