implement order LWRP using library code

This commit is contained in:
Adam Spiers 2014-03-22 12:20:24 +00:00
parent d00a17fb1e
commit cf0cd1a9fc
3 changed files with 115 additions and 38 deletions

View File

@ -1,8 +1,8 @@
# Author:: Robert Choi
# Cookbook Name:: pacemaker
# Provider:: order
#
# 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,46 +17,57 @@
# limitations under the License.
#
require ::File.expand_path('../libraries/pacemaker/cib_object',
::File.dirname(__FILE__))
this_dir = ::File.dirname(__FILE__)
require ::File.expand_path('../libraries/pacemaker/cib_object', this_dir)
require ::File.expand_path('../libraries/pacemaker', this_dir)
require ::File.expand_path('../libraries/chef/mixin/pacemaker', this_dir)
include Chef::Mixin::Pacemaker::StandardCIBObject
action :create do
name = new_resource.name
priority = new_resource.priority
resources = new_resource.resources
unless resource_exists?(name)
cmd = "crm configure order #{name} #{priority}:"
resources.each do |rsc|
cmd << " #{rsc}"
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 order '#{name}'."
else
Chef::Log.error "Failed to configure order #{name}."
end
rescue
Chef::Log.error "Failed to configure order #{name}."
end
if @current_resource_definition.nil?
create_resource(name)
else
maybe_modify_resource(name)
end
end
action :delete do
name = new_resource.name
cmd = "crm resource stop #{name}; crm configure delete #{name}"
e = execute "delete order #{name}" do
command cmd
only_if { resource_exists?(name) }
end
new_resource.updated_by_last_action(true)
Chef::Log.info "Deleted order '#{name}'."
next unless @current_resource
standard_delete_resource
end
def cib_object_class
::Pacemaker::Constraint::Order
end
def load_current_resource
standard_load_current_resource
end
def init_current_resource
name = @new_resource.name
@current_resource = Chef::Resource::PacemakerOrder.new(name)
attrs = [:score, :ordering]
@current_cib_object.copy_attrs_to_chef_resource(@current_resource, *attrs)
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_order = cib_object_class.from_chef_resource(new_resource)
if desired_order.definition_string != @current_cib_object.definition_string
Chef::Log.debug "changed from [#{@current_cib_object.definition_string}] to [#{desired_order.definition_string}]"
cmd = desired_order.reconfigure_command
execute cmd do
action :nothing
end.run_action(:run)
new_resource.updated_by_last_action(true)
end
end

View File

@ -21,6 +21,6 @@ actions :create, :delete
default_action :create
attribute :name, :kind_of => String, :name_attribute => true
attribute :priority, :kind_of => String
attribute :resources, :kind_of => Array
attribute :name, :kind_of => String, :name_attribute => true
attribute :score, :kind_of => String
attribute :ordering, :kind_of => String

View File

@ -0,0 +1,66 @@
require 'spec_helper'
this_dir = File.dirname(__FILE__)
require File.expand_path('../helpers/provider', this_dir)
require File.expand_path('../helpers/non_runnable_resource', this_dir)
require File.expand_path('../fixtures/order_constraint', this_dir)
describe "Chef::Provider::PacemakerOrder" do
# for use inside examples:
let(:fixture) { Chef::RSpec::Pacemaker::Config::ORDER_CONSTRAINT.dup }
# for use outside examples (e.g. when invoking shared_examples)
fixture = Chef::RSpec::Pacemaker::Config::ORDER_CONSTRAINT.dup
def lwrp_name
'order'
end
include_context "a Pacemaker LWRP"
before(:each) do
@resource.score fixture.score
@resource.ordering fixture.ordering.dup
end
def cib_object_class
Pacemaker::Constraint::Order
end
describe ":create action" do
include Chef::RSpec::Pacemaker::CIBObject
it "should modify the constraint if it has a different score" do
new_score = '100'
fixture.score = new_score
expected_configure_cmd_args = [fixture.reconfigure_command]
test_modify(expected_configure_cmd_args) do
@resource.score new_score
end
end
it "should modify the constraint if it has a resource added" do
new_resource = 'bar:Stopped'
expected = fixture.dup
expected.ordering = expected.ordering.dup + ' ' + new_resource
expected_configure_cmd_args = [expected.reconfigure_command]
test_modify(expected_configure_cmd_args) do
@resource.ordering expected.ordering
end
end
it "should modify the constraint if it has a different ordering" do
new_ordering = 'clone1 primitive1'
fixture.ordering = new_ordering
expected_configure_cmd_args = [fixture.reconfigure_command]
test_modify(expected_configure_cmd_args) do
@resource.ordering new_ordering
end
end
end
it_should_behave_like "a non-runnable resource", fixture
end