vagrant-ovirt4 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ require 'ovirtsdk4'
5
5
  module VagrantPlugins
6
6
  module OVirtProvider
7
7
  class Config < Vagrant.plugin('2', :config)
8
+ attr_reader :disks
8
9
 
9
10
  attr_accessor :url
10
11
  attr_accessor :username
@@ -29,7 +30,10 @@ module VagrantPlugins
29
30
  attr_accessor :optimized_for
30
31
  attr_accessor :description
31
32
  attr_accessor :comment
32
- attr_accessor :disks
33
+ attr_accessor :vmname
34
+ attr_accessor :timeout
35
+ attr_accessor :connect_timeout
36
+ attr_accessor :run_once
33
37
 
34
38
  def initialize
35
39
  @url = UNSET_VALUE
@@ -55,6 +59,10 @@ module VagrantPlugins
55
59
  @optimized_for = UNSET_VALUE
56
60
  @description = UNSET_VALUE
57
61
  @comment = UNSET_VALUE
62
+ @vmname = UNSET_VALUE
63
+ @timeout = UNSET_VALUE
64
+ @connect_timeout = UNSET_VALUE
65
+ @run_once = UNSET_VALUE
58
66
  @disks = []
59
67
 
60
68
  end
@@ -109,6 +117,10 @@ module VagrantPlugins
109
117
  @optimized_for = nil if @optimized_for == UNSET_VALUE
110
118
  @description = '' if @description == UNSET_VALUE
111
119
  @comment = '' if @comment == UNSET_VALUE
120
+ @vmname = nil if @vmname == UNSET_VALUE
121
+ @timeout = nil if @timeout == UNSET_VALUE
122
+ @connect_timeout = nil if @connect_timeout == UNSET_VALUE
123
+ @run_once = @run_once == UNSET_VALUE ? false : !!@run_once
112
124
 
113
125
  unless optimized_for.nil?
114
126
  raise "Invalid 'optimized_for'. Must be one of #{OvirtSDK4::VmType.constants.map { |s| "'#{s.downcase}'" }.join(' ')}" unless OvirtSDK4::VmType.constants.include? optimized_for.upcase.to_sym
@@ -133,6 +145,24 @@ module VagrantPlugins
133
145
  rescue ArgumentError
134
146
  raise "Not able to parse either `memory_size` or `memory_guaranteed`. Please verify and check again."
135
147
  end
148
+
149
+ unless timeout.nil?
150
+ begin
151
+ @timeout = Integer(@timeout)
152
+ raise ArgumentError if @timeout < 0
153
+ rescue ArgumentError, TypeError
154
+ raise "`timeout` argument #{@timeout.inspect} is not a valid nonnegative integer"
155
+ end
156
+ end
157
+
158
+ unless connect_timeout.nil?
159
+ begin
160
+ @connect_timeout = Integer(@connect_timeout)
161
+ raise ArgumentError if @connect_timeout < 0
162
+ rescue ArgumentError, TypeError
163
+ raise "`connect_timeout` argument #{@connect_timeout.inspect} is not a valid nonnegative integer"
164
+ end
165
+ end
136
166
  end
137
167
 
138
168
  end
@@ -7,6 +7,10 @@ module VagrantPlugins
7
7
  error_namespace("vagrant_ovirt4.errors")
8
8
  end
9
9
 
10
+ class ServiceConnectionError < VagrantOVirtError
11
+ error_key(:service_connection_error)
12
+ end
13
+
10
14
  class RsyncError < VagrantOVirtError
11
15
  error_key(:rsync_error)
12
16
  end
