Skip to content

Commit

Permalink
Merge pull request #1485 from OctopusDeploy/update-postgres
Browse files Browse the repository at this point in the history
Fixing typo in template
  • Loading branch information
twerthi authored Feb 13, 2024
2 parents df48512 + 5fbe083 commit 34ddf76
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions step-templates/postgres-execute-sql.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"Name": "Postgres - Execute SQL",
"Description": "Creates a Postgres database if it doesn't already exist.\n\nNote:\n- AWS EC2 IAM Role authentication requires the AWS CLI be installed.",
"ActionType": "Octopus.Script",
"Version": 2,
"Version": 3,
"CommunityActionTemplateId": null,
"Packages": [],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "# Define functions\nfunction Get-ModuleInstalled\n{\n # Define parameters\n param(\n $PowerShellModuleName\n )\n\n # Check to see if the module is installed\n if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))\n {\n # It is installed\n return $true\n }\n else\n {\n # Module not installed\n return $false\n }\n}\n\nfunction Install-PowerShellModule\n{\n # Define parameters\n param(\n $PowerShellModuleName,\n $LocalModulesPath\n )\n\n\t# Check to see if the package provider has been installed\n if ((Get-NugetPackageProviderNotInstalled) -ne $false)\n {\n \t# Display that we need the nuget package provider\n Write-Host \"Nuget package provider not found, installing ...\"\n \n # Install Nuget package provider\n Install-PackageProvider -Name Nuget -Force\n }\n\n\t# Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\n}\n\nfunction Get-NugetPackageProviderNotInstalled\n{\n\t# See if the nuget package provider has been installed\n return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\n# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PSScriptRoot\\Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules$([System.IO.Path]::PathSeparator)$env:PSModulePath\"\n$PowerShellModuleName = \"SimplySql\"\n\n# Set secure protocols\n[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12\n\n# Check to see if SimplySql module is installed\nif ((Get-ModuleInstalled -PowerShellModuleName $PowerShellModuleName) -ne $true)\n{\n # Tell user what we're doing\n Write-Output \"PowerShell module $PowerShellModuleName is not installed, downloading temporary copy ...\"\n\n # Install temporary copy\n Install-PowerShellModule -PowerShellModuleName $PowerShellModuleName -LocalModulesPath $LocalModules\n}\n\n# Display\nWrite-Output \"Importing module $PowerShellModuleName ...\"\n\n# Check to see if it was downloaded\nif ((Test-Path -Path \"$LocalModules\\$PowerShellModuleName\") -eq $true)\n{\n\t# Use specific version\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Get whether trust certificate is necessary\n$postgresqlTrustSSL = [System.Convert]::ToBoolean(\"$postgresqlTrustSSL\")\n\ntry\n{\n\t# Declare initial connection string\n $connectionString = \"Server=$postgresqlServerName;Port=$postgresqlServerPort;Database=$postgresqlDatabaseName;\"\n \n\t# Check to see if we need to trust the ssl cert\n\tif ($postgresqlTrustSSL -eq $true)\n\t{\n # Append SSL connection string components\n $connectionString += \"SSL Mode=Require;Trust Server Certificate=true;\"\n\t}\n\n # Update the connection string based on authentication method\n switch ($postgreSqlAuthenticationMethod)\n {\n \"azuremanagedidentity\"\n {\n \t# Get login token\n Write-Host \"Generating Azure Managed Identity token ...\"\n $token = Invoke-RestMethod -Method GET -Uri \"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://ossrdbms-aad.database.windows.net\" -Headers @{\"MetaData\" = \"true\"}\n \n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$($token.access_token)`\";\"\n \n break\n }\n \"awsiam\"\n {\n # Region is part of the RDS endpoint, extract\n $region = ($createPosgreSQLServerName.Split(\".\"))[2]\n\n Write-Host \"Generating AWS IAM token ...\"\n $createUserPassword = (aws rds generate-db-auth-token --hostname $postgresqlServerName --region $region --port $createPort --username $postgresqlUsername)\n\n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$postgesqlUserPassword`\";\"\n\n break\n }\n \"gcpserviceaccount\"\n {\n # Define header\n $header = @{ \"Metadata-Flavor\" = \"Google\"}\n\n # Retrieve service accounts\n $serviceAccounts = Invoke-RestMethod -Method Get -Uri \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/\" -Headers $header\n\n # Results returned in plain text format, get into array and remove empty entries\n $serviceAccounts = $serviceAccounts.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries)\n\n # Retreive the specific service account assigned to the VM\n $serviceAccount = $serviceAccounts | Where-Object {$_.Contains(\"iam.gserviceaccount.com\") }\n\n Write-Host \"Generating GCP IAM token ...\"\n # Retrieve token for account\n $token = Invoke-RestMethod -Method Get -Uri \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/$serviceAccount/token\" -Headers $header\n \n # Check to see if there was a username provided\n if ([string]::IsNullOrWhitespace($postgresqlUsername))\n {\n \t# Use the service account name, but strip off the .gserviceaccount.com part\n $postgresqlUsername = $serviceAccount.SubString(0, $serviceAccount.IndexOf(\".gserviceaccount.com\"))\n }\n \n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$($token.access_token)`\";\"\n \n break\n }\n \"usernamepassword\"\n {\n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$postgesqlUserPassword`\";\"\n\n break \n }\n\n \"windowsauthentication\"\n {\n # Append remaining portion of connection string\n $connectionString += \";Integrated Security=True;\"\n }\n }\n\n\t# Open connection\n Open-PostGreConnection -ConnectionString $connectionString\n\n # Execute the statement\n $executionResult = Invoke-SqlUpdate -Query \"$postgresqlCommand\" -CommandTimeout $postgresCommandTimeout\n \n # Display the result\n Get-SqlMessage\n}\nfinally\n{\n Close-SqlConnection\n}\n\n\n"
"Octopus.Action.Script.ScriptBody": "# Define functions\nfunction Get-ModuleInstalled\n{\n # Define parameters\n param(\n $PowerShellModuleName\n )\n\n # Check to see if the module is installed\n if ($null -ne (Get-Module -ListAvailable -Name $PowerShellModuleName))\n {\n # It is installed\n return $true\n }\n else\n {\n # Module not installed\n return $false\n }\n}\n\nfunction Install-PowerShellModule\n{\n # Define parameters\n param(\n $PowerShellModuleName,\n $LocalModulesPath\n )\n\n\t# Check to see if the package provider has been installed\n if ((Get-NugetPackageProviderNotInstalled) -ne $false)\n {\n \t# Display that we need the nuget package provider\n Write-Host \"Nuget package provider not found, installing ...\"\n \n # Install Nuget package provider\n Install-PackageProvider -Name Nuget -Force\n }\n\n\t# Save the module in the temporary location\n Save-Module -Name $PowerShellModuleName -Path $LocalModulesPath -Force\n}\n\nfunction Get-NugetPackageProviderNotInstalled\n{\n\t# See if the nuget package provider has been installed\n return ($null -eq (Get-PackageProvider -ListAvailable -Name Nuget -ErrorAction SilentlyContinue))\n}\n\n# Define PowerShell Modules path\n$LocalModules = (New-Item \"$PSScriptRoot\\Modules\" -ItemType Directory -Force).FullName\n$env:PSModulePath = \"$LocalModules$([System.IO.Path]::PathSeparator)$env:PSModulePath\"\n$PowerShellModuleName = \"SimplySql\"\n\n# Set secure protocols\n[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls12\n\n# Check to see if SimplySql module is installed\nif ((Get-ModuleInstalled -PowerShellModuleName $PowerShellModuleName) -ne $true)\n{\n # Tell user what we're doing\n Write-Output \"PowerShell module $PowerShellModuleName is not installed, downloading temporary copy ...\"\n\n # Install temporary copy\n Install-PowerShellModule -PowerShellModuleName $PowerShellModuleName -LocalModulesPath $LocalModules\n}\n\n# Display\nWrite-Output \"Importing module $PowerShellModuleName ...\"\n\n# Check to see if it was downloaded\nif ((Test-Path -Path \"$LocalModules\\$PowerShellModuleName\") -eq $true)\n{\n\t# Use specific version\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Get whether trust certificate is necessary\n$postgresqlTrustSSL = [System.Convert]::ToBoolean(\"$postgresqlTrustSSL\")\n\ntry\n{\n\t# Declare initial connection string\n $connectionString = \"Server=$postgresqlServerName;Port=$postgresqlServerPort;Database=$postgresqlDatabaseName;\"\n \n\t# Check to see if we need to trust the ssl cert\n\tif ($postgresqlTrustSSL -eq $true)\n\t{\n # Append SSL connection string components\n $connectionString += \"SSL Mode=Require;Trust Server Certificate=true;\"\n\t}\n\n # Update the connection string based on authentication method\n switch ($postgreSqlAuthenticationMethod)\n {\n \"azuremanagedidentity\"\n {\n \t# Get login token\n Write-Host \"Generating Azure Managed Identity token ...\"\n $token = Invoke-RestMethod -Method GET -Uri \"http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://ossrdbms-aad.database.windows.net\" -Headers @{\"MetaData\" = \"true\"}\n \n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$($token.access_token)`\";\"\n \n break\n }\n \"awsiam\"\n {\n # Region is part of the RDS endpoint, extract\n $region = ($createPosgreSQLServerName.Split(\".\"))[2]\n\n Write-Host \"Generating AWS IAM token ...\"\n $createUserPassword = (aws rds generate-db-auth-token --hostname $postgresqlServerName --region $region --port $createPort --username $postgresqlUsername)\n\n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$postgesqlUserPassword`\";\"\n\n break\n }\n \"gcpserviceaccount\"\n {\n # Define header\n $header = @{ \"Metadata-Flavor\" = \"Google\"}\n\n # Retrieve service accounts\n $serviceAccounts = Invoke-RestMethod -Method Get -Uri \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/\" -Headers $header\n\n # Results returned in plain text format, get into array and remove empty entries\n $serviceAccounts = $serviceAccounts.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries)\n\n # Retreive the specific service account assigned to the VM\n $serviceAccount = $serviceAccounts | Where-Object {$_.Contains(\"iam.gserviceaccount.com\") }\n\n Write-Host \"Generating GCP IAM token ...\"\n # Retrieve token for account\n $token = Invoke-RestMethod -Method Get -Uri \"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/$serviceAccount/token\" -Headers $header\n \n # Check to see if there was a username provided\n if ([string]::IsNullOrWhitespace($postgresqlUsername))\n {\n \t# Use the service account name, but strip off the .gserviceaccount.com part\n $postgresqlUsername = $serviceAccount.SubString(0, $serviceAccount.IndexOf(\".gserviceaccount.com\"))\n }\n \n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$($token.access_token)`\";\"\n \n break\n }\n \"usernamepassword\"\n {\n # Append remaining portion of connection string\n $connectionString += \";User Id=$postgresqlUsername;Password=`\"$postgesqlUserPassword`\";\"\n\n break \n }\n\n \"windowsauthentication\"\n {\n # Append remaining portion of connection string\n $connectionString += \";Integrated Security=True;\"\n }\n }\n\n\t# Open connection\n Open-PostGreConnection -ConnectionString $connectionString\n\n # Execute the statement\n $executionResult = Invoke-SqlUpdate -Query \"$postgresqlCommand\" -CommandTimeout $postgresqlCommandTimeout\n \n # Display the result\n Get-SqlMessage\n}\nfinally\n{\n Close-SqlConnection\n}\n\n\n"
},
"Parameters": [
{
Expand Down

0 comments on commit 34ddf76

Please sign in to comment.