Skip to content

Finish refactor to all methods as issue occurs while running against master #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/database_full.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
mssql::database{ 'testdb_full':
sqlserver::config{ 'MSSQLSERVER':
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
}
sqlserver::database{ 'testdb_full':
instance => 'MSSQLSERVER',
containment => 'PARTIAL',
compatibility => 110,
Expand Down
8 changes: 4 additions & 4 deletions examples/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
$sapwd = 'Pupp3t1@'
$instance_name = 'MSSQLSERVER'

mssql_instance{ $instance_name:
sqlserver_instance{ $instance_name:
source => 'E:/',
security_mode => 'SQL',
sa_pwd => $sapwd,
features => ['SQL'],
sql_sysadmin_accounts => ['vagrant'],
}

mssql::config{ 'MSSQLSERVER':
sqlserver::config{ 'MSSQLSERVER':
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
require => Mssql_instance[$instance_name],
}
mssql::login{ 'padmin':

sqlserver::login{ 'padmin':
password => 'PadminP@ssw0rd1',
instance => $instance_name,
require => Mssql::Config[$instance_name],
}
5 changes: 5 additions & 0 deletions examples/sp_configure.pp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
sqlserver::config{ 'MSSQLSERVER':
admin_user => 'sa',
admin_pass => 'Pupp3t1@',
require => Mssql_instance[$instance_name],
}
#Enable Filestream access on server
mssql::sp_configure{ 'filestream access level':
value => 1,
Expand Down
15 changes: 0 additions & 15 deletions files/install_dot_net_35.ps1

This file was deleted.

34 changes: 0 additions & 34 deletions files/run_discovery.ps1

This file was deleted.

1 change: 0 additions & 1 deletion files/sqlserver_management.psm1

This file was deleted.

10 changes: 0 additions & 10 deletions lib/puppet/parser/functions/mssql_is_domain_user.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/puppet/parser/functions/sqlserver_is_domain_or_local_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'lib/puppet_x/sqlserver/server_helper'))

module Puppet::Parser::Functions
newfunction(:sqlserver_is_domain_or_local_user, :type => :rvalue) do |args|
if args.length != 1
raise Puppet::ParseError, ("is_domain_or_local_user(): requires exactly 1 argument, you provided #{args.length}")
end
PuppetX::Sqlserver::ServerHelper.is_domain_or_local_user?(args[0], Facter.value(:hostname))
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# @raise [Puppet::ParserError] Instance name can not start or end with underscore (_)
#
module Puppet::Parser::Functions
newfunction(:mssql_validate_instance_name, :docs => <<DOC) do |args|
newfunction(:sqlserver_validate_instance_name, :docs => <<DOC) do |args|
Validate the MS SQL Instance name based on what Microsoft has set within the document located at
http://msdn.microsoft.com/en-us/library/ms143531(v=sql.120).aspx
DOC
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Puppet::Parser::Functions
newfunction(:mssql_validate_on_off) do |args|
newfunction(:sqlserver_validate_on_off) do |args|
if args.length != 1
raise Puppet::ParseError, ("validate_on_off(): requires exactly 1 argument, you provided #{args.length}")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Puppet::Parser::Functions
newfunction(:mssql_validate_range) do |args|
newfunction(:sqlserver_validate_range) do |args|
if (args.length < 3) or (args.length > 4) then
raise Puppet::ParseError, ("validate_range(): wrong number of arguments (#{args.length}; must be 3)")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Puppet::Parser::Functions
newfunction(:mssql_validate_size) do |args|
newfunction(:sqlserver_validate_size) do |args|
if args.length != 1
raise(Puppet::ParseError, "mssql_validate_size(): requires exactly 1 argument, you provided #{args.length}")
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Puppet::Parser::Functions
newfunction(:mssql_validate_svrroles_hash) do |args|
newfunction(:sqlserver_validate_svrroles_hash) do |args|
if args.length != 1 then
raise Puppet::ParseError, ("mssql_validate_svcrole_hash(): wrong number of arguments (#{args.length}; must be 1)")
raise Puppet::ParseError, ("sqlserver_validate_svcrole_hash(): wrong number of arguments (#{args.length}; must be 1)")
end

value = args[0]
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/property/login.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'puppet/property'

class Puppet::Property::MssqlLogin < Puppet::Property
class Puppet::Property::SqlserverLogin < Puppet::Property
desc 'A MS SQL Login, possible to be domain or local account'
validate do |value|
# @todo
Expand Down
79 changes: 0 additions & 79 deletions lib/puppet/provider/mssql.rb

This file was deleted.

140 changes: 140 additions & 0 deletions lib/puppet/provider/sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'lib/puppet_x/sqlserver/server_helper'))
require File.expand_path(File.join(File.dirname(__FILE__), 'sqlserver'))
require 'tempfile'

class Puppet::Provider::Sqlserver < Puppet::Provider
confine :operatingsystem => :windows

initvars

commands :powershell =>
if File.exists?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe"
elsif File.exists?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe")
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe"
else
'powershell.exe'
end

def try_execute(command, msg = nil)
begin
execute(command.compact)
rescue Puppet::ExecutionFailure => error
msg = "Failure occured when trying to install SQL Server #{@resource[:name]}" if msg.nil?
raise Puppet::Error, "#{msg} \n #{error}"
end
end

##
# Used by tsql provider
##
def self.run_authenticated_sqlcmd(query, opts)
b = binding
@sql_instance_config = "C:/Program Files/Microsoft SQL Server/.puppet/.#{opts[:instance_name]}.cfg"
if File.exists?(@sql_instance_config)
@sql_instance_config = native_path(@sql_instance_config)
else
raise Puppet::ParseError, "Config file does not exist"
end
temp = Tempfile.new(['puppet', '.sql'])
begin
temp.write(query)
temp.flush
temp.close
#input file is used in the authenticated_query.ps1.erb template
input_file = native_path(temp.path)
@instance = opts[:instance_name]
erb_template = File.join(template_path, 'authenticated_query.ps1.erb')
ps1 = ERB.new(File.new(erb_template).read, nil, '-')
temp_ps1 = Tempfile.new(['puppet', '.ps1'])
begin
temp_ps1.write(ps1.result(b))
temp_ps1.flush
temp_ps1.close
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
debug("Return result #{result.exitstatus}")
return result
ensure
temp_ps1.close
temp_ps1.unlink
end
ensure
temp.close
temp.unlink
end
return result
end

private
def self.native_path(path)
path.gsub(File::SEPARATOR, File::ALT_SEPARATOR)
end

def self.template_path
return File.expand_path(File.join(File.dirname(__FILE__), '../templates'))
end

def not_nil_and_not_empty?(obj)
!obj.nil? and !obj.empty?
end

def self.run_discovery_script
discovery = <<-DISCOVERY
if(Test-Path 'C:\\Program Files\\Microsoft SQL Server\\120\\Setup Bootstrap\\SQLServer2014\\setup.exe'){
pushd 'C:\\Program Files\\Microsoft SQL Server\\120\\Setup Bootstrap\\SQLServer2014\\'
Start-Process -FilePath .\\setup.exe -ArgumentList @("/Action=RunDiscovery","/q") -Wait -WindowStyle Hidden
popd
}elseif(Test-Path 'C:\\Program Files\\Microsoft SQL Server\\110\\Setup Bootstrap\\SQLServer2012\\setup.exe'){
pushd 'C:\\Program Files\\Microsoft SQL Server\\110\\Setup Bootstrap\\SQLServer2012\\'
Start-Process -FilePath .\\setup.exe -ArgumentList @("/Action=RunDiscovery","/q") -Wait -WindowStyle Hidden
popd
}

$file = gci 'C:\\Program Files\\Microsoft SQL Server\\*\\Setup Bootstrap\\Log\\*\\SqlDiscoveryReport.xml' -ErrorAction Ignore | sort -Descending | select -First 1
if($file -ne $null) {
[xml] $xml = cat $file
$json = $xml.ArrayOfDiscoveryInformation.DiscoveryInformation
$hash = @{"instances" = @();"TimeStamp"= ("{0:yyyy-MM-dd HH:mm:ss}" -f $file.CreationTime)}
foreach($instance in ($json | % { $_.Instance } | Get-Unique )){
$features = @()
$json | %{
if($_.instance -eq $instance){
$features += $_.feature
}
}
if($instance -eq "" ){
$hash.Add("Generic Features",$features)
}else{
$hash["instances"] += $instance
$hash.Add($instance,@{"features"=$features})
}
}
$file.Directory.Delete($true)
Write-Host (ConvertTo-Json $hash)
}else{
Write-host ("{}")
}
DISCOVERY
result = powershell([discovery])
JSON.parse(result)
end

def self.run_install_dot_net
install_dot_net = <<-DOTNET
Install-WindowsFeature NET-Framework-Core

Write-Host "Installing .Net Framework 3.5, do not close this prompt..."
DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:$LocalSource | Out-Null
$Result = Dism /online /Get-featureinfo /featurename:NetFx3
If($Result -contains "State : Enabled")
{
Write-Host "Install .Net Framework 3.5 successfully."
}
Else
{
Write-Host "Failed to install Install .Net Framework 3.5,please make sure the local source is correct."
}
DOTNET
powershell([install_dot_net])
end
end
Loading