Skip to content

Commit ec50b3a

Browse files
committed
Merge pull request #105 from Iristyle/ticket/master/FM-2577-remove-sqlcmd-remnants
(FM-2577) Minor SQL server connection building refactorings
2 parents 28cf097 + 01e6cb6 commit ec50b3a

File tree

4 files changed

+14
-121
lines changed

4 files changed

+14
-121
lines changed

lib/puppet/provider/sqlserver.rb

-47
Original file line numberDiff line numberDiff line change
@@ -25,53 +25,6 @@ def try_execute(command, msg = nil)
2525
end
2626
end
2727

28-
##
29-
# Used by tsql provider
30-
##
31-
def self.run_authenticated_sqlcmd(query, opts)
32-
b = binding
33-
@sql_instance_config = File.join(Puppet[:vardir], "cache/sqlserver/.#{resource[:instance]}.cfg")
34-
if File.exists?(@sql_instance_config)
35-
@sql_instance_config = native_path(@sql_instance_config)
36-
else
37-
raise Puppet::ParseError, "Config file does not exist"
38-
end
39-
temp = Tempfile.new(['puppet', '.sql'])
40-
begin
41-
temp.write(query)
42-
temp.flush
43-
temp.close
44-
#input file is used in the authenticated_query.ps1.erb template
45-
input_file = native_path(temp.path)
46-
@instance = opts[:instance_name]
47-
erb_template = File.join(template_path, 'authenticated_query.ps1.erb')
48-
ps1 = ERB.new(File.new(erb_template).read, nil, '-')
49-
temp_ps1 = Tempfile.new(['puppet', '.ps1'])
50-
begin
51-
temp_ps1.write(ps1.result(b))
52-
temp_ps1.flush
53-
temp_ps1.close
54-
#We want to not fail the exec but fail the overall process once we get the clean result back, otherwise we report the temp file which is meaningless
55-
result = Puppet::Util::Execution.execute(['powershell.exe', '-noprofile', '-executionpolicy', 'unrestricted', temp_ps1.path], {:failonfail => false}) #We expect some things to fail in order to run as an only if
56-
debug("Return result #{result}")
57-
if opts[:failonfail] && result.match(/THROW CAUGHT/)
58-
fail(result.gsub('THROW CAUGHT:', ''))
59-
end
60-
if result.match(/Msg \d+, Level 16/)
61-
fail(result)
62-
end
63-
return result
64-
ensure
65-
temp_ps1.close
66-
temp_ps1.unlink
67-
end
68-
ensure
69-
temp.close
70-
temp.unlink
71-
end
72-
return result
73-
end
74-
7528
private
7629
def self.native_path(path)
7730
path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)

lib/puppet/templates/authenticated_query.ps1.erb

-48
This file was deleted.

lib/puppet_x/sqlserver/sql_connection.rb

+11-23
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ module Sqlserver
66
class SqlConnection
77
attr_reader :exception_caught
88

9-
10-
def initialize
11-
@connection = nil
12-
@data = nil
13-
end
14-
159
def open_and_run_command(query, config)
1610
begin
1711
open(config)
@@ -34,19 +28,19 @@ def open(config)
3428
end
3529

3630
def get_connection_string(config)
37-
config = {'database' => 'master'}.merge(config)
38-
# Open ADO connection to the SQL Server database
39-
connection_string = "Provider=SQLOLEDB.1;"
40-
connection_string << "Persist Security Info=False;"
41-
connection_string << "User ID=#{config['admin']};"
42-
connection_string << "password=#{config['pass']};"
43-
connection_string << "Initial Catalog=#{config['database']};"
44-
connection_string << "Application Name=Puppet;"
31+
params = {
32+
'Provider' => 'SQLOLEDB.1',
33+
'User ID' => config['admin'],
34+
'Password' => config['pass'],
35+
'Initial Catalog' => config['database'] || 'master',
36+
'Application Name' => 'Puppet',
37+
'Data Source' => 'localhost'
38+
}
4539
if config['instance'] !~ /^MSSQLSERVER$/
46-
connection_string << "Data Source=localhost\\#{config['instance']};"
47-
else
48-
connection_string << "Data Source=localhost;"
40+
params['Data Source'] = "localhost\\#{config['instance']}"
4941
end
42+
43+
params.map { |k, v| "#{k}=#{v}" }.join(';')
5044
end
5145

5246
def command(sql)
@@ -80,8 +74,6 @@ def close
8074
end
8175

8276
def reset_instance
83-
@data = nil
84-
@fields = nil
8577
@exception_caught = nil
8678
end
8779

@@ -106,10 +98,6 @@ def parse_column_names(result)
10698
def win32_exception
10799
::WIN32OLERuntimeError
108100
end
109-
110-
def connection=(conn)
111-
@connection = conn
112-
end
113101
end
114102

115103
class ResultOutput

spec/unit/puppet_x/sql_connection_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def stub_no_errors
2222
before :each do
2323
stub_connection
2424
@connection.stubs(:State).returns(0)
25-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;')
25+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost')
2626
end
2727
it 'should not raise an error but populate has_errors with message' do
2828
subject.stubs(:win32_exception).returns(Exception)
@@ -48,11 +48,11 @@ def stub_no_errors
4848
@connection.stubs(:State).returns(0)
4949
end
5050
it 'should not add MSSQLSERVER to connection string' do
51-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost;')
51+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=sa;Password=Pupp3t1@;Initial Catalog=master;Application Name=Puppet;Data Source=localhost')
5252
subject.open_and_run_command('query', {'admin' => 'sa', 'pass' => 'Pupp3t1@', 'instance' => 'MSSQLSERVER'})
5353
end
5454
it 'should add a non default instance to connection string' do
55-
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;Persist Security Info=False;User ID=superuser;password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING;')
55+
@connection.stubs(:Open).with('Provider=SQLOLEDB.1;User ID=superuser;Password=puppetTested;Initial Catalog=master;Application Name=Puppet;Data Source=localhost\LOGGING')
5656
subject.open_and_run_command('query', {'admin' => 'superuser', 'pass' => 'puppetTested', 'instance' => 'LOGGING'})
5757
end
5858
end

0 commit comments

Comments
 (0)