Class: CORL::Machine::Fog

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/fog_machine.rb

Direct Known Subclasses

Aws, Rackspace

Instance Method Summary collapse

Instance Method Details

#close_ssh_sessionObject




305
306
307
# File 'lib/core/plugin/fog_machine.rb', line 305

def close_ssh_session
  Util::SSH.close_session(node.public_ip, node.user)
end

#computeObject



44
45
46
47
# File 'lib/core/plugin/fog_machine.rb', line 44

def compute
  set_connection unless @compute
  @compute
end

#compute=(compute) ⇒ Object




40
41
42
# File 'lib/core/plugin/fog_machine.rb', line 40

def compute=compute
  @compute = compute
end

#create(options = {}) ⇒ Object




138
139
140
141
142
143
144
145
146
# File 'lib/core/plugin/fog_machine.rb', line 138

def create(options = {})
  super do |config|
    if compute
      yield(config) if block_given?
      myself.server = compute.servers.bootstrap(config.export)
    end
    myself.server ? true : false
  end
end

#create_image(options = {}) ⇒ Object




234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/core/plugin/fog_machine.rb', line 234

def create_image(options = {})
  super do |config|
    success = false
    if server
      logger.debug("Imaging machine #{plugin_name}")
      
      image_name = sprintf("%s (%s)", node.plugin_name, Time.now.to_s)
      success    = yield(image_name, config, success) if block_given? # Implement in sub classes
      success    = init_ssh_session(server, true, config.get(:tries, 5), config.get(:sleep_time, 5)) if success
    end
    success
  end
end

#created?Boolean


Checks



13
14
15
# File 'lib/core/plugin/fog_machine.rb', line 13

def created?
  server && ! server.state != 'DELETED'
end

#destroy(options = {}) ⇒ Object




265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/core/plugin/fog_machine.rb', line 265

def destroy(options = {})
  super do |config|
    success = false
    if server
      logger.debug("Destroying machine #{plugin_name}")
      success = server.destroy
      success = yield(config) if success && block_given?
    end
    close_ssh_session if success
    success
  end
end

#download(remote_path, local_path, options = {}) ⇒ Object




150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/core/plugin/fog_machine.rb', line 150

def download(remote_path, local_path, options = {})
  super do |config, success|
    logger.debug("Executing SCP download to #{local_path} from #{remote_path} on machine #{plugin_name}") 
    
    begin
      if init_ssh_session(server)
        Util::SSH.download(node.public_ip, node.user, remote_path, local_path, config.export) do |name, received, total|
          yield(name, received, total) if block_given?
        end
        true
      else
        false
      end
    rescue Exception => error
      ui.error(error.message)
      false
    end
  end  
end

#exec(commands, options = {}) ⇒ Object




194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/core/plugin/fog_machine.rb', line 194

def exec(commands, options = {})
  super do |config, results|
    if commands
      logger.debug("Executing SSH commands ( #{commands.inspect} ) on machine #{plugin_name}")
      
      if init_ssh_session(server)
        results = Util::SSH.exec(node.public_ip, node.user, commands) do |type, command, data|
          yield(type, command, data) if block_given?  
        end
      else
        results = nil
      end
    end
    results
  end
end

#imageObject




112
113
114
115
# File 'lib/core/plugin/fog_machine.rb', line 112

def image
  return server.image.id if server
  nil
end

#imagesObject




105
106
107
108
# File 'lib/core/plugin/fog_machine.rb', line 105

def images
  return compute.images if compute
  []
end

#init_ssh_session(server, reset = false, tries = 5, sleep_secs = 5) ⇒ Object


Utilities



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/core/plugin/fog_machine.rb', line 281

def init_ssh_session(server, reset = false, tries = 5, sleep_secs = 5)
  server.wait_for { ready? }
  
  success = true
      
  begin
    Util::SSH.session(node.public_ip, node.user, node.ssh_port, node.private_key, reset)
          
  rescue Exception => error
    if tries > 1
      sleep(sleep_secs)
      
      tries -= 1
      reset  = true
      retry
    else
      success = false
    end
  end
  success
end

#loadObject




129
130
131
132
133
134
# File 'lib/core/plugin/fog_machine.rb', line 129

def load
  super do
    myself.server = plugin_name if compute && plugin_name
    ! plugin_name && @server.nil? ? false : true
  end    
end

#machine_typeObject




98
99
100
101
# File 'lib/core/plugin/fog_machine.rb', line 98

def machine_type
  return server.flavor.id if server
  nil
end

#machine_typesObject




91
92
93
94
# File 'lib/core/plugin/fog_machine.rb', line 91

def machine_types
  return compute.flavors if compute
  []
end

#private_ipObject




84
85
86
87
# File 'lib/core/plugin/fog_machine.rb', line 84

def private_ip
  return server.private_ip_address if server
  nil
end

#public_ipObject




77
78
79
80
# File 'lib/core/plugin/fog_machine.rb', line 77

def public_ip
  return server.public_ip_address if server
  nil
end

#reload(options = {}) ⇒ Object




221
222
223
224
225
226
227
228
229
230
# File 'lib/core/plugin/fog_machine.rb', line 221

def reload(options = {})
  super do |config|
    success = false
    if server
      success = block_given? ? yield(config) : true            
      success = init_ssh_session(server, true, config.get(:tries, 5), config.get(:sleep_time, 5)) if success
    end
    success
  end
end

#running?Boolean




19
20
21
# File 'lib/core/plugin/fog_machine.rb', line 19

def running?
  created? && server.ready?
end

#serverObject



62
63
64
65
66
# File 'lib/core/plugin/fog_machine.rb', line 62

def server
  compute
  load unless @server
  @server
end

#server=(id) ⇒ Object




51
52
53
54
55
56
57
58
59
60
# File 'lib/core/plugin/fog_machine.rb', line 51

def server=id
  @server = nil
  
  if id.is_a?(String)
    @server = compute.servers.get(id) unless id.empty?
  elsif ! id.nil?
    @server = id
  end    
  init_server
end

#stateObject




70
71
72
73
# File 'lib/core/plugin/fog_machine.rb', line 70

def state
  return translate_state(server.state) if server
  nil
end

#stop(options = {}) ⇒ Object




250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/core/plugin/fog_machine.rb', line 250

def stop(options = {})
  super do |config|
    success = true
    if server && create_image(config)      
      logger.debug("Stopping machine #{plugin_name}")
      success = destroy(config.import({ :stop => true }))
    else
      success = false            
    end
    success
  end
end

#terminal(user, options = {}) ⇒ Object




213
214
215
216
217
# File 'lib/core/plugin/fog_machine.rb', line 213

def terminal(user, options = {})
  super do |config|
    Util::SSH.terminal(node.public_ip, user, config.export)
  end
end

#upload(local_path, remote_path, options = {}) ⇒ Object




172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/core/plugin/fog_machine.rb', line 172

def upload(local_path, remote_path, options = {})
  super do |config, success|
    logger.debug("Executing SCP upload from #{local_path} to #{remote_path} on machine #{plugin_name}") 
    
    begin
      if init_ssh_session(server)
        Util::SSH.upload(node.public_ip, node.user, local_path, remote_path, config.export) do |name, sent, total|
          yield(name, sent, total) if block_given?
        end
        true
      else
        false
      end
    rescue Exception => error
      ui.error(error.message)
      false
    end
  end  
end