diff options
author | Tim Smith <tsmith@chef.io> | 2018-04-26 21:22:37 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2018-04-27 10:08:00 -0700 |
commit | 6eb23e0488cc894eb2b2ce1e6d1e4ce3e8c219f6 (patch) | |
tree | e374d1d02ff9732f61b6c0fb997443898fafa060 | |
parent | d64f00f476c9bbcab8cc8cb3e15c6bd1df2a8761 (diff) | |
download | chef-mdadm_cr.tar.gz |
Convert mdadm to a custom resourcemdadm_cr
Also use load_current_value and remove the exists property which is no
longer needed
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/chef/provider/mdadm.rb | 85 | ||||
-rw-r--r-- | lib/chef/providers.rb | 1 | ||||
-rw-r--r-- | lib/chef/resource/mdadm.rb | 66 | ||||
-rw-r--r-- | spec/unit/provider/mdadm_spec.rb | 140 | ||||
-rw-r--r-- | spec/unit/resource/mdadm_spec.rb | 129 |
5 files changed, 174 insertions, 247 deletions
diff --git a/lib/chef/provider/mdadm.rb b/lib/chef/provider/mdadm.rb deleted file mode 100644 index c5f15a851a..0000000000 --- a/lib/chef/provider/mdadm.rb +++ /dev/null @@ -1,85 +0,0 @@ -# -# Author:: Joe Williams (<joe@joetify.com>) -# Copyright:: Copyright 2009-2016, Joe Williams -# 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 "chef/log" -require "chef/provider" - -class Chef - class Provider - class Mdadm < Chef::Provider - - provides :mdadm - - def load_current_resource - @current_resource = Chef::Resource::Mdadm.new(new_resource.name) - current_resource.raid_device(new_resource.raid_device) - logger.trace("#{new_resource} checking for software raid device #{current_resource.raid_device}") - - device_not_found = 4 - mdadm = shell_out!("mdadm --detail --test #{new_resource.raid_device}", :returns => [0, device_not_found]) - exists = (mdadm.status == 0) - current_resource.exists(exists) - end - - def action_create - unless current_resource.exists - converge_by("create RAID device #{new_resource.raid_device}") do - command = "yes | mdadm --create #{new_resource.raid_device} --level #{new_resource.level}" - command << " --chunk=#{new_resource.chunk}" unless new_resource.level == 1 - command << " --metadata=#{new_resource.metadata}" - command << " --bitmap=#{new_resource.bitmap}" if new_resource.bitmap - command << " --layout=#{new_resource.layout}" if new_resource.layout - command << " --raid-devices #{new_resource.devices.length} #{new_resource.devices.join(" ")}" - logger.trace("#{new_resource} mdadm command: #{command}") - shell_out!(command) - logger.info("#{new_resource} created raid device (#{new_resource.raid_device})") - end - else - logger.trace("#{new_resource} raid device already exists, skipping create (#{new_resource.raid_device})") - end - end - - def action_assemble - unless current_resource.exists - converge_by("assemble RAID device #{new_resource.raid_device}") do - command = "yes | mdadm --assemble #{new_resource.raid_device} #{new_resource.devices.join(" ")}" - logger.trace("#{new_resource} mdadm command: #{command}") - shell_out!(command) - logger.info("#{new_resource} assembled raid device (#{new_resource.raid_device})") - end - else - logger.trace("#{new_resource} raid device already exists, skipping assemble (#{new_resource.raid_device})") - end - end - - def action_stop - if current_resource.exists - converge_by("stop RAID device #{new_resource.raid_device}") do - command = "yes | mdadm --stop #{new_resource.raid_device}" - logger.trace("#{new_resource} mdadm command: #{command}") - shell_out!(command) - logger.info("#{new_resource} stopped raid device (#{new_resource.raid_device})") - end - else - logger.trace("#{new_resource} raid device doesn't exist (#{new_resource.raid_device}) - not stopping") - end - end - - end - end -end diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb index 1b9aa84697..fa6b64022f 100644 --- a/lib/chef/providers.rb +++ b/lib/chef/providers.rb @@ -37,7 +37,6 @@ require "chef/provider/launchd" require "chef/provider/link" require "chef/provider/log" require "chef/provider/ohai" -require "chef/provider/mdadm" require "chef/provider/mount" require "chef/provider/noop" require "chef/provider/package" diff --git a/lib/chef/resource/mdadm.rb b/lib/chef/resource/mdadm.rb index 3b54a53e98..593d7187b4 100644 --- a/lib/chef/resource/mdadm.rb +++ b/lib/chef/resource/mdadm.rb @@ -23,23 +23,73 @@ class Chef class Resource class Mdadm < Chef::Resource resource_name :mdadm + provides :mdadm - description "Use the mdadm resource to manage RAID devices in a Linux environment using the mdadm utility. The mdadm resource"\ - " will create and assemble an array, but it will not create the config file that is used to persist the array upon"\ - " reboot. If the config file is required, it must be done by specifying a template with the correct array layout,"\ - " and then by using the mount provider to create a file systems table (fstab) entry." - - default_action :create - allowed_actions :create, :assemble, :stop + description "Use the mdadm resource to manage RAID devices in a Linux environment using the mdadm utility. The mdadm resource will create and assemble an array, but it will not create the config file that is used to persist the array upon reboot. If the config file is required, it must be done by specifying a template with the correct array layout, and then by using the mount provider to create a file systems table (fstab) entry." property :chunk, Integer, default: 16 property :devices, Array, default: lazy { [] } - property :exists, [ TrueClass, FalseClass ], default: false property :level, Integer, default: 1 property :metadata, String, default: "0.90" property :bitmap, String property :raid_device, String, identity: true, name_property: true property :layout, String + + load_current_value do |desired| + logger.debug("#{desired} checking for software raid device #{desired.raid_device}") + + device_not_found = 4 + mdadm = shell_out!("mdadm --detail --test #{desired.raid_device}", :returns => [0, device_not_found]) + if mdadm.status == 0 + raid_device desired.raid_device + else + current_value_does_not_exist! + end + end + + action :create do + unless current_resource + converge_by("create RAID device #{new_resource.raid_device}") do + command = "yes | mdadm --create #{new_resource.raid_device} --level #{new_resource.level}" + command << " --chunk=#{new_resource.chunk}" unless new_resource.level == 1 + command << " --metadata=#{new_resource.metadata}" + command << " --bitmap=#{new_resource.bitmap}" if new_resource.bitmap + command << " --layout=#{new_resource.layout}" if new_resource.layout + command << " --raid-devices #{new_resource.devices.length} #{new_resource.devices.join(" ")}" + logger.trace("#{new_resource} mdadm command: #{command}") + shell_out!(command) + logger.info("#{new_resource} created raid device (#{new_resource.raid_device})") + end + else + logger.debug("#{new_resource} raid device already exists, skipping create (#{new_resource.raid_device})") + end + end + + action :assemble do + unless current_resource + converge_by("assemble RAID device #{new_resource.raid_device}") do + command = "yes | mdadm --assemble #{new_resource.raid_device} #{new_resource.devices.join(" ")}" + logger.trace("#{new_resource} mdadm command: #{command}") + shell_out!(command) + logger.info("#{new_resource} assembled raid device (#{new_resource.raid_device})") + end + else + logger.debug("#{new_resource} raid device already exists, skipping assemble (#{new_resource.raid_device})") + end + end + + action :stop do + if current_resource + converge_by("stop RAID device #{new_resource.raid_device}") do + command = "yes | mdadm --stop #{new_resource.raid_device}" + logger.trace("#{new_resource} mdadm command: #{command}") + shell_out!(command) + logger.info("#{new_resource} stopped raid device (#{new_resource.raid_device})") + end + else + logger.debug("#{new_resource} raid device doesn't exist (#{new_resource.raid_device}) - not stopping") + end + end end end end diff --git a/spec/unit/provider/mdadm_spec.rb b/spec/unit/provider/mdadm_spec.rb deleted file mode 100644 index 8ef884e131..0000000000 --- a/spec/unit/provider/mdadm_spec.rb +++ /dev/null @@ -1,140 +0,0 @@ -# -# Author:: Joe Williams (<joe@joetify.com>) -# Copyright:: Copyright 2009-2016, Joe Williams -# 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 "spec_helper" -require "ostruct" - -describe Chef::Provider::Mdadm do - - before(:each) do - @node = Chef::Node.new - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, {}, @events) - @new_resource = Chef::Resource::Mdadm.new("/dev/md1") - @new_resource.devices ["/dev/sdz1", "/dev/sdz2", "/dev/sdz3"] - @provider = Chef::Provider::Mdadm.new(@new_resource, @run_context) - end - - describe "when determining the current metadevice status" do - it "should set the current resources mount point to the new resources mount point" do - allow(@provider).to receive(:shell_out!).and_return(OpenStruct.new(:status => 0)) - @provider.load_current_resource - expect(@provider.current_resource.name).to eq("/dev/md1") - expect(@provider.current_resource.raid_device).to eq("/dev/md1") - end - - it "determines that the metadevice exists when mdadm exit code is zero" do - allow(@provider).to receive(:shell_out!).with("mdadm --detail --test /dev/md1", :returns => [0, 4]).and_return(OpenStruct.new(:status => 0)) - @provider.load_current_resource - expect(@provider.current_resource.exists).to be_truthy - end - - it "determines that the metadevice does not exist when mdadm exit code is 4" do - allow(@provider).to receive(:shell_out!).with("mdadm --detail --test /dev/md1", :returns => [0, 4]).and_return(OpenStruct.new(:status => 4)) - @provider.load_current_resource - expect(@provider.current_resource.exists).to be_falsey - end - end - - describe "after the metadevice status is known" do - before(:each) do - @current_resource = Chef::Resource::Mdadm.new("/dev/md1") - @new_resource.level 5 - allow(@provider).to receive(:load_current_resource).and_return(true) - @provider.current_resource = @current_resource - end - - describe "when creating the metadevice" do - it "should create the raid device if it doesnt exist" do - @current_resource.exists(false) - expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" - expect(@provider).to receive(:shell_out!).with(expected_command) - @provider.run_action(:create) - end - - it "should specify a bitmap only if set" do - @current_resource.exists(false) - @new_resource.bitmap("grow") - expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --bitmap=grow --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" - expect(@provider).to receive(:shell_out!).with(expected_command) - @provider.run_action(:create) - expect(@new_resource).to be_updated_by_last_action - end - - it "should specify a layout only if set" do - @current_resource.exists(false) - @new_resource.layout("rs") - expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --layout=rs --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" - expect(@provider).to receive(:shell_out!).with(expected_command) - @provider.run_action(:create) - expect(@new_resource).to be_updated_by_last_action - end - - it "should not specify a chunksize if raid level 1" do - @current_resource.exists(false) - @new_resource.level 1 - expected_command = "yes | mdadm --create /dev/md1 --level 1 --metadata=0.90 --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" - expect(@provider).to receive(:shell_out!).with(expected_command) - @provider.run_action(:create) - expect(@new_resource).to be_updated_by_last_action - end - - it "should not create the raid device if it does exist" do - @current_resource.exists(true) - expect(@provider).not_to receive(:shell_out!) - @provider.run_action(:create) - expect(@new_resource).not_to be_updated_by_last_action - end - end - - describe "when asembling the metadevice" do - it "should assemble the raid device if it doesnt exist" do - @current_resource.exists(false) - expected_mdadm_cmd = "yes | mdadm --assemble /dev/md1 /dev/sdz1 /dev/sdz2 /dev/sdz3" - expect(@provider).to receive(:shell_out!).with(expected_mdadm_cmd) - @provider.run_action(:assemble) - expect(@new_resource).to be_updated_by_last_action - end - - it "should not assemble the raid device if it doesnt exist" do - @current_resource.exists(true) - expect(@provider).not_to receive(:shell_out!) - @provider.run_action(:assemble) - expect(@new_resource).not_to be_updated_by_last_action - end - end - - describe "when stopping the metadevice" do - - it "should stop the raid device if it exists" do - @current_resource.exists(true) - expected_mdadm_cmd = "yes | mdadm --stop /dev/md1" - expect(@provider).to receive(:shell_out!).with(expected_mdadm_cmd) - @provider.run_action(:stop) - expect(@new_resource).to be_updated_by_last_action - end - - it "should not attempt to stop the raid device if it does not exist" do - @current_resource.exists(false) - expect(@provider).not_to receive(:shell_out!) - @provider.run_action(:stop) - expect(@new_resource).not_to be_updated_by_last_action - end - end - end -end diff --git a/spec/unit/resource/mdadm_spec.rb b/spec/unit/resource/mdadm_spec.rb index 6d595dc0f8..45482c3c46 100644 --- a/spec/unit/resource/mdadm_spec.rb +++ b/spec/unit/resource/mdadm_spec.rb @@ -18,10 +18,17 @@ # require "spec_helper" +require "ostruct" describe Chef::Resource::Mdadm do - let(:resource) { Chef::Resource::Mdadm.new("fakey_fakerton") } + let(:node) { Chef::Node.new } + let(:events) { Chef::EventDispatch::Dispatcher.new } + let(:run_context) { Chef::RunContext.new(node, {}, events) } + let(:collection) { double("resource collection") } + let(:resource) { Chef::Resource::Mdadm.new("/dev/md1", run_context) } + let(:provider) { resource.provider_for_action(:create) } + let(:new_resource) { Chef::Resource::Mdadm.new("/dev/md1") } it "has a resource name of :mdadm" do expect(resource.resource_name).to eql(:mdadm) @@ -37,46 +44,41 @@ describe Chef::Resource::Mdadm do expect { resource.action :stop }.not_to raise_error end - it "allows you to set the raid_device attribute" do + it "allows you to set the raid_device property" do resource.raid_device "/dev/md3" expect(resource.raid_device).to eql("/dev/md3") end - it "allows you to set the chunk attribute" do + it "allows you to set the chunk property" do resource.chunk 256 expect(resource.chunk).to eql(256) end - it "allows you to set the level attribute" do + it "allows you to set the level property" do resource.level 1 expect(resource.level).to eql(1) end - it "allows you to set the metadata attribute" do + it "allows you to set the metadata property" do resource.metadata "1.2" expect(resource.metadata).to eql("1.2") end - it "allows you to set the bitmap attribute" do + it "allows you to set the bitmap property" do resource.bitmap "internal" expect(resource.bitmap).to eql("internal") end - it "allows you to set the layout attribute" do + it "allows you to set the layout property" do resource.layout "f2" expect(resource.layout).to eql("f2") end - it "allows you to set the devices attribute" do + it "allows you to set the devices property" do resource.devices ["/dev/sda", "/dev/sdb"] expect(resource.devices).to eql(["/dev/sda", "/dev/sdb"]) end - it "allows you to set the exists attribute" do - resource.exists true - expect(resource.exists).to eql(true) - end - describe "when it has devices, level, and chunk" do before do resource.raid_device("raider") @@ -97,4 +99,105 @@ describe Chef::Resource::Mdadm do end end + describe "when determining the current metadevice status" do + it "determines that the metadevice exists when mdadm exit code is zero" do + allow(resource).to receive(:shell_out!).with("mdadm --detail --test /dev/md1", :returns => [0, 4]).and_return(OpenStruct.new(:status => 0)) + provider.load_current_resource + expect(provider.current_resource).not_to be_nil + end + + it "determines that the metadevice does not exist when mdadm exit code is 4" do + allow(resource).to receive(:shell_out!).with("mdadm --detail --test /dev/md1", :returns => [0, 4]).and_return(OpenStruct.new(:status => 4)) + provider.load_current_resource + expect(provider.current_resource).to be_nil + end + end + + describe "after the metadevice status is known" do + before(:each) do + current_resource = Chef::Resource::Mdadm.new("/dev/md1") + new_resource.level 5 + allow(provider).to receive(:load_current_resource).and_return(true) + provider.current_resource = current_resource + end + + describe "when creating the metadevice" do + it "should create the raid device if it doesnt exist" do + @current_resource.exists(false) + expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" + expect(provider).to receive(:shell_out!).with(expected_command) + provider.run_action(:create) + end + + it "should specify a bitmap only if set" do + @current_resource.exists(false) + new_resource.bitmap("grow") + expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --bitmap=grow --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" + expect(provider).to receive(:shell_out!).with(expected_command) + provider.run_action(:create) + expect(new_resource).to be_updated_by_last_action + end + + it "should specify a layout only if set" do + @current_resource.exists(false) + new_resource.layout("rs") + expected_command = "yes | mdadm --create /dev/md1 --level 5 --chunk=16 --metadata=0.90 --layout=rs --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" + expect(provider).to receive(:shell_out!).with(expected_command) + provider.run_action(:create) + expect(new_resource).to be_updated_by_last_action + end + + it "should not specify a chunksize if raid level 1" do + @current_resource.exists(false) + @new_resource.level 1 + expected_command = "yes | mdadm --create /dev/md1 --level 1 --metadata=0.90 --raid-devices 3 /dev/sdz1 /dev/sdz2 /dev/sdz3" + expect(@provider).to receive(:shell_out!).with(expected_command) + @provider.run_action(:create) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not create the raid device if it does exist" do + @current_resource.exists(true) + expect(@provider).not_to receive(:shell_out!) + @provider.run_action(:create) + expect(@new_resource).not_to be_updated_by_last_action + end + end + + describe "when asembling the metadevice" do + it "should assemble the raid device if it doesnt exist" do + @current_resource.exists(false) + expected_mdadm_cmd = "yes | mdadm --assemble /dev/md1 /dev/sdz1 /dev/sdz2 /dev/sdz3" + expect(@provider).to receive(:shell_out!).with(expected_mdadm_cmd) + @provider.run_action(:assemble) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not assemble the raid device if it doesnt exist" do + @current_resource.exists(true) + expect(@provider).not_to receive(:shell_out!) + @provider.run_action(:assemble) + expect(@new_resource).not_to be_updated_by_last_action + end + end + + describe "when stopping the metadevice" do + + it "should stop the raid device if it exists" do + @current_resource.exists(true) + expected_mdadm_cmd = "yes | mdadm --stop /dev/md1" + expect(@provider).to receive(:shell_out!).with(expected_mdadm_cmd) + @provider.run_action(:stop) + expect(@new_resource).to be_updated_by_last_action + end + + it "should not attempt to stop the raid device if it does not exist" do + @current_resource.exists(false) + expect(@provider).not_to receive(:shell_out!) + @provider.run_action(:stop) + expect(@new_resource).not_to be_updated_by_last_action + end + end + end + end |