diff options
-rw-r--r-- | lib/ohai/plugins/cloud.rb | 329 | ||||
-rw-r--r-- | lib/ohai/plugins/cloud_v2.rb | 4 | ||||
-rw-r--r-- | spec/unit/plugins/cloud_spec.rb | 285 | ||||
-rw-r--r-- | spec/unit/plugins/cloud_v2_spec.rb | 1 |
4 files changed, 4 insertions, 615 deletions
diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb deleted file mode 100644 index e24c6965..00000000 --- a/lib/ohai/plugins/cloud.rb +++ /dev/null @@ -1,329 +0,0 @@ -# -# Author:: Cary Penniman (<cary@rightscale.com>) -# 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. - -Ohai.plugin(:Cloud) do - provides "cloud" - - depends "ec2" - depends "gce" - depends "rackspace" - depends "eucalyptus" - depends "linode" - depends "openstack" - depends "azure" - depends "digital_ocean" - depends "softlayer" - - # Make top-level cloud hashes - # - def create_objects - cloud Mash.new - cloud[:public_ips] = Array.new - cloud[:private_ips] = Array.new - end - #--------------------------------------- - # Google Compute Engine (gce) - #-------------------------------------- - - # Is current cloud gce? - # - # === Return - # true:: If gce Hash is defined - # false:: Otherwise - def on_gce? - gce != nil - end - - # Fill cloud hash with gce values - def get_gce_values - cloud[:public_ipv4] = [] - cloud[:local_ipv4] = [] - - public_ips = gce["instance"]["networkInterfaces"].collect do |interface| - if interface.has_key?("accessConfigs") - interface["accessConfigs"].collect { |ac| ac["externalIp"] } - end - end.flatten.compact - - private_ips = gce["instance"]["networkInterfaces"].collect do |interface| - interface["ip"] - end.compact - - cloud[:public_ips] += public_ips - cloud[:private_ips] += private_ips - cloud[:public_ipv4] += public_ips - cloud[:public_hostname] = nil - cloud[:local_ipv4] += private_ips - cloud[:local_hostname] = gce["instance"]["hostname"] - cloud[:provider] = "gce" - end - - # ---------------------------------------- - # ec2 - # ---------------------------------------- - - # Is current cloud ec2? - # - # === Return - # true:: If ec2 Hash is defined - # false:: Otherwise - def on_ec2? - ec2 != nil - end - - # Fill cloud hash with ec2 values - def get_ec2_values - cloud[:public_ips] << ec2["public_ipv4"] - cloud[:private_ips] << ec2["local_ipv4"] - cloud[:public_ipv4] = ec2["public_ipv4"] - cloud[:public_hostname] = ec2["public_hostname"] - cloud[:local_ipv4] = ec2["local_ipv4"] - cloud[:local_hostname] = ec2["local_hostname"] - cloud[:provider] = "ec2" - end - - # ---------------------------------------- - # rackspace - # ---------------------------------------- - - # Is current cloud rackspace? - # - # === Return - # true:: If rackspace Hash is defined - # false:: Otherwise - def on_rackspace? - rackspace != nil - end - - # Fill cloud hash with rackspace values - def get_rackspace_values - cloud[:public_ips] << rackspace["public_ipv4"] if rackspace["public_ipv4"] - cloud[:private_ips] << rackspace["local_ipv4"] if rackspace["local_ipv4"] - cloud[:public_ipv4] = rackspace["public_ipv4"] - cloud[:public_ipv6] = rackspace["public_ipv6"] - cloud[:public_hostname] = rackspace["public_hostname"] - cloud[:local_ipv4] = rackspace["local_ipv4"] - cloud[:local_ipv6] = rackspace["local_ipv6"] - cloud[:local_hostname] = rackspace["local_hostname"] - cloud[:provider] = "rackspace" - end - - # ---------------------------------------- - # linode - # ---------------------------------------- - - # Is current cloud linode? - # - # === Return - # true:: If linode Hash is defined - # false:: Otherwise - def on_linode? - linode != nil - end - - # Fill cloud hash with linode values - def get_linode_values - cloud[:public_ips] << linode["public_ip"] - cloud[:private_ips] << linode["private_ip"] - cloud[:public_ipv4] = linode["public_ipv4"] - cloud[:public_hostname] = linode["public_hostname"] - cloud[:local_ipv4] = linode["local_ipv4"] - cloud[:local_hostname] = linode["local_hostname"] - cloud[:provider] = "linode" - end - - # ---------------------------------------- - # eucalyptus - # ---------------------------------------- - - # Is current cloud eucalyptus? - # - # === Return - # true:: If eucalyptus Hash is defined - # false:: Otherwise - def on_eucalyptus? - eucalyptus != nil - end - - def get_eucalyptus_values - cloud[:public_ips] << eucalyptus["public_ipv4"] - cloud[:private_ips] << eucalyptus["local_ipv4"] - cloud[:public_ipv4] = eucalyptus["public_ipv4"] - cloud[:public_hostname] = eucalyptus["public_hostname"] - cloud[:local_ipv4] = eucalyptus["local_ipv4"] - cloud[:local_hostname] = eucalyptus["local_hostname"] - cloud[:provider] = "eucalyptus" - end - - # ---------------------------------------- - # openstack - # ---------------------------------------- - - # Is current cloud openstack-based? - # - # === Return - # true:: If openstack Hash is defined - # false:: Otherwise - def on_openstack? - openstack != nil - end - - # Fill cloud hash with openstack values - def get_openstack_values - cloud[:public_ips] << openstack["public_ipv4"] - cloud[:private_ips] << openstack["local_ipv4"] - cloud[:public_ipv4] = openstack["public_ipv4"] - cloud[:public_hostname] = openstack["public_hostname"] - cloud[:local_ipv4] = openstack["local_ipv4"] - cloud[:local_hostname] = openstack["local_hostname"] - cloud[:provider] = openstack["provider"] - end - - # ---------------------------------------- - # azure - # ---------------------------------------- - - # Is current cloud azure? - # - # === Return - # true:: If azure Hash is defined - # false:: Otherwise - def on_azure? - azure != nil - end - - # Fill cloud hash with azure values - def get_azure_values - cloud[:vm_name] = azure["vm_name"] - cloud[:private_ips] << azure["private_ip"] - cloud[:public_ips] << azure["public_ip"] - cloud[:public_ipv4] = azure["public_ip"] - cloud[:public_fqdn] = azure["public_fqdn"] - cloud[:public_hostname] = azure["public_fqdn"] - cloud[:public_ssh_port] = azure["public_ssh_port"] if azure["public_ssh_port"] - cloud[:public_winrm_port] = azure["public_winrm_port"] if azure["public_winrm_port"] - cloud[:provider] = "azure" - end - - # ---------------------------------------- - # digital_ocean - # ---------------------------------------- - - # Is current cloud digital_ocean? - # - # === Return - # true:: If digital_ocean Mash is defined - # false:: Otherwise - def on_digital_ocean? - digital_ocean != nil - end - - # Fill cloud hash with digital ocean values - def get_digital_ocean_values - public_ipv4 = digital_ocean["interfaces"]["public"][0]["ipv4"]["ip_address"] rescue nil - private_ipv4 = digital_ocean["interfaces"]["private"][0]["ipv4"]["ip_address"] rescue nil - public_ipv6 = digital_ocean["interfaces"]["public"][0]["ipv6"]["ip_address"] rescue nil - private_ipv6 = digital_ocean["interfaces"]["private"][0]["ipv6"]["ip_address"] rescue nil - cloud[:public_ips] << public_ipv4 unless public_ipv4.nil? - cloud[:public_ips] << public_ipv6 unless public_ipv6.nil? - cloud[:private_ips] << private_ipv4 unless private_ipv4.nil? - cloud[:private_ips] << private_ipv6 unless private_ipv6.nil? - cloud[:public_ipv4] = public_ipv4 - cloud[:public_ipv6] = public_ipv6 - cloud[:local_ipv4] = private_ipv4 - cloud[:local_ipv6] = private_ipv6 - cloud[:public_hostname] = digital_ocean["hostname"] - cloud[:provider] = "digital_ocean" - end - - # ---------------------------------------- - # softlayer - # ---------------------------------------- - - # Is current cloud softlayer? - # - # === Return - # true:: If softlayer Hash is defined - # false:: Otherwise - def on_softlayer? - softlayer != nil - end - - # Fill cloud hash with softlayer values - def get_softlayer_values - cloud[:public_ipv4] = softlayer["public_ipv4"] - cloud[:local_ipv4] = softlayer["local_ipv4"] - cloud[:public_ips] << softlayer["public_ipv4"] if softlayer["public_ipv4"] - cloud[:private_ips] << softlayer["local_ipv4"] if softlayer["local_ipv4"] - cloud[:public_hostname] = softlayer["public_fqdn"] - cloud[:provider] = "softlayer" - end - - collect_data do - # setup gce cloud - if on_gce? - create_objects - get_gce_values - end - - # setup ec2 cloud - if on_ec2? - create_objects - get_ec2_values - end - - # setup rackspace cloud - if on_rackspace? - create_objects - get_rackspace_values - end - - # setup linode cloud data - if on_linode? - create_objects - get_linode_values - end - - if on_eucalyptus? - create_objects - get_eucalyptus_values - end - - # setup openstack cloud - if on_openstack? - create_objects - get_openstack_values - end - - # setup azure cloud data - if on_azure? - create_objects - get_azure_values - end - - # setup digital_ocean cloud data - if on_digital_ocean? - create_objects - get_digital_ocean_values - end - - # setup softlayer cloud - if on_softlayer? - create_objects - get_softlayer_values - end - end -end diff --git a/lib/ohai/plugins/cloud_v2.rb b/lib/ohai/plugins/cloud_v2.rb index 47a3a31c..f09b345d 100644 --- a/lib/ohai/plugins/cloud_v2.rb +++ b/lib/ohai/plugins/cloud_v2.rb @@ -16,6 +16,7 @@ Ohai.plugin(:CloudV2) do provides "cloud_v2" + provides "cloud" depends "ec2" depends "gce" @@ -306,7 +307,8 @@ Ohai.plugin(:CloudV2) do get_azure_values if on_azure? get_digital_ocean_values if on_digital_ocean? - # set node[:cloud] hash here + # set node[:cloud] and node[:cloud_v2] hash here cloud_v2 @cloud_attr_obj.cloud_mash + cloud @cloud_attr_obj.cloud_mash end end diff --git a/spec/unit/plugins/cloud_spec.rb b/spec/unit/plugins/cloud_spec.rb deleted file mode 100644 index 78865ba2..00000000 --- a/spec/unit/plugins/cloud_spec.rb +++ /dev/null @@ -1,285 +0,0 @@ -# -# Author:: Cary Penniman (<cary@rightscale.com>) -# 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. -# - -require_relative "../../spec_helper.rb" - -describe Ohai::System, "plugin cloud" do - before(:each) do - @plugin = get_plugin("cloud") - end - - describe "with no cloud mashes" do - it "doesn't populate the cloud data" do - @plugin[:ec2] = nil - @plugin[:rackspace] = nil - @plugin[:eucalyptus] = nil - @plugin[:linode] = nil - @plugin[:azure] = nil - @plugin[:digital_ocean] = nil - @plugin.run - expect(@plugin[:cloud]).to be_nil - end - end - - describe "with EC2 mash" do - before do - @plugin[:ec2] = Mash.new - end - - it "populates cloud public ip" do - @plugin[:ec2]["public_ipv4"] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips][0]).to eq(@plugin[:ec2]["public_ipv4"]) - end - - it "populates cloud private ip" do - @plugin[:ec2]["local_ipv4"] = "10.252.42.149" - @plugin.run - expect(@plugin[:cloud][:private_ips][0]).to eq(@plugin[:ec2]["local_ipv4"]) - end - - it "populates cloud provider" do - @plugin.run - expect(@plugin[:cloud][:provider]).to eq("ec2") - end - end - - describe "with rackspace" do - before do - @plugin[:rackspace] = Mash.new - end - - it "populates cloud public ip" do - @plugin[:rackspace][:public_ipv4] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ipv4]).to eq(@plugin[:rackspace][:public_ipv4]) - end - - it "populates cloud public ipv6" do - @plugin[:rackspace][:public_ipv6] = "2a00:1a48:7805:111:e875:efaf:ff08:75" - @plugin.run - expect(@plugin[:cloud][:public_ipv6]).to eq(@plugin[:rackspace][:public_ipv6]) - end - - it "populates cloud private ip" do - @plugin[:rackspace][:local_ipv4] = "10.252.42.149" - @plugin.run - expect(@plugin[:cloud][:local_ipv4]).to eq(@plugin[:rackspace][:local_ipv4]) - end - - it "populates cloud private ipv6" do - @plugin[:rackspace][:local_ipv6] = "2a00:1a48:7805:111:e875:efaf:ff08:75" - @plugin.run - expect(@plugin[:cloud][:local_ipv6]).to eq(@plugin[:rackspace][:local_ipv6]) - end - - it "populates first cloud public ip" do - @plugin[:rackspace][:public_ipv4] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips].first).to eq(@plugin[:rackspace][:public_ipv4]) - end - - it "populates first cloud public ip" do - @plugin[:rackspace][:local_ipv4] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:private_ips].first).to eq(@plugin[:rackspace][:local_ipv4]) - end - - it "populates cloud provider" do - @plugin.run - expect(@plugin[:cloud][:provider]).to eq("rackspace") - end - end - - describe "with linode mash" do - before do - @plugin[:linode] = Mash.new - end - - it "populates cloud public ip" do - @plugin[:linode]["public_ip"] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips][0]).to eq(@plugin[:linode][:public_ip]) - end - - it "populates cloud private ip" do - @plugin[:linode]["private_ip"] = "10.252.42.149" - @plugin.run - expect(@plugin[:cloud][:private_ips][0]).to eq(@plugin[:linode][:private_ip]) - end - - it "populates first cloud public ip" do - @plugin[:linode]["public_ip"] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips].first).to eq(@plugin[:linode][:public_ip]) - end - - it "populates cloud provider" do - @plugin.run - expect(@plugin[:cloud][:provider]).to eq("linode") - end - end - - describe "with eucalyptus mash" do - before do - @plugin[:eucalyptus] = Mash.new - end - - it "populates cloud public ip" do - @plugin[:eucalyptus]["public_ipv4"] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips][0]).to eq(@plugin[:eucalyptus]["public_ipv4"]) - end - - it "populates cloud private ip" do - @plugin[:eucalyptus]["local_ipv4"] = "10.252.42.149" - @plugin.run - expect(@plugin[:cloud][:private_ips][0]).to eq(@plugin[:eucalyptus]["local_ipv4"]) - end - - it "populates cloud provider" do - @plugin.run - expect(@plugin[:cloud][:provider]).to eq("eucalyptus") - end - end - - describe "with Azure mash" do - before do - @plugin[:azure] = Mash.new - end - - it "populates cloud private ip" do - @plugin[:azure]["private_ip"] = "10.0.0.1" - @plugin.run - expect(@plugin[:cloud][:private_ips][0]).to eq(@plugin[:azure]["private_ip"]) - end - - it "populates cloud public ip" do - @plugin[:azure]["public_ip"] = "174.129.150.8" - @plugin.run - expect(@plugin[:cloud][:public_ips][0]).to eq(@plugin[:azure]["public_ip"]) - expect(@plugin[:cloud][:public_ipv4]).to eq(@plugin[:azure]["public_ip"]) - end - - it "populates cloud vm_name" do - @plugin[:azure]["vm_name"] = "linux-vm" - @plugin.run - expect(@plugin[:cloud][:vm_name]).to eq(@plugin[:azure]["vm_name"]) - end - - it "populates cloud public_fqdn" do - @plugin[:azure]["public_fqdn"] = "linux-vm-svc.cloudapp.net" - @plugin.run - expect(@plugin[:cloud][:public_fqdn]).to eq(@plugin[:azure]["public_fqdn"]) - expect(@plugin[:cloud][:public_hostname]).to eq(@plugin[:azure]["public_fqdn"]) - end - - it "populates cloud public_ssh_port" do - @plugin[:azure]["public_ssh_port"] = "22" - @plugin.run - expect(@plugin[:cloud][:public_ssh_port]).to eq(@plugin[:azure]["public_ssh_port"]) - end - - it "should not populate cloud public_ssh_port when winrm is used" do - @plugin[:azure]["public_winrm_port"] = "5985" - @plugin.run - expect(@plugin[:cloud][:public_ssh_port]).to be_nil - end - - it "populates cloud public_winrm_port" do - @plugin[:azure]["public_winrm_port"] = "5985" - @plugin.run - expect(@plugin[:cloud][:public_winrm_port]).to eq(@plugin[:azure]["public_winrm_port"]) - end - - it "populates cloud provider" do - @plugin.run - expect(@plugin[:cloud][:provider]).to eq("azure") - end - end - - describe "with digital_ocean mash" do - before do - @plugin[:digital_ocean] = Mash.new - @plugin[:digital_ocean][:hostname] = "public.example.com" - @plugin[:digital_ocean][:interfaces] = Mash.new - @plugin[:digital_ocean][:interfaces] = { - "public" => [ - { - "ipv4" => { - "ip_address" => "159.203.92.161", - "netmask" => "255.255.240.0", - "gateway" => "159.203.80.1", - }, - "ipv6" => { - "ip_address" => "2604:A880:0800:00A1:0000:0000:0201:0001", - "cidr" => 64, - "gateway" => "2604:A880:0800:00A1:0000:0000:0000:0001", - }, - "anchor_ipv4" => { - "ip_address" => "10.17.0.5", - "netmask" => "255.255.0.0", - "gateway" => "10.17.0.1", - }, - "mac" => "04:01:e5:14:03:01", - "type" => "public", - }, - ], - } - end - - before(:each) do - @plugin.run - end - - it "populates cloud public hostname" do - expect(@plugin[:cloud][:public_hostname]).to eq("public.example.com") - end - - it "populates cloud local hostname" do - expect(@plugin[:cloud][:local_hostname]).to be_nil - end - - it "populates cloud public ips" do - expect(@plugin[:cloud][:public_ips]).to eq([ "159.203.92.161", "2604:A880:0800:00A1:0000:0000:0201:0001" ]) - end - - it "populates cloud private ips" do - expect(@plugin[:cloud][:private_ips]).to be_empty - end - - it "populates cloud public_ipv4" do - expect(@plugin[:cloud][:public_ipv4]).to eq("159.203.92.161") - end - - it "populates cloud local_ipv4" do - expect(@plugin[:cloud][:local_ipv4]).to be_nil - end - - it "populates cloud public_ipv6" do - expect(@plugin[:cloud][:public_ipv6]).to eq("2604:A880:0800:00A1:0000:0000:0201:0001") - end - - it "populates cloud local_ipv6" do - expect(@plugin[:cloud][:local_ipv6]).to be_nil - end - - it "populates cloud provider" do - expect(@plugin[:cloud][:provider]).to eq("digital_ocean") - end - end -end diff --git a/spec/unit/plugins/cloud_v2_spec.rb b/spec/unit/plugins/cloud_v2_spec.rb index d7f8654c..eb770a34 100644 --- a/spec/unit/plugins/cloud_v2_spec.rb +++ b/spec/unit/plugins/cloud_v2_spec.rb @@ -88,6 +88,7 @@ describe Ohai::System, "plugin cloud" do @plugin[:digital_ocean] = nil @plugin.run expect(@plugin[:cloud_v2]).to be_nil + expect(@plugin[:cloud]).to be_nil end end |