From d25843b345e927ca85a53c05eb960fbb270d931d Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Tue, 6 May 2014 11:28:03 -0500 Subject: [PATCH 01/12] Add public_info Ohai plugin --- files/default/public_info.rb | 41 ++++++++++++++++++++++++++++++++++++ metadata.rb | 2 +- recipes/default.rb | 1 + recipes/public_info.rb | 34 ++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 files/default/public_info.rb create mode 100644 recipes/public_info.rb diff --git a/files/default/public_info.rb b/files/default/public_info.rb new file mode 100644 index 0000000..3786f93 --- /dev/null +++ b/files/default/public_info.rb @@ -0,0 +1,41 @@ +#available at https://github.com/rackops/public_info +# +# Author:: Thomas Cate (thomas.cate@rackspace.com) +# Copyright:: Copyright (c) 2014 Rackspace, Inc. +# License:: Apache License, Version 2.0 +# +# 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. + +provides 'public_info' + +require 'json' +require 'rest-client' + +url = 'http://whoami.rackops.org/api' + +begin + response = RestClient.get(url) + results = JSON.parse(response) + + if not results.nil? + public_info Mash.new + public_info[:remote_ip] = results['remote_ip'] + public_info[:X_Forwarded] = results['X_Forwarded'] + public_info[:asn] = results['asn'] + public_info[:city] = results['city'] + public_info[:country] = results['country'] + end + +rescue RestClient::Exception + Ohai::Log.debug("Failed to return info from whoami.rackops.org") +end \ No newline at end of file diff --git a/metadata.rb b/metadata.rb index 9203be1..d5a1506 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'All rights reserved' description 'Installs/Configures rackops_rolebook' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '1.2.1' +version '1.3.0' depends 'chef-client', '~> 3.3' depends 'rackspace_user', '~> 1.0' diff --git a/recipes/default.rb b/recipes/default.rb index 7937b31..597c50c 100644 --- a/recipes/default.rb +++ b/recipes/default.rb @@ -11,6 +11,7 @@ rackspace_user::rack_user rackspace_motd rackspace_ntp + rackops_rolebook::public_info ) if node['rackops_rolebook']['include_acl'] == true diff --git a/recipes/public_info.rb b/recipes/public_info.rb new file mode 100644 index 0000000..6128c0a --- /dev/null +++ b/recipes/public_info.rb @@ -0,0 +1,34 @@ +# +# Cookbook Name:: rackops_rolebook +# Recipe:: public_info +# +# Populate public_info attributes via an ohai plugin +# +# Copyright 2014, Rackspace, US Inc. +# +# All rights reserved - Do Not Redistribute +# + +# Ensure the plugin directory exists +plugin_directory = directory "#{node['ohai']['plugin_path']}" do + owner "root" + group "root" + mode "0755" + recursive true +end + +plugin_install = cookbook_file "#{node['ohai']['plugin_path']}/public_info.rb" do + action :create + owner "root" + group "root" + mode "0644" +end + +reload_ohai = ohai "reload" do + action :nothing +end + +# Run during compile, not convergance +plugin_directory.run_action(:create) +plugin_install.run_action(:create) +reload_ohai.run_action(:reload) From 779d3732e28c77228ce1f378d44d36ac28351235 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Tue, 6 May 2014 13:07:27 -0500 Subject: [PATCH 02/12] Update for v1.3.0 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90b7a9a..8729f08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ This file is used to list changes made in each version of rackops_rolebook. +## 1.3.0 +* Add public_info Ohai plugin to populate public_info attributes + ## 1.2.0 * Add rackspace_cloudmonitoring as a dependency to enable standard checks From 94c02b3192fbcfadd4597f899f5957c6cb2857ac Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 12:13:48 -0500 Subject: [PATCH 03/12] Add ExternalIP tag --- recipes/public_info.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index 6128c0a..ad2658d 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -32,3 +32,7 @@ plugin_directory.run_action(:create) plugin_install.run_action(:create) reload_ohai.run_action(:reload) + +# Assign the external_ip tag to the node +tag("ExternalIP:#{node['public_info']['remote_ip'] }") + From 4cf9c65d273ee2bf4139e89ed9e7daefc5b8f963 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 12:14:41 -0500 Subject: [PATCH 04/12] Use RemoteIP not ExternalIP: keep it consistent --- recipes/public_info.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index ad2658d..989b30e 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -34,5 +34,5 @@ reload_ohai.run_action(:reload) # Assign the external_ip tag to the node -tag("ExternalIP:#{node['public_info']['remote_ip'] }") +tag("RemoteIP:#{node['public_info']['remote_ip'] }") From d59985e6f223ce0085544e6dd5ae5ed1d3432f9f Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 12:46:59 -0500 Subject: [PATCH 05/12] Style Fixes --- Gemfile | 2 +- files/default/public_info.rb | 10 +++++----- recipes/public_info.rb | 15 +++++++-------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index a54d492..db93b78 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,7 @@ group :testing do gem 'strainer', '~> 3.3' gem 'chef', '~> 11.8' gem 'rspec', '~> 2.14' - gem 'rubocop', '~> 0.21.0' + gem 'rubocop', '~> 0.18.0' gem 'rake', '~> 10.1' end diff --git a/files/default/public_info.rb b/files/default/public_info.rb index 3786f93..6878c89 100644 --- a/files/default/public_info.rb +++ b/files/default/public_info.rb @@ -1,4 +1,4 @@ -#available at https://github.com/rackops/public_info +# available at https://github.com/rackops/public_info # # Author:: Thomas Cate (thomas.cate@rackspace.com) # Copyright:: Copyright (c) 2014 Rackspace, Inc. @@ -26,8 +26,8 @@ begin response = RestClient.get(url) results = JSON.parse(response) - - if not results.nil? + + unless results.nil? public_info Mash.new public_info[:remote_ip] = results['remote_ip'] public_info[:X_Forwarded] = results['X_Forwarded'] @@ -37,5 +37,5 @@ end rescue RestClient::Exception - Ohai::Log.debug("Failed to return info from whoami.rackops.org") -end \ No newline at end of file + Ohai::Log.debug('Failed to return info from whoami.rackops.org') +end diff --git a/recipes/public_info.rb b/recipes/public_info.rb index 989b30e..cc0dc32 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -11,20 +11,20 @@ # Ensure the plugin directory exists plugin_directory = directory "#{node['ohai']['plugin_path']}" do - owner "root" - group "root" - mode "0755" + owner 'root' + group 'root' + mode '0755' recursive true end plugin_install = cookbook_file "#{node['ohai']['plugin_path']}/public_info.rb" do action :create - owner "root" - group "root" - mode "0644" + owner 'root' + group 'root' + mode '0644' end -reload_ohai = ohai "reload" do +reload_ohai = ohai 'reload' do action :nothing end @@ -35,4 +35,3 @@ # Assign the external_ip tag to the node tag("RemoteIP:#{node['public_info']['remote_ip'] }") - From ee4f2c04bc6568f94d3f73cf7fa7327cbbb371ac Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 13:26:50 -0500 Subject: [PATCH 06/12] Add ohai dep, only add tag with a IPv4 IP --- metadata.rb | 1 + recipes/public_info.rb | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/metadata.rb b/metadata.rb index d5a1506..f10de4c 100644 --- a/metadata.rb +++ b/metadata.rb @@ -13,3 +13,4 @@ depends 'rackspace_ntp', '~> 2.0' depends 'rackspace_iptables', '~> 1.2' depends 'rackspace_cloudmonitoring', '~> 3.1' +depends 'ohai', '~> 1.1' diff --git a/recipes/public_info.rb b/recipes/public_info.rb index cc0dc32..f49fa74 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -9,8 +9,10 @@ # All rights reserved - Do Not Redistribute # +include_recipe 'ohai' + # Ensure the plugin directory exists -plugin_directory = directory "#{node['ohai']['plugin_path']}" do +plugin_directory = directory node['ohai']['plugin_path'] do owner 'root' group 'root' mode '0755' @@ -33,5 +35,7 @@ plugin_install.run_action(:create) reload_ohai.run_action(:reload) -# Assign the external_ip tag to the node -tag("RemoteIP:#{node['public_info']['remote_ip'] }") +# Assign the external_ip tag to the node if node['public_info']['remote_ip'] looks like an IP. +if node['public_info']['remote_ip'] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ + tag("RemoteIP:#{node['public_info']['remote_ip']}") +end From 09c8e49c205659029380b776f6aa1a93edea1bef Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 16:10:03 -0500 Subject: [PATCH 07/12] Fail run if unable to determine remote IP --- recipes/public_info.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index f49fa74..c66f2dd 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -35,7 +35,10 @@ plugin_install.run_action(:create) reload_ohai.run_action(:reload) -# Assign the external_ip tag to the node if node['public_info']['remote_ip'] looks like an IP. -if node['public_info']['remote_ip'] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ - tag("RemoteIP:#{node['public_info']['remote_ip']}") +# Stop the run if the IP is invalid, assume failure here is preferable to running with invalid data +unless node['public_info']['remote_ip'] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ + fail "ERROR: Unable to determine server remote IP. (Got \"#{node['public_info']['remote_ip']}\") Halting to avoid use of bad data." end + +# Assign the external_ip tag to the node if node['public_info']['remote_ip'] looks like an IP. +tag("RemoteIP:#{node['public_info']['remote_ip']}") From e71187c05cbc7cc2c744d23c61f8672e89d81126 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 17:01:56 -0500 Subject: [PATCH 08/12] Add comments and failure on nil ohai node data --- recipes/public_info.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index c66f2dd..f1fe6a2 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -9,8 +9,15 @@ # All rights reserved - Do Not Redistribute # +# Load the ohai recipe to populate node['ohai'] include_recipe 'ohai' +# Fail in a slightly more descriptive manner than the directory block below +# if the plugin directory is unset. +if node['ohai']['plugin_path'].nil? + fail 'ERROR: Ohai plugin path not set' +end + # Ensure the plugin directory exists plugin_directory = directory node['ohai']['plugin_path'] do owner 'root' From 93bfffdebef23ae92973a753dac528dd05a406e0 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 17:03:51 -0500 Subject: [PATCH 09/12] Whitespace --- recipes/public_info.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index f1fe6a2..5c6d700 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -46,6 +46,6 @@ unless node['public_info']['remote_ip'] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ fail "ERROR: Unable to determine server remote IP. (Got \"#{node['public_info']['remote_ip']}\") Halting to avoid use of bad data." end - + # Assign the external_ip tag to the node if node['public_info']['remote_ip'] looks like an IP. tag("RemoteIP:#{node['public_info']['remote_ip']}") From 5b5a556b2ad88c1b560090e779a39a61efb6ce28 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Mon, 12 May 2014 17:07:03 -0500 Subject: [PATCH 10/12] Improve comments --- recipes/public_info.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/recipes/public_info.rb b/recipes/public_info.rb index 5c6d700..d9c20a3 100644 --- a/recipes/public_info.rb +++ b/recipes/public_info.rb @@ -43,6 +43,8 @@ reload_ohai.run_action(:reload) # Stop the run if the IP is invalid, assume failure here is preferable to running with invalid data +# This check is also important for testing: if the plugin fails to load and operate this exception will +# halt convergance breaking Kitchen runs. unless node['public_info']['remote_ip'] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ fail "ERROR: Unable to determine server remote IP. (Got \"#{node['public_info']['remote_ip']}\") Halting to avoid use of bad data." end From 256f3bdc4c211831d96e4dda694a71f1033d71e1 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Tue, 13 May 2014 13:25:27 -0500 Subject: [PATCH 11/12] Update changelog --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8729f08..b423f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,16 @@ This file is used to list changes made in each version of rackops_rolebook. +## 1.4.0 (https://github.com/rackops/rackops_rolebook/pull/14) +* Add public_info Ohai plugin to populate public_info attributes + +## 1.3.1 +* Add Openssh dependency (https://github.com/rackops/rackops_rolebook/pull/11) + ## 1.3.0 +* Add Logrotation (https://github.com/rackops/rackops_rolebook/pull/9) + +## 1.4.0 * Add public_info Ohai plugin to populate public_info attributes ## 1.2.0 From fc331984e9cdeed064fdd6b43189d033a96b2326 Mon Sep 17 00:00:00 2001 From: Tom Noonan II Date: Tue, 13 May 2014 13:26:18 -0500 Subject: [PATCH 12/12] Fix formatting error --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b423f51..3835e1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ This file is used to list changes made in each version of rackops_rolebook. -## 1.4.0 (https://github.com/rackops/rackops_rolebook/pull/14) -* Add public_info Ohai plugin to populate public_info attributes +## 1.4.0 +* Add public_info Ohai plugin to populate public_info attributes (https://github.com/rackops/rackops_rolebook/pull/14) ## 1.3.1 * Add Openssh dependency (https://github.com/rackops/rackops_rolebook/pull/11)