openstack-compute 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
@@ -34,6 +34,7 @@ module Compute
34
34
  require 'compute/flavor'
35
35
  require 'compute/exception'
36
36
  require 'compute/address'
37
+ require 'compute/personalities'
37
38
 
38
39
  # Constants that set limits on server creation
39
40
  MAX_PERSONALITY_ITEMS = 5
@@ -7,7 +7,7 @@ module Compute
7
7
  if index.class == Symbol then
8
8
  self.each do |address|
9
9
  if address.label == index.to_s then
10
- addresses << address.address
10
+ addresses << address
11
11
  end
12
12
  end
13
13
  addresses
@@ -203,7 +203,7 @@ module Compute
203
203
  # => "NewServerSHMGpvI"
204
204
  def create_server(options)
205
205
  raise OpenStack::Compute::Exception::MissingArgument, "Server name, flavorRef, and imageRef, must be supplied" unless (options[:name] && options[:flavorRef] && options[:imageRef])
206
- options[:personality] = get_personality(options[:personality])
206
+ options[:personality] = Personalities.get_personality(options[:personality])
207
207
  data = JSON.generate(:server => options)
208
208
  response = csreq("POST",svrmgmthost,"#{svrmgmtpath}/servers",svrmgmtport,svrmgmtscheme,{'content-type' => 'application/json'},data)
209
209
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
@@ -330,23 +330,6 @@ module Compute
330
330
  end
331
331
  end
332
332
 
333
- # Handles parsing the Personality hash to load it up with Base64-encoded data.
334
- def get_personality(options)
335
- return if options.nil?
336
- require 'base64'
337
- data = []
338
- itemcount = 0
339
- options.each do |localpath,svrpath|
340
- raise OpenStack::Compute::Exception::TooManyPersonalityItems, "Personality files are limited to a total of #{MAX_PERSONALITY_ITEMS} items" if itemcount >= MAX_PERSONALITY_ITEMS
341
- raise OpenStack::Compute::Exception::PersonalityFilePathTooLong, "Server-side path of #{svrpath} exceeds the maximum length of #{MAX_SERVER_PATH_LENGTH} characters" if svrpath.size > MAX_SERVER_PATH_LENGTH
342
- raise OpenStack::Compute::Exception::PersonalityFileTooLarge, "Local file #{localpath} exceeds the maximum size of #{MAX_PERSONALITY_FILE_SIZE} bytes" if File.size(localpath) > MAX_PERSONALITY_FILE_SIZE
343
- b64 = Base64.encode64(IO.read(localpath))
344
- data.push({:path => svrpath, :contents => b64})
345
- itemcount += 1
346
- end
347
- data
348
- end
349
-
350
333
  end
351
334
  end
352
335
  end
@@ -28,7 +28,6 @@ module Compute
28
28
  def initialize(connection,id)
29
29
  @id = id
30
30
  @connection = connection
31
- @metadata = OpenStack::Compute::ImageMetadata.new(connection, id)
32
31
  populate
33
32
  end
34
33
 
@@ -38,7 +37,8 @@ module Compute
38
37
  # >> image.populate
39
38
  # => true
40
39
  def populate
41
- response = @connection.csreq("GET",@connection.svrmgmthost,"#{@connection.svrmgmtpath}/images/#{URI.escape(self.id.to_s)}",@connection.svrmgmtport,@connection.svrmgmtscheme)
40
+ path = "/images/#{URI.escape(self.id.to_s)}"
41
+ response = @connection.req("GET", path)
42
42
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
43
43
  data = JSON.parse(response.body)['image']
44
44
  @id = data['id']
@@ -48,6 +48,7 @@ module Compute
48
48
  @updated = DateTime.parse(data['updated'])
49
49
  end
50
50
  @created = DateTime.parse(data['created'])
51
+ @metadata = OpenStack::Compute::Metadata.new(@connection, path, data['metadata'])
51
52
  @status = data['status']
52
53
  @minDisk = data['minDisk']
53
54
  @minRam = data['minRam']
@@ -1,51 +1,110 @@
1
1
  module OpenStack
2
2
  module Compute
3
3
 
4
- class AbstractMetadata
4
+ class Metadata
5
5
 
6
- def initialize(connection, server_id)
6
+ def initialize(connection, parent_url, metadata=nil)
7
7
  @connection = connection
8
- @server_id = server_id
8
+ @base_url = "#{parent_url}/metadata"
9
+ @metadata = metadata
9
10
  end
10
11
 
