implement order LWRP using library code
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
# Author:: Robert Choi
|
|
||||||
# Cookbook Name:: pacemaker
|
# Cookbook Name:: pacemaker
|
||||||
# Provider:: order
|
# Provider:: order
|
||||||
#
|
#
|
||||||
# Copyright:: 2013, Robert Choi
|
# Copyright:: 2013, Robert Choi
|
||||||
|
# Copyright:: 2014, SUSE
|
||||||
#
|
#
|
||||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
# you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
@@ -17,46 +17,57 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
require ::File.expand_path('../libraries/pacemaker/cib_object',
|
this_dir = ::File.dirname(__FILE__)
|
||||||
::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
|
action :create do
|
||||||
name = new_resource.name
|
name = new_resource.name
|
||||||
priority = new_resource.priority
|
|
||||||
resources = new_resource.resources
|
|
||||||
|
|
||||||
unless resource_exists?(name)
|
if @current_resource_definition.nil?
|
||||||
cmd = "crm configure order #{name} #{priority}:"
|
create_resource(name)
|
||||||
resources.each do |rsc|
|
else
|
||||||
cmd << " #{rsc}"
|
maybe_modify_resource(name)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
action :delete do
|
action :delete do
|
||||||
name = new_resource.name
|
next unless @current_resource
|
||||||
cmd = "crm resource stop #{name}; crm configure delete #{name}"
|
standard_delete_resource
|
||||||
|
end
|
||||||
e = execute "delete order #{name}" do
|
|
||||||
command cmd
|
def cib_object_class
|
||||||
only_if { resource_exists?(name) }
|
::Pacemaker::Constraint::Order
|
||||||
end
|
end
|
||||||
|
|
||||||
new_resource.updated_by_last_action(true)
|
def load_current_resource
|
||||||
Chef::Log.info "Deleted order '#{name}'."
|
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
|
end
|
||||||
|
@@ -21,6 +21,6 @@ actions :create, :delete
|
|||||||
|
|
||||||
default_action :create
|
default_action :create
|
||||||
|
|
||||||
attribute :name, :kind_of => String, :name_attribute => true
|
attribute :name, :kind_of => String, :name_attribute => true
|
||||||
attribute :priority, :kind_of => String
|
attribute :score, :kind_of => String
|
||||||
attribute :resources, :kind_of => Array
|
attribute :ordering, :kind_of => String
|
||||||
|
66
spec/providers/order_spec.rb
Normal file
66
spec/providers/order_spec.rb
Normal 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
|
Reference in New Issue
Block a user