Skip to content

Commit

Permalink
Ohai lives - first meaningful commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhjk committed Dec 2, 2008
1 parent 3a9543f commit 69a778d
Show file tree
Hide file tree
Showing 26 changed files with 2,119 additions and 226 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pkg/
tmp/
875 changes: 674 additions & 201 deletions LICENSE

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions PostInstall.txt

This file was deleted.

26 changes: 16 additions & 10 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ FIX (describe your package)

== LICENSE:

Copyright (c) 2008 OpsCode, Inc.
Ohai - system information application

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
Copyright (C) 2008, OpsCode Inc.

http://www.apache.org/licenses/LICENSE-2.0
Portions of this program benefitted significantly from Facter,
primarily plugins. (http://reductivelabs.com/projects/facter/)

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.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
27 changes: 24 additions & 3 deletions bin/ohai
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@
# See the License for the specific language governing permissions and
# limitations under the License.

$: << File.join(File.dirname(__FILE__), "..", "lib")
require 'optparse'
require 'rubygems'
#require 'ohai'
require 'ohai'
require 'json'

config = {
:directory => nil,
:file => nil,
:kthxbye => false
}

opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [-d DIR|-r FILE] (options)"
opts.banner = "Usage: #{$0} (options)"
opts.on("-d", "--directory NAME", "A directory to add to the Ohai search path") do |d|
config[:directory] = d
end
opts.on("-f", "--file NAME", "A file to run Ohai against") do |f|
config[:file] = f
end
opts.on("-k", "--kthxbye", "LOLcats") do |k|
config[:kthxbye] = true
end
Expand All @@ -41,4 +50,16 @@ opts.parse!(ARGV)

if config[:kthxbye]
puts JSON.generate({ :lolcat => "http://icanhascheezburger.com/?random" })
end
end

ohai = Ohai::System.new
if config[:file]
ohai.from_file(config[:file])
else
if config[:directory]
Ohai::Config[:plugin_path] << config[:directory]
end
ohai.all_plugins
end

puts ohai.json_pretty_print
13 changes: 13 additions & 0 deletions features/development.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Feature: Development processes of newgem itself (rake tasks)

As a Newgem maintainer or contributor
I want rake tasks to maintain and release the gem
So that I can spend time on the tests and code, and not excessive time on maintenance processes

Scenario: Generate RubyGem
Given this project is active project folder
And 'pkg' folder is deleted
When task 'rake gem' is invoked
Then folder 'pkg' is created
And file with name matching 'pkg/*.gem' is created else you should run "rake manifest" to fix this
And gem spec key 'rdoc_options' contains /--mainREADME.rdoc/
21 changes: 21 additions & 0 deletions features/ohai.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: Collects data about a system
In order to understand the state of my system
As a Systems Administrator
I want to have a program that detects information for me

Scenario: Collect data about the system via a single plugin
Given a plugin called 'platform'
When I run ohai
Then I should have a 'platform' of 'kitties'

Scenario: Collect data about the system via a directory of plugins
Given a plugin directory at './plugins'
When I run ohai
Then I should have a 'platform' of 'kitties'
And I should have a 'foghorn' of 'leghorn'

Scenario: Collect data about the system via an external script
Given a plugin called 'perl'
When I run ohai
Then I should have a 'perl_major_version' of '5'

174 changes: 174 additions & 0 deletions features/steps/common.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
def in_project_folder(&block)
project_folder = @active_project_folder || @tmp_root
FileUtils.chdir(project_folder, &block)
end

def in_home_folder(&block)
FileUtils.chdir(@home_path, &block)
end

Given %r{^a safe folder} do
FileUtils.rm_rf @tmp_root = File.dirname(__FILE__) + "/../../tmp"
FileUtils.mkdir_p @tmp_root
FileUtils.mkdir_p @home_path = File.expand_path(File.join(@tmp_root, "home"))
@lib_path = File.expand_path(File.dirname(__FILE__) + '/../../lib')
Given "env variable $HOME set to '#{@home_path}'"
end

Given %r{^this project is active project folder} do
Given "a safe folder"
@active_project_folder = File.expand_path(File.dirname(__FILE__) + "/../..")
end

Given %r{^env variable \$([\w_]+) set to '(.*)'} do |env_var, value|
ENV[env_var] = value
end

def force_local_lib_override(project_name = @project_name)
rakefile = File.read(File.join(project_name, 'Rakefile'))
File.open(File.join(project_name, 'Rakefile'), "w+") do |f|
f << "$:.unshift('#{@lib_path}')\n"
f << rakefile
end
end

def setup_active_project_folder project_name
@active_project_folder = File.join(@tmp_root, project_name)
@project_name = project_name
end

Given %r{'(.*)' folder is deleted} do |folder|
in_project_folder do
FileUtils.rm_rf folder
end
end

When %r{^'(.*)' generator is invoked with arguments '(.*)'$} do |generator, arguments|
FileUtils.chdir(@active_project_folder) do
if Object.const_defined?("APP_ROOT")
APP_ROOT.replace(FileUtils.pwd)
else
APP_ROOT = FileUtils.pwd
end
run_generator(generator, arguments.split(' '), SOURCES)
end
end

When %r{run executable '(.*)' with arguments '(.*)'} do |executable, arguments|
@stdout = File.expand_path(File.join(@tmp_root, "executable.out"))
FileUtils.chdir(@active_project_folder) do
system "ruby #{executable} #{arguments} > #{@stdout}"
end
end

When %r{^task 'rake (.*)' is invoked$} do |task|
@stdout = File.expand_path(File.join(@tmp_root, "tests.out"))
FileUtils.chdir(@active_project_folder) do
system "rake #{task} --trace > #{@stdout} 2> #{@stdout}"
end
end

Then %r{^folder '(.*)' is created} do |folder|
in_project_folder do
File.exists?(folder).should be_true
end
end

Then %r{^file '(.*)' (is|is not) created} do |file, is|
in_project_folder do
File.exists?(file).should(is == 'is' ? be_true : be_false)
end
end

Then %r{^file with name matching '(.*)' is created} do |pattern|
in_project_folder do
Dir[pattern].should_not be_empty
end
end

Then %r{gem file '(.*)' and generated file '(.*)' should be the same} do |gem_file, project_file|
File.exists?(gem_file).should be_true
File.exists?(project_file).should be_true
gem_file_contents = File.read(File.dirname(__FILE__) + "/../../#{gem_file}")
project_file_contents = File.read(File.join(@active_project_folder, project_file))
project_file_contents.should == gem_file_contents
end

Then %r{^output same as contents of '(.*)'$} do |file|
expected_output = File.read(File.join(File.dirname(__FILE__) + "/../expected_outputs", file))
actual_output = File.read(File.dirname(__FILE__) + "/../../tmp/#{@stdout}")
actual_output.should == expected_output
end

Then %r{^(does|does not) invoke generator '(.*)'$} do |does_invoke, generator|
actual_output = File.read(File.dirname(__FILE__) + "/../../tmp/#{@stdout}")
does_invoke == "does" ?
actual_output.should(match(/dependency\s+#{generator}/)) :
actual_output.should_not(match(/dependency\s+#{generator}/))
end

Then %r{help options '(.*)' and '(.*)' are displayed} do |opt1, opt2|
actual_output = File.read(@stdout)
actual_output.should match(/#{opt1}/)
actual_output.should match(/#{opt2}/)
end

Then %r{^output (does|does not) match \/(.*)\/} do |does, regex|
actual_output = File.read(@stdout)
(does == 'does') ?
actual_output.should(match(/#{regex}/)) :
actual_output.should_not(match(/#{regex}/))
end

Then %r{^contents of file '(.*)' (does|does not) match \/(.*)\/} do |file, does, regex|
in_project_folder do
actual_output = File.read(file)
(does == 'does') ?
actual_output.should(match(/#{regex}/)) :
actual_output.should_not(match(/#{regex}/))
end
end

Then %r{^all (\d+) tests pass} do |expected_test_count|
expected = %r{^#{expected_test_count} tests, \d+ assertions, 0 failures, 0 errors}
actual_output = File.read(@stdout)
actual_output.should match(expected)
end

Then %r{^all (\d+) examples pass} do |expected_test_count|
expected = %r{^#{expected_test_count} examples?, 0 failures}
actual_output = File.read(@stdout)
actual_output.should match(expected)
end

Then %r{^yaml file '(.*)' contains (\{.*\})} do |file, yaml|
in_project_folder do
yaml = eval yaml
YAML.load(File.read(file)).should == yaml
end
end

Then %r{^Rakefile can display tasks successfully} do
@stdout = File.expand_path(File.join(@tmp_root, "rakefile.out"))
FileUtils.chdir(@active_project_folder) do
system "rake -T > #{@stdout} 2> #{@stdout}"
end
actual_output = File.read(@stdout)
actual_output.should match(/^rake\s+\w+\s+#\s.*/)
end

Then %r{^task 'rake (.*)' is executed successfully} do |task|
@stdout.should_not be_nil
actual_output = File.read(@stdout)
actual_output.should_not match(/^Don't know how to build task '#{task}'/)
actual_output.should_not match(/Error/i)
end

Then %r{^gem spec key '(.*)' contains \/(.*)\/} do |key, regex|
in_project_folder do
gem_file = Dir["pkg/*.gem"].first
gem_spec = Gem::Specification.from_yaml(`gem spec #{gem_file}`)
spec_value = gem_spec.send(key.to_sym)
spec_value.to_s.should match(/#{regex}/)
end
end
6 changes: 6 additions & 0 deletions features/steps/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require File.dirname(__FILE__) + "/../../lib/ohai"

gem 'cucumber'
require 'cucumber'
gem 'rspec'
require 'spec'
24 changes: 24 additions & 0 deletions lib/ohai.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
#
# Author:: Adam Jacob (<[email protected]>)
# Copyright:: Copyright (c) 2008 OpsCode, Inc.
# License:: GNU GPL, Version 3
#
# Copyright (C) 2008, OpsCode Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

require 'ohai/config'
require 'ohai/system'

module Ohai
VERSION = '0.0.1'
end
Loading

0 comments on commit 69a778d

Please sign in to comment.