Class: DNApi::Environment

Inherits:
Struct
  • Object
show all
Includes:
ComponentPossessor
Defined in:
lib/dnapi/environment.rb

Instance Attribute Summary collapse

Attributes inherited from Struct

#parent

Instance Method Summary collapse

Methods included from ComponentPossessor

#add_component, #component, #component_group, #has_component?, included

Methods inherited from Struct

#==, #_many, _many, #_many_values, #_ones, _ones, #_ones_values, _umembers, belongs_to, from, #inspect, many, map, one, #to_hash, #umember_inspect, unserialized_member

Methods included from Test::Ext

#default_fauxture_name, #fixture, #generate, #generate_attributes, #pick

Constructor Details

#initialize(attributes = {}) ⇒ Environment

Returns a new instance of Environment.



19
20
21
22
23
24
25
26
27
28
# File 'lib/dnapi/environment.rb', line 19

def initialize(attributes = {})
  if stack = attributes[:stack]
    attributes[:stack_name] = stack.name
  end

  super(attributes)

  self.backup_window ||= 14
  self.monitoring    ||= "monit"
end

Instance Attribute Details

#admin_ssh_keyObject

Returns the value of attribute admin_ssh_key

Returns:

  • (Object)

    the current value of admin_ssh_key



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def admin_ssh_key
  @admin_ssh_key
end

#alert_emailObject

Returns the value of attribute alert_email

Returns:

  • (Object)

    the current value of alert_email



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def alert_email
  @alert_email
end

#aws_secret_idObject

Returns the value of attribute aws_secret_id

Returns:

  • (Object)

    the current value of aws_secret_id



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def aws_secret_id
  @aws_secret_id
end

#aws_secret_keyObject

Returns the value of attribute aws_secret_key

Returns:

  • (Object)

    the current value of aws_secret_key



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def aws_secret_key
  @aws_secret_key
end

#backup_bucketObject

Returns the value of attribute backup_bucket

Returns:

  • (Object)

    the current value of backup_bucket



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def backup_bucket
  @backup_bucket
end

#backup_intervalObject

Returns the value of attribute backup_interval

Returns:

  • (Object)

    the current value of backup_interval



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def backup_interval
  @backup_interval
end

#backup_windowObject

Returns the value of attribute backup_window

Returns:

  • (Object)

    the current value of backup_window



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def backup_window
  @backup_window
end

#db_stack_nameObject

Returns the value of attribute db_stack_name

Returns:

  • (Object)

    the current value of db_stack_name



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def db_stack_name
  @db_stack_name
end

#framework_envObject

Returns the value of attribute framework_env

Returns:

  • (Object)

    the current value of framework_env



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def framework_env
  @framework_env
end

#internal_ssh_private_keyObject

Returns the value of attribute internal_ssh_private_key

Returns:

  • (Object)

    the current value of internal_ssh_private_key



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def internal_ssh_private_key
  @internal_ssh_private_key
end

#internal_ssh_public_keyObject

Returns the value of attribute internal_ssh_public_key

Returns:

  • (Object)

    the current value of internal_ssh_public_key



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def internal_ssh_public_key
  @internal_ssh_public_key
end

#mailserverObject

Returns the value of attribute mailserver

Returns:

  • (Object)

    the current value of mailserver



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def mailserver
  @mailserver
end

#monitoringObject

Returns the value of attribute monitoring

Returns:

  • (Object)

    the current value of monitoring



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def monitoring
  @monitoring
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def name
  @name
end

#newrelic_keyObject

Returns the value of attribute newrelic_key

Returns:

  • (Object)

    the current value of newrelic_key



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def newrelic_key
  @newrelic_key
end

#regionObject

Returns the value of attribute region

Returns:

  • (Object)

    the current value of region



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def region
  @region
end

#ruby_version=(value) ⇒ Object

Sets the attribute ruby_version

Parameters:

  • value (Object)

    the value to set the attribute ruby_version to.

Returns:

  • (Object)

    the newly set value



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def ruby_version=(value)
  @ruby_version = value
