Skip to content

Commit b9ae791

Browse files
author
Travis Fields
committed
Fix issue with install_switches, changed to use config file instead due to how ruby writes path to Windows exec
1 parent d7f3102 commit b9ae791

File tree

4 files changed

+124
-35
lines changed

4 files changed

+124
-35
lines changed

lib/puppet/provider/sqlserver_features/mssql.rb

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,43 @@ def modify_features(action, features)
5858
if not_nil_and_not_empty?(@resource[:pid])
5959
cmd_args << "/PID=#{@resource[:pid]}"
6060
end
61-
if not_nil_and_not_empty?(@resource[:install_switches])
62-
@resource[:install_switches].each_pair do |k, v|
63-
if v.is_a?(Numeric) || (v.is_a?(String) && v =~ /\d/)
64-
cmd_args << "/#{k}=#{v}"
65-
else
66-
cmd_args << "/#{k}='#{v}'"
67-
end
68-
end
61+
end
62+
begin
63+
config_file = create_temp_for_install_switch unless action == 'uninstall'
64+
cmd_args << "/ConfigurationFile=\"#{config_file.path}\"" unless config_file.nil?
65+
try_execute(cmd_args, "Unable to #{action} features (#{features.join(', ')})")
66+
ensure
67+
if config_file
68+
config_file.close
69+
config_file.unlink
70+
end
71+
end
72+
end
73+
end
74+
75+
def create_temp_for_install_switch
76+
if not_nil_and_not_empty? @resource[:install_switches]
77+
config_file = ["[OPTIONS]"]
78+
@resource[:install_switches].each_pair do |k, v|
79+
if RESERVED_SWITCHES.include? k
80+
warn("Reserved switch [#{k}] found for `install_switches`, please know the provided value
81+
may be overridden by some command line arguments")
82+
end
83+
if v.is_a?(Numeric) || (v.is_a?(String) && v =~ /\d/)
84+
config_file << "#{k}=#{v}"
85+
elsif v.nil?
86+
config_file << k
87+
else
88+
config_file << "#{k}=\"#{v}\""
6989
end
7090
end
71-
try_execute(cmd_args, "Unable to #{action} features (#{features.join(', ')})")
91+
config_temp = Tempfile.new(['sqlconfig', '.ini'])
92+
config_temp.write(config_file.join("\n"))
93+
config_temp.flush
94+
config_temp.close
95+
return config_temp
7296
end
97+
return nil
7398
end
7499

75100
def installNet35

lib/puppet/provider/sqlserver_instance/mssql.rb

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'sqlserver'))
33

44
Puppet::Type::type(:sqlserver_instance).provide(:mssql, :parent => Puppet::Provider::Sqlserver) do
5+
RESERVED_SWITCHES =
6+
%w(AGTSVCACCOUNT AGTSVCPASSWORD ASSVCACCOUNT AGTSVCPASSWORD PID
7+
RSSVCACCOUNT RSSVCPASSWORD SAPWD SECURITYMODE SQLSYSADMINACCOUNTS FEATURES)
58

69
def self.instances
710
instances = []
@@ -42,7 +45,17 @@ def add_features(features)
4245
def modify_features(features, action)
4346
if not_nil_and_not_empty? features
4447
debug "#{action.capitalize}ing features '#{features.join(',')}'"
45-
try_execute(build_cmd_args(features, action), "Error trying to #{action} features (#{features.join(', ')}")
48+
cmd_args = build_cmd_args(features, action)
49+
begin
50+
config_file = create_temp_for_install_switch unless action == 'uninstall'
51+
cmd_args << "/ConfigurationFile=\"#{config_file.path}\"" unless config_file.nil?
52+
try_execute(cmd_args, "Error trying to #{action} features (#{features.join(', ')}")
53+
ensure
54+
if config_file
55+
config_file.close
56+
config_file.unlink
57+
end
58+
end
4659
end
4760
end
4861

@@ -56,11 +69,46 @@ def create
5669
destroy
5770
else
5871
installNet35
59-
cmd_args = build_cmd_args(@resource[:features])
60-
try_execute(cmd_args)
72+
add_features(@resource[:features])
73+
# cmd_args = build_cmd_args(@resource[:features])
74+
# begin
75+
# config_file = create_temp_for_install_switch
76+
# cmd_args << "/ConfigurationFile=\"#{config_file.path}\"" unless config_file.nil?
77+
# try_execute(cmd_args)
78+
# ensure
79+
# if config_file
80+
# config_file.close
81+
# config_file.unlink
82+
# end
83+
# end
6184
end
6285
end
6386

87+
def create_temp_for_install_switch
88+
if not_nil_and_not_empty? @resource[:install_switches]
89+
config_file = ["[OPTIONS]"]
90+
@resource[:install_switches].each_pair do |k, v|
91+
if RESERVED_SWITCHES.include? k
92+
warn("Reserved switch [#{k}] found for `install_switches`, please know the provided value
93+
may be overridden by some command line arguments")
94+
end
95+
if v.is_a?(Numeric) || (v.is_a?(String) && v =~ /\d/)
96+
config_file << "#{k}=#{v}"
97+
elsif v.nil?
98+
config_file << k
99+
else
100+
config_file << "#{k}=\"#{v}\""
101+
end
102+
end
103+
config_temp = Tempfile.new(['sqlconfig', '.ini'])
104+
config_temp.write(config_file.join("\n"))
105+
config_temp.flush
106+
config_temp.close
107+
return config_temp
108+
end
109+
return nil
110+
end
111+
64112
def basic_cmd_args(features, action)
65113
cmd_args = ["#{@resource[:source]}/setup.exe",
66114
"/ACTION=#{action}",
@@ -83,15 +131,6 @@ def build_cmd_args(features, action="install")
83131
cmd_args << "/SQLSYSADMINACCOUNTS=\"#{@resource[:sql_sysadmin_accounts]}\""
84132
end
85133
end
86-
if not_nil_and_not_empty? @resource[:install_switches]
87-
@resource[:install_switches].each_pair do |k, v|
88-
if v.is_a?(Numeric) || (v.is_a?(String) && v =~ /\d/)
89-
cmd_args << "/#{k}=#{v}"
90-
else
91-
cmd_args << "/#{k}='#{v}'"
92-
end
93-
end
94-
end
95134
end
96135
cmd_args
97136
end

spec/unit/puppet/provider/sqlserver__instance_spec.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'spec_helper'
2+
require 'mocha'
23

34
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'sqlserver_install_context.rb'))
45

