Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[OHAI-458] Include Joyent SmartOS specific attributes in Ohai #133

Merged
merged 15 commits into from

3 participants

@sawanoboly

Hi,

This is code of OHAI-458.

Attribute Sample


"joyent"=>
  {"sm_uuid"=>"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
   "sm_id"=>"5",
   "sm_image_id"=>"chefserver",
   "sm_image_ver"=>"1.0.18",
   "sm_baseimage_id"=>"base64",
   "sm_baseimage_ver"=>"1.8.4",
   "sm_pkgsrc"=>"http://pkgsrc.joyent.com/sdc6/2012Q2/x86_64/All"}}

Test


$ rspec -fd --color spec/unit/plugins/joyent_spec.rb 
Run options: exclude {:requires_root=>true, :ruby_18_only=>true, :windows_only=>true}

Ohai::System plugin joyent
  without joyent
    should NOT create joyent
  with joyent
    should create joyent
    under global zone
      should ditect global zone
      should NOT create sm_id
    under smartmachine
      should retrive zone uuid
      should create sm_id

Finished in 0.01397 seconds
6 examples, 0 failures

Regards,

@sersut
Owner

Thanks @sawanoboly for the patch. Looks like the comments went into our ticketing system but not here:

Yukihiko Sawanobori, the data collected from zonename and zoneadm is not Joyent specific. On my SmartOS VM I get a UUID from zonename in a zone as well. The common data should be implemented in the existing solaris2/virtualization plugin.
Bonus points for implementing the system calls using mixlib-shellout instead of run_command.

Also we will need to port this to Ohai 7 syntax more info here.

Let us know if you need any help pushing this forward.

@sawanoboly

Thank you. I'll try to update code.

lib/ohai/plugins/joyent.rb
((8 lines not shown))
+# 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.
+#
+#
+
+# Reference from: sm-summary command
+
+Ohai.plugin(:Joyent) do
+ %w{ sm_uuid sm_id sm_image sm_image_id sm_image_ver sm_baseimage sm_baseimage_id sm_baseimage_ver sm_pkgsrc }.each do |info|
@sersut Owner
sersut added a note

It's good to be specific about the info that is going to be provided. provides statement here can also be simply provides "joyent" if there is a potential that all of this information will not be collected.

@mcquin Collaborator
mcquin added a note

provides "joyent" should be included. It will make the syntax for dependencies in other plugin files easier and more clear.

Thanks. @sersut @mcquin

I'll update code more simply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sersut
Owner

Thanks for taking time to do this @sawanoboly. :+1: for me.

@mcquin can I get a second :+1: from you?

@mcquin mcquin commented on the diff
lib/ohai/plugins/joyent.rb
((40 lines not shown))
+
+ def collect_pkgsrc
+ if File.exist?('/opt/local/etc/pkg_install.conf')
+ sm_pkgsrc = ::File.read("/opt/local/etc/pkg_install.conf").split("=")
+ sm_pkgsrc[1].chomp
+ else
+ nil
+ end
+ end
+
+ def is_smartos?
+ platform == 'smartos'
+ end
+
+ collect_data do
+ if is_smartos?
@mcquin Collaborator
mcquin added a note

I'm not personally familiar with SmartOS, but if ::RbConfig::CONFIG['host_os'] matches SmartOS you can add a line that determines that here (https://github.com/opscode/ohai/blob/master/lib%2Fohai%2Fmixin%2Fos.rb#L26) and then write collect_data(:smartos) do instead of wrapping the code in an if-statement.

Thanks for tell me usage for collect_data!

But ::RbConfig::CONFIG['host_os'] returns solaris2.11 on smartos.

> ::RbConfig::CONFIG['host_os']
=> "solaris2.11"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
spec/unit/plugins/joyent_spec.rb
((51 lines not shown))
+ @plugin.stub(:collect_pkgsrc).and_return('http://pkgsrc.joyent.com/packages/SmartOS/2013Q4/x86_64/All')
+ @plugin.run
+ end
+
+ it "should retrive zone uuid" do
+ @plugin[:joyent][:sm_uuid].should eql 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
+ end
+
+ it "should collect sm_id" do
+ @plugin[:joyent][:sm_id].should eql '30'
+ end
+
+ it "should collect images" do
+ @plugin[:joyent][:sm_image].should_not nil
+ @plugin[:joyent][:sm_image_id].should_not nil
+ @plugin[:joyent][:sm_image_ver].should_not nil
@mcquin Collaborator
mcquin added a note

The syntax here should be .should_not be_nil. I just checked and nil.should_not nil passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sersut sersut added Ready for Merge and removed Attack List labels
@sersut
Owner

Thanks @sawanoboly for the updates. Marked for merge. We should get to merging this in the next few weeks.

@mcquin mcquin merged commit 8bff1fe into chef:master

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
82 lib/ohai/plugins/joyent.rb
@@ -0,0 +1,82 @@
+#
+# Author: sawanoboriyu@higanworks.com
+# Copyright (C) 2014 HiganWorks LLC
+# 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.
+#
+#
+
+# Reference from: sm-summary command
+
+Ohai.plugin(:Joyent) do
+ provides 'joyent'
+ provides 'virtualization/guest_id'
+ depends 'os', 'platform', 'virtualization'
+
+ def collect_product_file
+ lines = []
+ if ::File.exists?("/etc/product")
+ ::File.open("/etc/product") do |file|
+ while line = file.gets
+ lines << line
+ end
+ end
+ end
+ lines
+ end
+
+ def collect_pkgsrc
+ if File.exist?('/opt/local/etc/pkg_install.conf')
+ sm_pkgsrc = ::File.read("/opt/local/etc/pkg_install.conf").split("=")
+ sm_pkgsrc[1].chomp
+ else
+ nil
+ end
+ end
+
+ def is_smartos?
+ platform == 'smartos'
+ end
+
+ collect_data do
+ if is_smartos?
@mcquin Collaborator
mcquin added a note