@@ -0,0 +1,40 @@
1
+ require 'ovirtsdk4'
2
+ require 'ovirtsdk4/connection'
3
+
4
+ module VagrantPlugins
5
+ module OVirtProvider
6
+ module Util
7
+ module Connection
8
+ # Use OVirtSDK4::ConnectionError if available; SDK versions older than
9
+ # 4.2.0 used the generic OVirtSDK4::Error.
10
+ ERROR_CLASSES = (OvirtSDK4.const_defined?(:ConnectionError) ? [OvirtSDK4::ConnectionError, OvirtSDK4::Error] : [OvirtSDK4::Error]).freeze
11
+
12
+ module_function
13
+
14
+ # Close a connection, suppressing errors generated by the SDK. Yield
15
+ # the error to the caller to re-raise if appropriate (or log, or do
16
+ # whatever).
17
+ def safe_close_connection!(conn)
18
+ conn.close
19
+ rescue *ERROR_CLASSES => e
20
+ yield e if block_given?
21
+ rescue StandardError => e
22
+ yield e if block_given?
23
+ raise e
24
+ end
25
+
26
+ # Wrapper for "#safe_close_connection" that issues a warning message
27
+ # with diagnostic information about the exception raised.
28
+ def safe_close_connection_with_warning!(conn, ui)
29
+ safe_close_connection!(conn) { |e| ui.warn("Encountered exception of class #{e.class}: #{e.message}") }
30
+ end
31
+
32
+ # Wrapper for "#safe_close_connection_with_warning" that extracts the
33
+ # connection and UI from a Vagrant environment.
34
+ def safe_close_connection_standard!(env)
35
+ safe_close_connection_with_warning!(env[:connection], env[:ui])
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module VagrantPlugins
2
+ module OVirtProvider
3
+ module Util
4
+ module MachineNames
5
+ DEFAULT_NAME = 'vagrant'.freeze
6
+
7
+ module_function
8
+
9
+ def machine_hostname(machine)
10
+ machine.config.vm.hostname || DEFAULT_NAME
11
+ end
12
+
13
+ def machine_vmname(machine)
14
+ machine.provider_config.vmname || machine_hostname(machine)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+
@@ -1,6 +1,6 @@
1
1
  module VagrantPlugins
2
2
  module OVirtProvider
3
- VERSION = '2.0.0'
3
+ VERSION = '2.2.0'
4
4
  end
5
5
  end
6
6
 
@@ -1,3 +1,4 @@
1
+ require 'log4r'
1
2
  require 'pathname'
2
3
  require 'vagrant-ovirt4/plugin'
3
4
 
@@ -8,9 +9,11 @@ module VagrantPlugins
8
9
  autoload :Errors, lib_path.join("errors")
9
10
  autoload :Util, lib_path.join("util")
10
11
 
12
+ @@logger = Log4r::Logger.new("vagrant_ovirt4::provider")
11
13
  @@ovirt_connection = nil
12
14
  @@vms_service = nil
13
15
  def self.ovirt_connection
16
+ @@logger.warn('Use of deprecated OVirtProvider.ovirt_connection detected.')
14
17
  @@ovirt_connection
15
18
  end
16
19
 
@@ -19,6 +22,7 @@ module VagrantPlugins
19
22
  end
20
23
 
21
24
  def self.vms_service
25
+ @@logger.warn('Use of deprecated OVirtProvider.vms_service detected.')
22
26
  @@vms_service
23
27
  end
24
28
 
data/locales/en.yml CHANGED
@@ -60,6 +60,8 @@ en:
60
60
  short_not_created: |-
61
61
  not created
62
62
  errors:
63
+ service_connection_error: |-
64
+ Error connecting to the oVirt cluster endpoint: "%{error_message}"
63
65
  remove_vm_error: |-
64
66
  Error removing VM '%{vm_name}'. oVirt error message was '%{error_message}'
65
67
  no_vm_error: |-
data/spec/spec_helper.rb CHANGED
@@ -1 +1,3 @@
1
1
  require 'vagrant'
