Skip to content

Commit ce47f66

Browse files
authored
fix(mssql): self-hosted pre-install check now supports SQL Auth credentials and fixes TCP timeout (#1390)
fix(mssql): self-hosted pre-install check now supports SQL Auth credentials and fixes TCP timeout (#1390)
1 parent 9584ba9 commit ce47f66

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

recipes/newrelic/infrastructure/ohi/sql/ms-sql.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,21 @@ preInstall:
106106
107107
$NEW_RELIC_MSSQL_DB_HOSTNAME = if ($env:NEW_RELIC_MSSQL_DB_HOSTNAME) { $env:NEW_RELIC_MSSQL_DB_HOSTNAME } else { $env:NR_CLI_DB_HOSTNAME };
108108
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_HOSTNAME)) {$NEW_RELIC_MSSQL_DB_HOSTNAME = hostname}
109-
109+
110110
$NEW_RELIC_MSSQL_DB_PORT = if ($env:NEW_RELIC_MSSQL_DB_PORT) { $env:NEW_RELIC_MSSQL_DB_PORT } else { $env:NR_CLI_DB_PORT };
111111
if ([string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_DB_PORT)) {$NEW_RELIC_MSSQL_DB_PORT = "1433"}
112112
113+
$sqlUsername = if ($env:NEW_RELIC_MSSQL_SQL_USERNAME) { $env:NEW_RELIC_MSSQL_SQL_USERNAME } else { $env:NR_CLI_SQL_USERNAME }
114+
$sqlPassword = if ($env:NEW_RELIC_MSSQL_SQL_PASSWORD) { $env:NEW_RELIC_MSSQL_SQL_PASSWORD } else { $env:NR_CLI_SQL_PASSWORD }
115+
$sqlCredArgs = @()
116+
if (-not [string]::IsNullOrWhiteSpace($sqlUsername)) {
117+
$sqlCredArgs = @("-U", $sqlUsername, "-P", $sqlPassword)
118+
}
119+
113120
# Loop on each instance names to see if we can connect
114121
foreach ($instance in $instances) {
115122
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance},${NEW_RELIC_MSSQL_DB_PORT}"
116-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
123+
$name=(sqlcmd -S $connection @sqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
117124
If ($name.Length -ge 3) {
118125
$actualName = $name[2].ToString().Trim();
119126
If ($actualName -eq $instance) {
@@ -126,7 +133,7 @@ preInstall:
126133
}
127134
128135
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance}"
129-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
136+
$name=(sqlcmd -S $connection @sqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
130137
If ($name.Length -ge 3) {
131138
$actualName = $name[2].ToString().Trim();
132139
If ($actualName -eq $instance) {
@@ -139,7 +146,7 @@ preInstall:
139146
}
140147
141148
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME},${NEW_RELIC_MSSQL_DB_PORT}"
142-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
149+
$name=(sqlcmd -S $connection @sqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
143150
If ($name.Length -ge 3) {
144151
$actualName = $name[2].ToString().Trim();
145152
If ($actualName -eq $instance) {
@@ -152,7 +159,7 @@ preInstall:
152159
}
153160
154161
}
155-
exit 3
162+
exit 3
156163
}
157164
'
158165
info: |
@@ -655,10 +662,15 @@ install:
655662
$instances = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server").InstalledInstances
656663
$loginModes = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\*\*" -name "LoginMode" -ErrorAction SilentlyContinue | Where-Object -Property loginMode -eq -Value "2")
657664
665+
$installSqlCredArgs = @()
666+
if (-not [string]::IsNullOrWhiteSpace($NEW_RELIC_MSSQL_SQL_USERNAME)) {
667+
$installSqlCredArgs = @("-U", $NEW_RELIC_MSSQL_SQL_USERNAME, "-P", $NEW_RELIC_MSSQL_SQL_PASSWORD)
668+
}
669+
658670
foreach ($instance in $instances) {
659671
660672
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance},${NEW_RELIC_MSSQL_DB_PORT}"
661-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
673+
$name=(sqlcmd -S $connection @installSqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
662674
If ($name.Length -ge 3) {
663675
$actualName = $name[2].ToString().Trim();
664676
If ($actualName -eq $instance) {
@@ -671,7 +683,7 @@ install:
671683
$isQueryStoreEnabled = Enable-QueryStore -connection $connection -InstanceName $instance -Port $NEW_RELIC_MSSQL_DB_PORT
672684
if($isQueryStoreEnabled -ne $true) {
673685
Write-Error "Failed to enable QueryStore for Azure SQL Managed Instance: ${NEW_RELIC_MSSQL_DB_HOSTNAME}. Check SQL connectivity and admin credentials."
674-
Exit 139
686+
Exit 139
675687
}
676688
}
677689
@@ -685,7 +697,7 @@ install:
685697
}
686698
687699
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME}\${instance}"
688-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
700+
$name=(sqlcmd -S $connection @installSqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
689701
If ($name.Length -ge 3) {
690702
$actualName = $name[2].ToString().Trim();
691703
If ($actualName -eq $instance) {
@@ -698,7 +710,7 @@ install:
698710
$isQueryStoreEnabled = Enable-QueryStore -connection $connection -InstanceName $instance
699711
if($isQueryStoreEnabled -ne $true) {
700712
Write-Error "Failed to enable QueryStore for Azure SQL Managed Instance: ${NEW_RELIC_MSSQL_DB_HOSTNAME}. Check SQL connectivity and admin credentials."
701-
Exit 135
713+
Exit 135
702714
}
703715
}
704716
@@ -712,7 +724,7 @@ install:
712724
}
713725
714726
$connection = "${NEW_RELIC_MSSQL_DB_HOSTNAME},${NEW_RELIC_MSSQL_DB_PORT}"
715-
$name=(sqlcmd -S $connection -Q "SELECT @@SERVICENAME" -l 1 2>&1)
727+
$name=(sqlcmd -S $connection @installSqlCredArgs -Q "SELECT @@SERVICENAME" -l 30 2>&1)
716728
If ($name.Length -ge 3) {
717729
$actualName = $name[2].ToString().Trim();
718730
If ($actualName -eq $instance) {

0 commit comments

Comments
 (0)