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.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/beaker/network_manager.rb', line 22

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

  # user provided prefix has top priority
  if not @options[:log_prefix]
    # name it after the hosts file
    if @options[:hosts_file]
      @options[:log_prefix] = File.basename(@options[:hosts_file], '.yml')
    else
      #here be the default
      @options[:log_prefix] = @options[:default_log_prefix]
    end
  end
  @options[:timestamp]      = Time.now unless @options.has_key?(:timestamp)
  @options[:xml_dated_dir]  = Beaker::Logger.generate_dated_log_folder(@options[:xml_dir], @options[:log_prefix], @options[:timestamp])
  @options[:log_dated_dir]  = Beaker::Logger.generate_dated_log_folder(@options[:log_dir], @options[:log_prefix], @options[:timestamp])
  @options[:logger_sut]     = Beaker::Logger.new(File.join(@options[:log_dated_dir], @options[:log_sut_event]), { :quiet => true })
end

Instance Method Details

#cleanupObject

Shut down network connections and revert all provisioned virtual machines



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/beaker/network_manager.rb', line 105

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

  if @hypervisors
    @hypervisors.each_key do |type|
      @hypervisors[type].cleanup
      @hypervisors[type].instance_variable_get(:@hosts).each do |host|
        log_sut_event host, false
      end
    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



86
87
88
89
90
91
92
# File 'lib/beaker/network_manager.rb', line 86

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

#log_sut_event(host, create) ⇒ String

logs provisioning events

Parameters:

  • host (Host)

    The host that the event is happening to

  • create (Boolean)

    Whether the event is creation or cleaning up

Returns:

  • (String)

    the log line created for this event



126
127
128
129
130
131
132
133
134
135
# File 'lib/beaker/network_manager.rb', line 126

def log_sut_event host, create
  raise ArgumentError.new "log_sut_event called before sut logger created. skipping #{host}, #{create}" unless @options.has_key?(:logger_sut)
  sut_logger = @options[:logger_sut]
  time = Time.new
  stamp = time.strftime('%Y-%m-%d %H:%M:%S')
  verb = create ? '+' : '-'
  line = "#{stamp}\t[#{verb}]\t#{host['hypervisor']}\t#{host['platform']}\t#{host.log_prefix}"
  sut_logger.notify line
  line
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.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/beaker/network_manager.rb', line 47

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]
    @machines[type].each do |host|
      log_sut_event host, true
    end
  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)


16
17
18
19
20
# File 'lib/beaker/network_manager.rb', line 16

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

#proxy_package_managerObject

configure proxy on all provioned machines

Raises:

  • (Exception)

    Raise an exception if virtual machines fail to be configured



96
97
98
99
100
101
102
# File 'lib/beaker/network_manager.rb', line 96

def proxy_package_manager
  if @hypervisors
    @hypervisors.each_key do |type|
      @hypervisors[type].proxy_package_manager
    end
  end
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



76
77
78
79
80
81
82
# File 'lib/beaker/network_manager.rb', line 76

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