Class: Beaker::NetworkManager

Inherits:
Object
  • Object
show all
Defined in:
lib/beaker/network_manager.rb

Overview

Object that holds all the provisioned and non-provisioned virtual machines. Controls provisioning, configuration, validation and cleanup of those virtual machines.

Instance Method Summary collapse

Constructor Details

#initialize(options, logger) ⇒ NetworkManager

Returns a new instance of NetworkManager.



26
27
28
29
30
31
32
# File 'lib/beaker/network_manager.rb', line 26

def initialize(options, logger)
  @logger = logger
  @options = options
  @hosts = []
  @machines = {}
  @hypervisors = nil
end

Instance Method Details

#cleanupObject

Shut down network connections and revert all provisioned virtual machines



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/beaker/network_manager.rb', line 81

def cleanup
  #shut down connections
  @hosts.each {|host| host.close }

  if @hypervisors
    @hypervisors.each_key do |type|
      @hypervisors[type].cleanup
    end
  end
  @hypervisors = nil
end

#configureObject

Configure all provisioned machines, adding any packages or settings required for SUTs

Raises:

  • (Exception)

    Raise an exception if virtual machines fail to be configured



72
73
74
75
76
77
78
# File 'lib/beaker/network_manager.rb', line 72

def configure
  if @hypervisors
    @hypervisors.each_key do |type|
      @hypervisors[type].configure
    end
  end
end

#provisionObject

Provision all virtual machines. Provision machines according to their set hypervisor, if no hypervisor is selected assume that the described hosts are already up and reachable and do no provisioning.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/beaker/network_manager.rb', line 36

def provision
  if @hypervisors
    cleanup
  end
  @hypervisors = {}
  #sort hosts by their hypervisor, use hypervisor 'none' if no hypervisor is specified
  @options['HOSTS'].each_key do |name|
    host = @options['HOSTS'][name]
    hypervisor = host['hypervisor']
    hypervisor = provision?(@options, host) ? host['hypervisor'] : 'none'
    @logger.debug "Hypervisor for #{name} is #{hypervisor}"
    @machines[hypervisor] = [] unless @machines[hypervisor]
    @machines[hypervisor] << Beaker::Host.create(name, @options)
  end

  @machines.each_key do |type|
    @hypervisors[type] = Beaker::Hypervisor.create(type, @machines[type], @options)
    @hosts << @machines[type]
  end
  @hosts = @hosts.flatten
  @hosts
end

#provision?(options, host) ⇒ Boolean

Determine if a given host should be provisioned. Provision if:

  • only if we are running with —provision

  • only if we have a hypervisor

  • only if either the specific hosts has no specification or has ‘provision’ in its config

  • always if it is a vagrant box (vagrant boxes are always provisioned as they always need ssh key hacking)

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/beaker/network_manager.rb', line 20

def provision? options, host
  command_line_says = options[:provision]
  host_says = host['hypervisor'] && (host.has_key?('provision') ? host['provision'] : true)
  (command_line_says && host_says) or (host['hypervisor'] =~/vagrant/)
end

#validateObject

Validate all provisioned machines, ensure that required packages are installed - if they are missing attempt to add them.

Raises:

  • (Exception)

    Raise an exception if virtual machines fail to be validated



62
63
64
65
66
67
68
# File 'lib/beaker/network_manager.rb', line 62

def validate
  if @hypervisors
    @hypervisors.each_key do |type|
      @hypervisors[type].validate
    end
  end
end