fog-libvirt 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16a2da13c5fbd9d2c90b771de515227de201df13
4
- data.tar.gz: 09ea82a8f5f472158821bc8190e07018ba00cf00
3
+ metadata.gz: 2d00b54cddae7049cf3de22c6ba3b0b64b8a0a7f
4
+ data.tar.gz: a8c5e11a8d0e93f0f3a27141c4684d22f5e9fed2
5
5
  SHA512:
6
- metadata.gz: feab17cc3360f3b1392e4b634406d2d455ab552737a197c2bc421dd2e3f246d9747b5beb665d47887d8c6111e5eae4c8fe6fa0fc8d18ba309ad3cdfc21643999
7
- data.tar.gz: 6282c89565f040f88c6ae831a883dd47337eb898cdfaf60547edbd9408c95923cff6c3173c99ba26a3555594978f232f69f090c47b08e968a1cea1f5f7f87f88
6
+ metadata.gz: 78b727f4016fcbf9d6794378298666d04495dd7477f475c7fda1b594eb28c3f68dd7b5299c9c67f7cc182d0762690eca55406d115d2cac1331f605d8b84d6a66
7
+ data.tar.gz: 55f21716980e467ce27c1518998665c80eb4afbd74c4b47acb8d6afc682f0dc0485680511505425ab3aa469e812e5840ef28ef7529ac1ae8a8f7bcf19385b0fb
data/Rakefile CHANGED
@@ -34,9 +34,14 @@ end
34
34
  #############################################################################
35
35
 
36
36
  GEM_NAME = "#{name}"
37
- task :default => :test
38
-
39
- Rake::TestTask.new do |t|
37
+ task :default => [:test, :minitest]
38
+
39
+ Rake::TestTask.new(:minitest) do |t|
40
+ t.libs << '.'
41
+ t.libs << 'lib'
42
+ t.libs << 'minitests'
43
+ t.test_files = Dir.glob('minitests/**/*_test.rb')
44
+ t.verbose = true
40
45
  end
41
46
 
42
47
  desc 'Run tests'
data/fog-libvirt.gemspec CHANGED
@@ -27,6 +27,10 @@ Gem::Specification.new do |s|
27
27
  s.add_dependency("fog-json")
28
28
  s.add_dependency("fog-xml", "~> 0.1.1")
29
29
  s.add_dependency('ruby-libvirt','~> 0.5.0')
30
+ s.add_dependency('mime-types','< 2.0') if RUBY_VERSION < '1.9'
31
+ s.add_dependency('nokogiri', '< 1.6') if RUBY_VERSION < '1.9'
32
+ s.add_dependency('octokit', '< 3.0') if RUBY_VERSION < '1.9'
33
+ s.add_dependency('rest-client', '<= 1.7.0') if RUBY_VERSION < '1.9'
30
34
 
31
35
  # Fedora and derivates need explicit require
32
36
  s.add_dependency("json")
@@ -38,9 +42,8 @@ Gem::Specification.new do |s|
38
42
  s.add_development_dependency("rubocop") if RUBY_VERSION > "1.9"
39
43
  s.add_development_dependency("shindo", "~> 0.3.4")
40
44
  s.add_development_dependency("simplecov")
41
- s.add_development_dependency("thor")
42
45
  s.add_development_dependency("yard")
43
- s.add_development_dependency("redcarpet")
46
+ s.add_development_dependency("mocha", "~> 1.1.0")
44
47
 
45
48
  # Let's not ship dot files and gemfiles
46
49
  git_files = `git ls-files`.split("\n")
@@ -36,6 +36,7 @@ module Fog
36
36
  request :list_volumes
37
37
  request :volume_action
38
38
  request :create_volume
39
+ request :upload_volume
39
40
  request :clone_volume
40
41
  request :list_networks
41
42
  request :destroy_network
@@ -1,6 +1,7 @@
1
1
  require 'fog/compute/models/server'
2
2
  require 'fog/libvirt/models/compute/util/util'
3
3
  require 'net/ssh/proxy/command'
4
+ require 'fileutils'
4
5
 
5
6
  module Fog
6
7
  module Compute
@@ -36,6 +37,7 @@ module Fog
36
37
  attr_accessor :network_interface_type ,:network_nat_network, :network_bridge_name
37
38
  attr_accessor :volume_format_type, :volume_allocation,:volume_capacity, :volume_name, :volume_pool_name, :volume_template_name, :volume_path
38
39
  attr_accessor :password
40
+ attr_accessor :user_data
39
41
 
40
42
  # Can be created by passing in :xml => "<xml to create domain/server>"
41
43
  # or by providing :template_options => {
@@ -48,6 +50,7 @@ module Fog
48
50
  super defaults.merge(attributes)
49
51
  initialize_nics
50
52
  initialize_volumes
53
+ @user_data = attributes.delete(:user_data)
51
54
  end
52
55
 
53
56
  def new?
@@ -57,6 +60,7 @@ module Fog
57
60
  def save
