-
Notifications
You must be signed in to change notification settings - Fork 506
Expand file tree
/
Copy pathpostgres-execute-sql.json
More file actions
115 lines (115 loc) · 10.7 KB
/
postgres-execute-sql.json
File metadata and controls
115 lines (115 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{
"Id": "9a9c8c2c-d50e-4dc8-8e7e-b561f6e8fc15",
"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": 5,
"CommunityActionTemplateId": null,
"Packages": [],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "# Define variables\n$connectionName = \"OctopusDeploy\"\n\n# 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 -ConnectionName $connectionName\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 -ConnectionName $connectionName\n\n # Execute the statement\n $executionResult = Invoke-SqlUpdate -Query \"$postgresqlCommand\" -CommandTimeout $postgresqlCommandTimeout -ConnectionName $connectionName\n \n # Display the result\n Get-SqlMessage -ConnectionName $connectionName\n}\nfinally\n{\n\t# Close connection if open\n if ((Test-SqlConnection -ConnectionName $connectionName) -eq $true)\n {\n \tClose-SqlConnection -ConnectionName $connectionName\n }\n}\n\n\n"
},
"Parameters": [
{
"Id": "3adf249d-aef2-41dc-922d-a3cfb67c4afe",
"Name": "postgresqlServerName",
"Label": "Server",
"HelpText": "Hostname (or IP) of the MySQL database server.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "0fbfc453-3495-42b9-b13a-c7db532bc030",
"Name": "postgresqlAuthenticationMethod",
"Label": "Authentication Method",
"HelpText": "Method used to authenticate to the PostgreSQL server.",
"DefaultValue": "usernamepassword",
"DisplaySettings": {
"Octopus.ControlType": "Select",
"Octopus.SelectOptions": "awsiam|AWS EC2 IAM Role\nazuremanagedidentity|Azure Managed Identity\ngcpserviceaccount|GCP Service Account\nusernamepassword|Username\\Password\nwindowsauthentication|Windows Authentication"
}
},
{
"Id": "cba7ecb6-e23d-462e-97d8-2d72e28d3233",
"Name": "postgresqlUsername",
"Label": "Username",
"HelpText": "Username to use for the connection",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "0fc033f0-7446-4698-b003-bc18549d45b3",
"Name": "postgesqlUserPassword",
"Label": "Password",
"HelpText": "Password for the user account",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Id": "0f2cea01-dd43-4f3e-87aa-1d1ca2974e3e",
"Name": "postgresqlDatabaseName",
"Label": "Database Name",
"HelpText": "Name of the database to execute against.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "ef1a940d-cd67-49b6-b584-c79d918b4707",
"Name": "postgresqlServerPort",
"Label": "Port",
"HelpText": "Port for the database instance.",
"DefaultValue": "5432",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "355fb14a-2dfd-4b41-98a3-87b7d177f716",
"Name": "postgresqlTrustSSL",
"Label": "Trust SSL Certificate",
"HelpText": "Force trusting an SSL Certificate.",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
},
{
"Id": "4d7dbc06-697c-4585-b18d-043ed1c120a7",
"Name": "postgresqlCommandTimeout",
"Label": "Command Timeout",
"HelpText": "Timeout value (in seconds) for SQL commands",
"DefaultValue": "30",
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Id": "5a810fee-d9e9-4200-8540-2a22b3681be9",
"Name": "postgresqlCommand",
"Label": "Command",
"HelpText": "SQL statement(s) to execute.",
"DefaultValue": "",
"DisplaySettings": {
"Octopus.ControlType": "MultiLineText"
}
}
],
"StepPackageId": "Octopus.Script",
"$Meta": {
"ExportedAt": "2025-10-06T18:58:48.869Z",
"OctopusVersion": "2025.3.14336",
"Type": "ActionTemplate"
},
"LastModifiedBy": "twerthi",
"Category": "postgresql"
}