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, allocate_resources: true) ⇒ Network

Returns a new instance of Network.



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

def initialize(i, cfg, allocate_resources: true)
  @allocate_resources = allocate_resources
  @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

#index ⇒ String, Integer (readonly)

Returns:

  • (String)
  • (Integer)


97
98
99
# File 'lib/osvm/machine_config.rb', line 97

def index
  @index
end

#mac ⇒ String (readonly)

Returns:

  • (String)


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

def mac
  @mac
end

#model ⇒ String (readonly)

Returns:

  • (String)


106
107
108
# File 'lib/osvm/machine_config.rb', line 106

def model
  @model
end

#type ⇒ String (readonly)

Returns:

  • (String)


100
101
102
# File 'lib/osvm/machine_config.rb', line 100

def type
  @type
end

Class Method Details

.from_config(i, cfg, allocate_resources: true) ⇒ Network

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osvm/machine_config.rb', line 78

def self.from_config(i, cfg, allocate_resources: true)
  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, allocate_resources:)
end

Instance Method Details

#default_opts ⇒ Object (protected)



138
139
140
141
142
143
144
# File 'lib/osvm/machine_config.rb', line 138

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

#qemu_options ⇒ Object

Raises:

  • (NotImplementedError)


117
118
119
# File 'lib/osvm/machine_config.rb', line 117

def qemu_options
  raise NotImplementedError
end

#resolve_mac_address(cfg) ⇒ Object (protected)



123
124
125
126
127
128
129
130
131
# File 'lib/osvm/machine_config.rb', line 123

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

  MacAddressGenerator.next_mac if @allocate_resources
end

#resolve_model(cfg) ⇒ Object (protected)



133
134
135
136
# File 'lib/osvm/machine_config.rb', line 133

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