end

#ssh_keysObject

Returns the value of attribute ssh_keys

Returns:

  • (Object)

    the current value of ssh_keys



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def ssh_keys
  @ssh_keys
end

#ssh_passwordObject

Returns the value of attribute ssh_password

Returns:

  • (Object)

    the current value of ssh_password



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def ssh_password
  @ssh_password
end

#ssh_usernameObject

Returns the value of attribute ssh_username

Returns:

  • (Object)

    the current value of ssh_username



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def ssh_username
  @ssh_username
end

#stack_nameObject

Returns the value of attribute stack_name

Returns:

  • (Object)

    the current value of stack_name



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def stack_name
  @stack_name
end

#stats_passwordObject

Returns the value of attribute stats_password

Returns:

  • (Object)

    the current value of stats_password



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def stats_password
  @stats_password
end

#stonith_endpointObject

Returns the value of attribute stonith_endpoint

Returns:

  • (Object)

    the current value of stonith_endpoint



2
3
4
# File 'lib/dnapi/environment.rb', line 2

def stonith_endpoint
  @stonith_endpoint
end

Instance Method Details

#_apps=Object



90
# File 'lib/dnapi/environment.rb', line 90

alias_method :_apps=, :apps=

#_instances=Object



74
# File 'lib/dnapi/environment.rb', line 74

alias_method :_instances=, :instances=

#app_masterObject



123
124
125
# File 'lib/dnapi/environment.rb', line 123

def app_master
  instances.detect {|i| i.role.to_s == 'app_master' }
end

#app_master_hashObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/dnapi/environment.rb', line 147

def app_master_hash
  if app_master
    {:master_app_server => {
      :public_ip => app_master.public_hostname,
      :private_dns_name => app_master.private_hostname
    }}
  else
    {}
  end
end

#app_serversObject



127
128
129
# File 'lib/dnapi/environment.rb', line 127

def app_servers
  (app_slaves << app_master).compact
end

#app_slavesObject



115
116
117
# File 'lib/dnapi/environment.rb', line 115

def app_slaves
  instances.select {|i| i.role.to_s == 'app' }
end

#app_slaves_hostnamesObject



174
175
176
177
178
# File 'lib/dnapi/environment.rb', line 174

def app_slaves_hostnames
  unless app_slaves.empty?
    app_slaves.map {|i| i.private_hostname }
  end
end

#applicationsObject



198
199
200
201
202
# File 'lib/dnapi/environment.rb', line 198

def applications
  apps.inject({}) do |h, app|
    h.update(app.name => app.to_legacy_hash)
  end
end

#applications_hashObject



212
213
214
215
216
# File 'lib/dnapi/environment.rb', line 212

def applications_hash
  apps.inject({}) do |h, app|
    h.update(app.name => app.to_legacy_hash)
  end
end

#apps=(array) ⇒ Object



92
93
94
95
96
97
# File 'lib/dnapi/environment.rb', line 92

def apps=(array)
  array.each do |item|
    item.environment = self
  end
  self._apps = array
end

#backup_cronObject



298
299
300
301
302
303
304
305
306
# File 'lib/dnapi/environment.rb', line 298

def backup_cron
  Cron.new(
    :hour     => backup_hour,
    :day      => '*',
    :month    => '*',
    :weekday  => '*',
    :user     => 'root'
  ) if backups?
end

#backup_hourObject



308
309
310
# File 'lib/dnapi/environment.rb', line 308

def backup_hour
  backup_interval.to_s == '24' ? '1' : "*/#{backup_interval}"
end

#backups?Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/dnapi/environment.rb', line 264

def backups?
  backup_window && backup_window.to_s != '0'
end

#build_app(attributes = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/dnapi/environment.rb', line 60

def build_app(attributes = {})
  app = App.new(attributes)
  app.environment = self
  apps << app
  app
end

#build_cron(attributes = {}) ⇒ Object



83
84
85
86
87
88
# File 'lib/dnapi/environment.rb', line 83

def build_cron(attributes = {})
  cron = Cron.new(attributes)
  cron.environment = self
  crons << cron
  cron
