class Raca::Servers

Represents a collection of cloud servers within a single region.

There’s currently no methods that relate to the entire collection, this is primarily used to retrieve a single Raca::Server object.

You probably don’t want to instantiate this directly, see Raca::Account#servers

Public Class Methods

new(account, region) click to toggle source
   # File lib/raca/servers.rb
11 def initialize(account, region)
12   @account, @region = account, region
13   @servers_url = @account.public_endpoint("cloudServersOpenStack", region)
14 end

Public Instance Methods

create(server_name, flavor_name, image_name, files = {}) click to toggle source

create a new server on Rackspace.

server_name is a free text name you want to assign the server.

flavor_name is a string that describes the amount of RAM. If you enter an invalid option a list of valid options will be raised.

image_name is a string that describes the OS image to use. If you enter an invalid option a list of valid options will be raised. I suggest starting with ‘Ubuntu 10.04 LTS’

files is an optional Hash of path to blobs. Use it to place a file on the disk of the new server.

Use it like this:

server.create("my-server", 512, "Ubuntu 10.04 LTS", "/root/.ssh/authorised_keys" => File.read("/foo"))
   # File lib/raca/servers.rb
43 def create(server_name, flavor_name, image_name, files = {})
44   request = {
45     "server" => {
46       "name" => server_name,
47       "imageRef" => image_name_to_id(image_name),
48       "flavorRef" => flavor_name_to_id(flavor_name),
49     }
50   }
51   files.each do |path, blob|
52     request['server']['personality'] ||= []
53     request['server']['personality'] << {
54       'path' => path,
55       'contents' => Base64.encode64(blob)
56     }
57   end
58 
59   response = servers_client.post(servers_path, JSON.dump(request), json_headers)
60   data = JSON.parse(response.body)['server']
61   Raca::Server.new(@account, @region, data['id'])
62 end
get(server_name) click to toggle source
   # File lib/raca/servers.rb
16 def get(server_name)
17   server_id = find_server_id(server_name)
18   if server_id
19     Raca::Server.new(@account, @region, server_id)
20   else
21     nil
22   end
23 end
inspect() click to toggle source
   # File lib/raca/servers.rb
64 def inspect
65   "#<Raca::Servers:#{__id__} region=#{@region}>"
66 end

Private Instance Methods

account_path() click to toggle source
   # File lib/raca/servers.rb
81 def account_path
82   @account_path ||= URI.parse(@servers_url).path
83 end
find_server_id(server_name) click to toggle source
    # File lib/raca/servers.rb
102 def find_server_id(server_name)
103   server = list.detect {|row|
104     row["name"] == server_name
105   }
106   server ? server["id"] : nil
107 end
flavor_name_to_id(str) click to toggle source
    # File lib/raca/servers.rb
120 def flavor_name_to_id(str)
121   flavor = flavors.detect {|row|
122     row['name'].downcase.include?(str.to_s.downcase)
123   }
124   if flavor
125     flavor['id']
126   else
127     raise ArgumentError, "valid flavors are: #{flavor_names.join(', ')}"
128   end
129 end
flavor_names() click to toggle source
    # File lib/raca/servers.rb
116 def flavor_names
117   flavors.map {|row| row['name'] }
118 end
flavors() click to toggle source
    # File lib/raca/servers.rb
109 def flavors
110   @flavors ||= begin
111     data = servers_client.get(flavors_path, json_headers).body
112     JSON.parse(data)['flavors']
113   end
114 end
flavors_path() click to toggle source
   # File lib/raca/servers.rb
85 def flavors_path
86   @flavors_path ||= File.join(account_path, "flavors")
87 end
image_name_to_id(str) click to toggle source
    # File lib/raca/servers.rb
142 def image_name_to_id(str)
143   str = str.to_s.downcase
144   image = images.detect { |row|
145     row['name'].downcase == str
146   } || images.detect { |row|
147     row['name'].downcase.include?(str)
148   }
149   if image
150     image['id']
151   else
152     raise ArgumentError, "valid images are: #{image_names.join(', ')}"
153   end
154 end
image_names() click to toggle source
    # File lib/raca/servers.rb
138 def image_names
139   images.map {|row| row['name'] }
140 end
images() click to toggle source
    # File lib/raca/servers.rb
131 def images
132   @images ||= begin
133     data = servers_client.get(images_path, json_headers).body
134     JSON.parse(data)['images']
135   end
136 end
images_path() click to toggle source
   # File lib/raca/servers.rb
89 def images_path
90   @images_path ||= File.join(account_path, "images")
91 end
json_headers() click to toggle source
   # File lib/raca/servers.rb
70 def json_headers
71   {
72     'Content-Type' => 'application/json',
73     'Accept' => 'application/json'
74   }
75 end
list() click to toggle source
    # File lib/raca/servers.rb
 97 def list
 98   json = servers_client.get(servers_path, json_headers).body
 99   JSON.parse(json)['servers']
100 end
log(msg) click to toggle source
    # File lib/raca/servers.rb
160 def log(msg)
161   if defined?(Rails)
162     Rails.logger.info msg
163   else
164     puts msg
165   end
166 end
servers_client() click to toggle source
    # File lib/raca/servers.rb
156 def servers_client
157   @servers_client ||= @account.http_client(servers_host)
158 end
servers_host() click to toggle source
   # File lib/raca/servers.rb
77 def servers_host
78   @servers_host ||= URI.parse(@servers_url).host
79 end
servers_path() click to toggle source
   # File lib/raca/servers.rb
93 def servers_path
94   @servers_path ||= File.join(account_path, "servers")
95 end