Skip to content

Commit

Permalink
Remove dep on ohai
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Oct 21, 2014
1 parent 052d045 commit d82d0af
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 178 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@ Omnijack Gem CHANGELOG

v?.?.? (????-??-??)
-------------------
- Remove dependency on `ohai`; require platform arguments for metadata
- Swap out `json` dependency for `multi_json`

v0.2.0 (2014-09-19)
-------------------
Expand Down
29 changes: 15 additions & 14 deletions README.md
Expand Up @@ -36,12 +36,21 @@ Or install it yourself as:
Usage
-----

Getting Chef-DK package metadata from the official Chef API:
Getting Chef-DK project data from the official Chef API:

```ruby
require 'omnijack'

chef_dk = Omnijack::Project::ChefDk.new
```

Getting Chef-DK package metadata (requires additional target platform
information):

```ruby
chef_dk = Omnijack::Project::ChefDk.new(platform: 'ubuntu',
platform_version: '14.04',
machine_arch: 'x86_64')
metadata = chef_dk.metadata

puts metadata
Expand All @@ -62,6 +71,7 @@ puts metadata[:yolo]
puts metadata[:version]
puts metadata[:build]
```

Getting Chef-DK project data from an unofficial Omnitruck API:

```ruby
Expand All @@ -80,16 +90,6 @@ Omnijack::Project::ChefDk.new(
)
```

Getting Chef-DK project data for a platform other than the one running:

```ruby
Omnijack::Project::ChefDk.new(
platform: 'ubuntu',
platform_version: '14.04',
machine_arch: 'x86_64'
)
```

Getting the Chef project's list of packages:

```ruby
Expand Down Expand Up @@ -132,6 +132,7 @@ Contributing

1. Fork it ( https://github.com/[my-github-username]/omnijack/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
3. Add tests for any new code and ensure all tests pass (`rake`)
4. Commit your changes (`git commit -am 'Add some feature'`)
5. Push to the branch (`git push origin my-new-feature`)
6. Create a new Pull Request
51 changes: 19 additions & 32 deletions lib/omnijack/metadata.rb
Expand Up @@ -16,7 +16,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require 'ohai'
require 'open-uri'
require_relative 'config'
require_relative '../omnijack'
Expand All @@ -29,8 +28,12 @@ class Metadata < Omnijack
include ::Chef::Mixin::ParamsValidate
include Config

def initialize(name, args = {})
def initialize(name, args)
super
[:platform, :platform_version, :machine_arch].each do |i|
send(i, args[i])
args.delete(i)
end
args.each { |k, v| send(k, v) unless v.nil? } unless args.nil?
to_h
end
Expand Down Expand Up @@ -104,7 +107,7 @@ def version(arg = nil)
# @return [String]
#
def platform(arg = nil)
set_or_return(:platform, arg, kind_of: String, default: node[:platform])
set_or_return(:platform, arg, kind_of: String, required: true)
end

#
Expand All @@ -114,12 +117,12 @@ def platform(arg = nil)
# @return [String]
#
def platform_version(arg = nil)
# TODO: The platform version parser living in `node` means passing e.g.
# '10.9.2' here won't result in it being shortened to '10.9'
set_or_return(:platform_version,
arg,
kind_of: String,
default: node[:platform_version])
!arg.nil? && arg = case platform
when 'mac_os_x' then platform_version_mac_os_x(arg)
when 'windows' then platform_version_windows(arg)
else arg
end
set_or_return(:platform_version, arg, kind_of: String, required: true)
end

#
Expand All @@ -132,7 +135,7 @@ def machine_arch(arg = nil)
set_or_return(:machine_arch,
arg,
kind_of: String,
default: node[:kernel][:machine])
required: true)
end

private
Expand Down Expand Up @@ -180,46 +183,30 @@ def endpoint
OMNITRUCK_PROJECTS[name][:endpoints][:metadata]
end

#
# Fetch and return node data from Ohai
#
# @return [Mash]
#
def node
unless @node
@node = Ohai::System.new.all_plugins('platform')[0].data
case @node[:platform]
when 'mac_os_x'
@node[:platform_version] = platform_version_mac_os_x
when 'windows'
@node[:platform_version] = platform_version_windows
end
end
@node
end

#
# Apply special logic for the version of an OS X platform
#
# @param [String] arg
# @return [String]
#
def platform_version_mac_os_x
node[:platform_version].match(/^[0-9]+\.[0-9]+/).to_s
def platform_version_mac_os_x(arg)
arg.match(/^[0-9]+\.[0-9]+/).to_s
end

#
# Apply special logic for the version of a Windows platform
#
# @param [String] arg
# @return [String]
#
def platform_version_windows
def platform_version_windows(arg)
# Make a best guess and assume a server OS
# See: http://msdn.microsoft.com/en-us/library/windows/
# desktop/ms724832(v=vs.85).aspx
{
'6.3' => '2012r2', '6.2' => '2012', '6.1' => '2008r2', '6.0' => '2008',
'5.2' => '2003r2', '5.1' => 'xp', '5.0' => '2000'
}[node[:platform_version].match(/^[0-9]+\.[0-9]+/).to_s]
}[arg.match(/^[0-9]+\.[0-9]+/).to_s]
end

#
Expand Down
1 change: 0 additions & 1 deletion omnijack.gemspec
Expand Up @@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 1.9.3'

spec.add_dependency 'multi_json'
spec.add_dependency 'ohai'

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
Expand Down

0 comments on commit d82d0af

Please sign in to comment.