From 81a3b2e83c160206771d19fe9b9ab248ef18f032 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 13 Jun 2013 18:42:34 +0800 Subject: [PATCH 1/4] Initial Pool resource & provider --- providers/pool.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ resources/pool.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 providers/pool.rb create mode 100644 resources/pool.rb diff --git a/providers/pool.rb b/providers/pool.rb new file mode 100644 index 0000000..e32c8c1 --- /dev/null +++ b/providers/pool.rb @@ -0,0 +1,43 @@ +# +# Cookbook Name:: ceph +# Provider:: pool +# +# Author:: Hunter Nield +# +# Copyright 2013, Hunter Nield +# +# 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. +# + + +action :create do + Chef::Log.info("Creating pool '#{new_resource.name}'") + + execute "create pool" do + not_if "rados lspools | grep #{new_resource.name}" + command("rados mkpool #{new_resource.name}") + end +end + +action :delete do + Chef::Log.info("Deleting pool '#{new_resource.name}'") + + execute "delete pool" do + only_if "rados lspools | grep #{new_resource.name}" + if new_resource.force == true + command("rados rmpool #{new_resource.name} #{new_resource.name} --yes-i-really-really-mean-it") + else + command("rados rmpool #{new_resource.name}") + end + end +end \ No newline at end of file diff --git a/resources/pool.rb b/resources/pool.rb new file mode 100644 index 0000000..29c3ad3 --- /dev/null +++ b/resources/pool.rb @@ -0,0 +1,26 @@ +# +# Author:: Hunter Nield () +# Cookbook Name:: ceph +# Resource:: pool +# +# Copyright 2013, Hunter NIeld +# +# 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. + +actions :create, :delete +default_action :create + +attribute :name, :kind_of => String, + :name_attribute => true +attribute :force, :kind_of => [ TrueClass, FalseClass ], + :default => false \ No newline at end of file From df7de35d16b558c1714352d8b314afa8c5f92c28 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 13 Jun 2013 18:42:48 +0800 Subject: [PATCH 2/4] Basic RBD image provider & resource --- providers/rbd_image.rb | 46 ++++++++++++++++++++++++++++++++++++++++++ resources/rbd_image.rb | 25 +++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 providers/rbd_image.rb create mode 100644 resources/rbd_image.rb diff --git a/providers/rbd_image.rb b/providers/rbd_image.rb new file mode 100644 index 0000000..6685b0b --- /dev/null +++ b/providers/rbd_image.rb @@ -0,0 +1,46 @@ +# +# Cookbook Name:: ceph +# Provider:: rbd_image +# +# Author:: Hunter Nield +# +# Copyright 2013, Hunter Nield +# +# 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. +# + + +action :create do + Chef::Log.info("Creating block device '#{new_resource.name}' of #{new_resource.size}MB") + + if new_resource.pool + pool = "-p #{new_resource.pool}" + end + + execute "create rbd image" do + not_if "rbd #{pool} ls | grep #{new_resource.name}" + command("rbd create #{pool} --size #{new_resource.size} #{new_resource.name}") + end +end + +action :delete do + Chef::Log.info("Deleting block device '#{new_resource.name}'") + if new_resource.pool + pool = "-p #{new_resource.pool}" + end + + execute "create rbd image" do + only_if "rbd #{pool} ls | grep #{new_resource.name}" + command("rbd rm #{pool} #{new_resource.name}") + end +end \ No newline at end of file diff --git a/resources/rbd_image.rb b/resources/rbd_image.rb new file mode 100644 index 0000000..243a8cb --- /dev/null +++ b/resources/rbd_image.rb @@ -0,0 +1,25 @@ +# +# Author:: Hunter Nield () +# Cookbook Name:: ceph +# Resource:: rbd_image +# +# Copyright 2013, Hunter NIeld +# +# 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. + +actions :create, :delete +default_action :create + +attribute :name, :kind_of => String, :name_attribute => true +attribute :pool, :kind_of => String, :default => nil +attribute :size, :kind_of => Fixnum, :default => nil From e1c7dbf0e5ed76caedf9d0d54275b5fd5648237d Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Mon, 17 Jun 2013 12:49:20 +0800 Subject: [PATCH 3/4] RBD map and unmap support --- providers/rbd_image.rb | 28 +++++++++++++++++++++++++++- resources/rbd_image.rb | 5 +++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/providers/rbd_image.rb b/providers/rbd_image.rb index 6685b0b..29c830a 100644 --- a/providers/rbd_image.rb +++ b/providers/rbd_image.rb @@ -43,4 +43,30 @@ only_if "rbd #{pool} ls | grep #{new_resource.name}" command("rbd rm #{pool} #{new_resource.name}") end -end \ No newline at end of file +end + +action :map do + Chef::Log.info("Mapping block device '#{new_resource.name}'") + + if new_resource.pool + pool = "-p #{new_resource.pool}" + end + + execute "map rbd image" do + not_if "rbd #{pool} showmapped | grep #{new_resource.name}" + command("rbd map #{pool} #{new_resource.name}") + end +end + +action :unmap do + Chef::Log.info("Unmapping block device '#{new_resource.name}' mounted at '#{new_resource.device}'") + + if new_resource.pool + pool = "-p #{new_resource.pool}" + end + + execute "unmap rbd image" do + only_if "rbd #{pool} showmapped | grep #{new_resource.device}" + command("rbd unmap #{pool} #{new_resource.device}") + end +end diff --git a/resources/rbd_image.rb b/resources/rbd_image.rb index 243a8cb..80a09f6 100644 --- a/resources/rbd_image.rb +++ b/resources/rbd_image.rb @@ -17,9 +17,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -actions :create, :delete +actions :create, :delete, :map, :unmap default_action :create -attribute :name, :kind_of => String, :name_attribute => true +attribute :name, :kind_of => String, :name_attribute => true attribute :pool, :kind_of => String, :default => nil attribute :size, :kind_of => Fixnum, :default => nil +attribute :device, :kind_of => String, :default => nil From cd422803cc47f9ba73eea8cf047460b9538042f3 Mon Sep 17 00:00:00 2001 From: Hunter Nield Date: Thu, 11 Jul 2013 15:14:29 +0800 Subject: [PATCH 4/4] Use converge_by and handle whyrun for Pool/RBD providers --- providers/pool.rb | 30 ++++++++++------- providers/rbd_image.rb | 73 ++++++++++++++++++++++++++---------------- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/providers/pool.rb b/providers/pool.rb index e32c8c1..60d6b09 100644 --- a/providers/pool.rb +++ b/providers/pool.rb @@ -19,25 +19,31 @@ # limitations under the License. # +def whyrun_supported? + true +end action :create do - Chef::Log.info("Creating pool '#{new_resource.name}'") - - execute "create pool" do - not_if "rados lspools | grep #{new_resource.name}" - command("rados mkpool #{new_resource.name}") + converge_by("Creating pool '#{new_resource.name}'") do + execute "create pool" do + not_if "rados lspools | grep #{new_resource.name}" + command("rados mkpool #{new_resource.name}") + end + Chef::Log.info("Created` pool '#{new_resource.name}'") end end action :delete do - Chef::Log.info("Deleting pool '#{new_resource.name}'") - execute "delete pool" do - only_if "rados lspools | grep #{new_resource.name}" - if new_resource.force == true - command("rados rmpool #{new_resource.name} #{new_resource.name} --yes-i-really-really-mean-it") - else - command("rados rmpool #{new_resource.name}") + converge_by("Deleting pool '#{new_resource.name}'") do + execute "delete pool" do + only_if "rados lspools | grep #{new_resource.name}" + if new_resource.force == true + command("rados rmpool #{new_resource.name} #{new_resource.name} --yes-i-really-really-mean-it") + else + command("rados rmpool #{new_resource.name}") + end + Chef::Log.info("Deleted pool '#{new_resource.name}'") end end end \ No newline at end of file diff --git a/providers/rbd_image.rb b/providers/rbd_image.rb index 29c830a..cca31dc 100644 --- a/providers/rbd_image.rb +++ b/providers/rbd_image.rb @@ -19,54 +19,71 @@ # limitations under the License. # +def whyrun_supported? + true +end + action :create do - Chef::Log.info("Creating block device '#{new_resource.name}' of #{new_resource.size}MB") + + converge_by("Creating block device '#{new_resource.name}' of #{new_resource.size}MB") do + if new_resource.pool + pool = "-p #{new_resource.pool}" + end - if new_resource.pool - pool = "-p #{new_resource.pool}" + execute "create rbd image" do + not_if "rbd #{pool} ls | grep #{new_resource.name}" + command("rbd create #{pool} --size #{new_resource.size} #{new_resource.name}") + end + Chef::Log.info("Created block device '#{new_resource.name}' of #{new_resource.size}MB") end - execute "create rbd image" do - not_if "rbd #{pool} ls | grep #{new_resource.name}" - command("rbd create #{pool} --size #{new_resource.size} #{new_resource.name}") - end end action :delete do - Chef::Log.info("Deleting block device '#{new_resource.name}'") - if new_resource.pool - pool = "-p #{new_resource.pool}" - end - execute "create rbd image" do - only_if "rbd #{pool} ls | grep #{new_resource.name}" - command("rbd rm #{pool} #{new_resource.name}") + converge_by("Deleting block device '#{new_resource.name}'") do + if new_resource.pool + pool = "-p #{new_resource.pool}" + end + + execute "create rbd image" do + only_if "rbd #{pool} ls | grep #{new_resource.name}" + command("rbd rm #{pool} #{new_resource.name}") + end + Chef::Log.info("Deleted block device '#{new_resource.name}'") end + end action :map do - Chef::Log.info("Mapping block device '#{new_resource.name}'") - if new_resource.pool - pool = "-p #{new_resource.pool}" - end + converge_by("Mapping block device '#{new_resource.name}'") do + if new_resource.pool + pool = "-p #{new_resource.pool}" + end - execute "map rbd image" do - not_if "rbd #{pool} showmapped | grep #{new_resource.name}" - command("rbd map #{pool} #{new_resource.name}") + execute "map rbd image" do + not_if "rbd #{pool} showmapped | grep #{new_resource.name}" + command("rbd map #{pool} #{new_resource.name}") + end + Chef::Log.info("Mapped block device '#{new_resource.name}'") end + end action :unmap do - Chef::Log.info("Unmapping block device '#{new_resource.name}' mounted at '#{new_resource.device}'") + - if new_resource.pool - pool = "-p #{new_resource.pool}" - end + converge_by("Unmapping block device '#{new_resource.name}' mounted at '#{new_resource.device}'") do + if new_resource.pool + pool = "-p #{new_resource.pool}" + end - execute "unmap rbd image" do - only_if "rbd #{pool} showmapped | grep #{new_resource.device}" - command("rbd unmap #{pool} #{new_resource.device}") + execute "unmap rbd image" do + only_if "rbd #{pool} showmapped | grep #{new_resource.device}" + command("rbd unmap #{pool} #{new_resource.device}") + end + Chef::Log.info("Unmapped block device '#{new_resource.name}' mounted at '#{new_resource.device}'") end end