Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public info #14

Merged
merged 13 commits into from
May 13, 2014
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

This file is used to list changes made in each version of rackops_rolebook.

## 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)

## 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
* Add rackspace_cloudmonitoring as a dependency to enable standard checks

Expand Down
41 changes: 41 additions & 0 deletions files/default/public_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# available at https://github.com/rackops/public_info
#
# Author:: Thomas Cate ([email protected])
# 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)

unless 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
3 changes: 2 additions & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
description 'Installs/Configures rackops_rolebook'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))

version '1.3.1'
version '1.4.0'

depends 'chef-client', '~> 3.3'
depends 'rackspace_user', '~> 1.0'
Expand All @@ -15,3 +15,4 @@
depends 'rackspace_iptables', '~> 1.2'
depends 'rackspace_openssh', '~> 3.1'
depends 'rackspace_cloudmonitoring', '~> 3.1'
depends 'ohai', '~> 1.1'
1 change: 1 addition & 0 deletions recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
rackspace_motd
rackspace_ntp
rackspace_openssh
rackops_rolebook::public_info
)

if node['rackops_rolebook']['include_acl'] == true
Expand Down
53 changes: 53 additions & 0 deletions recipes/public_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# 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
#

# 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'
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)

# 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

# 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']}")