58
61
  raise Fog::Errors::Error.new('Saving an existing server may create a duplicate') unless new?
59
62
  create_or_clone_volume unless xml or @volumes
63
+ create_user_data_iso if user_data
60
64
  @xml ||= to_xml
61
65
  self.id = (persistent ? service.define_domain(xml) : service.create_domain(xml)).uuid
62
66
  reload
@@ -215,6 +219,35 @@ module Fog
215
219
  display[:port]
216
220
  end
217
221
 
222
+ def generate_config_iso(user_data, &blk)
223
+ Dir.mktmpdir('config') do |wd|
224
+ generate_config_iso_in_dir(wd, user_data, &blk)
225
+ end
226
+ end
227
+
228
+ def generate_config_iso_in_dir(dir_path, user_data, &blk)
229
+ FileUtils.touch(File.join(dir_path, "meta-data"))
230
+ File.open(File.join(dir_path, 'user-data'), 'w') { |f| f.write user_data }
231
+
232
+ isofile = Tempfile.new(['init', '.iso']).path
233
+ unless system("genisoimage -output #{isofile} -volid cidata -joliet -rock #{File.join(dir_path, 'user-data')} #{File.join(dir_path, 'meta-data')}")
234
+ raise Fog::Errors::Error("Couldn't generate cloud-init iso disk.")
235
+ end
236
+ blk.call(isofile)
237
+ end
238
+
239
+ def create_user_data_iso
240
+ generate_config_iso(user_data) do |iso|
241
+ vol = service.volumes.create(:name => cloud_init_volume_name, :capacity => "#{File.size(iso)}b", :allocation => "0G")
242
+ vol.upload_image(iso)
243
+ @iso_file = cloud_init_volume_name
244
+ end
245
+ end
246
+
247
+ def cloud_init_volume_name
248
+ "#{name}-cloud-init.iso"
249
+ end
250
+
218
251
  private
219
252
  attr_accessor :volumes_path
220
253
 
@@ -71,6 +71,11 @@ module Fog
71
71
  new_volume.reload
72
72
  end
73
73
 
74
+ def upload_image(file_path)
75
+ requires :pool_name
76
+ service.upload_volume(pool_name, name, file_path)
77
+ end
78
+
74
79
  private
75
80
 
76
81
  def image_suffix
@@ -0,0 +1,31 @@
1
+ module Fog
2
+ module Compute
3
+ class Libvirt
4
+ class Real
5
+ def upload_volume(pool_name, volume_name, file_path)
6
+ volume = client.lookup_storage_pool_by_name(pool_name).lookup_volume_by_name(volume_name)
7
+ stream = client.stream
8
+
9
+ image_file = File.open(file_path, "rb")
10
+ volume.upload(stream, 0, image_file.size)
11
+ stream.sendall do |_opaque, n|
12
+ begin
13
+ r = image_file.read(n)
14
+ [r.length < n ? 0 : r.length, r]
15
+ rescue Exception => e
16
+ [-1, ""]
17
+ end
18
+ end
19
+ stream.finish
20
+ ensure
21
+ image_file.close if image_file
22
+ end
23
+ end
24
+
25
+ class Mock
26
+ def upload_volume(pool_name, volume_name, file_path)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  module Fog
2
2
  module Libvirt
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
@@ -0,0 +1,69 @@
1
+ require 'test_helper'
2
+
3
+ class UserDataIsoTest < Minitest::Test
4
+ def setup
5
+ @compute = Fog::Compute[:libvirt]
6
+ @server = @compute.servers.new(:name => "test")
7
+ @test_data = "test data"
8
+ end
9
+
10
+ def test_contains_meta_data_file
11
+ @server.stubs(:system).returns(true)
12
+ in_a_temp_dir do |d|
13
+ @server.generate_config_iso_in_dir(d, @test_data) {|iso| assert File.exist?(File.join(d, 'meta-data')) }
14
+ end
15
+ end
16
+
17
+ def test_contains_user_data_file
18
+ @server.stubs(:system).returns(true)
19
+ in_a_temp_dir do |d|
20
+ @server.generate_config_iso_in_dir(d, @test_data) do |iso|
21
+ assert File.exist?(File.join(d, 'user-data'))
22
+ assert_equal @test_data, File.read(File.join(d, 'user-data'))
23
+ end
24
+ end
25
+ end
26
+
27
+ def test_iso_is_generated
28
+ in_a_temp_dir do |d|
29
+ @server.expects(:system).with(regexp_matches(/^genisoimage/)).returns(true)
30
+ @server.generate_config_iso_in_dir(d, @test_data) {|iso| }
31
+ end
32
+ end
33
+
34
+ def test_volume_is_created_during_user_data_iso_generation
35
+ iso_path = "iso_file_path"
36
+ @server.stubs(:system).returns(true)
37
+ Fog::Compute::Libvirt::Volumes.any_instance.expects(:create).
38
+ with(has_entries(:name => @server.cloud_init_volume_name)).
39
+ returns(@compute.volumes.new)
40
+ Fog::Compute::Libvirt::Volume.any_instance.stubs(:upload_image)
41
+
42
+ @server.create_user_data_iso
43
+ end
44
+
45
+ def test_volume_is_uploaded_during_user_data_iso_generation
46
+ iso_path = "iso_file_path"
47
+ @server.stubs(:system).returns(true)
48
+ Fog::Compute::Libvirt::Volumes.any_instance.stubs(:create).returns(@compute.volumes.new)
49
+ Fog::Compute::Libvirt::Volume.any_instance.expects(:upload_image).returns(true)
50
+
51
+ @server.create_user_data_iso
52
+ end
53
+
54
+ def test_iso_file_is_set_during_user_data_iso_generation
55
+ iso_path = "iso_file_path"
56
+ @server.stubs(:system).returns(true)
57
+ Fog::Compute::Libvirt::Volumes.any_instance.stubs(:create).returns(@compute.volumes.new)
58
+ Fog::Compute::Libvirt::Volume.any_instance.stubs(:upload_image)
59
+
60
+ @server.create_user_data_iso
61
+ assert_equal @server.cloud_init_volume_name, @server.iso_file
62
+ end
63
+
64
+ def in_a_temp_dir
65
+ Dir.mktmpdir('test-dir') do |d|
66
+ yield d
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,18 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/mini_test'
3
+ require 'fileutils'
4
+
5
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
6
+
7
+ logdir = File.join(File.dirname(__FILE__), '..', 'logs')
8
+ FileUtils.mkdir_p(logdir) unless File.exist?(logdir)
9
+
10
+ ENV['TMPDIR'] = 'test/tmp'
11
+ FileUtils.rm_f Dir.glob 'test/tmp/*.tmp'
12
+
13
+ require 'fog/libvirt'
14
+
15
+ Fog.mock!
16
+ Fog.credentials = {
17
+ :libvirt_uri => 'qemu://libvirt/system',
18
+ }.merge(Fog.credentials)
@@ -54,5 +54,4 @@ Shindo.tests('Fog::Compute[:libvirt] | server model', ['libvirt']) do
54
54
  end
