class VagrantHosts::Cap::SyncHosts::Base

Abstract guest capability for syncing host resources

@abstract @since 2.0.0

Public Class Methods

new(machine, config) click to toggle source
# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 14
def initialize(machine, config)
  @machine, @config = machine, config
  @env = @machine.env
end
sync_hosts(machine, config) click to toggle source
# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 10
def self.sync_hosts(machine, config)
  new(machine, config).sync!
end

Public Instance Methods

sync!() click to toggle source
# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 19
def sync!
  # This ensures that a default hostname is created from the macine name
  # if the VM wasn't configured with a hostname.
  #
  # FIXME: Write tests for this behavior.
  # TODO: Move this behavior into a config block on the hosts provisioner
  # so that this capability can remain focused on updating /etc/hosts.
  if @config.change_hostname
    hostname = @machine.config.vm.hostname || @machine.name.to_s
    change_host_name(hostname)
  end

  update_hosts
end

Private Instance Methods

change_host_name(name) click to toggle source

@param name [String] The new hostname to apply on the guest

# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 58
def change_host_name(name)
  case Vagrant::VERSION
  when /^1\.1/
    @machine.guest.change_host_name(name)
  else
    @machine.guest.capability(:change_host_name, name)
  end
end
update_hosts() click to toggle source

Update the hosts file on a machine

Subclasses should implement this method with OS-specific logic.

# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 53
def update_hosts
  raise NotImplementedError
end
upload_temphosts(hosts_content, dest_path = '/tmp/vagrant-hosts.txt') click to toggle source

Upload /etc/hosts content to a temporary file on the guest

# File lib/vagrant-hosts/cap/sync_hosts/base.rb, line 37
def upload_temphosts(hosts_content, dest_path = '/tmp/vagrant-hosts.txt')
  temp_file = nil

  temp_file = Tempfile.new('vagrant-hosts')
  temp_file.binmode # Don't convert line endings.

  temp_file.write(hosts_content)
  temp_file.flush
  @machine.communicate.upload(temp_file.path, dest_path)
ensure
  temp_file.close unless temp_file.nil?
end