I'm not personally familiar with SmartOS, but if ::RbConfig::CONFIG['host_os'] matches SmartOS you can add a line that determines that here (https://github.com/opscode/ohai/blob/master/lib%2Fohai%2Fmixin%2Fos.rb#L26) and then write collect_data(:smartos) do instead of wrapping the code in an if-statement.

Thanks for tell me usage for collect_data!

But ::RbConfig::CONFIG['host_os'] returns solaris2.11 on smartos.

> ::RbConfig::CONFIG['host_os']
=> "solaris2.11"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ joyent Mash.new
+
+ # copy uuid
+ joyent[:sm_uuid] = virtualization[:guest_uuid]
+
+ # get zone id unless globalzone
+ unless joyent[:sm_uuid] == "global"
+ joyent[:sm_id] = virtualization[:guest_id]
+ end
+
+ # retrieve image name and pkgsrc
+ collect_product_file.each do |line|
+ case line
+ when /^Image/
+ sm_image = line.split(" ")
+ joyent[:sm_image_id] = sm_image[1]
+ joyent[:sm_image_ver] = sm_image[2]
+ when /^Base Image/
+ sm_baseimage = line.split(" ")
+ joyent[:sm_baseimage_id] = sm_baseimage[2]
+ joyent[:sm_baseimage_ver] = sm_baseimage[3]
+ end
+ end
+
+ ## retrieve pkgsrc
+ joyent[:sm_pkgsrc] = collect_pkgsrc if collect_pkgsrc
+ end
+ end
+end
View
7 lib/ohai/plugins/solaris2/virtualization.rb
@@ -21,6 +21,12 @@
Ohai.plugin(:Virtualization) do
provides "virtualization"
+ def collect_solaris_guestid
+ command = '/usr/sbin/zoneadm list -p'
+ so = shell_out(command)
+ so.stdout.split(':').first
+ end
+
collect_data(:solaris2) do
virtualization Mash.new
@@ -75,6 +81,7 @@
virtualization[:system] = 'zone'
virtualization[:role] = 'guest'
virtualization[:guest_uuid] = zones[first_zone]['uuid']
+ virtualization[:guest_id] = collect_solaris_guestid
end
elsif (zones.length > 1)
virtualization[:system] = 'zone'
View
74 spec/unit/plugins/joyent_spec.rb
@@ -0,0 +1,74 @@
+require 'spec_helper'
+
+
+describe Ohai::System, "plugin joyent" do
+ before(:each) do
+ @plugin = get_plugin('joyent')
+ end
+
+ describe "without joyent" do
+ before(:each) do
+ @plugin.stub(:is_smartos?).and_return(false)
+ end
+
+ it "should NOT create joyent" do
+ @plugin.run
+ @plugin[:joyent].should be_nil
+ end
+ end
+
+ describe "with joyent" do
+ before(:each) do
+ @plugin.stub(:is_smartos?).and_return(true)
+ @plugin[:virtualization] = Mash.new
+ @plugin[:virtualization][:guest_uuid] = "global"
+ end
+
+ it "should create joyent" do
+ @plugin.run
+ @plugin[:joyent].should_not be_nil
+ end
+
+ describe "under global zone" do
+ before(:each) do
+ @plugin.run
+ end
+
+ it "should ditect global zone" do
+ @plugin[:joyent][:sm_uuid].should eql 'global'
+ end
+
+ it "should NOT create sm_id" do
+ @plugin[:joyent][:sm_id].should be_nil
+ end
+ end
+
+ describe "under smartmachine" do
+ before(:each) do
+ @plugin[:virtualization][:guest_uuid] = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
+ @plugin[:virtualization][:guest_id] = '30'
+ @plugin.stub(:collect_product_file).and_return(["Name: Joyent Instance", "Image: base64 13.4.2", "Documentation: http://wiki.joyent.com/jpc2/SmartMachine+Base"])
+ @plugin.stub(:collect_pkgsrc).and_return('http://pkgsrc.joyent.com/packages/SmartOS/2013Q4/x86_64/All')
+ @plugin.run
+ end
+
+ it "should retrive zone uuid" do
+ @plugin[:joyent][:sm_uuid].should eql 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx'
+ end
+
+ it "should collect sm_id" do
+ @plugin[:joyent][:sm_id].should eql '30'
+ end
+
+ it "should collect images" do
+ @plugin[:joyent][:sm_image_id].should_not be_nil
+ @plugin[:joyent][:sm_image_ver].should_not be_nil
+ end
+
+ it "should collect pkgsrc" do
+ @plugin[:joyent][:sm_pkgsrc].should eql 'http://pkgsrc.joyent.com/packages/SmartOS/2013Q4/x86_64/All'
+ end
+ end
+ end
+end
+
Something went wrong with that request. Please try again.