This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
forked from kohder/chef-hosts-awareness
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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! | ||
|
@@ -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 | ||
|
||
|
@@ -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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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' | ||
|
@@ -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 |