Sha256: 2f42410cd8aa5a94862c936f90adca1933bdec2fd7d128ef0d32c608b47389a2

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

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


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

  def self.config_class
    ::VagrantHosts::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!
      upload_tmphosts
      atomic_sync
    end

    def upload_tmphosts
      cache = Tempfile.new('tmp-hosts')
      cache.write(format_hosts)
      cache.flush
      @env[:vm].channel.upload(cache.path, '/tmp/hosts')
    end

    def atomic_sync
      script = <<-ATOMIC
hostname #{@env[:vm].name}
domainname vagrantup.internal
install -m 644 /tmp/hosts /etc/hosts
      ATOMIC

      sync = Tempfile.new('sync')
      sync.write(script)
      sync.flush
      @env[:vm].channel.upload(sync.path, '/tmp/sync')

      @env[:vm].channel.sudo('bash /tmp/sync')
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-hosts-0.0.2 lib/vagrant-hosts/provisioners/hosts.rb