Class: Ironfan::Broker::Computer

Inherits:
Ironfan::Builder show all
Defined in:
lib/ironfan/headers.rb,
lib/ironfan/broker/computer.rb

Instance Method Summary collapse

Methods inherited from Ironfan::Builder

ui, #ui

Constructor Details

#initialize(*args) ⇒ Computer

Returns a new instance of Computer.


16
17
18
19
20
21
22
23
24
25
# File 'lib/ironfan/broker/computer.rb', line 16

def initialize(*args)
  super
  providers[:chef] ||=    Ironfan::Provider::ChefServer
  return unless server
  providers[:iaas]   = server.selected_cloud.provider
  volumes            = server.volumes.values
  volumes           += server.implied_volumes
  volumes.each{|vol| self.drive vol.name, :volume => vol }
rescue StandardError => err ; err.polish("#{self.class} on '#{args.inspect}'") rescue nil ; raise
end

Instance Method Details

#bogusObject

Accessors


169
170
171
# File 'lib/ironfan/broker/computer.rb', line 169

def bogus
  resources.values.map(&:bogus).flatten
end

#bogus?Boolean

Status flags

Returns:

  • (Boolean)

217
218
219
# File 'lib/ironfan/broker/computer.rb', line 217

def bogus?
  resources.values.any?(&:bogus?)
end

#bootstrap_distroObject


199
# File 'lib/ironfan/broker/computer.rb', line 199

def bootstrap_distro ; (server && server.selected_cloud) ? server.selected_cloud.bootstrap_distro : nil ; end

#chef_client_script_contentObject


129
130
131
132
133
134
135
# File 'lib/ironfan/broker/computer.rb', line 129

def chef_client_script_content
  cloud = server.selected_cloud
  return @chef_client_script_content if @chef_client_script_content
  return unless cloud.chef_client_script
  script_filename = File.expand_path("../../../config/#{cloud.chef_client_script}", File.dirname(File.realdirpath(__FILE__)))
  @chef_client_script_content = Ironfan.safely{ File.read(script_filename) }
end

#chef_providerObject


103
# File 'lib/ironfan/broker/computer.rb', line 103

def chef_provider ; providers[:chef] ; end

#chosen_providers(options = {}) ⇒ Object

Utility


109
110
111
112
113
114
# File 'lib/ironfan/broker/computer.rb', line 109

def chosen_providers(options={})
  case options[:providers]
  when :all,nil   then    [:chef,:iaas]
  else                    [ (options[:providers]) ]
  end
end

#chosen_resources(options = {}) ⇒ Object


116
117
118
119
120
# File 'lib/ironfan/broker/computer.rb', line 116

def chosen_resources(options={})
  chosen_providers(options).map do |name|
    providers[name].resources
  end.flatten
end

#clientObject

client


184
185
186
# File 'lib/ironfan/broker/computer.rb', line 184

def client
  self[:client]
end

#client?Boolean

Returns:

  • (Boolean)

220
221
222
# File 'lib/ironfan/broker/computer.rb', line 220

def client?
  not client.nil?
end

#correlateObject

Discovery


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ironfan/broker/computer.rb', line 36

def correlate
  Chef::Log.info chosen_resources
  chosen_resources.each do |res|
    res.expected_ids(self).each do |id|
      next unless res.recall? id

      recalled =  res.recall id
      recalled.users << self

      target  = res.resource_type.to_s
      target += "__#{id}" if res.multiple?
      self[target.to_sym] = recalled

      recalled.on_correlate(self)
    end
  end
end

#created?Boolean

Returns:

  • (Boolean)

223
224
225
# File 'lib/ironfan/broker/computer.rb', line 223

def created?
  machine? && machine.created?
end

#display_boolean(value) ⇒ Object


164
# File 'lib/ironfan/broker/computer.rb', line 164

def display_boolean(value)        value ? "yes" : "no";   end

#dns_nameObject


198
# File 'lib/ironfan/broker/computer.rb', line 198

def dns_name         ; machine? ? machine.dns_name : nil ; end

#ensure_dependenciesObject


122
123
124
125
126
127
# File 'lib/ironfan/broker/computer.rb', line 122

def ensure_dependencies
  chosen_resources.each do |res|
    # ensure_resource res unless res < Ironfan::IaasProvider::Machine
    res.create! self unless res < Ironfan::IaasProvider::Machine
  end
end

#environmentObject


179
# File 'lib/ironfan/broker/computer.rb', line 179

def environment ; server && server.environment ; end

#iaas_providerObject


104
# File 'lib/ironfan/broker/computer.rb', line 104

def iaas_provider ; providers[:iaas] ; end

#keypairObject


201
202
203
# File 'lib/ironfan/broker/computer.rb', line 201

def keypair
  resources[:keypair]
end

#kill(options = {}) ⇒ Object

Manipulation


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ironfan/broker/computer.rb', line 62

def kill(options={})
  target_resources = chosen_resources(options)
  resources.each do |res|
    next unless target_resources.include? res.class
    descriptor = "#{res.class.resource_type} named #{res.name}"
    if res.shared?
      Chef::Log.debug("Not killing shared resource #{descriptor}")
    else
      Ironfan.step(self.name, "Killing #{descriptor}")
      res.destroy
    end
  end
end

#killable?Boolean

Returns:

  • (Boolean)

229
230
231
# File 'lib/ironfan/broker/computer.rb', line 229

def killable?
  (not bogus?) && (not permanent?) && (node? || client? || created?)
end

#launchObject


76
77
78
79
80
81
82
83
84
85
# File 'lib/ironfan/broker/computer.rb', line 76

