Sha256: 9bfcbf1274d39aef190a76201865d4b6f623bb73339f15b86493eb2b1780dd50

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require 'vagrant'
require 'vagrant-hosts'
require 'vagrant-hosts/ssh' # Guerrilla patch ssh download
require 'tempfile'


class VagrantHosts::Provisioner < Vagrant::Provisioners::Base

  class Config < Vagrant::Config::Base

    attr_reader :hosts

    def initialize
      @hosts = []
    end

    # Register a host for entry
    #
    # @param [String] address The IP address for aliases
    # @param [Array] aliases An array of hostnames to assign to the IP address
    def add_host(address, aliases)
      @hosts << [address, aliases]
    end

    def validate(env, errors)
      @hosts.each do |(address, aliases)|
        unless aliases.is_a? Array
          errors.add("#{address} should have an array of aliases, got #{aliases.inspect}:#{aliases.class}")
        end
      end
    end
  end

  def self.config_class
    Config
  end

  def provision!
    # too tired to do this. detect target platform, select according provider,
    # add entries that are specified in the config and are not on the client

    driver = Linux.new(@env, config)
    driver.sync!
  end

  class Linux

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

    def sync!
      cache = Tempfile.new('vagrant-hosts')

      # It would be really cool to synchronize this file instead of blindly
      # smashing the remote hosts file, but this is the quick and dirty solution.
      #@env[:vm].channel.download('/etc/hosts', cache.path)
      #cache.rewind
      #cache.truncate(0)

      cache.write hosts_format
      cache.flush

      @env[:vm].channel.upload(cache.path, '/tmp/hosts')
      @env[:vm].channel.sudo('install -m 644 /tmp/hosts /etc/hosts')
    end

    # Generates content appropriate for a linux hosts file
    #
    # @return [String] All hosts in the config joined into hosts records
    def hosts_format
      @config.hosts.inject('') do |str, (address, aliases)|
        str << "#{address} #{aliases.join(' ')}\n"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vagrant-hosts-0.0.1 lib/vagrant-hosts/provisioners/hosts.rb
vagrant-hosts-0.0.1rc1 lib/vagrant-hosts/provisioners/hosts.rb