Sha256: 34675e49139f90e4c6795bc8e8ee7c4fe8a0c46319a41c182657b7096744d3f7

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

require 'auto_network'
require 'yaml'

class AutoNetwork::Action::LoadPool

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

    @config_path = @env[:home_path].join('auto_network')
    @statefile   = @config_path.join('pool.yaml')
  end

  # Handle the loading and unloading of the auto_network pool
  #
  # @param env [Hash]
  #
  # @option env [AutoNetwork::Pool] auto_network_pool The global auto network pool
  # @option env [Vagrant::Environment] env The Vagrant environment containing
  #   the active machines that need to be filtered.
  #
  # @return [void]
  def call(env)
    @env = env

    deserialize!
    @app.call(@env)
    serialize!
  end

  private

  def deserialize!
    pool = nil
    if @statefile.exist?
      pool = YAML.load(@statefile.read)
    else
      range = AutoNetwork.default_pool
      @env[:ui].info "No auto_network pool available, generating a pool with the range #{range}"
      pool = AutoNetwork::Pool.new(range)
    end
    @env[:auto_network_pool] = pool
  end

  def serialize!
    @config_path.mkpath unless @config_path.exist?

    pool_data = YAML.dump(@env[:auto_network_pool])
    @statefile.open('w') { |fh| fh.write(pool_data) }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-auto_network-0.2.1 lib/auto_network/action/load_pool.rb
vagrant-auto_network-0.2.0 lib/auto_network/action/load_pool.rb