2
+
3
+ require_relative 'support/shared_context'
@@ -0,0 +1,39 @@
1
+ require 'vagrant-ovirt4/config'
2
+
3
+ shared_context 'provider:action' do
4
+ let(:app) { double('app') }
5
+
6
+ let(:env) {
7
+ {
8
+ connection: double('connection',
9
+ system_service: double('system_service', disks_service: double('disks_service'))
10
+ ),
11
+ machine: double('machine',
12
+ provider: double('provider'),
13
+ provider_config: VagrantPlugins::OVirtProvider::Config.new,
14
+ name: 'machname',
15
+ id: 'ID',
16
+ config: double('config',
17
+ vm: double('vm_config',
18
+ networks: [],
19
+ ),
20
+ ),
21
+ ),
22
+ ui: double('ui'),
23
+ vms_service: double('vms_service'),
24
+ }
25
+ }
26
+
27
+ let(:vm_service) {
28
+ double('vm_service',
29
+ disk_attachments_service: double('disk_attachments_service', list: []),
30
+ get: double('get'),
31
+ )
32
+ }
33
+
34
+ before do
35
+ allow(app).to receive(:call)
36
+ allow(env[:vms_service]).to receive(:vm_service).with(env[:machine].id).and_return(vm_service)
37
+ env[:machine].provider_config.finalize!
38
+ end
39
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'vagrant-ovirt4/action/create_vm'
3
+ require 'vagrant-ovirt4/config'
4
+
5
+ describe VagrantPlugins::OVirtProvider::Action::CreateVM do
6
+ include_context 'provider:action'
7
+
8
+ let(:vm) { double('vm', id: 'ID') }
9
+
10
+ subject(:action) { described_class.new(app, env) }
11
+
12
+ before do
13
+ allow(env[:ui]).to receive(:info)
14
+ allow(env[:machine]).to receive(:"id=").with(vm.id)
15
+ allow(vm_service.get).to receive(:status).and_return('down')
16
+ end
17
+
18
+ context 'given a custom vmname' do
19
+ let(:vmname) { 'VMNAME' }
20
+
21
+ it 'uses that as the name for machine creation' do
22
+ env[:machine].provider_config.vmname = vmname
23
+ expect(env[:vms_service]).to receive(:add).with(hash_including(name: vmname)).and_return(vm)
24
+ action.call(env)
25
+ end
26
+ end
27
+
28
+ context 'given no custom vmname' do
29
+ context 'given a custom hostname' do
30
+ let(:hostname) { 'HOSTNAME' }
31
+
32
+ it 'uses that as the name for machine creation' do
33
+ expect(env[:machine].config.vm).to receive(:hostname).and_return(hostname)
34
+ expect(env[:vms_service]).to receive(:add).with(hash_including(name: hostname)).and_return(vm)
35
+ action.call(env)
36
+ end
37
+ end
38
+
39
+ context 'given no custom hostname' do
40
+ it 'uses a default name for machine creation' do
41
+ expect(env[:machine].config.vm).to receive(:hostname)
42
+ expect(env[:vms_service]).to receive(:add).with(hash_including(name: 'vagrant')).and_return(vm)
43
+ action.call(env)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -17,7 +17,7 @@ describe VagrantPlugins::OVirtProvider::Action::ReadSSHInfo do
17
17
  expect(action).to receive(:call).with(env)
18
18
  action.call(env)
19
19
  end
20
-
20
+
21
21
  context 'unknown API error' do
22
22
  before do
23
23
  allow(env.machine).to receive(:id).and_return('wat')
@@ -25,35 +25,35 @@ describe VagrantPlugins::OVirtProvider::Action::ReadSSHInfo do
25
25
  allow(env.vms_service.vm_service).to receive(:get).and_raise('boom')
26
26
 
27
27
  end
28
-
29
- it 'continues the middleware chain' do
28
+
29
+ it 'returns nil' do
30
30
  expect(app).to receive(:call).with(env)
31
31
  action.call(env)
32
- expect(env.machine_ssh_info).to eq(:not_created)
32
+ expect(env.machine_ssh_info).to be_nil
33
33
  end
34
-
35
34
  end
36
35
 
37
36
  context 'machine does not exist' do
38
37
  before do
39
38
  allow(env.machine).to receive(:id).and_return(nil)
40
39
  end
41
-
42
- it 'continues the middleware chain' do
40
+
41
+ it 'returns nil' do
43
42
  expect(app).to receive(:call).with(env)
44
43
  action.call(env)
45
- expect(env.machine_ssh_info).to eq(:not_created)
44
+ expect(env.machine_ssh_info).to be_nil
46
45
  end
47
-
48
46
  end
49
47
 
50
48
  context 'machine exists' do
