From da029b09923bca5140dec73315978583b312dd35 Mon Sep 17 00:00:00 2001 From: mattray Date: Fri, 27 Jul 2012 14:44:16 -0500 Subject: [PATCH] synced to osops-utils 1.0.3 from rcbops --- cookbooks/osops-utils/libraries/patch.rb | 47 +++++++++++ cookbooks/osops-utils/recipes/autoetchosts.rb | 78 +++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 cookbooks/osops-utils/libraries/patch.rb create mode 100644 cookbooks/osops-utils/recipes/autoetchosts.rb diff --git a/cookbooks/osops-utils/libraries/patch.rb b/cookbooks/osops-utils/libraries/patch.rb new file mode 100644 index 0000000..444904e --- /dev/null +++ b/cookbooks/osops-utils/libraries/patch.rb @@ -0,0 +1,47 @@ +# +# Cookbook Name:: osops-utils +# Library:: Chef::Recipe::Patch +# +# Copyright 2009, Rackspace Hosting, Inc. +# +# 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. +# + +class Chef::Recipe::Patch + def self.check_package_version(package,version,nodeish = nil) + nodeish = node unless nodeish + if not nodeish["nova"]["apply_patches"] + Chef::Log.info("osops-utils/patch: package #{package} skipping hotfix for #{version} due to node settings") + return false + end + case nodeish["platform"] + when "ubuntu", "debian" + Chef::ShellOut.new("apt-cache policy #{package}").run_command.stdout.each_line do |line| + case line + when /^\s{2}Installed: (.+)$/ + Chef::Log.info("osops-utils/patch: package #{package} requires a hotfix for version #{version}") + return $1 == version + end + end + when "fedora", "centos", "rhel", "scientific" + #TODO(breu): need to test this for fedora + Chef::ShellOut.new("rpm -q --queryformat '%{VERSION}-%{RELEASE}\n' #{package}").run_command.stdout.each_line do |line| + case line + when /^([\w\d_.-]+)$/ + Chef::Log.info("osops-utils/patch: package #{package} requires a hotfix for version #{version}") + return $1 == version + end + end + end + end +end diff --git a/cookbooks/osops-utils/recipes/autoetchosts.rb b/cookbooks/osops-utils/recipes/autoetchosts.rb new file mode 100644 index 0000000..b8094a1 --- /dev/null +++ b/cookbooks/osops-utils/recipes/autoetchosts.rb @@ -0,0 +1,78 @@ +# +# Cookbook Name:: osops-utils +# Recipe:: autoetchosts +# +# Copyright 2012, Rackspace Hosting +# +# 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 'tempfile' +require 'fileutils' + +# Find all nodes, sorting by Chef ID so their +# order doesn't change between runs. +if Chef::Config[:solo] + Chef::Log.warn("This recipe uses search. Chef Solo does not support search.") +else + hosts = search(:node, "chef_environment:#{node.chef_environment}") + + Chef::Log.info("osops-utils/autoetchosts: Setting up /etc/hosts for #{hosts.length} entries") + + Chef::Log.info("osops-utils/autoetchosts: reading /etc/hosts") + hostsfile = Array.new + File.open("/etc/hosts", "r") do |infile| + marker=0 + while (line = infile.gets) + if line =~ /^# \*\*\* START CHEF MANAGED HOSTS - DO NOT DELETE THIS MARKER \*\*\*$/i + marker=1 + end + if (marker==1) + # drop this line on the ground + else + # store this line for later use + marker=0 + hostsfile << line + end + if line =~ /^# \*\*\* END CHEF MANAGED HOSTS - DO NOT DELETE THIS MARKER\*\*\*$/i + # drop this line on the ground + marker=0 + end + end + end + + hostsfile << "# *** START CHEF MANAGED HOSTS - DO NOT DELETE THIS MARKER ***\n" + hostsfile << "# *** Do not edit anything between the START and END blocks ***\n" + hostsfile << "# *** Chef will overwrite anything between these blocks ***\n" + hosts.each do |host| + begin + ip = ::Chef::Recipe::IPManagement.get_ip_for_net("management",host) + stra = String.new("#{ip} #{host["fqdn"]} #{host["hostname"]}\n") + hostsfile << stra + rescue + Chef::Log.info("osops-utils/autoetchosts: skipping this node because it doesn't have a network assigned yet") + end + end + hostsfile << "# *** END CHEF MANAGED HOSTS - DO NOT DELETE THIS MARKER***\n" + + f = Tempfile.new('hosts','/tmp') + tmppath = f.path + Chef::Log.info("osops-utils/autoetchosts: writing #{tmppath}") + hostsfile.each do |line| + f.write(line) + end + f.close + Chef::Log.info("osops-utils/autoetchosts: moving #{tmppath} to /etc/hosts") + FileUtils.chmod(0644,tmppath); + FileUtils.mv(tmppath,'/etc/hosts', :force => true) +end