implement clone LWRP using library code

This commit is contained in:
Adam Spiers 2014-03-04 15:52:48 +00:00
parent 64bd9c8fb4
commit cfc1eeb4c1
3 changed files with 112 additions and 44 deletions

View File

@ -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 clone #{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 clone '#{name}'."
else
Chef::Log.error "Failed to configure clone #{name}."
end
rescue
Chef::Log.error "Failed to configure clone #{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 clone #{name}" do
command cmd
only_if { resource_exists?(name) }
end
new_resource.updated_by_last_action(true)
Chef::Log.info "Deleted clone '#{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::Clone
end
def load_current_resource
standard_load_current_resource
end
def init_current_resource
name = @new_resource.name
@current_resource = Chef::Resource::PacemakerClone.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

View File

@ -1,8 +1,7 @@
# Author:: Robert Choi
# Cookbook Name:: pacemaker
# Resource:: clone
#
# 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 => {}

View File

@ -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/clone_resource', File.dirname(__FILE__))
describe "Chef::Provider::PacemakerClone" do
# for use inside examples:
let(:fixture) { Chef::RSpec::Pacemaker::Config::CLONE_RESOURCE.dup }
# for use outside examples (e.g. when invoking shared_examples)
fixture = Chef::RSpec::Pacemaker::Config::CLONE_RESOURCE.dup
before(:each) do
runner_opts = {
:step_into => ['pacemaker_clone']
}
@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::PacemakerClone.new(fixture.name, @run_context)
@resource.rsc fixture.rsc.dup
@resource.meta Hash[fixture.meta.dup]
end
let (:provider) { Chef::Provider::PacemakerClone.new(@resource, @run_context) }
def cib_object_class
Pacemaker::Resource::Clone
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 clone if the resource is 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