Update hostsfile and add zerotier cookbook
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
# Cookbook:: hostsfile
|
||||
# Library:: entry
|
||||
#
|
||||
# Copyright 2012-2013, Seth Vargo
|
||||
# Copyright 2012, CustomInk, LCC
|
||||
# Copyright:: 2012-2013, Seth Vargo
|
||||
# Copyright:: 2012, CustomInk, LCC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -24,160 +24,163 @@ require 'ipaddr'
|
||||
# An object representation of a single line in a hostsfile.
|
||||
#
|
||||
# @author Seth Vargo <sethvargo@gmail.com>
|
||||
class Entry
|
||||
class << self
|
||||
# Creates a new Hostsfile::Entry object by parsing a text line. The
|
||||
# `line` attribute will be in the following format:
|
||||
#
|
||||
# 1.2.3.4 hostname [alias[, alias[, alias]]] [# comment [@priority]]
|
||||
#
|
||||
# @param [String] line
|
||||
# the line to parse
|
||||
# @return [Entry]
|
||||
# a new entry object
|
||||
def parse(line)
|
||||
entry, comment = extract_comment(line)
|
||||
comment, priority = extract_priority(comment)
|
||||
entries = extract_entries(entry)
|
||||
module HostsFile
|
||||
class Entry
|
||||
class << self
|
||||
# Creates a new Hostsfile::Entry object by parsing a text line. The
|
||||
# `line` attribute will be in the following format:
|
||||
#
|
||||
# 1.2.3.4 hostname [alias[, alias[, alias]]] [# comment [@priority]]
|
||||
#
|
||||
# @param [String] line
|
||||
# the line to parse
|
||||
# @return [Entry]
|
||||
# a new entry object
|
||||
def parse(line)
|
||||
entry, comment = extract_comment(line)
|
||||
comment, priority = extract_priority(comment)
|
||||
entries = extract_entries(entry)
|
||||
|
||||
# Return nil if the line is empty
|
||||
return nil if entries.nil? || entries.empty?
|
||||
# Return nil if the line is empty
|
||||
return nil if entries.nil? || entries.empty?
|
||||
|
||||
# If /etc/hosts has a broken content we throw a descriptive exception
|
||||
if entries[0].nil?
|
||||
raise ArgumentError, "/etc/hosts has a line without IP address: #{line}"
|
||||
end
|
||||
if entries[1].nil?
|
||||
raise ArgumentError, "/etc/hosts has a line without hostname: #{line}"
|
||||
# If /etc/hosts has a broken content we throw a descriptive exception
|
||||
if entries[0].nil?
|
||||
raise ArgumentError, "/etc/hosts has a line without IP address: #{line}"
|
||||
end
|
||||
if entries[1].nil?
|
||||
raise ArgumentError, "/etc/hosts has a line without hostname: #{line}"
|
||||
end
|
||||
|
||||
new(
|
||||
ip_address: entries[0],
|
||||
hostname: entries[1],
|
||||
aliases: entries[2..-1],
|
||||
comment: comment,
|
||||
priority: priority
|
||||
)
|
||||
end
|
||||
|
||||
return self.new(
|
||||
ip_address: entries[0],
|
||||
hostname: entries[1],
|
||||
aliases: entries[2..-1],
|
||||
comment: comment,
|
||||
priority: priority,
|
||||
)
|
||||
private
|
||||
|
||||
def extract_comment(line)
|
||||
return nil if presence(line).nil?
|
||||
line.split('#', 2).collect { |part| presence(part) }
|
||||
end
|
||||
|
||||
def extract_priority(comment)
|
||||
return nil if comment.nil?
|
||||
|
||||
if comment.include?('@')
|
||||
comment.split('@', 2).collect { |part| presence(part) }
|
||||
else
|
||||
[comment, nil]
|
||||
end
|
||||
end
|
||||
|
||||
def extract_entries(entry)
|
||||
return nil if entry.nil?
|
||||
entry.split(/\s+/).collect { |entry| presence(entry) }.compact
|
||||
end
|
||||
|
||||
def presence(string)
|
||||
return nil if string.nil?
|
||||
return nil if string.strip.empty?
|
||||
string.strip
|
||||
end
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
attr_accessor :ip_address, :hostname, :aliases, :comment, :priority
|
||||
|
||||
# Creates a new entry from the given options.
|
||||
#
|
||||
# @param [Hash] options
|
||||
# a list of options to create the entry with
|
||||
# @option options [String] :ip_address
|
||||
# the IP Address for this entry
|
||||
# @option options [String] :hostname
|
||||
# the hostname for this entry
|
||||
# @option options [String, Array<String>] :aliases
|
||||
# a alias or array of aliases for this entry
|
||||
# @option options[String] :comment
|
||||
# an optional comment for this entry
|
||||
# @option options [Integer] :priority
|
||||
# the relative priority of this entry (compared to others)
|
||||
#
|
||||
# @raise [ArgumentError]
|
||||
# if neither :ip_address nor :hostname are supplied
|
||||
def initialize(options = {})
|
||||
if options[:ip_address].nil? || options[:hostname].nil?
|
||||
raise ArgumentError, ':ip_address and :hostname are both required options'
|
||||
end
|
||||
|
||||
@ip_address = IPAddr.new(remove_ip_scope(options[:ip_address]))
|
||||
@hostname = options[:hostname]
|
||||
@aliases = [options[:aliases]].flatten.compact
|
||||
@comment = options[:comment]
|
||||
@priority = options[:priority] || calculated_priority
|
||||
end
|
||||
|
||||
# Set a the new priority for an entry.
|
||||
#
|
||||
# @param [Integer] new_priority
|
||||
# the new priority to set
|
||||
def priority=(new_priority)
|
||||
@calculated_priority = false
|
||||
@priority = new_priority
|
||||
end
|
||||
|
||||
# The line representation of this entry.
|
||||
#
|
||||
# @return [String]
|
||||
# the string representation of this entry
|
||||
def to_line
|
||||
hosts = [hostname, aliases].flatten.join(' ')
|
||||
|
||||
comments = "# #{comment}".strip
|
||||
comments << " @#{priority}" unless priority.nil? || @calculated_priority
|
||||
comments = comments.strip
|
||||
comments = nil if comments == '#'
|
||||
|
||||
[ip_address, hosts, comments].compact.join("\t").strip
|
||||
end
|
||||
|
||||
# Returns true if priority is calculated
|
||||
#
|
||||
# @return [Boolean]
|
||||
# true if priority is calculated and false otherwise
|
||||
def calculated_priority?
|
||||
@calculated_priority
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract_comment(line)
|
||||
return nil if presence(line).nil?
|
||||
line.split('#', 2).collect { |part| presence(part) }
|
||||
# Calculates the relative priority of this entry.
|
||||
#
|
||||
# @return [Integer]
|
||||
# the relative priority of this item
|
||||
def calculated_priority
|
||||
@calculated_priority = true
|
||||
|
||||
return 82 if ip_address == IPAddr.new('127.0.0.1')
|
||||
return 81 if ip_address == IPAddr.new('::1')
|
||||
return 80 if IPAddr.new('127.0.0.0/8').include?(ip_address) # local
|
||||
return 60 if ip_address.ipv4? # ipv4
|
||||
return 20 if ip_address.ipv6? # ipv6
|
||||
00
|
||||
end
|
||||
|
||||
def extract_priority(comment)
|
||||
return nil if comment.nil?
|
||||
|
||||
if comment.include?('@')
|
||||
comment.split('@', 2).collect { |part| presence(part) }
|
||||
else
|
||||
[comment, nil]
|
||||
end
|
||||
# Removes the scopes pieces of the address, because reasons.
|
||||
#
|
||||
# @see https://bugs.ruby-lang.org/issues/8464
|
||||
# @see https://github.com/customink-webops/hostsfile/issues/51
|
||||
#
|
||||
# @return [String, nil]
|
||||
#
|
||||
def remove_ip_scope(address)
|
||||
return nil if address.nil?
|
||||
address.to_s.sub(/%.*/, '')
|
||||
end
|
||||
|
||||
def extract_entries(entry)
|
||||
return nil if entry.nil?
|
||||
entry.split(/\s+/).collect { |entry| presence(entry) }.compact
|
||||
end
|
||||
|
||||
def presence(string)
|
||||
return nil if string.nil?
|
||||
return nil if string.strip.empty?
|
||||
string.strip
|
||||
end
|
||||
end
|
||||
|
||||
# @return [String]
|
||||
attr_accessor :ip_address, :hostname, :aliases, :comment, :priority
|
||||
|
||||
# Creates a new entry from the given options.
|
||||
#
|
||||
# @param [Hash] options
|
||||
# a list of options to create the entry with
|
||||
# @option options [String] :ip_address
|
||||
# the IP Address for this entry
|
||||
# @option options [String] :hostname
|
||||
# the hostname for this entry
|
||||
# @option options [String, Array<String>] :aliases
|
||||
# a alias or array of aliases for this entry
|
||||
# @option options[String] :comment
|
||||
# an optional comment for this entry
|
||||
# @option options [Fixnum] :priority
|
||||
# the relative priority of this entry (compared to others)
|
||||
#
|
||||
# @raise [ArgumentError]
|
||||
# if neither :ip_address nor :hostname are supplied
|
||||
def initialize(options = {})
|
||||
if options[:ip_address].nil? || options[:hostname].nil?
|
||||
raise ArgumentError, ':ip_address and :hostname are both required options'
|
||||
end
|
||||
|
||||
@ip_address = IPAddr.new(remove_ip_scope(options[:ip_address]))
|
||||
@hostname = options[:hostname]
|
||||
@aliases = [options[:aliases]].flatten.compact
|
||||
@comment = options[:comment]
|
||||
@priority = options[:priority] || calculated_priority
|
||||
end
|
||||
|
||||
# Set a the new priority for an entry.
|
||||
#
|
||||
# @param [Fixnum] new_priority
|
||||
# the new priority to set
|
||||
def priority=(new_priority)
|
||||
@calculated_priority = false
|
||||
@priority = new_priority
|
||||
end
|
||||
|
||||
# The line representation of this entry.
|
||||
#
|
||||
# @return [String]
|
||||
# the string representation of this entry
|
||||
def to_line
|
||||
hosts = [hostname, aliases].flatten.join(' ')
|
||||
|
||||
comments = "# #{comment.to_s}".strip
|
||||
comments << " @#{priority}" unless priority.nil? || @calculated_priority
|
||||
comments = comments.strip
|
||||
comments = nil if comments == '#'
|
||||
|
||||
[ip_address, hosts, comments].compact.join("\t").strip
|
||||
end
|
||||
|
||||
# Returns true if priority is calculated
|
||||
#
|
||||
# @return [Boolean]
|
||||
# true if priority is calculated and false otherwise
|
||||
def calculated_priority?
|
||||
@calculated_priority
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Calculates the relative priority of this entry.
|
||||
#
|
||||
# @return [Fixnum]
|
||||
# the relative priority of this item
|
||||
def calculated_priority
|
||||
@calculated_priority = true
|
||||
|
||||
return 81 if ip_address == IPAddr.new('127.0.0.1')
|
||||
return 80 if IPAddr.new('127.0.0.0/8').include?(ip_address) # local
|
||||
return 60 if ip_address.ipv4? # ipv4
|
||||
return 20 if ip_address.ipv6? # ipv6
|
||||
return 00
|
||||
end
|
||||
|
||||
# Removes the scopes pieces of the address, because reasons.
|
||||
#
|
||||
# @see https://bugs.ruby-lang.org/issues/8464
|
||||
# @see https://github.com/customink-webops/hostsfile/issues/51
|
||||
#
|
||||
# @return [String, nil]
|
||||
#
|
||||
def remove_ip_scope(address)
|
||||
return nil if address.nil?
|
||||
address.to_s.sub(/%.*/, '')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
# Cookbook:: hostsfile
|
||||
# Library:: manipulator
|
||||
#
|
||||
# Copyright 2012-2013, Seth Vargo
|
||||
# Copyright 2012, CustomInk, LCC
|
||||
# Copyright:: 2012-2013, Seth Vargo
|
||||
# Copyright:: 2012, CustomInk, LCC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
@@ -20,274 +20,283 @@
|
||||
#
|
||||
|
||||
require 'chef/application'
|
||||
require 'digest/sha2'
|
||||
require 'openssl'
|
||||
|
||||
class Manipulator
|
||||
attr_reader :node
|
||||
attr_reader :entries
|
||||
module HostsFile
|
||||
class Manipulator
|
||||
attr_reader :node
|
||||
attr_reader :entries
|
||||
|
||||
# Create a new Manipulator object (aka an /etc/hosts manipulator). If a
|
||||
# hostsfile is not found, an exception is raised.
|
||||
#
|
||||
# @param [Chef::node] node
|
||||
# the current Chef node
|
||||
# @return [Manipulator]
|
||||
# a class designed to manipulate the node's /etc/hosts file
|
||||
def initialize(node)
|
||||
@node = node
|
||||
# Create a new Manipulator object (aka an /etc/hosts manipulator). If a
|
||||
# hostsfile is not found, an exception is raised.
|
||||
#
|
||||
# @param [Chef::node] node
|
||||
# the current Chef node
|
||||
# @return [Manipulator]
|
||||
# a class designed to manipulate the node's /etc/hosts file
|
||||
def initialize(node)
|
||||
@node = node
|
||||
|
||||
# Fail if no hostsfile is found
|
||||
unless ::File.exists?(hostsfile_path)
|
||||
raise RuntimeError, "No hostsfile exists at `#{hostsfile_path}'!"
|
||||
end
|
||||
|
||||
@entries = []
|
||||
collect_and_flatten(::File.readlines(hostsfile_path))
|
||||
end
|
||||
|
||||
# Return a list of all IP Addresses for this hostsfile.
|
||||
#
|
||||
# @return [Array<IPAddr>]
|
||||
# the list of IP Addresses
|
||||
def ip_addresses
|
||||
@entries.collect do |entry|
|
||||
entry.ip_address
|
||||
end.compact || []
|
||||
end
|
||||
|
||||
# Add a new record to the hostsfile.
|
||||
#
|
||||
# @param [Hash] options
|
||||
# a list of options to create the entry with
|
||||
# @option options [String] :ip_address
|
||||
# the IP Address for this entry
|
||||
# @option options [String] :hostname
|
||||
# the hostname for this entry
|
||||
# @option options [String, Array<String>] :aliases
|
||||
# a alias or array of aliases for this entry
|
||||
# @option options[String] :comment
|
||||
# an optional comment for this entry
|
||||
# @option options [Fixnum] :priority
|
||||
# the relative priority of this entry (compared to others)
|
||||
def add(options = {})
|
||||
entry = Entry.new(
|
||||
ip_address: options[:ip_address],
|
||||
hostname: options[:hostname],
|
||||
aliases: options[:aliases],
|
||||
comment: options[:comment],
|
||||
priority: options[:priority],
|
||||
)
|
||||
|
||||
@entries << entry
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
end
|
||||
|
||||
# Update an existing entry. This method will do nothing if the entry
|
||||
# does not exist.
|
||||
#
|
||||
# @param (see #add)
|
||||
def update(options = {})
|
||||
if entry = find_entry_by_ip_address(options[:ip_address])
|
||||
entry.hostname = options[:hostname]
|
||||
entry.aliases = options[:aliases]
|
||||
entry.comment = options[:comment]
|
||||
entry.priority = options[:priority]
|
||||
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
end
|
||||
end
|
||||
|
||||
# Append content to an existing entry. This method will add a new entry
|
||||
# if one does not already exist.
|
||||
#
|
||||
# @param (see #add)
|
||||
def append(options = {})
|
||||
if entry = find_entry_by_ip_address(options[:ip_address])
|
||||
hosts = normalize(entry.hostname, entry.aliases, options[:hostname], options[:aliases])
|
||||
entry.hostname = hosts.shift
|
||||
entry.aliases = hosts
|
||||
|
||||
unless entry.comment && options[:comment] && entry.comment.include?(options[:comment])
|
||||
entry.comment = normalize(entry.comment, options[:comment]).join(', ')
|
||||
# Fail if no hostsfile is found
|
||||
unless ::File.exist?(hostsfile_path)
|
||||
raise "No hostsfile exists at `#{hostsfile_path}'!"
|
||||
end
|
||||
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
else
|
||||
add(options)
|
||||
@entries = []
|
||||
collect_and_flatten(::File.readlines(hostsfile_path))
|
||||
end
|
||||
end
|
||||
|
||||
# Remove an entry by it's IP Address
|
||||
#
|
||||
# @param [String] ip_address
|
||||
# the IP Address of the entry to remove
|
||||
def remove(ip_address)
|
||||
if entry = find_entry_by_ip_address(ip_address)
|
||||
@entries.delete(entry)
|
||||
# Return a list of all IP Addresses for this hostsfile.
|
||||
#
|
||||
# @return [Array<IPAddr>]
|
||||
# the list of IP Addresses
|
||||
def ip_addresses
|
||||
@entries.collect(&:ip_address).compact || []
|
||||
end
|
||||
end
|
||||
|
||||
# Save the new hostsfile to the target machine. This method will only write the
|
||||
# hostsfile if the current version has changed. In other words, it is convergent.
|
||||
def save
|
||||
file = Chef::Resource::File.new(hostsfile_path, node.run_context)
|
||||
file.content(new_content)
|
||||
file.run_action(:create)
|
||||
end
|
||||
|
||||
# Determine if the content of the hostfile has changed by comparing sha
|
||||
# values of existing file and new content
|
||||
#
|
||||
# @return [Boolean]
|
||||
def content_changed?
|
||||
new_sha = Digest::SHA512.hexdigest(new_content)
|
||||
new_sha != current_sha
|
||||
end
|
||||
|
||||
# Find an entry by the given IP Address.
|
||||
#
|
||||
# @param [String] ip_address
|
||||
# the IP Address of the entry to find
|
||||
# @return [Entry, nil]
|
||||
# the corresponding entry object, or nil if it does not exist
|
||||
def find_entry_by_ip_address(ip_address)
|
||||
@entries.find do |entry|
|
||||
!entry.ip_address.nil? && entry.ip_address == ip_address
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if the current hostsfile contains the given resource. This
|
||||
# is really just a proxy to {find_resource_by_ip_address} /
|
||||
#
|
||||
# @param [Chef::Resource] resource
|
||||
#
|
||||
# @return [Boolean]
|
||||
def contains?(resource)
|
||||
!!find_entry_by_ip_address(resource.ip_address)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# The path to the current hostsfile.
|
||||
#
|
||||
# @return [String]
|
||||
# the full path to the hostsfile, depending on the operating system
|
||||
# can also be overriden in the node attributes
|
||||
def hostsfile_path
|
||||
return @hostsfile_path if @hostsfile_path
|
||||
@hostsfile_path = node['hostsfile']['path'] || case node['platform_family']
|
||||
when 'windows'
|
||||
"#{node['kernel']['os_info']['system_directory']}\\drivers\\etc\\hosts"
|
||||
else
|
||||
'/etc/hosts'
|
||||
end
|
||||
end
|
||||
|
||||
# The header of the new hostsfile
|
||||
#
|
||||
# @return [Array]
|
||||
# an array of header comments
|
||||
def hostsfile_header
|
||||
lines = []
|
||||
lines << '#'
|
||||
lines << '# This file is managed by Chef, using the hostsfile cookbook.'
|
||||
lines << '# Editing this file by hand is highly discouraged!'
|
||||
lines << '#'
|
||||
lines << '# Comments containing an @ sign should not be modified or else'
|
||||
lines << '# hostsfile will be unable to guarantee relative priority in'
|
||||
lines << '# future Chef runs!'
|
||||
lines << '#'
|
||||
lines << ''
|
||||
end
|
||||
|
||||
# The content that will be written to the hostfile
|
||||
#
|
||||
# @return [String]
|
||||
# the full contents of the hostfile to be written
|
||||
def new_content
|
||||
entries = hostsfile_header
|
||||
entries += unique_entries.map(&:to_line)
|
||||
entries << ''
|
||||
entries.join("\n")
|
||||
end
|
||||
|
||||
# The current sha of the system hostsfile.
|
||||
#
|
||||
# @return [String]
|
||||
# the sha of the current hostsfile
|
||||
def current_sha
|
||||
@current_sha ||= Digest::SHA512.hexdigest(File.read(hostsfile_path))
|
||||
end
|
||||
|
||||
# Normalize the given list of elements into a single array with no nil
|
||||
# values and no duplicate values.
|
||||
#
|
||||
# @param [Object] things
|
||||
#
|
||||
# @return [Array]
|
||||
# a normalized array of things
|
||||
def normalize(*things)
|
||||
things.flatten.compact.uniq
|
||||
end
|
||||
|
||||
# This is a crazy way of ensuring unique objects in an array using a Hash.
|
||||
#
|
||||
# @return [Array]
|
||||
# the sorted list of entires that are unique
|
||||
def unique_entries
|
||||
entries = Hash[*@entries.map { |entry| [entry.ip_address, entry] }.flatten].values
|
||||
entries.sort_by { |e| [-e.priority.to_i, e.hostname.to_s] }
|
||||
end
|
||||
|
||||
# Takes /etc/hosts file contents and builds a flattened entries
|
||||
# array so that each IP address has only one line and multiple hostnames
|
||||
# are flattened into a list of aliases.
|
||||
#
|
||||
# @param [Array] contents
|
||||
# Array of lines from /etc/hosts file
|
||||
def collect_and_flatten(contents)
|
||||
contents.each do |line|
|
||||
entry = Entry.parse(line)
|
||||
next if entry.nil?
|
||||
|
||||
append(
|
||||
ip_address: entry.ip_address,
|
||||
hostname: entry.hostname,
|
||||
aliases: entry.aliases,
|
||||
comment: entry.comment,
|
||||
priority: !entry.calculated_priority? && entry.priority,
|
||||
# Add a new record to the hostsfile.
|
||||
#
|
||||
# @param [Hash] options
|
||||
# a list of options to create the entry with
|
||||
# @option options [String] :ip_address
|
||||
# the IP Address for this entry
|
||||
# @option options [String] :hostname
|
||||
# the hostname for this entry
|
||||
# @option options [String, Array<String>] :aliases
|
||||
# a alias or array of aliases for this entry
|
||||
# @option options[String] :comment
|
||||
# an optional comment for this entry
|
||||
# @option options [Integer] :priority
|
||||
# the relative priority of this entry (compared to others)
|
||||
def add(options = {})
|
||||
entry = HostsFile::Entry.new(
|
||||
ip_address: options[:ip_address],
|
||||
hostname: options[:hostname],
|
||||
aliases: options[:aliases],
|
||||
comment: options[:comment],
|
||||
priority: options[:priority]
|
||||
)
|
||||
|
||||
@entries << entry
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
end
|
||||
end
|
||||
|
||||
# Removes duplicate hostnames in other files ensuring they are unique
|
||||
#
|
||||
# @param [Entry] entry
|
||||
# the entry to keep the hostname and aliases from
|
||||
#
|
||||
# @return [nil]
|
||||
def remove_existing_hostnames(entry)
|
||||
@entries.delete(entry)
|
||||
changed_hostnames = [entry.hostname, entry.aliases].flatten.uniq
|
||||
# Update an existing entry. This method will do nothing if the entry
|
||||
# does not exist.
|
||||
#
|
||||
# @param (see #add)
|
||||
def update(options = {})
|
||||
if entry = find_entry_by_ip_address(options[:ip_address])
|
||||
entry.hostname = options[:hostname]
|
||||
entry.aliases = options[:aliases]
|
||||
entry.comment = options[:comment]
|
||||
entry.priority = options[:priority]
|
||||
|
||||
@entries = @entries.collect do |entry|
|
||||
entry.hostname = nil if changed_hostnames.include?(entry.hostname)
|
||||
entry.aliases = entry.aliases - changed_hostnames
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
end
|
||||
end
|
||||
|
||||
if entry.hostname.nil?
|
||||
if entry.aliases.empty?
|
||||
nil
|
||||
# Append content to an existing entry. This method will add a new entry
|
||||
# if one does not already exist.
|
||||
#
|
||||
# @param (see #add)
|
||||
def append(options = {})
|
||||
if entry = find_entry_by_ip_address(options[:ip_address])
|
||||
hosts = normalize(entry.hostname, entry.aliases, options[:hostname], options[:aliases])
|
||||
entry.hostname = hosts.shift
|
||||
entry.aliases = hosts
|
||||
|
||||
unless entry.comment && options[:comment] && entry.comment.include?(options[:comment])
|
||||
entry.comment = normalize(entry.comment, options[:comment]).join(', ')
|
||||
end
|
||||
|
||||
remove_existing_hostnames(entry) if options[:unique]
|
||||
else
|
||||
add(options)
|
||||
end
|
||||
end
|
||||
|
||||
# Remove an entry by it's IP Address
|
||||
#
|
||||
# @param [String] ip_address
|
||||
# the IP Address of the entry to remove
|
||||
def remove(ip_address)
|
||||
if entry = find_entry_by_ip_address(ip_address)
|
||||
@entries.delete(entry)
|
||||
end
|
||||
end
|
||||
|
||||
# Save the new hostsfile to the target machine. This method will only write the
|
||||
# hostsfile if the current version has changed. In other words, it is convergent.
|
||||
def save
|
||||
file = Chef::Resource::File.new(hostsfile_path, node.run_context)
|
||||
file.content(new_content)
|
||||
file.atomic_update false if docker_guest?
|
||||
file.run_action(:create)
|
||||
end
|
||||
|
||||
# Determine if the content of the hostfile has changed by comparing sha
|
||||
# values of existing file and new content
|
||||
#
|
||||
# @return [Boolean]
|
||||
def content_changed?
|
||||
new_sha = OpenSSL::Digest::SHA512.hexdigest(new_content)
|
||||
new_sha != current_sha
|
||||
end
|
||||
|
||||
# Find an entry by the given IP Address.
|
||||
#
|
||||
# @param [String] ip_address
|
||||
# the IP Address of the entry to find
|
||||
# @return [Entry, nil]
|
||||
# the corresponding entry object, or nil if it does not exist
|
||||
def find_entry_by_ip_address(ip_address)
|
||||
@entries.find do |entry|
|
||||
!entry.ip_address.nil? && entry.ip_address == ip_address
|
||||
end
|
||||
end
|
||||
|
||||
# Determine if the current hostsfile contains the given resource. This
|
||||
# is really just a proxy to {find_resource_by_ip_address} /
|
||||
#
|
||||
# @param [Chef::Resource] resource
|
||||
#
|
||||
# @return [Boolean]
|
||||
def contains?(resource)
|
||||
!!find_entry_by_ip_address(resource.ip_address)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Determine if we are running inside a Docker container
|
||||
#
|
||||
# @return [Boolean]
|
||||
def docker_guest?
|
||||
node['virtualization'] && node['virtualization']['systems'] &&
|
||||
node['virtualization']['systems']['docker'] && node['virtualization']['systems']['docker'] == 'guest'
|
||||
end
|
||||
|
||||
# The path to the current hostsfile.
|
||||
#
|
||||
# @return [String]
|
||||
# the full path to the hostsfile, depending on the operating system
|
||||
# can also be overriden in the node attributes
|
||||
def hostsfile_path
|
||||
return @hostsfile_path if @hostsfile_path
|
||||
@hostsfile_path = node['hostsfile']['path'] || case node['platform_family']
|
||||
when 'windows'
|
||||
"#{node['kernel']['os_info']['system_directory']}\\drivers\\etc\\hosts"
|
||||
else
|
||||
'/etc/hosts'
|
||||
end
|
||||
end
|
||||
|
||||
# The header of the new hostsfile
|
||||
#
|
||||
# @return [Array]
|
||||
# an array of header comments
|
||||
def hostsfile_header
|
||||
lines = []
|
||||
lines << '#'
|
||||
lines << '# This file is managed by Chef, using the hostsfile cookbook.'
|
||||
lines << '# Editing this file by hand is highly discouraged!'
|
||||
lines << '#'
|
||||
lines << '# Comments containing an @ sign should not be modified or else'
|
||||
lines << '# hostsfile will be unable to guarantee relative priority in'
|
||||
lines << '# future Chef runs!'
|
||||
lines << '#'
|
||||
lines << ''
|
||||
end
|
||||
|
||||
# The content that will be written to the hostfile
|
||||
#
|
||||
# @return [String]
|
||||
# the full contents of the hostfile to be written
|
||||
def new_content
|
||||
entries = hostsfile_header
|
||||
entries += unique_entries.map(&:to_line)
|
||||
entries << ''
|
||||
entries.join("\n")
|
||||
end
|
||||
|
||||
# The current sha of the system hostsfile.
|
||||
#
|
||||
# @return [String]
|
||||
# the sha of the current hostsfile
|
||||
def current_sha
|
||||
@current_sha ||= OpenSSL::Digest::SHA512.hexdigest(File.read(hostsfile_path))
|
||||
end
|
||||
|
||||
# Normalize the given list of elements into a single array with no nil
|
||||
# values and no duplicate values.
|
||||
#
|
||||
# @param [Object] things
|
||||
#
|
||||
# @return [Array]
|
||||
# a normalized array of things
|
||||
def normalize(*things)
|
||||
things.flatten.compact.uniq
|
||||
end
|
||||
|
||||
# This is a crazy way of ensuring unique objects in an array using a Hash.
|
||||
#
|
||||
# @return [Array]
|
||||
# the sorted list of entires that are unique
|
||||
def unique_entries
|
||||
entries = Hash[*@entries.map { |entry| [entry.ip_address, entry] }.flatten].values
|
||||
entries.sort_by { |e| [-e.priority.to_i, e.hostname.to_s] }
|
||||
end
|
||||
|
||||
# Takes /etc/hosts file contents and builds a flattened entries
|
||||
# array so that each IP address has only one line and multiple hostnames
|
||||
# are flattened into a list of aliases.
|
||||
#
|
||||
# @param [Array] contents
|
||||
# Array of lines from /etc/hosts file
|
||||
def collect_and_flatten(contents)
|
||||
contents.each do |line|
|
||||
entry = HostsFile::Entry.parse(line)
|
||||
next if entry.nil?
|
||||
|
||||
append(
|
||||
ip_address: entry.ip_address,
|
||||
hostname: entry.hostname,
|
||||
aliases: entry.aliases,
|
||||
comment: entry.comment,
|
||||
priority: !entry.calculated_priority? && entry.priority
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# Removes duplicate hostnames in other files ensuring they are unique
|
||||
#
|
||||
# @param [Entry] entry
|
||||
# the entry to keep the hostname and aliases from
|
||||
#
|
||||
# @return [nil]
|
||||
def remove_existing_hostnames(entry)
|
||||
@entries.delete(entry)
|
||||
changed_hostnames = [entry.hostname, entry.aliases].flatten.uniq
|
||||
|
||||
@entries = @entries.collect do |entry|
|
||||
entry.hostname = nil if changed_hostnames.include?(entry.hostname)
|
||||
entry.aliases = entry.aliases - changed_hostnames
|
||||
|
||||
if entry.hostname.nil?
|
||||
if entry.aliases.empty?
|
||||
nil
|
||||
else
|
||||
entry.hostname = entry.aliases.shift
|
||||
entry
|
||||
end
|
||||
else
|
||||
entry.hostname = entry.aliases.shift
|
||||
entry
|
||||
end
|
||||
else
|
||||
entry
|
||||
end
|
||||
end.compact
|
||||
end.compact
|
||||
|
||||
@entries << entry
|
||||
@entries << entry
|
||||
|
||||
nil
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user