class Vagrant::LXC::Action::GcPrivateNetworkBridges

Public Class Methods

new(app, env) click to toggle source
# File lib/vagrant-lxc/action/gc_private_network_bridges.rb, line 5
def initialize(app, env)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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