|
| 1 | +# |
| 2 | +# Author:: Joe Williams (<joe@joetify.com>) |
| 3 | +# Copyright:: Copyright (c) 2009 Joe Williams |
| 4 | +# License:: Apache License, Version 2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +require 'chef/log' |
| 20 | +require 'chef/mixin/command' |
| 21 | +require 'chef/provider' |
| 22 | + |
| 23 | +class Chef |
| 24 | + class Provider |
| 25 | + class Mdadm < Chef::Provider |
| 26 | + |
| 27 | + include Chef::Mixin::Command |
| 28 | + |
| 29 | + def initialize(node, new_resource) |
| 30 | + super(node, new_resource) |
| 31 | + end |
| 32 | + |
| 33 | + def load_current_resource |
| 34 | + @current_resource = Chef::Resource::Mdadm.new(@new_resource.name) |
| 35 | + @current_resource.raid_device(@new_resource.raid_device) |
| 36 | + Chef::Log.debug("Checking for software raid device #{@current_resource.raid_device}") |
| 37 | + |
| 38 | + command = "mdadm --detail --scan" |
| 39 | + exists = false |
| 40 | + status = popen4(command) do |pid, stdin, stdout, stderr| |
| 41 | + stdout.each do |line| |
| 42 | + if line.include? @new_resource.raid_device |
| 43 | + exists = true |
| 44 | + end |
| 45 | + end |
| 46 | + end |
| 47 | + @current_resource.exists(exists) |
| 48 | + end |
| 49 | + |
| 50 | + def action_create |
| 51 | + unless @current_resource.exists |
| 52 | + command = "yes | mdadm --create #{@new_resource.raid_device} --chunk=#{@new_resource.chunk} --level #{@new_resource.level} --raid-devices #{@new_resource.devices.length} #{@new_resource.devices.collect{|x| x + " "}.to_s.chop}" |
| 53 | + Chef::Log.debug("mdadm command: #{command}") |
| 54 | + pid, stdin, stdout, stderr = popen4(command) |
| 55 | + Chef::Log.info("Created mdadm raid device (#{@new_resource.raid_device})") |
| 56 | + @new_resource.updated = true |
| 57 | + else |
| 58 | + Chef::Log.debug("mdadm raid device already exists (#{@new_resource.raid_device})") |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def action_assemble |
| 63 | + unless @current_resource.exists |
| 64 | + command = "yes | mdadm --assemble #{@new_resource.raid_device} #{@new_resource.devices.collect{|x| x + " "}.to_s.chop}" |
| 65 | + Chef::Log.debug("mdadm command: #{command}") |
| 66 | + pid, stdin, stdout, stderr = popen4(command) |
| 67 | + Chef::Log.info("Assembled mdadm raid device (#{@new_resource.raid_device})") |
| 68 | + @new_resource.updated = true |
| 69 | + else |
| 70 | + Chef::Log.debug("mdadm raid device already exists (#{@new_resource.raid_device})") |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def action_stop |
| 75 | + if @current_resource.exists |
| 76 | + command = "yes | mdadm --stop #{@new_resource.raid_device}" |
| 77 | + Chef::Log.debug("mdadm command: #{command}") |
| 78 | + pid, stdin, stdout, stderr = popen4(command) |
| 79 | + Chef::Log.info("Stopped mdadm raid device (#{@new_resource.raid_device})") |
| 80 | + @new_resource.updated = true |
| 81 | + else |
| 82 | + Chef::Log.debug("mdadm raid device doesn't exist (#{@new_resource.raid_device})") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + end |
| 87 | + end |
| 88 | +end |
0 commit comments