49
+ let(:port) { 44 }
50
+
51
51
  before do
52
52
  allow(env.machine).to receive(:id).and_return('wat')
53
53
  allow(env.machine).to receive(:config).and_return(OpenStruct.new({
54
54
  ssh: OpenStruct.new({
55
- guest_port: 44,
56
- }),
55
+ guest_port: port
56
+ })
57
57
  }))
58
58
  allow(env.vms_service).to receive(:vm_service).and_return({})
59
59
  allow(env.vms_service.vm_service).to receive(:get).and_return({})
@@ -61,13 +61,27 @@ describe VagrantPlugins::OVirtProvider::Action::ReadSSHInfo do
61
61
  allow(env.vms_service.vm_service.nics_service).to receive(:list).and_return({})
62
62
  allow(env.vms_service.vm_service.get).to receive(:status).and_return('active')
63
63
  end
64
-
65
- it 'continues the middleware chain' do
66
- expect(app).to receive(:call).with(env)
67
- action.call(env)
68
- expect(env.machine_ssh_info).to eq({:host=>nil, :port=>44, :username=>nil, :private_key_path=>nil, :forward_agent=>nil, :forward_x11=>nil})
64
+
65
+ context 'with no IP addresses defined' do
66
+ it 'returns nil' do
67
+ expect(app).to receive(:call).with(env)
68
+ action.call(env)
69
+ expect(env.machine_ssh_info).to be_nil
70
+ end
69
71
  end
70
72
 
73
+ context 'with at least one IP address defined' do
74
+ let(:host) { '10.10.10.10' }
75
+
76
+ before do
77
+ allow(action).to receive(:first_active_ipv4_address).and_return(host)
78
+ end
79
+
80
+ it 'returns filled-out SSH information' do
81
+ expect(app).to receive(:call).with(env)
82
+ action.call(env)
83
+ expect(env.machine_ssh_info).to eq(host: host, port: port, username: nil, private_key_path: nil, forward_agent: nil, forward_x11: nil)
84
+ end
85
+ end
71
86
  end
72
87
  end
73
-
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'vagrant-ovirt4/action/start_vm'
3
+ require 'vagrant-ovirt4/config'
4
+
5
+ describe VagrantPlugins::OVirtProvider::Action::StartVM do
6
+ include_context 'provider:action'
7
+
8
+ subject(:action) { described_class.new(app, env) }
9
+
10
+ before do
11
+ allow(env[:ui]).to receive(:info)
12
+ allow(env[:machine].config.vm).to receive(:hostname)
13
+ allow(vm_service.get).to receive(:status).and_return('nominal')
14
+ allow(vm_service).to receive(:start)
15
+ end
16
+
17
+ def vm_initialization_hash_including_hostname(hostname)
18
+ hash_including(vm: hash_including(initialization: hash_including(host_name: hostname)))
19
+ end
20
+
21
+ context 'given a custom hostname' do
22
+ let(:hostname) { 'HOSTNAME' }
23
+
24
+ it 'uses that as the hostname for machine initialization' do
25
+ expect(env[:machine].config.vm).to receive(:hostname).and_return(hostname)
26
+ expect(vm_service).to receive(:start).with(vm_initialization_hash_including_hostname(hostname))
27
+ action.call(env)
28
+ end
29
+ end
30
+
31
+ context 'given no custom hostname' do
32
+ it 'uses a default value as the hostname for machine initialization' do
33
+ expect(vm_service).to receive(:start).with(vm_initialization_hash_including_hostname('vagrant'))
34
+ action.call(env)
35
+ end
36
+ end
37
+
38
+ context 'given a custom run_once setting' do
39
+ [true, false].each do |value|
40
+ it 'uses that setting' do
41
+ env[:machine].provider_config.run_once = value
42
+ expect(vm_service).to receive(:start).with(hash_including(vm: hash_including(run_once: value)))
43
+ action.call(env)
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'given no custom run_once setting' do
49
+ it 'defaults to false' do
50
+ expect(vm_service).to receive(:start).with(hash_including(vm: hash_including(run_once: false)))
51
+ action.call(env)
52
+ end
53
+ end
54
+ end