11
- def get_item(key)
12
- response = @connection.req('GET', "#{@base_url}/#{key}")
13
- return JSON.parse(response.body)['meta'][key]
12
+ def [](key)
13
+ refresh if @metadata.nil?
14
+ @metadata[key]
14
15
  end
15
16
 
16
- def set_item(key, value)
17
- json = JSON.generate(:meta => { key => value })
18
- @connection.req('PUT', "#{@base_url}/#{key}", :data => json)
17
+ def []=(key, value)
18
+ @metadata = {} if @metadata.nil?
19
+ @metadata[key] = value
19
20
  end
20
21
 
21
- def delete_item(key)
22
- @connection.req('DELETE', "#{@base_url}/#{key}")
22
+ def store(key, value)
23
+ @metadata = {} if @metadata.nil?
24
+ @metadata[key] = value
23
25
  end
24
26
 
25
- def get_data()
26
- response = @connection.req('GET', @base_url)
27
- return JSON.parse(response.body)['metadata']
27
+ def each_pair
28
+ @metadata = {} if @metadata.nil?
29
+ @metadata.each_pair do |k,v|
30
+ yield k, v
31
+ end
28
32
  end
29
33
 
30
- def set_data(data = {})
31
- json = JSON.generate(:metadata => data)
32
- @connection.req('POST', @base_url, :data => json)
34
+ def each
35
+ refresh if @metadata.nil?
36
+ @metadata.each
33
37
  end
34
38
 
35
- end
39
+ def save
40
+ return if @metadata.nil?
41
+ json = JSON.generate(:metadata => @metadata)
42
+ response = @connection.req('PUT', @base_url, :data => json)
43
+ @metadata = JSON.parse(response.body)['metadata']
44
+ end
36
45
 
37
- class ServerMetadata < AbstractMetadata
38
- def initialize(connection, server_id)
39
- super(connection, server_id)
40
- @base_url = "/servers/#{@server_id}/metadata"
46
+ def update(keys=nil)
47
+ return if @metadata.nil?
48
+ if keys.nil?
49
+ json = JSON.generate(:metadata => @metadata)
50
+ response = @connection.req('POST', @base_url, :data => json)
51
+ @metadata = JSON.parse(response.body)['metadata']
52
+ else
53
+ keys.each { |key|
54
+ next if not @metadata.has_key?(key)
55
+ json = JSON.generate(:meta => { key => @metadata[key] })
56
+ @connection.req('PUT', "#{@base_url}/#{key}", :data => json)
57
+ }
58
+ end
59
+ end
60
+
61
+ def refresh(keys=nil)
62
+ if keys.nil?
63
+ response = @connection.req('GET', @base_url)
64
+ @metadata = JSON.parse(response.body)['metadata']
65
+ else
66
+ @metadata = {} if @metadata == nil
67
+ keys.each { |key|
68
+ response = @connection.req('GET', "#{@base_url}/#{key}")
69
+ next if response.code == "404"
70
+ meta = JSON.parse(response.body)['meta']
71
+ meta.each { |k, v| @metadata[k] = v }
72
+ }
73
+ end
74
+ end
75
+
76
+ def delete(keys)
77
+ return if @metadata.nil?
78
+ keys.each { |key|
79
+ @metadata.delete(key)
80
+ }
41
81
  end
42
- end
43
82
 
44
- class ImageMetadata < AbstractMetadata
45
- def initialize(connection, server_id)
46
- super(connection, server_id)
47
- @base_url = "/images/#{@server_id}/metadata"
83
+ def delete!(keys)
84
+ keys.each { |key|
85
+ @connection.req('DELETE', "#{@base_url}/#{key}")
86
+ @metadata.delete(key) if not @metadata.nil?
87
+ }
48
88
  end
89
+
90
+ def clear
91
+ if @metadata.nil?
92
+ @metadata = {}
93
+ else
94
+ @metadata.clear
95
+ end
96
+ end
97
+
98
+ def clear!
99
+ clear
100
+ save
101
+ end
102
+
103
+ def has_key?(key)
104
+ return False if @metadata.nil?
105
+ return @metadata.has_key?(key)
106
+ end
107
+
49
108
  end
50
109
 
51
110
  end
