Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
added support for hostname aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
kohder committed Jul 12, 2012
1 parent 0a774fc commit cb77776
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 26 deletions.
10 changes: 6 additions & 4 deletions attributes/ssh_known_hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Cookbook Name:: hosts-awareness
# Attributes:: ssh_known_hosts
#
# Copyright 2011, Rob Lewis <[email protected]>
# Copyright 2012, Rob Lewis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@
# limitations under the License.
#

default['hosts_aware']['ssh_known_hosts']['file_owner'] = nil
default['hosts_aware']['ssh_known_hosts']['file_group'] = nil
default['hosts_aware']['ssh_known_hosts']['all_users'] = false
default['hosts_awareness']['ssh_known_hosts']['file_owner'] = nil

default['hosts_awareness']['ssh_known_hosts']['file_group'] = nil

default['hosts_awareness']['ssh_known_hosts']['all_users'] = false
49 changes: 31 additions & 18 deletions libraries/etc_hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Cookbook Name:: hosts-awareness
# Library:: EtcHosts
#
# Copyright 2011, Rob Lewis <[email protected]>
# Copyright 2012, Rob Lewis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,8 @@ def block_token
@block_token || 'chef nodes'
end

def set_hosts(hosts)
write_out!(hosts)
def set_hosts(hosts, alias_mappings=nil)
write_out!(hosts, alias_mappings)
end

def empty!
Expand All @@ -42,38 +42,51 @@ def empty!

protected

def host_entry(host)
def host_entry(host, alias_mappings)
aliases = []
aliases << (host.has_short_hostname? ? host.short_hostname : '')
aliases << host.provider_public_hostname
aliases << host.provider_private_hostname
aliases.concat(alias_mappings[host.hostname]) unless alias_mappings.nil? || alias_mappings[host.hostname].nil?

if use_private_addresses
[host.private_ipv4, host.hostname, host.short_hostname, host.provider_public_hostname, host.provider_private_hostname]
[host.private_ipv4, host.hostname] + aliases
else
[host.public_ipv4, host.hostname, host.short_hostname, host.provider_public_hostname, host.provider_private_hostname]
[host.public_ipv4, host.hostname] + aliases
end
end

def format_host_entries(hosts)
host_entries = hosts.sort{|a,b| a.hostname <=> b.hostname}.collect{|host| host_entry(host)}
a = host_entries.transpose
a = a.map do |col|
w = col.map{|cell| cell.to_s.length}.max
col.map{|cell| cell.to_s.ljust(w)}
def format_host_entries(hosts, alias_mappings)
host_entries = hosts.sort{|a,b| a.hostname <=> b.hostname}.collect{|host| host_entry(host, alias_mappings)}
column_widths = []
host_entries.each do |row|
row.each_with_index do |cell, i|
len = cell.to_s.length
column_widths[i] = len if column_widths[i].nil? || len > column_widths[i]
end
end
a.transpose.inject(''){|s, row| s << row.join(' ').rstrip + "\n"; s}
host_entries.map do |row|
row.each_with_index.inject('') do |s, (cell, i)|
s << cell.to_s.ljust(column_widths[i]+2)
s
end.rstrip
end.join("\n")
end

def write_out!(hosts)
host_entries_string = format_host_entries(hosts)
def write_out!(hosts, alias_mappings=nil)
host_entries_string = format_host_entries(hosts, alias_mappings)
File.open(@etc_hosts_file, 'r+') do |f|
out, over, seen_tokens = '', false, false
f.each do |line|
if line =~ /^#{start_token}/
over = seen_tokens = true
out << line << host_entries_string
out << line << host_entries_string << "\n"
elsif line =~ /^#{end_token}/
over = false
end
out << line unless over
end
if !seen_tokens
unless seen_tokens
out << surround_with_tokens(host_entries_string)
end

Expand All @@ -84,7 +97,7 @@ def write_out!(hosts)
end

def surround_with_tokens(str)
"\n#{start_token}\n" + str + "#{end_token}\n"
"\n#{start_token}\n" + str + "\n#{end_token}\n"
end

def start_token()
Expand Down
6 changes: 5 additions & 1 deletion libraries/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Cookbook Name:: hosts-awareness
# Library:: Host
#
# Copyright 2011, Rob Lewis <[email protected]>
# Copyright 2021, Rob Lewis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,6 +71,10 @@ def short_hostname
end
end

def has_short_hostname?
!hostname.eql?(short_hostname)
end

def domain_name
@domain_name ||= @hostname.partition('.').last
end
Expand Down
54 changes: 54 additions & 0 deletions libraries/host_aliases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#
# Cookbook Name:: hosts-awareness
# Library:: HostAliases
#
# Copyright 2012, Rob Lewis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/mixin/language'
include Chef::Mixin::Language

module HostsAwareness
class HostAliases
attr_accessor :alias_mappings

def self.from_data_bags(mapping_names)
all_host_alias_mappings = data_bag('host_aliases')
host_aliases = HostsAwareness::HostAliases.new
mapping_names.each do |mapping_name|
if all_host_alias_mappings.include?(mapping_name)
mapping_data_bag_item = data_bag_item('host_aliases', mapping_name)
alias_mappings = mapping_data_bag_item['alias_mappings']
unless alias_mappings.nil?
alias_mappings.each do |hostname, hostname_aliases|
if host_aliases.alias_mappings[hostname].nil?
host_aliases.alias_mappings[hostname] = Array(hostname_aliases)
else
host_aliases.alias_mappings[hostname].concat(Array(hostname_aliases))
end
end
end
else
Chef::Log.error("No data bag entry for host alias mappings \"#{mapping_name}\"")
end
end
host_aliases
end

def initialize
@alias_mappings = {}
end
end
end
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
license 'Apache 2.0'
description 'Installs/Configures hosts-awareness'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.rdoc'))
version '0.0.3'
version '0.0.4'

recipe 'hosts-awareness', 'Includes all recipes'
recipe 'hosts-awareness::etc_hosts', 'Updates /etc/hosts with node entries.'
Expand Down
8 changes: 6 additions & 2 deletions recipes/etc_hosts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Cookbook Name:: hosts-awareness
# Recipe:: etc_hosts
#
# Copyright 2011, Rob Lewis <[email protected]>
# Copyright 2012, Rob Lewis <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,10 @@

ruby_block 'update_etc_hosts' do
block do
all_host_alias_mappings = data_bag('host_aliases')
host_alias_mappings = node['hosts_awareness'].nil? ? [] : Array(node['hosts_awareness']['host_aliases'])
host_alias_mappings = all_host_alias_mappings if host_alias_mappings.first == 'all'
alias_mappings = HostsAwareness::HostAliases.from_data_bags(host_alias_mappings).alias_mappings
all_network_names = data_bag('networks')
networks = node['hosts_awareness'].nil? ? [] : Array(node['hosts_awareness']['networks'])
networks = all_network_names if networks.first == 'all'
Expand All @@ -27,7 +31,7 @@
etc_hosts = HostsAwareness::EtcHosts.new('/etc/hosts')
etc_hosts.block_token = network_name
etc_hosts.use_private_addresses = network.member?(node)
etc_hosts.set_hosts(network.hosts)
etc_hosts.set_hosts(network.hosts, alias_mappings)
end
end
end

0 comments on commit cb77776

Please sign in to comment.