end

#build_instance(attributes = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/dnapi/environment.rb', line 67

def build_instance(attributes = {})
  instance = Instance.new(attributes)
  instance.environment = self
  instances << instance
  instance
end

#db_hostObject



99
100
101
# File 'lib/dnapi/environment.rb', line 99

def db_host
  host = (db_master || solo) and host.public_hostname
end

#db_masterObject



119
120
121
# File 'lib/dnapi/environment.rb', line 119

def db_master
  instances.detect {|i| i.role.to_s == 'db_master' }
end

#db_slavesObject



111
112
113
# File 'lib/dnapi/environment.rb', line 111

def db_slaves
  instances.select {|i| i.role.to_s == 'db_slave' }
end

#db_slaves_hostnamesObject



166
167
168
# File 'lib/dnapi/environment.rb', line 166

def db_slaves_hostnames
  db_slaves.map {|i| i.private_hostname }
end

#db_stackObject



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

def db_stack
  DbStack.get(db_stack_name)
end

#db_stack=(db_stack) ⇒ Object



52
53
54
# File 'lib/dnapi/environment.rb', line 52

def db_stack=(db_stack)
  self.db_stack_name = db_stack.name
end

#dna_for_instance(instance) ⇒ Object



244
245
246
# File 'lib/dnapi/environment.rb', line 244

def dna_for_instance(instance)
  dna_for_instance_id(instance.id)
end

#dna_for_instance_id(amazon_id) ⇒ Object



248
249
250
251
252
253
254
# File 'lib/dnapi/environment.rb', line 248

def dna_for_instance_id(amazon_id)
  if instance = instance_for(amazon_id)
    instance.to_legacy_hash
  else
    raise InstanceNotFound, "Unknown instance #{amazon_id.inspect} in environment #{name.inspect}"
  end
end

#ebuildsObject



204
205
206
# File 'lib/dnapi/environment.rb', line 204

def ebuilds
  apps.map {|app| app.ebuilds }.flatten
end

#enabled_app_serversObject



180
181
182
# File 'lib/dnapi/environment.rb', line 180

def enabled_app_servers
  app_servers.select {|i| i.enabled?}
end

#enabled_app_servers_hashObject



158
159
160
161
162
163
164
# File 'lib/dnapi/environment.rb', line 158

def enabled_app_servers_hash
  unless app_servers.empty?
    {:members => enabled_app_servers_hostnames}
  else
    {}
  end
end

#enabled_app_servers_hostnamesObject



184
185
186
# File 'lib/dnapi/environment.rb', line 184

def enabled_app_servers_hostnames
  enabled_app_servers.map {|i| i.private_hostname }
end

#environment_hashObject



139
140
141
# File 'lib/dnapi/environment.rb', line 139

def environment_hash
  {:name => name, :framework_env => framework_env, :stack => (stack and stack.name)}
end

#gemsObject



208
209
210
# File 'lib/dnapi/environment.rb', line 208

def gems
  apps.map {|app| app.gems_hash }.flatten
end

#haproxy_hashObject



143
144
145
# File 'lib/dnapi/environment.rb', line 143

def haproxy_hash
  {:username => ssh_username, :password => stats_password}
end

#instance_for(instance_id) ⇒ Object



260
261
262
# File 'lib/dnapi/environment.rb', line 260

def instance_for(instance_id)
  instances.find { |instance| instance.id == instance_id }
end

#instance_idsObject



256
257
258
# File 'lib/dnapi/environment.rb', line 256

def instance_ids
  instances.map {|instance| instance.id}
end

#instances=(array) ⇒ Object



76
77
78
79
80
81
# File 'lib/dnapi/environment.rb', line 76

def instances=(array)
  array.each do |item|
    item.environment = self
  end
  self._instances = array
end

#mysql_backup_cronObject



278
279
280
281
282
283
284
285
286
# File 'lib/dnapi/environment.rb', line 278

