refactor shell quoting into #quoted_definition_string

This commit is contained in:
Adam Spiers
2014-02-04 18:20:17 +00:00
parent 4f144955ee
commit 2550ef2647
6 changed files with 38 additions and 20 deletions

View File

@@ -108,6 +108,17 @@ module Pacemaker
"%s '%s'" % [self.class.description, name]
end
# Returns a single-quoted shell-escaped version of the definition
# string, suitable for use in a command like:
#
# echo '...' | crm configure load update -
def quoted_definition_string
"'%s'" % \
definition_string \
.gsub('\\') { '\\\\' } \
.gsub("'") { "\\'" }
end
def delete_command
"crm configure delete '#{name}'"
end

View File

@@ -64,10 +64,8 @@ def maybe_modify_resource(name)
desired_colocation = cib_object_class.from_chef_resource(new_resource)
if desired_colocation.definition_string != @current_cib_object.definition_string
Chef::Log.debug "changed from [#{@current_cib_object.definition_string}] to [#{desired_colocation.definition_string}]"
to_echo = desired_colocation.definition_string.chomp
to_echo.gsub!('\\') { '\\\\' }
to_echo.gsub!("'", "\\'")
cmd = "echo '#{to_echo}' | crm configure load update -"
to_echo = desired_colocation.quoted_definition_string
cmd = "echo #{to_echo} | crm configure load update -"
execute cmd do
action :nothing
end.run_action(:run)

View File

@@ -103,10 +103,8 @@ def maybe_modify_resource(name)
desired_primitive = cib_object_class.from_chef_resource(new_resource)
if desired_primitive.op_string != @current_cib_object.op_string
Chef::Log.debug "op changed from [#{@current_cib_object.op_string}] to [#{desired_primitive.op_string}]"
to_echo = desired_primitive.definition_string.chomp
to_echo.gsub!('\\') { '\\\\' }
to_echo.gsub!("'", "\\'")
cmds = ["echo '#{to_echo}' | crm configure load update -"]
to_echo = desired_primitive.quoted_definition_string
cmds = ["echo #{to_echo} | crm configure load update -"]
else
maybe_configure_params(name, cmds, :params)
maybe_configure_params(name, cmds, :meta)

View File

@@ -123,6 +123,21 @@ EOF
end
end
describe "#quoted_definition_string" do
it "should return the quoted definition string" do
primitive = Pacemaker::Resource::Primitive.new('foo')
primitive.definition = <<'EOF'.chomp
primitive foo ocf:openstack:keystone \
params bar="baz\\qux" bar2="baz'qux"
EOF
primitive.parse_definition
expect(primitive.quoted_definition_string).to eq(<<'EOF'.chomp)
'primitive foo ocf:openstack:keystone \\
params bar="baz\\qux" bar2="baz\'qux"'
EOF
end
end
describe "#parse_definition" do
before(:each) do
@parsed = Pacemaker::Resource::Primitive.new(fixture.name)

View File

@@ -46,10 +46,9 @@ describe "Chef::Provider::PacemakerColocation" do
end
it "should modify the constraint if it has a different score" do
echo_string = colo.definition_string.chomp.gsub('inf', '100')
echo_string.gsub!('\\') { '\\\\' }
echo_string = colo.quoted_definition_string.gsub('inf', '100')
expected_configure_cmd_args = [
"echo '#{echo_string}' | crm configure load update -"
"echo #{echo_string} | crm configure load update -"
]
test_modify(expected_configure_cmd_args) do
@resource.score '100'
@@ -60,10 +59,9 @@ describe "Chef::Provider::PacemakerColocation" do
new_resource = 'bar:Stopped'
expected = colo.dup
expected.resources = expected.resources.dup + [new_resource]
echo_string = expected.definition_string.chomp
echo_string.gsub!('\\') { '\\\\' }
echo_string = expected.quoted_definition_string
expected_configure_cmd_args = [
"echo '#{echo_string}' | crm configure load update -"
"echo #{echo_string} | crm configure load update -"
]
test_modify(expected_configure_cmd_args) do
@resource.resources expected.resources
@@ -73,10 +71,9 @@ describe "Chef::Provider::PacemakerColocation" do
it "should modify the constraint if it has a different resource" do
new_resources = ['bar:Started']
colo.resources = new_resources
echo_string = colo.definition_string.chomp
echo_string.gsub!('\\') { '\\\\' }
echo_string = colo.quoted_definition_string
expected_configure_cmd_args = [
"echo '#{echo_string}' | crm configure load update -"
"echo #{echo_string} | crm configure load update -"
]
test_modify(expected_configure_cmd_args) do
@resource.resources new_resources

View File

@@ -91,10 +91,9 @@ describe "Chef::Provider::PacemakerPrimitive" do
end
it "should modify the primitive if it has different op values" do
echo_string = rsc.definition_string.chomp
echo_string.gsub!('\\') { '\\\\' }.gsub!('60', '120')
echo_string = rsc.quoted_definition_string.gsub!('60', '120')
expected_configure_cmd_args = [
"echo '#{echo_string}' | crm configure load update -"
"echo #{echo_string} | crm configure load update -"
]
test_modify(expected_configure_cmd_args) do
new_op = Hash[rsc.op]