@@ -144,10 +145,18 @@ def stub_uninstall(args, installed_features)
144145
end
145146

146147
describe 'install_switches' do
148+
before :each do
149+
@file_double = Tempfile.new(['sqlconfig', '.ini'])
150+
@file_double.stubs(:write)
151+
@file_double.stubs(:flush)
152+
@file_double.stubs(:close)
153+
Tempfile.stubs(:new).with(['sqlconfig', '.ini']).returns(@file_double)
154+
end
155+
147156
it_behaves_like 'create' do
148157
args = get_basic_args
149158
args[:install_switches] = {'ERRORREPORTING' => 1}
150-
let(:additional_install_switches) { ['/ERRORREPORTING=1'] }
159+
let(:additional_install_switches) { ["/ConfigurationFile=\"#{@file_double.path}\""] }
151160
let(:args) { args }
152161
munged = {:features => Array.new(args[:features])}
153162
munged[:features].delete('SQL')
@@ -158,7 +167,7 @@ def stub_uninstall(args, installed_features)
158167
it_behaves_like 'create' do
159168
args = get_basic_args
160169
args[:install_switches] = {'ERRORREPORTING' => 1, 'SQLBACKUPDIR' => 'I:\DBbackup'}
161-
let(:additional_install_switches) { ['/ERRORREPORTING=1', '/SQLBACKUPDIR=\'I:\DBbackup\''] }
170+
let(:additional_install_switches) { ["/ConfigurationFile=\"#{@file_double.path}\""] }
162171
let(:args) { args }
163172
munged = {:features => Array.new(args[:features])}
164173
munged[:features].delete('SQL')

spec/unit/puppet/provider/sqlserver_features_spec.rb

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
require 'spec_helper'
22
require 'rspec'
33
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'sqlserver_spec_helper.rb'))
4+
require 'mocha'
45

56
provider_class = Puppet::Type.type(:sqlserver_features).provider(:mssql)
67

78
RSpec.describe provider_class do
89
subject { provider_class }
9-
10-
shared_examples 'create' do |args, munged_args = {}, additional_switches = []|
10+
let(:params) { {
11+
:name => 'Base features',
12+
:source => 'C:\myinstallexecs',
13+
:features => %w(BC SSMS)
14+
} }
15+
let(:additional_params) { {} }
16+
let(:munged_args) { {} }
17+
let(:additional_switches) { [] }
18+
shared_examples 'create' do
1119
it {
12-
@resource = Puppet::Type::Sqlserver_features.new(args)
20+
params.merge!(additional_params)
21+
@resource = Puppet::Type::Sqlserver_features.new(params)
1322
@provider = provider_class.new(@resource)
1423

1524
stub_powershell_call(subject)
1625

17-
executed_args = args.merge(munged_args)
26+
executed_args = params.merge(munged_args)
1827
stub_add_features(executed_args, executed_args[:features], additional_switches)
1928
@provider.create
2029
}
@@ -32,21 +41,28 @@
3241

3342
context 'it should provide the correct command default command' do
3443
include_context 'features'
35-
it_should_behave_like 'create', @feature_params
44+
it_should_behave_like 'create'
3645
end
3746

3847
context 'it should provide the correct command default command' do
39-
include_context 'features'
40-
@feature_params[:install_switches] ={'ERRORREPORTING' => 1, 'SQLBACKUPDIR' => 'I:\DBbackup'}
41-
additional_switches = ['/ERRORREPORTING=1', '/SQLBACKUPDIR=\'I:\DBbackup\'']
42-
it_should_behave_like 'create', @feature_params, {}, additional_switches
48+
before :each do
49+
@file_double = Tempfile.new(['sqlconfig', '.ini'])
50+
@file_double.stubs(:write)
51+
@file_double.stubs(:flush)
52+
@file_double.stubs(:close)
53+
Tempfile.stubs(:new).with(['sqlconfig', '.ini']).returns(@file_double)
54+
end
55+
it_should_behave_like 'create' do
56+
let(:additional_params) { {:install_switches => {'ERRORREPORTING' => 1, 'SQLBACKUPDIR' => 'I:\DBbackup'}} }
57+
let(:additional_switches) { ["/ConfigurationFile=\"#{@file_double.path}\""] }
58+
end
4359
end
4460

4561
context 'it should expand the superset for features' do
4662
include_context 'features'
47-
@feature_params[:features] = %w(Tools)
48-
munged = {:features => %w(ADV_SSMS Conn SSMS)}
49-
it_should_behave_like 'create', @feature_params, munged
63+
let(:additional_params) { {:features => %w(Tools)} }
64+
let(:munged_args) { {:features => %w(ADV_SSMS Conn SSMS)} }
65+
it_should_behave_like 'create'
5066
end
5167

5268
shared_examples 'features=' do |args|

0 commit comments

Comments
 (0)