fog-libvirt-csem 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTORS.md +24 -0
  3. data/Gemfile +9 -0
  4. data/LICENSE.md +20 -0
  5. data/README.md +83 -0
  6. data/Rakefile +127 -0
  7. data/fog-libvirt-csem.gemspec +49 -0
  8. data/lib/fog/bin/libvirt.rb +58 -0
  9. data/lib/fog/libvirt/compute.rb +140 -0
  10. data/lib/fog/libvirt/models/compute/README.md +92 -0
  11. data/lib/fog/libvirt/models/compute/interface.rb +25 -0
  12. data/lib/fog/libvirt/models/compute/interfaces.rb +20 -0
  13. data/lib/fog/libvirt/models/compute/network.rb +33 -0
  14. data/lib/fog/libvirt/models/compute/networks.rb +20 -0
  15. data/lib/fog/libvirt/models/compute/nic.rb +50 -0
  16. data/lib/fog/libvirt/models/compute/nics.rb +12 -0
  17. data/lib/fog/libvirt/models/compute/node.rb +29 -0
  18. data/lib/fog/libvirt/models/compute/nodes.rb +20 -0
  19. data/lib/fog/libvirt/models/compute/pool.rb +84 -0
  20. data/lib/fog/libvirt/models/compute/pools.rb +20 -0
  21. data/lib/fog/libvirt/models/compute/server.rb +514 -0
  22. data/lib/fog/libvirt/models/compute/servers.rb +21 -0
  23. data/lib/fog/libvirt/models/compute/templates/network.xml.erb +6 -0
  24. data/lib/fog/libvirt/models/compute/templates/pool.xml.erb +6 -0
  25. data/lib/fog/libvirt/models/compute/templates/server.xml.erb +155 -0
  26. data/lib/fog/libvirt/models/compute/templates/volume.xml.erb +30 -0
  27. data/lib/fog/libvirt/models/compute/util/uri.rb +138 -0
  28. data/lib/fog/libvirt/models/compute/util/util.rb +32 -0
  29. data/lib/fog/libvirt/models/compute/volume.rb +129 -0
  30. data/lib/fog/libvirt/models/compute/volumes.rb +20 -0
  31. data/lib/fog/libvirt/requests/compute/clone_volume.rb +18 -0
  32. data/lib/fog/libvirt/requests/compute/create_domain.rb +17 -0
  33. data/lib/fog/libvirt/requests/compute/create_volume.rb +16 -0
  34. data/lib/fog/libvirt/requests/compute/define_domain.rb +17 -0
  35. data/lib/fog/libvirt/requests/compute/define_pool.rb +16 -0
  36. data/lib/fog/libvirt/requests/compute/destroy_interface.rb +18 -0
  37. data/lib/fog/libvirt/requests/compute/destroy_network.rb +17 -0
  38. data/lib/fog/libvirt/requests/compute/dhcp_leases.rb +37 -0
  39. data/lib/fog/libvirt/requests/compute/get_node_info.rb +37 -0
  40. data/lib/fog/libvirt/requests/compute/libversion.rb +18 -0
  41. data/lib/fog/libvirt/requests/compute/list_domains.rb +128 -0
  42. data/lib/fog/libvirt/requests/compute/list_interfaces.rb +57 -0
  43. data/lib/fog/libvirt/requests/compute/list_networks.rb +63 -0
  44. data/lib/fog/libvirt/requests/compute/list_pool_volumes.rb +19 -0
  45. data/lib/fog/libvirt/requests/compute/list_pools.rb +71 -0
  46. data/lib/fog/libvirt/requests/compute/list_volumes.rb +106 -0
  47. data/lib/fog/libvirt/requests/compute/mock_files/domain.xml +40 -0
  48. data/lib/fog/libvirt/requests/compute/pool_action.rb +19 -0
  49. data/lib/fog/libvirt/requests/compute/update_autostart.rb +18 -0
  50. data/lib/fog/libvirt/requests/compute/update_display.rb +36 -0
  51. data/lib/fog/libvirt/requests/compute/upload_volume.rb +31 -0
  52. data/lib/fog/libvirt/requests/compute/vm_action.rb +19 -0
  53. data/lib/fog/libvirt/requests/compute/volume_action.rb +18 -0
  54. data/lib/fog/libvirt/version.rb +5 -0
  55. data/lib/fog/libvirt.rb +16 -0
  56. data/minitests/server/server_test.rb +64 -0
  57. data/minitests/server/user_data_iso_test.rb +77 -0
  58. data/minitests/test_helper.rb +18 -0
  59. data/tests/helper.rb +17 -0
  60. data/tests/helpers/formats_helper.rb +100 -0
  61. data/tests/helpers/formats_helper_tests.rb +107 -0
  62. data/tests/helpers/mock_helper.rb +14 -0
  63. data/tests/helpers/succeeds_helper.rb +9 -0
  64. data/tests/libvirt/compute_tests.rb +19 -0
  65. data/tests/libvirt/models/compute/interface_tests.rb +27 -0
  66. data/tests/libvirt/models/compute/interfaces_tests.rb +14 -0
  67. data/tests/libvirt/models/compute/network_tests.rb +31 -0
  68. data/tests/libvirt/models/compute/networks_tests.rb +13 -0
  69. data/tests/libvirt/models/compute/nic_tests.rb +31 -0
  70. data/tests/libvirt/models/compute/nics_tests.rb +10 -0
  71. data/tests/libvirt/models/compute/pool_tests.rb +27 -0
  72. data/tests/libvirt/models/compute/pools_tests.rb +13 -0
  73. data/tests/libvirt/models/compute/server_tests.rb +63 -0
  74. data/tests/libvirt/models/compute/servers_tests.rb +14 -0
  75. data/tests/libvirt/models/compute/volume_tests.rb +38 -0
  76. data/tests/libvirt/models/compute/volumes_tests.rb +15 -0
  77. data/tests/libvirt/requests/compute/create_domain_tests.rb +21 -0
  78. data/tests/libvirt/requests/compute/define_domain_tests.rb +11 -0
  79. data/tests/libvirt/requests/compute/dhcp_leases_tests.rb +15 -0
  80. data/tests/libvirt/requests/compute/update_autostart_tests.rb +12 -0
  81. data/tests/libvirt/requests/compute/update_display.rb +13 -0
  82. metadata +335 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dfd0bcd881f8bb3ef03f9b584933344a461014c1d0b0c888c44db34f0a2a03e9
