Class: Vagrant::LXC::Action::GcPrivateNetworkBridges

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxc/action/gc_private_network_bridges.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ GcPrivateNetworkBridges

Returns a new instance of GcPrivateNetworkBridges.



5
6
7
# File 'lib/vagrant-lxc/action/gc_private_network_bridges.rb', line 5

def initialize(app, env)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-lxc/action/gc_private_network_bridges.rb', line 9

def call(env)
  was_running = env[:machine].provider.state.id == :running

  # Continue execution, we need the container to be stopped
  @app.call(env)

  was_running = was_running && env[:machine].provider.state.id != :running

  if was_running && private_network_configured?(env[:machine].config)
    private_network_configured?(env[:machine].config)
    remove_bridges_that_are_not_in_use(env)
  end
end

#private_network_configured?(config) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/vagrant-lxc/action/gc_private_network_bridges.rb', line 23

def private_network_configured?(config)
  config.vm.networks.find do |type, _|
    type.to_sym == :private_network
  end
end

#remove_bridges_that_are_not_in_use(env) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant-lxc/action/gc_private_network_bridges.rb', line 29

def remove_bridges_that_are_not_in_use(env)
  env[:machine].config.vm.networks.find do |type, config|
    next if type.to_sym != :private_network

    bridge = config.fetch(:lxc__bridge_name)
    driver = env[:machine].provider.driver

    if ! driver.bridge_is_in_use?(bridge)
      env[:ui].info I18n.t("vagrant_lxc.messages.remove_bridge", name: bridge)
      unless ['lxcbr0', 'virbr0'].include? bridge
        driver.remove_bridge(bridge)
      end
    end
  end
end