Class: OsVm::MachineConfig::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/osvm/machine_config.rb

Direct Known Subclasses

BridgeNetwork, SocketNetwork, UserNetwork

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i, cfg) ⇒ Network

Returns a new instance of Network.



81
82
83
84
85
86
87
# File 'lib/osvm/machine_config.rb', line 81

def initialize(i, cfg)
  @index = i
  @type = cfg.fetch('type')
  @opts = cfg.fetch('opts', default_opts)
  @mac = resolve_mac_address(cfg)
  @model = resolve_model(cfg)
end

Instance Attribute Details

#indexString, Integer (readonly)

Returns:

  • (String)
  • (Integer)


70
71
72
# File 'lib/osvm/machine_config.rb', line 70

def index
  @index
end

#macString (readonly)

Returns:

  • (String)


76
77
78
# File 'lib/osvm/machine_config.rb', line 76

def mac
  @mac
end

#modelString (readonly)

Returns:

  • (String)


79
80
81
# File 'lib/osvm/machine_config.rb', line 79

def model
  @model
end

#typeString (readonly)

Returns:

  • (String)


73
74
75
# File 'lib/osvm/machine_config.rb', line 73

def type
  @type
end

Class Method Details

.from_config(i, cfg) ⇒ Network

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/osvm/machine_config.rb', line 51

def self.from_config(i, cfg)
  type = cfg.fetch('type')
  klass =
    case type
    when 'user'
      UserNetwork
    when 'socket'
      SocketNetwork
    when 'bridge'
      BridgeNetwork
    else
      raise ArgumentError, "unknown network type #{type.inspect}"
    end

  klass.new(i, cfg)
end

Instance Method Details

#default_optsObject (protected)



108
109
110
111
112
113
114
# File 'lib/osvm/machine_config.rb', line 108

def default_opts
  {
    'network' => '10.0.2.0/24',
    'host' => '10.0.2.2',
    'dns' => '10.0.2.3'
  }
end

#qemu_optionsObject

Raises:

  • (NotImplementedError)


89
90
91
# File 'lib/osvm/machine_config.rb', line 89

def qemu_options
  raise NotImplementedError
end

#resolve_mac_address(cfg) ⇒ Object (protected)



95
96
97
98
99
100
101
# File 'lib/osvm/machine_config.rb', line 95

def resolve_mac_address(cfg)
  mac = cfg['macAddress'] || cfg['mac'] || @opts['macAddress'] || @opts['mac']
  mac = mac.to_s.downcase unless mac.nil?
  return MacAddressGenerator.register_mac(mac) if mac && !mac.empty?

  MacAddressGenerator.next_mac
end

#resolve_model(cfg) ⇒ Object (protected)



103
104
105
106
# File 'lib/osvm/machine_config.rb', line 103

def resolve_model(cfg)
  model = cfg['model'] || @opts['model'] || 'virtio-net'
  model.to_s
end