4
+ data.tar.gz: ee3b95c80546536c1cfeae8b1df206860081422b35ee41076e15cc179c4798f3
5
+ SHA512:
6
+ metadata.gz: b66669f38b82e91336bb765bcd7178f5ee9246ec961892302fdf52e42a8ef63cd87b98249d0ec1db85f55fe620d244dbedecb081ab41fc97358907b7c157a5fa
7
+ data.tar.gz: 8b62a1cb4b5eb575fe241c75807ad3cda9700b8d8d71ed229bed03c3ca269246baa81a27dba1c88575912ad670cd48383267283cd0715f168d317755410b513e
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,24 @@
1
+ * Amos Benari <abenari@redhat.com>
2
+ * brookemckim <brooke.mckim@gmail.com>
3
+ * Carl Caum <carl@carlcaum.com>
4
+ * Carlos Sanchez <csanchez@maestrodev.com>
5
+ * David Wittman <david@wittman.com>
6
+ * Dominic Cleal <dcleal@redhat.com>
7
+ * Greg Sutcliffe <gsutclif@redhat.com>
8
+ * James Herdman <james.herdman@me.com>
9
+ * Josef Strzibny <strzibny@strzibny.name>
10
+ * Kevin Menard <nirvdrum@gmail.com>
11
+ * Konstantin Haase <konstantin.mailinglists@googlemail.com>
12
+ * Kyle Rames <kyle.rames@rackspace.com>
13
+ * Lance Ivy <lance@cainlevy.net>
14
+ * Ohad Levy <ohadlevy@redhat.com>
15
+ * Patrick Debois <Patrick.Debois@jedi.be>
16
+ * Paul Thornthwaite <tokengeek@gmail.com>
17
+ * Romain Vrignaud <romain@yakaz.com>
18
+ * Ryan Davies <ryan@ryandavies.co.nz>
19
+ * Sergio Rubio <rubiojr@frameos.org>
20
+ * Shlomi Zadok <shlomi@ben-hanna.com>
21
+ * Steve Smith <github@scsworld.co.uk>
22
+ * Vincent Demeester <vincent+git@demeester.fr>
23
+ * Wesley Beary <geemus@gmail.com>
24
+ * Tomer Brisker <tbrisker@redhat.com>
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development, :test do
4
+ # This is here because gemspec doesn't support require: false
5
+ gem "netrc", :require => false
6
+ gem "octokit", :require => false
7
+ end
8
+
9
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2009-2014 [CONTRIBUTORS.md](https://github.com/fog/fog/blob/master/CONTRIBUTORS.md)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Fog::Libvirt
2
+
3
+ fog-libvirt is a libvirt provider for [fog](https://github.com/fog/fog).
4
+
5
+ [![Build Status](https://github.com/fog/fog-libvirt/actions/workflows/ruby.yml/badge.svg)](https://github.com/fog/fog-libvirt/actions/workflows/ruby.yml)
6
+ [![Dependency Status](https://gemnasium.com/fog/fog.png)](https://gemnasium.com/fog/fog-libvirt)
7
+ [![Code Climate](https://codeclimate.com/github/fog/fog.png)](https://codeclimate.com/github/fog/fog-libvirt)
8
+ [![Gem Version](https://fury-badge.herokuapp.com/rb/fog.png)](http://badge.fury.io/rb/fog-libvirt)
9
+ [![Gittip](http://img.shields.io/gittip/geemus.png)](https://www.gittip.com/geemus/)
10
+
11
+ ## Installation
12
+
13
+ fog-libvirt can be used as a module for fog or installed separately as:
14
+
15
+ ```
16
+ $ sudo gem install fog-libvirt
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Example REPL session:
22
+
23
+ ```
24
+ >> require "fog/libvirt"
25
+ => true
26
+ >> compute = Fog::Compute.new(provider: :libvirt, libvirt_uri: "qemu:///session")
27
+ => #<Fog::Libvirt::Compute::Real:46980 @uri=#<Fog::Libvirt::Util::URI:0x0000000002def920 @parsed_uri=#<URI::Generic qemu:/session>, @uri="qemu:///session"...
28
+ >> server = compute.servers.create(name: "test")
29
+ =>
30
+ <Fog::Libvirt::Compute::Server
31
+ id="bbb663e4-723b-4165-bc29-c77b54b12bca",
32
+ cpus=1,
33
+ cputime=0,
34
+ os_type="hvm",
35
+ memory_size=262144,
36
+ max_memory_size=262144,
37
+ name="test",
38
+ arch="x86_64",
39
+ persistent=true,
40
+ domain_type="kvm",
41
+ uuid="bbb663e4-723b-4165-bc29-c77b54b12bca",
42
+ autostart=false,
43
+ nics=[ <Fog::Libvirt::Compute::Nic
44
+ mac="52:54:00:d1:18:23",
45
+ id=nil,
46
+ type="network",
47
+ network="default",
48
+ bridge=nil,
49
+ model="virtio"
50
+ >],
51
+ volumes=[ <Fog::Libvirt::Compute::Volume
52
+ id=nil,
53
+ pool_name="1Download",
54
+ key=nil,
55
+ name="test.img",
56
+ path="/home/lzap/1Download/test.img",
57
+ capacity="10G",
58
+ allocation="1G",
59
+ owner=nil,
60
+ group=nil,
61
+ format_type="raw",
62
+ backing_volume=nil
63
+ >],
64
+ active=false,
65
+ boot_order=["hd", "cdrom", "network"],
66
+ display={:type=>"vnc", :port=>"-1", :listen=>"127.0.0.1"},
67
+ cpu={},
68
+ hugepages=false,
69
+ guest_agent=true,
70
+ virtio_rng={},
71
+ state="shutoff"
72
+ >
73
+ ```
74
+
75
+ See [README.md](https://github.com/fog/fog-libvirt/blob/master/lib/fog/libvirt/models/compute/README.md).
76
+
77
+ ## Contributing
78
+
79
+ Please refer to [CONTRIBUTING.md](https://github.com/fog/fog/blob/master/CONTRIBUTING.md).
80
+
81
+ ## License
82
+
83
+ Please refer to [LICENSE.md](https://github.com/fog/fog-libvirt/blob/master/LICENSE.md).
data/Rakefile ADDED
@@ -0,0 +1,127 @@
1
+ require 'bundler/setup'
2
+ require 'rake/testtask'
3
+ require 'rubygems'
4
+ require 'rubygems/package_task'
5
+ require 'yard'
6
+ require File.dirname(__FILE__) + '/lib/fog/libvirt'
7
+
8
+ #############################################################################
9
+ #
10
+ # Helper functions
11
+ #
12
+ #############################################################################
13
+
14
+ def name
15
+ @name ||= Dir['*.gemspec'].first.split('.').first
16
+ end
17
+
18
+ def version
19
+ Fog::Libvirt::VERSION
20
+ end
21
+
22
+ def gemspec_file
23
+ "#{name}.gemspec"
24
+ end
25
+
26
+ def gem_file
27
+ "#{name}-#{version}.gem"
28
+ end
29
+
30
+ #############################################################################
31
+ #
32
+ # Standard tasks
33
+ #
34
+ #############################################################################
35
+
36
+ GEM_NAME = "#{name}"
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
45
+ end
46
+
47
+ desc 'Run tests'
48
+ task :test do
49
+ mock = ENV['FOG_MOCK'] || 'true'
50
+ sh("export FOG_MOCK=#{mock} && bundle exec shindont tests")
51
+ end
52
+
53
+ desc 'Run mocked tests'
54
+ task :mock do
55
+ sh("export FOG_MOCK=true && bundle exec shindont tests")
56
+ end
57
+
58
+ desc 'Run live tests'
59
+ task :live do
60
+ sh("export FOG_MOCK=false && bundle exec shindont tests")
61
+ end
62
+
63
+ desc "Open an irb session preloaded with this library"
64
+ task :console do
65
+ sh "irb -rubygems -r ./lib/fog/libvirt.rb"
66
+ end
67
+
68
+ #############################################################################
69
+ #
70
+ # Packaging tasks
71
+ #
72
+ #############################################################################
73
+
74
+ task :release => ["release:prepare", "release:publish"]
75
+
76
+ namespace :release do
77
+ task :preflight do
78
+ unless `git branch` =~ /^\* master$/
79
+ puts "You must be on the master branch to release!"
80
+ exit!
81
+ end
82
+ if `git tag` =~ /^\* v#{version}$/
83
+ puts "Tag v#{version} already exists!"
84
+ exit!
85
+ end
86
+ end
87
+
88
+ task :prepare => :preflight do
89
+ Rake::Task[:build].invoke
90
+ sh "gem install pkg/#{name}-#{version}.gem"
91
+ Rake::Task[:git_mark_release].invoke
92
+ end
93
+
94
+ task :publish do
95
+ Rake::Task[:git_push_release].invoke
96
+ Rake::Task[:gem_push].invoke
97
+ end
98
+ end
99
+
100
+ task :git_mark_release do
101
+ sh "git commit --allow-empty -a -m 'Release #{version}'"
102
+ sh "git tag v#{version}"
103
+ end
104
+
105
+ task :git_push_release do
106
+ sh "git push origin master"
107
+ sh "git push origin v#{version}"
108
+ end
109
+
110
+ task :gem_push do
111
+ sh "gem push pkg/#{name}-#{version}.gem"
112
+ end
113
+
114
+ desc "Build #{name}-#{version}.gem"
115
+ task :build do
116
+ sh "mkdir -p pkg"
117
+ sh "gem build #{gemspec_file}"
118
+ sh "mv #{gem_file} pkg"
119
+ end
120
+ task :gem => :build
121
+
122
+ # Include Yard tasks for rake yard
123
+ YARDOC_LOCATION = "doc"
124
+ YARD::Rake::YardocTask.new do |t|
125
+ t.files = ['lib/**/*.rb', "README"]
126
+ t.options = ["--output-dir", YARDOC_LOCATION, "--title", "#{name} #{version}"]
127
+ end
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fog/libvirt/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.specification_version = 2 if s.respond_to? :specification_version=
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+
10
+ s.name = "fog-libvirt-csem"
11
+ s.version = Fog::Libvirt::VERSION
12
+
13
+ s.summary = "Module for the 'fog' gem to support libvirt"
14
+ s.description = "This library can be used as a module for 'fog' or as standalone libvirt provider."
15
+
16
+ s.authors = ["geemus (Wesley Beary)"]
17
+ s.email = "geemus@gmail.com"
18
+ s.homepage = "http://github.com/csem/fog-libvirt"
19
+ s.license = "MIT"
20
+
21
+ s.require_paths = %w[lib]
22
+
23
+ s.rdoc_options = ["--charset=UTF-8"]
24
+ s.extra_rdoc_files = %w[README.md]
25
+
26
+ s.required_ruby_version = '>= 2.5.0'
27
+
28
+ s.add_dependency("fog-core", ">= 1.27.4")
29
+ s.add_dependency("fog-json")
30
+ s.add_dependency("fog-xml", "~> 0.1.1")
31
+ s.add_dependency('ruby-libvirt','>= 0.7.0')
32
+ s.add_dependency("json")
33
+
34
+ s.add_development_dependency("net-ssh")
35
+ s.add_development_dependency("minitest", "~> 5.0")
36
+ s.add_development_dependency("minitest-stub-const")
37
+ s.add_development_dependency("pry")
38
+ s.add_development_dependency("rake")
39
+ s.add_development_dependency("rubocop") if RUBY_VERSION > "2.0"
40
+ s.add_development_dependency("shindo", "~> 0.3.4")
41
+ s.add_development_dependency("simplecov")
42
+ s.add_development_dependency("yard")
43
+ s.add_development_dependency("mocha", "~> 1.13.0")
44
+
45
+ # Let's not ship dot files and gemfiles
46
+ git_files = `git ls-files`.split("\n")
47
+ s.files = git_files.delete_if{ |f| f =~ /^\..*/ || f =~ /^gemfiles\/*/ }
48
+ s.test_files = `git ls-files -- {spec,tests}/*`.split("\n")
49
+ end
@@ -0,0 +1,58 @@
1
+ module Libvirt # deviates from other bin stuff to accommodate gem
2
+ class << self
3
+ def class_for(key)
4
+ case key
5
+ when :compute
6
+ Fog::Libvirt::Compute
7
+ else
8
+ raise ArgumentError, "Unrecognized service: #{key}"
9
+ end
10
+ end
11
+
12
+ def [](service)
13
+ @@connections ||= Hash.new do |hash, key|
14
+ hash[key] = case key
15
+ when :compute
16
+ Fog::Logger.warning("Libvirt[:compute] is not recommended, use Compute[:libvirt] for portability")
17
+ Fog::Compute.new(:provider => 'Libvirt')
18
+ else
19
+ raise ArgumentError, "Unrecognized service: #{key.inspect}"
20
+ end
21
+ end
22
+ @@connections[service]
23
+ end
24
+
25
+ def available?
26
+ begin
27
+ availability=true unless Gem::Specification::find_by_name("ruby-libvirt").nil?
28
+ rescue Gem::LoadError
29
+ availability=false
30
+ rescue
31
+ availability_gem=Gem.available?("ruby-libvirt")
32
+ end
33
+
34
+ if availability
35
+ for service in services
36
+ for collection in self.class_for(service).collections
37
+ unless self.respond_to?(collection)
38
+ self.class_eval <<-EOS, __FILE__, __LINE__
39
+ def self.#{collection}
40
+ self[:#{service}].#{collection}
41
+ end
42
+ EOS
43
+ end
44
+ end
45
+ end
46
+ end
47
+ availability
48
+ end
49
+
50
+ def collections
51
+ services.map {|service| self[service].collections}.flatten.sort_by {|service| service.to_s}
52
+ end
53
+
54
+ def services
55
+ Fog::Libvirt.services
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,140 @@
1
+ require 'fog/libvirt/models/compute/util/util'
2
+ require 'fog/libvirt/models/compute/util/uri'
3
+
4
+ module Fog
5
+ module Libvirt
6
+ class Compute < Fog::Service
7
+ requires :libvirt_uri
8
+ recognizes :libvirt_username, :libvirt_password
9
+ recognizes :libvirt_ip_command
10
+
11
+ model_path 'fog/libvirt/models/compute'
12
+ model :server
13
+ collection :servers
14
+ model :network
15
+ collection :networks
16
+ model :interface
17
+ collection :interfaces
18
+ model :volume
19
+ collection :volumes
20
+ model :pool
21
+ collection :pools
22
+ model :node
23
+ collection :nodes
24
+ model :nic
25
+ collection :nics
26
+
27
+ request_path 'fog/libvirt/requests/compute'
28
+ request :list_domains
29
+ request :create_domain
30
+ request :define_domain
31
+ request :vm_action
32
+ request :list_pools
33
+ request :list_pool_volumes
34
+ request :define_pool
35
+ request :pool_action
36
+ request :list_volumes
37
+ request :volume_action
38
+ request :create_volume
39
+ request :upload_volume
40
+ request :clone_volume
41
+ request :list_networks
42
+ request :destroy_network
43
+ request :dhcp_leases
44
+ request :list_interfaces
45
+ request :destroy_interface
46
+ request :get_node_info
47
+ request :update_autostart
48
+ request :update_display
49
+ request :libversion
50
+
51
+ module Shared
52
+ include Fog::Libvirt::Util
53
+ end
54
+
55
+ class Mock
56
+ include Shared
57
+ def initialize(options={})
58
+ # libvirt is part of the gem => ruby-libvirt
59
+ require 'libvirt'
60
+ end
61
+
62
+ private
63
+
64
+ def client
65
+ return @client if defined?(@client)
66
+ end
67
+
68
+ #read mocks xml
69
+ def read_xml(file_name)
70
+ file_path = File.join(File.dirname(__FILE__),"requests","compute","mock_files",file_name)
71
+ File.read(file_path)
72
+ end
73
+ end
74
+
75
+ class Real
76
+ include Shared
77
+ attr_reader :client
78
+ attr_reader :uri
79
+ attr_reader :ip_command
80
+
81
+ def initialize(options={})
82
+ @uri = ::Fog::Libvirt::Util::URI.new(enhance_uri(options[:libvirt_uri]))
83
+ @ip_command = options[:libvirt_ip_command]
84
+
85
+ # libvirt is part of the gem => ruby-libvirt
86
+ begin
87
+ require 'libvirt'
88
+ rescue LoadError => e
89
+ retry if require('rubygems')
90
+ raise e.message
91
+ end
92
+
93
+ begin
94
+ if options[:libvirt_username] and options[:libvirt_password]
95
+ @client = ::Libvirt::open_auth(uri.uri, [::Libvirt::CRED_AUTHNAME, ::Libvirt::CRED_PASSPHRASE]) do |cred|
96
+ case cred['type']
97
+ when ::Libvirt::CRED_AUTHNAME
98
+ options[:libvirt_username]
99
+ when ::Libvirt::CRED_PASSPHRASE
100
+ options[:libvirt_password]
101
+ end
102
+ end
103
+ else
104
+ @client = ::Libvirt::open(uri.uri)
105
+ end
106
+
107
+ rescue ::Libvirt::ConnectionError
108
+ raise Fog::Errors::Error.new("Error making a connection to libvirt URI #{uri.uri}:\n#{$!}")
109
+ end
110
+ end
111
+
112
+ def terminate
113
+ @client.close if @client and !@client.closed?
114
+ end
115
+
116
+ def enhance_uri(uri)
117
+ require 'cgi'
118
+ append=""
119
+
120
+ # on macosx, chances are we are using libvirt through homebrew
121
+ # the client will default to a socket location based on it's own location (/opt)
122
+ # we conveniently point it to /var/run/libvirt/libvirt-sock
123
+ # if no socket option has been specified explicitly and
124
+ # if the socket exists
125
+
126
+ socketpath="/var/run/libvirt/libvirt-sock"
127
+ if RUBY_PLATFORM =~ /darwin/ && File.exist?(socketpath)
128
+ querystring=::URI.parse(uri).query
129
+ if querystring.nil?
130
+ append="?socket=#{socketpath}"
131
+ elsif !::CGI.parse(querystring).key?("socket")
132
+ append="&socket=#{socketpath}"
133
+ end
134
+ end
135
+ uri+append
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,92 @@
1
+ This model implements the connection with a libvirt URI.
2
+ A libvirt URI can either be local or remote.
3
+
4
+ To learn more on the specific libvirt URI syntax see:
5
+
6
+ - [http://libvirt.org/uri.html](http://libvirt.org/uri.html)
7
+ - [http://libvirt.org/remote.html#Remote_URI_reference](http://libvirt.org/remote.html#Remote_URI_reference)
8
+
9
+ Only ssh is supported as the transport for remote URI's. TLS is NOT supported, as we can't easily login to the server
10
+
11
+ ## Dependencies
12
+
13
+ - the interaction with libvirt is done through the official libvirt gem called 'ruby-libvirt'.
14
+ - be aware that there is another libvirt gem called 'libvirt', which is not compatible
15
+ - If this gem is not installed the models for libvirt will not be available
16
+
17
+ - libvirt needs to be setup so that it can be used
18
+ - for a remote ssh connection this requires to be member of the libvirt group before you can use the libvirt commands
19
+ - verify if you can execute virsh command to see if you have correct access
20
+
21
+ ## Libvirt on Macosx
22
+
23
+ - There is a libvirt client for Macosx, available via homebrew
24
+ - By default this will install things in /usr/local/somewhere
25
+ - This means that also the default locations of the libvirt-socket are assumed to be in /usr/local
26
+ - To check the connection you need to override your libvirt socket location in the URI
27
+ - "qemu+ssh://patrick@myserver/system?socket=/var/run/libvirt/libvirt-sock"
28
+
29
+ ## Ceph RBD volumes
30
+ To configure Ceph RBD volumes, the file ``/etc/foreman/ceph.conf`` is used.
31
+ After adding the authentication key to a libvirt secret, it can be configured as follows:
32
+ ```
33
+ monitor=mon001.example.com,mon002.example.com,mon003.example.com
34
+ port=6789
35
+ libvirt_ceph_pool=rbd_pool_name
36
+ auth_username=libvirt
37
+ auth_uuid=uuid_of_libvirt_secret
38
+ bus_type=virtio
39
+ ```
40
+ For more recent versions of libvirt which support using the secret by name (`usage` attribute in the `secret` tag),
41
+ you can also drop `auth_uuid` and specify `auth_usage` instead. If both are specified, `auth_uuid` will be preferred for maximum compatibility.
42
+
43
+ The `bus_type` can be set to `virtio` or `scsi`. If it is ommited, the default is `scsi`.
44
+
45
+ ## Configuration
46
+
47
+ The URI can be configured in two ways:
48
+ 1) via the .fog file
49
+ :default
50
+ :libvirt_uri: "qemu+ssh://patrick@myserver/system?socket=/var/run/libvirt/libvirt-sock"
51
+
52
+ 2) you can also pass it during creation :
53
+ f=Fog::Compute.new(:provider => "Libvirt", :libvirt_uri => "qemu+ssh://patrick@myserver/system")
54
+
55
+ ## IP-addresses of guests
56
+ Libvirt does not provide a way to query guests for Ip-addresses.
57
+ The way we solve this problem is by installing arpwatch: this watches an interface for new mac-addresses and ip-addresses requested by DHCP
58
+ We query that logfile for the mac-address and can retrieve the ip-address
59
+
60
+ vi /etc/rsyslog.d/30-arpwatch.conf
61
+ #:msg, contains, "arpwatch:" -/var/log/arpwatch.log
62
+ #& ~
63
+ if $programname =='arpwatch' then /var/log/arpwatch.log
64
+ & ~
65
+
66
+ This log files needs to be readable for the users of libvirt
67
+
68
+ ## SSh-ing into the guests
69
+ Once we have retrieved the ip-address of the guest we can ssh into it. This works great if the URI is local.
70
+ But when the URI is remote our machine can't ssh directly into the guest sometimes (due to NAT or firewall issues)
71
+
72
+ Luckily libvirt over ssh requires netcat to be installed on the libvirt server.
73
+ We use this to proxy our ssh requests to the guest over the ssh connection to the libvirt server.
74
+ Thanks to the requirement that you need ssh login to work to a libvirt server, we can login and tunnel the ssh to the guest.
75
+
76
+ ## Bridge configuration (slowness)
77
+ We had noticed that sometimes it takes about 30 seconds before the server gets a DHCP response from the server.
78
+ In our case it was because the new machine Mac-address was not allowed immediately by the bridge.
79
+ Adding the flag 'bridge_fd 0' solved that problem.
80
+
81
+ /etc/network/interfaces
82
+ auto br0
83
+ iface br0 inet static
84
+ address 10.247.4.13
85
+ netmask 255.255.255.0
86
+ network 10.247.4.0
87
+ broadcast 10.247.4.255
88
+ bridge_ports eth0.4
89
+ bridge_stp on
90
+ bridge_maxwait 0
91
+ bridge_fd 0
92
+
@@ -0,0 +1,25 @@
1
+ require 'fog/core/model'
2
+
3
+ module Fog
4
+ module Libvirt
5
+ class Compute
6
+ class Interface < Fog::Model
7
+ identity :name
8
+ attribute :mac
9
+ attribute :active
10
+
11
+ def save
12
+ raise Fog::Errors::Error.new('Creating a new interface is not yet implemented. Contributions welcome!')
13
+ end
14
+
15
+ def shutdown
16
+ service.destroy_interface(mac)
17
+ end
18
+
19
+ def active?
20
+ active
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ require 'fog/core/collection'
2
+ require 'fog/libvirt/models/compute/interface'
3
+
4
+ module Fog
5
+ module Libvirt
6
+ class Compute
7
+ class Interfaces < Fog::Collection
8
+ model Fog::Libvirt::Compute::Interface
9
+
10
+ def all(filter={})
11
+ load(service.list_interfaces(filter))
12
+ end
13
+
14
+ def get(name)
15
+ self.all(:name => name).first
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end