@@ -0,0 +1,23 @@
1
+ module OpenStack
2
+ module Compute
3
+ module Personalities
4
+
5
+ # Handles parsing the Personality hash to load it up with Base64-encoded data.
6
+ def self.get_personality(options)
7
+ return if options.nil?
8
+ require 'base64'
9
+ data = []
10
+ itemcount = 0
11
+ options.each do |localpath,svrpath|
12
+ raise OpenStack::Compute::Exception::TooManyPersonalityItems, "Personality files are limited to a total of #{MAX_PERSONALITY_ITEMS} items" if itemcount >= MAX_PERSONALITY_ITEMS
13
+ raise OpenStack::Compute::Exception::PersonalityFilePathTooLong, "Server-side path of #{svrpath} exceeds the maximum length of #{MAX_SERVER_PATH_LENGTH} characters" if svrpath.size > MAX_SERVER_PATH_LENGTH
14
+ raise OpenStack::Compute::Exception::PersonalityFileTooLarge, "Local file #{localpath} exceeds the maximum size of #{MAX_PERSONALITY_FILE_SIZE} bytes" if File.size(localpath) > MAX_PERSONALITY_FILE_SIZE
15
+ b64 = Base64.encode64(IO.read(localpath))
16
+ data.push({:path => svrpath, :contents => b64})
17
+ itemcount += 1
18
+ end
19
+ data
20
+ end
21
+ end
22
+ end
23
+ end
@@ -45,16 +45,19 @@ module Compute
45
45
  #
46
46
  # >> server.refresh
47
47
  # => true
48
- def populate
49
- response = @connection.csreq("GET",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(@id.to_s)}",@svrmgmtport,@svrmgmtscheme)
50
- OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
51
- data = JSON.parse(response.body)["server"]
48
+ def populate(data=nil)
49
+ path = "/servers/#{URI.encode(@id.to_s)}"
50
+ if data.nil? then
51
+ response = @connection.req("GET", path)
52
+ OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
53
+ data = JSON.parse(response.body)["server"]
54
+ end
52
55
  @id = data["id"]
53
56
  @name = data["name"]
54
57
  @status = data["status"]
55
58
  @progress = data["progress"]
56
59
  @addresses = get_addresses(data["addresses"])
57
- @metadata = OpenStack::Compute::ServerMetadata.new(@connection, @id)
60
+ @metadata = OpenStack::Compute::Metadata.new(@connection, path, data["metadata"])
58
61
  @hostId = data["hostId"]
59
62
  @image = data["image"]
60
63
  @flavor = data["flavor"]
@@ -145,9 +148,13 @@ module Compute
145
148
  # >> server.rebuild!
146
149
  # => true
147
150
  def rebuild!(options)
151
+ options[:personality] = Personalities.get_personality(options[:personality])
148
152
  json = JSON.generate(:rebuild => options)
149
- @connection.req('POST', "/servers/#{@id}/action", :data => json)
150
- self.populate
153
+ response = @connection.req('POST', "/servers/#{@id}/action", :data => json)
154
+ OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
155
+ data = JSON.parse(response.body)['server']
156
+ self.populate(data)
157
+ self.adminPass = data['adminPass']
151
158
  true
152
159
  end
153
160
 
@@ -160,10 +167,10 @@ module Compute
160
167
  # The image is saved as a backup, of which there are only three available slots. If there are no backup slots available,
161
168
  # A OpenStack::Compute::Exception::OpenStackComputeFault will be raised.
162
169
  #
163
- # >> image = server.create_image("My Rails Server")
170
+ # >> image = server.create_image(:name => "My Rails Server")
164
171
  # =>
165
- def create_image(name)
166
- data = JSON.generate(:createImage => {:name => name})
172
+ def create_image(options)
173
+ data = JSON.generate(:createImage => options)
167
174
  response = @connection.csreq("POST",@svrmgmthost,"#{@svrmgmtpath}/servers/#{URI.encode(self.id.to_s)}/action",@svrmgmtport,@svrmgmtscheme,{'content-type' => 'application/json'},data)
168
175
  OpenStack::Compute::Exception.raise_exception(response) unless response.code.match(/^20.$/)
169
176
  image_id = response["Location"].scan(/.*\/(.*)/).flatten
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstack-compute
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 1
9
- - 2
10
- version: 1.1.2
9
+ - 3
10
+ version: 1.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dan Prince
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-19 00:00:00 -04:00
18
+ date: 2011-10-28 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,7 @@ files:
52
52
  - lib/openstack/compute/flavor.rb
53
53
  - lib/openstack/compute/image.rb
54
54
  - lib/openstack/compute/metadata.rb
55
+ - lib/openstack/compute/personalities.rb
55
56
  - lib/openstack/compute/server.rb
56
57
  has_rdoc: true
57
58
  homepage: https://launchpad.net/ruby-openstack-compute