Class: DNApi::Environment
Instance Attribute Summary collapse
Attributes inherited from Struct
#parent
Instance Method Summary
collapse
#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_key ⇒ Object
Returns the value of attribute admin_ssh_key
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def admin_ssh_key
@admin_ssh_key
end
|
#alert_email ⇒ Object
Returns the value of attribute alert_email
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def alert_email
@alert_email
end
|
#aws_secret_id ⇒ Object
Returns the value of attribute aws_secret_id
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def aws_secret_id
@aws_secret_id
end
|
#aws_secret_key ⇒ Object
Returns the value of attribute aws_secret_key
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def aws_secret_key
@aws_secret_key
end
|
#backup_bucket ⇒ Object
Returns the value of attribute backup_bucket
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def backup_bucket
@backup_bucket
end
|
#backup_interval ⇒ Object
Returns the value of attribute backup_interval
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def backup_interval
@backup_interval
end
|
#backup_window ⇒ Object
Returns the value of attribute backup_window
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def backup_window
@backup_window
end
|
#db_stack_name ⇒ Object
Returns the value of attribute db_stack_name
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def db_stack_name
@db_stack_name
end
|
#framework_env ⇒ Object
Returns the value of attribute framework_env
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def framework_env
@framework_env
end
|
#internal_ssh_private_key ⇒ Object
Returns the value of attribute 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_key ⇒ Object
Returns the value of attribute internal_ssh_public_key
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def internal_ssh_public_key
@internal_ssh_public_key
end
|
#mailserver ⇒ Object
Returns the value of attribute mailserver
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def mailserver
@mailserver
end
|
#monitoring ⇒ Object
Returns the value of attribute monitoring
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def monitoring
@monitoring
end
|
#name ⇒ Object
Returns the value of attribute name
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def name
@name
end
|
#newrelic_key ⇒ Object
Returns the value of attribute newrelic_key
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def newrelic_key
@newrelic_key
end
|
#region ⇒ Object
Returns the value of attribute region
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def region
@region
end
|
#ruby_version=(value) ⇒ Object
Sets the attribute ruby_version
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def ruby_version=(value)
@ruby_version = value
end
|
#ssh_keys ⇒ Object
Returns the value of attribute ssh_keys
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def ssh_keys
@ssh_keys
end
|
#ssh_password ⇒ Object
Returns the value of attribute ssh_password
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def ssh_password
@ssh_password
end
|
#ssh_username ⇒ Object
Returns the value of attribute ssh_username
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def ssh_username
@ssh_username
end
|
#stack_name ⇒ Object
Returns the value of attribute stack_name
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def stack_name
@stack_name
end
|
#stats_password ⇒ Object
Returns the value of attribute stats_password
2
3
4
|
# File 'lib/dnapi/environment.rb', line 2
def stats_password
@stats_password
end
|
#stonith_endpoint ⇒ Object
Returns the value of attribute 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_master ⇒ Object
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_hash ⇒ Object
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_servers ⇒ Object
127
128
129
|
# File 'lib/dnapi/environment.rb', line 127
def app_servers
(app_slaves << app_master).compact
end
|
#app_slaves ⇒ Object
115
116
117
|
# File 'lib/dnapi/environment.rb', line 115
def app_slaves
instances.select {|i| i.role.to_s == 'app' }
end
|
#app_slaves_hostnames ⇒ Object
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
|
#applications ⇒ Object
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_hash ⇒ Object
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_cron ⇒ Object
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_hour ⇒ Object
308
309
310
|
# File 'lib/dnapi/environment.rb', line 308
def backup_hour
backup_interval.to_s == '24' ? '1' : "*/#{backup_interval}"
end
|
#backups? ⇒ 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_host ⇒ Object
99
100
101
|
# File 'lib/dnapi/environment.rb', line 99
def db_host
host = (db_master || solo) and host.public_hostname
end
|
#db_master ⇒ Object
119
120
121
|
# File 'lib/dnapi/environment.rb', line 119
def db_master
instances.detect {|i| i.role.to_s == 'db_master' }
end
|
#db_slaves ⇒ Object
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_hostnames ⇒ Object
166
167
168
|
# File 'lib/dnapi/environment.rb', line 166
def db_slaves_hostnames
db_slaves.map {|i| i.private_hostname }
end
|
#db_stack ⇒ Object
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
|
#ebuilds ⇒ Object
204
205
206
|
# File 'lib/dnapi/environment.rb', line 204
def ebuilds
apps.map {|app| app.ebuilds }.flatten
end
|
#enabled_app_servers ⇒ Object
180
181
182
|
# File 'lib/dnapi/environment.rb', line 180
def enabled_app_servers
app_servers.select {|i| i.enabled?}
end
|
#enabled_app_servers_hash ⇒ Object
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_hostnames ⇒ Object
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_hash ⇒ Object
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
|
#gems ⇒ Object
208
209
210
|
# File 'lib/dnapi/environment.rb', line 208
def gems
apps.map {|app| app.gems_hash }.flatten
end
|
#haproxy_hash ⇒ Object
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_ids ⇒ Object
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_cron ⇒ Object
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_cron ⇒ Object
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
|
#recipes ⇒ Object
313
314
315
|
# File 'lib/dnapi/environment.rb', line 313
def recipes
apps.map {|a| a.recipes }.flatten.uniq
end
|
#ruby_component ⇒ Object
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_cron ⇒ Object
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
|
#solo ⇒ Object
107
108
109
|
# File 'lib/dnapi/environment.rb', line 107
def solo
instances.detect {|i| i.role.to_s == 'solo' }
end
|
#solo? ⇒ 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
135
136
137
|
# File 'lib/dnapi/environment.rb', line 135
def solo_cluster?
instances.size == 1 && instances.first.role.to_s == 'solo'
end
|
#stack ⇒ Object
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_hash ⇒ Object
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
|
#users ⇒ Object
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_instances ⇒ Object
131
132
133
|
# File 'lib/dnapi/environment.rb', line 131
def utility_instances
instances.select {|i| i.role.to_s == 'util' }
end
|
#utility_instances_hostnames ⇒ Object
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
|