def launch
  ensure_dependencies
  iaas_provider.machine_class.create! self
  node.announce_state :started
  server.to_machine_manifest.components.each do |component|
    component.announce_to(node)
  end
  save
  self
end

#launchable?Boolean

Returns:

  • (Boolean)

232
233
234
# File 'lib/ironfan/broker/computer.rb', line 232

def launchable?
  (not bogus?) && (not created?)
end

#machineObject

Machine


192
193
194
# File 'lib/ironfan/broker/computer.rb', line 192

def machine
  self[:machine]
end

#machine=(value) ⇒ Object


195
196
197
# File 'lib/ironfan/broker/computer.rb', line 195

def machine=(value)
  self[:machine] = value
end

#machine?Boolean

Returns:

  • (Boolean)

226
227
228
# File 'lib/ironfan/broker/computer.rb', line 226

def machine?
  not machine.nil?
end

#nameObject


27
28
29
30
31
# File 'lib/ironfan/broker/computer.rb', line 27

def name
  return server.full_name if server?
  return @name            if @name
  "unnamed:#{object_id}"
end

#nodeObject


172
173
174
# File 'lib/ironfan/broker/computer.rb', line 172

def node
  self[:node]
end

#node=(value) ⇒ Object


175
176
177
# File 'lib/ironfan/broker/computer.rb', line 175

def node=(value)
  self[:node]= value
end

#node?Boolean

Returns:

  • (Boolean)

235
236
237
# File 'lib/ironfan/broker/computer.rb', line 235

def node?
  not node.nil?
end

#permanent?Boolean

Returns:

  • (Boolean)

238
239
240
241
242
# File 'lib/ironfan/broker/computer.rb', line 238

def permanent?
  return false unless server?
  return false unless server.selected_cloud.respond_to? :permanent
  [true, :true, 'true'].include? server.selected_cloud.permanent
end

#private_keyObject


187
# File 'lib/ironfan/broker/computer.rb', line 187

def private_key ; client? ? client.private_key : nil ; end

#receive_providers(objs) ⇒ Object


259
260
261
262
263
264
265
# File 'lib/ironfan/broker/computer.rb', line 259

def receive_providers(objs)
  objs = objs.map do |obj|
    if obj.is_a?(String) then obj = Gorillib::Inflector.constantize(Gorillib::Inflector.camelize(obj.gsub(/\./, '/'))) ; end
    obj
  end
  super(objs)
end

#running?Boolean

Returns:

  • (Boolean)

243
244
245
# File 'lib/ironfan/broker/computer.rb', line 243

def running?
  machine? && machine.running?
end

#save(options = {}) ⇒ Object


99
100
101
# File 'lib/ironfan/broker/computer.rb', line 99

def save(options={})
  chosen_resources(options).each {|res| res.save! self}
end

#server?Boolean

Returns:

  • (Boolean)

246
247
248
# File 'lib/ironfan/broker/computer.rb', line 246

def server?
  not server.nil?
end

#ssh_identity_fileObject


207
208
209
210
211
212
# File 'lib/ironfan/broker/computer.rb', line 207

def ssh_identity_file
  if    keypair                         then keypair.key_filename
  elsif server && server.selected_cloud then server.selected_cloud.ssh_identity_file(self)
  else nil
  end
end

#ssh_userObject


205
# File 'lib/ironfan/broker/computer.rb', line 205

def ssh_user         ; (server && server.selected_cloud) ? server.selected_cloud.ssh_user : nil ; end

#sshable?Boolean

Returns true if machine is likely to be reachable by ssh.

Returns:

  • (Boolean)

    true if machine is likely to be reachable by ssh


254
255
256
257
# File 'lib/ironfan/broker/computer.rb', line 254

def sshable?  # if there's any hope of success, give it a try.

  running? || node?
end

#startObject


92
93
94
95
96
97
# File 'lib/ironfan/broker/computer.rb', line 92

def start
  ensure_dependencies
  machine.start
  node.announce_state :started
  save
end

#stopObject


87
88
89
90
# File 'lib/ironfan/broker/computer.rb', line 87

def stop
  machine.stop
  node.announce_state :stopped
end

#stopped?Boolean

Returns:

  • (Boolean)

249
250
251
# File 'lib/ironfan/broker/computer.rb', line 249

def stopped?
  machine? && machine.stopped?
end

#to_display(style, values = {}) ⇒ Object

Display


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ironfan/broker/computer.rb', line 140

def to_display(style,values={})
  unless [:minimal,:default,:expanded].include? style
    raise "Bad display style #{style}"
  end

  values["Name"] =        name  # We expect these to be over-ridden by the contained classes

  values["Chef?"] =       "no"
  values["State"] =       "not running"

  [ server, node, machine ].compact.each do |part|
    part.to_display style, values
  end

  if style == :expanded
    values["Startable"]  = display_boolean(stopped?)
    values["Launchable"] = display_boolean(launchable?)
  end
  values["Bogus"] =       bogus.join(',')

  # Only show values that actually have something to show
  values.delete_if {|k,v| v.to_s.empty?}
  values
end

#to_sObject


267
268
269
# File 'lib/ironfan/broker/computer.rb', line 267

def to_s
  "<#{self.class}(server=#{server}, resources=#{resources && resources.inspect_compact}, providers=#{providers && providers.inspect_compact})>"
end

#validateObject


54
55
56
57
# File 'lib/ironfan/broker/computer.rb', line 54

def validate
  computer = self
  chosen_resources.each {|r| r.validate_computer! computer }
end