Class: Skewer::Rackspace::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/rackspace/node.rb

Overview

Build out a Rackspace node using Fog.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flavor = 1, image = 112, name = 'my_server', instance = nil) ⇒ Node

By default, boot an Ubuntu 10.04 LTS (lucid) server.



13
14
15
16
17
18
19
# File 'lib/rackspace/node.rb', line 13

def initialize(flavor = 1, image = 112, name = 'my_server', instance = nil)
  region = SkewerConfig.get('region')
  connection = self.class.find_service(region)

  # Get our SSH key to attach it to the server.
  instance ? @node = instance : @node = build(connection, flavor, image, name)
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



10
11
12
# File 'lib/rackspace/node.rb', line 10

def node
  @node
end

Class Method Details

.find_by_ip(ip_address, service = self.find_service()) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/rackspace/node.rb', line 61

def self.find_by_ip(ip_address, service = self.find_service())
  node = service.servers.select { |server| server.public_ip_address == ip_address }
  if node.size > 0
    return self.new(nil, nil, nil, node[0])
  else
    return false
  end
end

.find_service(short_region = 'usa') ⇒ Object



21
22
23
# File 'lib/rackspace/node.rb', line 21

def self.find_service(short_region = 'usa')
  Skewer::Rackspace::Service.new(short_region).build
end

Instance Method Details

#build(connection, flavor, image, name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rackspace/node.rb', line 25

def build(connection, flavor, image, name)
  key = find_key()

  images = Skewer::Rackspace::Images.new
  image_id = images.get_id(image)
  options = {
    :flavor_id  => flavor,
    :image_id   => image_id,
    :name       => name,
    :public_key => key
  }
  begin
    connection.servers.bootstrap(options)
  rescue Fog::Compute::Rackspace::NotFound
    raise "Sorry, it looks like there's no such Image Id as #{image_id}"
  end
end

#destroyObject



57
58
59
# File 'lib/rackspace/node.rb', line 57

def destroy
  @node.destroy unless @node.nil?
end

#find_keyObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rackspace/node.rb', line 43

def find_key
  ssh_key = nil
  ['id_rsa.pub', 'id_dsa.pub', "#{SkewerConfig.instance.get(:key_name)}.pub"].each do |key|
    key_path =  File.expand_path(File.join(ENV['HOME'],'.ssh', key))
    if File.exist?(key_path)
      ssh_key = key_path
      break
    end
  end

  raise "Couldn't find a public key" unless ssh_key
  File.read(ssh_key)
end