diff --git a/providers/ms.rb b/providers/ms.rb index c58cb34..1d772fd 100644 --- a/providers/ms.rb +++ b/providers/ms.rb @@ -1,8 +1,8 @@ -# Author:: Robert Choi # Cookbook Name:: pacemaker -# Provider:: +# Provider:: ms # # Copyright:: 2013, Robert Choi +# Copyright:: 2014, SUSE # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,49 +17,56 @@ # limitations under the License. # -require ::File.expand_path('../libraries/pacemaker/cib_object', +require ::File.expand_path('../libraries/pacemaker', ::File.dirname(__FILE__)) +require ::File.expand_path('../libraries/chef/mixin/pacemaker', ::File.dirname(__FILE__)) +include Chef::Mixin::Pacemaker::RunnableResource + action :create do - name = new_resource.name - rsc = new_resource.rsc - - unless resource_exists?(name) - cmd = "crm configure ms #{name} #{rsc}" - - if new_resource.meta - cmd << " meta" - new_resource.meta.each do |key, value| - cmd << " #{key}=\"#{value}\"" - end - end - - cmd_ = Mixlib::ShellOut.new(cmd) - cmd_.environment['HOME'] = ENV.fetch('HOME', '/root') - cmd_.run_command - begin - cmd_.error! - if resource_exists?(name) - new_resource.updated_by_last_action(true) - Chef::Log.info "Successfully configured ms '#{name}'." - else - Chef::Log.error "Failed to configure ms #{name}." - end - rescue - Chef::Log.error "Failed to configure ms #{name}." - end - end + standard_create_action end action :delete do - name = new_resource.name - cmd = "crm resource stop #{name}; crm configure delete #{name}" - - e = execute "delete ms #{name}" do - command cmd - only_if { resource_exists?(name) } - end - - new_resource.updated_by_last_action(true) - Chef::Log.info "Deleted ms '#{name}'." + delete_runnable_resource +end + +action :start do + start_runnable_resource +end + +action :stop do + stop_runnable_resource +end + +def cib_object_class + ::Pacemaker::Resource::MasterSlave +end + +def load_current_resource + standard_load_current_resource +end + +def init_current_resource + name = @new_resource.name + @current_resource = Chef::Resource::PacemakerMs.new(name) + @current_cib_object.copy_attrs_to_chef_resource(@current_resource, :rsc) +end + +def create_resource(name) + standard_create_resource +end + +def maybe_modify_resource(name) + Chef::Log.info "Checking existing #{@current_cib_object} for modifications" + + desired_clone = cib_object_class.from_chef_resource(new_resource) + if desired_clone.definition_string != @current_cib_object.definition_string + Chef::Log.debug "changed from [#{@current_cib_object.definition_string}] to [#{desired_clone.definition_string}]" + cmd = desired_clone.reconfigure_command + execute cmd do + action :nothing + end.run_action(:run) + new_resource.updated_by_last_action(true) + end end diff --git a/resources/ms.rb b/resources/ms.rb index 56d0c8a..49901aa 100644 --- a/resources/ms.rb +++ b/resources/ms.rb @@ -1,8 +1,7 @@ -# Author:: Robert Choi # Cookbook Name:: pacemaker # Resource:: ms # -# Copyright:: 2013, Robert Choi +# Copyright:: 2013, Robert Choi, SUSE # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -17,10 +16,10 @@ # limitations under the License. # -actions :create, :delete +actions :create, :delete, :start, :stop default_action :create attribute :name, :kind_of => String, :name_attribute => true -attribute :rsc, :kind_of => String -attribute :meta, :kind_of => Hash +attribute :rsc, :kind_of => String +attribute :meta, :kind_of => Hash, :default => {} diff --git a/spec/providers/ms_spec.rb b/spec/providers/ms_spec.rb new file mode 100644 index 0000000..5f15e89 --- /dev/null +++ b/spec/providers/ms_spec.rb @@ -0,0 +1,62 @@ +require 'chef/application' +require File.expand_path('../spec_helper', File.dirname(__FILE__)) +require File.expand_path('../helpers/cib_object', File.dirname(__FILE__)) +require File.expand_path('../helpers/runnable_resource', File.dirname(__FILE__)) +require File.expand_path('../fixtures/ms_resource', File.dirname(__FILE__)) + +describe "Chef::Provider::PacemakerMs" do + # for use inside examples: + let(:fixture) { Chef::RSpec::Pacemaker::Config::MS_RESOURCE.dup } + # for use outside examples (e.g. when invoking shared_examples) + fixture = Chef::RSpec::Pacemaker::Config::MS_RESOURCE.dup + + before(:each) do + runner_opts = { + :step_into => ['pacemaker_ms'] + } + @chef_run = ::ChefSpec::Runner.new(runner_opts) + @chef_run.converge "pacemaker::default" + @node = @chef_run.node + @run_context = @chef_run.run_context + + @resource = Chef::Resource::PacemakerMs.new(fixture.name, @run_context) + @resource.rsc fixture.rsc.dup + @resource.meta Hash[fixture.meta.dup] + end + + let (:provider) { Chef::Provider::PacemakerMs.new(@resource, @run_context) } + + def cib_object_class + Pacemaker::Resource::MasterSlave + end + + include Chef::RSpec::Pacemaker::CIBObject + + describe ":create action" do + def test_modify(expected_cmds) + yield + + stub_shellout(fixture.definition_string) + + provider.run_action :create + + expected_cmds.each do |cmd| + expect(@chef_run).to run_execute(cmd) + end + expect(@resource).to be_updated + end + + it "should modify the resource if it's changed" do + expected = fixture.dup + expected.rsc = 'primitive2' + expected_configure_cmd_args = [expected.reconfigure_command] + test_modify(expected_configure_cmd_args) do + @resource.rsc expected.rsc + end + end + + end + + it_should_behave_like "a runnable resource", fixture + +end