55
55
  test('be a kind of Fog::Compute::Libvirt::Server') { server.kind_of? Fog::Compute::Libvirt::Server }
56
56
  end
57
-
58
57
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fog-libvirt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - geemus (Wesley Beary)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-13 00:00:00.000000000 Z
11
+ date: 2015-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fog-core
@@ -184,20 +184,6 @@ dependencies:
184
184
  - - ">="
185
185
  - !ruby/object:Gem::Version
186
186
  version: '0'
187
- - !ruby/object:Gem::Dependency
188
- name: thor
189
- requirement: !ruby/object:Gem::Requirement
190
- requirements:
191
- - - ">="
192
- - !ruby/object:Gem::Version
193
- version: '0'
194
- type: :development
195
- prerelease: false
196
- version_requirements: !ruby/object:Gem::Requirement
197
- requirements:
198
- - - ">="
199
- - !ruby/object:Gem::Version
200
- version: '0'
201
187
  - !ruby/object:Gem::Dependency
202
188
  name: yard
203
189
  requirement: !ruby/object:Gem::Requirement
@@ -213,19 +199,19 @@ dependencies:
213
199
  - !ruby/object:Gem::Version
214
200
  version: '0'
215
201
  - !ruby/object:Gem::Dependency
216
- name: redcarpet
202
+ name: mocha
217
203
  requirement: !ruby/object:Gem::Requirement
218
204
  requirements:
219
- - - ">="
205
+ - - "~>"
220
206
  - !ruby/object:Gem::Version
221
- version: '0'
207
+ version: 1.1.0
222
208
  type: :development
223
209
  prerelease: false
224
210
  version_requirements: !ruby/object:Gem::Requirement
225
211
  requirements:
226
- - - ">="
212
+ - - "~>"
227
213
  - !ruby/object:Gem::Version
228
- version: '0'
214
+ version: 1.1.0
229
215
  description: This library can be used as a module for 'fog' or as standalone libvirt
230
216
  provider.
231
217
  email: geemus@gmail.com
@@ -281,9 +267,12 @@ files:
281
267
  - lib/fog/libvirt/requests/compute/mock_files/domain.xml
282
268
  - lib/fog/libvirt/requests/compute/pool_action.rb
283
269
  - lib/fog/libvirt/requests/compute/update_display.rb
270
+ - lib/fog/libvirt/requests/compute/upload_volume.rb
284
271
  - lib/fog/libvirt/requests/compute/vm_action.rb
285
272
  - lib/fog/libvirt/requests/compute/volume_action.rb
286
273
  - lib/fog/libvirt/version.rb
274
+ - minitests/server/user_data_iso_test.rb
275
+ - minitests/test_helper.rb
287
276
  - tests/helper.rb
288
277
  - tests/helpers/formats_helper.rb
289
278
  - tests/helpers/formats_helper_tests.rb