def mysql_backup_cron
  if backups?
    cron = backup_cron
    cron.name = 'eybackup'
    cron.command = db_stack.backup_command
    cron.minute = '10'
    cron
  end
end

#postgres_backup_cronObject



288
289
290
291
292
293
294
295
296
# File 'lib/dnapi/environment.rb', line 288

def postgres_backup_cron
  if backups? && (db_stack == DbStack::Postgres || db_stack == DbStack::Postgres9 || db_stack == DbStack::Postgres91)
    cron = backup_cron
    cron.name = 'eybackup postgres'
    cron.command = db_stack.backup_command
    cron.minute = '20'
    cron
  end
end

#recipesObject

chef-ey selectors



313
314
315
# File 'lib/dnapi/environment.rb', line 313

def recipes
  apps.map {|a| a.recipes }.flatten.uniq
end

#ruby_componentObject



39
40
41
42
43
44
45
46
# File 'lib/dnapi/environment.rb', line 39

def ruby_component
  component(:ruby_186) || component(:ruby_187) ||
    component(:ree) || component(:ruby_192) ||
    component(:jruby_187) || component(:jruby_192) ||
    component(:rubinius) || component(:ruby_193) ||
    component(:nodejs) || component(:rubinius19) ||
    component(:php)
end

#snapshot_cronObject



268
269
270
271
272
273
274
275
276
# File 'lib/dnapi/environment.rb', line 268

def snapshot_cron
  if backups?
    cron = backup_cron
    cron.name = 'ey-snapshots'
    cron.command = 'ey-snapshots --snapshot --quiet'
    cron.minute = "0/#{rand(30) + 30}"
    cron
  end
end

#soloObject



107
108
109
# File 'lib/dnapi/environment.rb', line 107

def solo
  instances.detect {|i| i.role.to_s == 'solo' }
end

#solo?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/dnapi/environment.rb', line 103

def solo?
  instances.any? {|i| i.role.to_s == 'solo' }
end

#solo_cluster?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/dnapi/environment.rb', line 135

def solo_cluster?
  instances.size == 1 && instances.first.role.to_s == 'solo'
end

#stackObject



48
49
50
# File 'lib/dnapi/environment.rb', line 48

def stack
  Stack.get(stack_name)
end

#stack=(stack) ⇒ Object



31
32
33
# File 'lib/dnapi/environment.rb', line 31

def stack=(stack)
  self.stack_name = stack.name
end

#to_legacy_hashObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/dnapi/environment.rb', line 218

def to_legacy_hash
  shared = %w(alert_email aws_secret_key aws_secret_id
    backup_interval backup_window
    admin_ssh_key mailserver
    internal_ssh_public_key
    internal_ssh_private_key
    ruby_version
    newrelic_key)
  data = to_hash.select {|k,v| shared.include?(k.to_s)}
  Hash[*data.flatten].merge(
    :db_host             => db_host,
    :db_slaves           => db_slaves_hostnames,
    :user_ssh_key        => ssh_keys,
    :utility_instances   => utility_instances_hostnames,
    :environment         => environment_hash,
    :haproxy             => haproxy_hash,
    :users               => users,
    :packages_to_install => ebuilds,
    :gems_to_install     => gems,
    :applications        => applications_hash,
    :crons               => crons,
    :ruby_version        => ruby_version
  ).merge(app_master_hash).
    merge(enabled_app_servers_hash).reject {|k,v| v.nil? }
end

#usersObject



188
189
190
191
192
193
194
195
196
# File 'lib/dnapi/environment.rb', line 188

def users
  [{
    :username => ssh_username,
    :password => ssh_password,
    :gid      => '1000',
    :uid      => '1000',
    :comment  => ''
  }]
end

#utility_instancesObject



131
132
133
# File 'lib/dnapi/environment.rb', line 131

def utility_instances
  instances.select {|i| i.role.to_s == 'util' }
end

#utility_instances_hostnamesObject



170
171
172
# File 'lib/dnapi/environment.rb', line 170

def utility_instances_hostnames
  utility_instances.map {|i| {:hostname => i.private_hostname, :name => i.name } }
end