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.
Added initial impl of etc_hosts recipe
- Loading branch information
Showing
9 changed files
with
346 additions
and
47 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.bundle/ | ||
.idea | ||
/bin |
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,98 @@ | ||
# | ||
# Cookbook Name:: hosts-awareness | ||
# Library:: EtcHosts | ||
# | ||
# Copyright 2011, 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. | ||
# | ||
|
||
module HostsAwareness | ||
class EtcHosts | ||
attr_reader :etc_hosts_file | ||
attr_writer :block_token | ||
attr_accessor :use_private_addresses | ||
|
||
def initialize(etc_hosts_file) | ||
@etc_hosts_file = etc_hosts_file | ||
@use_private_addresses = false | ||
end | ||
|
||
def block_token | ||
@block_token || 'chef nodes' | ||
end | ||
|
||
def set_hosts(hosts) | ||
write_out!(hosts) | ||
end | ||
|
||
def empty! | ||
write_out!([]) | ||
end | ||
|
||
protected | ||
|
||
def host_entry(host) | ||
if use_private_addresses | ||
[host.private_ipv4, host.hostname, host.short_hostname, host.provider_public_hostname, host.provider_private_hostname] | ||
else | ||
[host.public_ipv4, host.hostname, host.short_hostname, host.provider_public_hostname, host.provider_private_hostname] | ||
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)} | ||
end | ||
a.transpose.inject(''){|s, row| s << row.join(' ').rstrip + "\n"; s} | ||
end | ||
|
||
def write_out!(hosts) | ||
host_entries_string = format_host_entries(hosts) | ||
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 | ||
elsif line =~ /^#{end_token}/ | ||
over = false | ||
end | ||
out << line unless over | ||
end | ||
if !seen_tokens | ||
out << surround_with_tokens(host_entries_string) | ||
end | ||
|
||
f.pos = 0 | ||
f.print out | ||
f.truncate(f.pos) | ||
end | ||
end | ||
|
||
def surround_with_tokens(str) | ||
"\n#{start_token}\n" + str + "#{end_token}\n" | ||
end | ||
|
||
def start_token() | ||
"## #{block_token} start" | ||
end | ||
|
||
def end_token() | ||
"## #{block_token} end" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# | ||
# Cookbook Name:: hosts-awareness | ||
# Library:: Host | ||
# | ||
# Copyright 2011, 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. | ||
# | ||
|
||
module HostsAwareness | ||
class Host | ||
attr_reader :network | ||
attr_accessor :hostname | ||
attr_accessor :private_ipv4 | ||
attr_accessor :public_ipv4 | ||
attr_accessor :provider_private_hostname | ||
attr_accessor :provider_public_hostname | ||
|
||
def self.from_node(node, network=nil) | ||
host = new(network) | ||
host.hostname = node['fqdn'] || "#{node['hostname']}.#{node['domain']}" | ||
if node['cloud'] && node['cloud']['local_ipv4'] | ||
host.private_ipv4 = node['cloud']['local_ipv4'] | ||
elsif node['ec2'] && node['ec2']['local_ipv4'] | ||
host.private_ipv4 = node['ec2']['local_ipv4'] | ||
else | ||
host.private_ipv4 = node['ipaddress'] | ||
end | ||
|
||
if node['cloud'] && node['cloud']['public_ipv4'] | ||
host.public_ipv4 = node['cloud']['public_ipv4'] | ||
elsif node['ec2'] && node['ec2']['public_ipv4'] | ||
host.public_ipv4 = node['ec2']['public_ipv4'] | ||
end | ||
|
||
if node['cloud'] && node['cloud']['local_hostname'] | ||
host.provider_private_hostname = node['cloud']['local_hostname'] | ||
elsif node['ec2'] && node['ec2']['local_hostname'] | ||
host.provider_private_hostname = node['ec2']['local_hostname'] | ||
end | ||
|
||
if node['cloud'] && node['cloud']['public_hostname'] | ||
host.provider_public_hostname = node['cloud']['public_hostname'] | ||
elsif node['ec2'] && node['ec2']['public_hostname'] | ||
host.provider_public_hostname = node['ec2']['public_hostname'] | ||
end | ||
host | ||
end | ||
|
||
def initialize(network=nil) | ||
@network = network | ||
end | ||
|
||
def short_hostname | ||
@short_hostname ||= begin | ||
if @network && @network.short_hostname_parts && @network.short_hostname_parts > 0 | ||
@hostname.split('.').shift(@network.short_hostname_parts).join('.') | ||
else | ||
@hostname.split('.').first | ||
end | ||
end | ||
end | ||
|
||
def domain_name | ||
@domain_name ||= @hostname.partition('.').last | ||
end | ||
|
||
def ==(other) | ||
other.equal?(self) || (other.instance_of?(self.class) && other.hostname == @hostname) | ||
end | ||
|
||
def eql?(other) | ||
self == (other) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# | ||
# Cookbook Name:: hosts-awareness | ||
# Library:: Network | ||
# | ||
# Copyright 2011, 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 Network | ||
attr_reader :name | ||
attr_accessor :provider | ||
attr_accessor :node_search_query | ||
attr_accessor :short_hostname_parts | ||
|
||
def self.from_data_bag(network_name) | ||
network = HostsAwareness::Network.new(network_name) | ||
all_network_names = data_bag('networks') | ||
if all_network_names.include?(network_name) | ||
network_data_bag_item = data_bag_item('networks', network_name) | ||
network.provider = network_data_bag_item['provider'] | ||
network.node_search_query = network_data_bag_item['node_search_query'] | ||
network.short_hostname_parts = network_data_bag_item['short_hostname_parts'].to_i | ||
else | ||
Chef::Log.error("No data bag entry for network \"#{network_name}\"") | ||
end | ||
network | ||
end | ||
|
||
def initialize(network_name) | ||
@name = network_name | ||
end | ||
|
||
def hosts | ||
@hosts ||= begin | ||
hosts = nodes.collect{|n| HostsAwareness::Host.from_node(n, self)} | ||
hosts.uniq | ||
end | ||
end | ||
|
||
def nodes | ||
@nodes ||= @node_search_query.nil? ? [] : search(:node, @node_search_query) | ||
end | ||
|
||
def member?(node_or_host) | ||
if node_or_host.is_a?(HostsAwareness::Host) | ||
hosts.any?{|host| host == node_or_host} | ||
else | ||
nodes.any?{|node| node.name == node_or_host.name} | ||
end | ||
end | ||
|
||
def ==(other) | ||
other.equal?(self) || (other.instance_of?(self.class) && other.name == @name) | ||
end | ||
|
||
def eql?(other) | ||
self == (other) | ||
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
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
Oops, something went wrong.