Skip to content

Commit

Permalink
Merge pull request #1570 from OctopusDeploy/bug-mongodb
Browse files Browse the repository at this point in the history
Fixing bug in mongodb templates
  • Loading branch information
twerthi authored Oct 29, 2024
2 parents 2e92a2e + 6df6f1c commit 5430023
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions step-templates/mongodb-add-update-user-roles.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"Name": "MongoDB - Add or update user roles ",
"Description": "Adds roles to an existing user in a MongoDB database.",
"ActionType": "Octopus.Script",
"Version": 2,
"Version": 3,
"Author": "twerthi",
"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\nfunction Get-DatabaseUserExists\n{\n\t# Define parameters\n param ($UserName)\n \n # Define working variables\n $userExists = $false\n \n\t# Get users for database\n $command = @\"\n{ usersInfo: 1 }\n\"@\n\n\t$results = Invoke-MdbcCommand -Command $command\n $users = $results[\"users\"]\n \n # Loop through returned results\n foreach ($user in $users)\n {\n \tif ($user[\"user\"] -eq $UserName)\n {\n \treturn $true\n }\n }\n \n return $false\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 = \"Mdbc\"\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 location\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Connect to mongodb instance\n$connectionUrl = \"mongodb://$($MongoDBAdminUsername):$($MogoDBAdminUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl $MongoDBDatabaseName\n\n# Get whether the database exits\nif ((Get-DatabaseUserExists -UserName $MongoDBUsername) -eq $true)\n{\n\t# Create user\n Write-Output \"Adding $MongoDBRoles to $MongoDBUsername.\"\n \n # Create Roles array for adding\n $roles = @()\n foreach ($MongoDBRole in $MongoDBRoles.Split(\",\"))\n {\n \t$roles += @{\n \trole = $MongoDBRole.Trim()\n db = $MongoDBDatabaseName\n }\n }\n\n # Define create user command\n $command = @\"\n{\n\tupdateUser: `\"$MongoDBUsername`\" \n roles: $(ConvertTo-Json $roles)\n}\n\"@\n\n\t# Create user account\n $result = Invoke-MdbcCommand -Command $command\n \n # Check to make sure it was created successfully\n if ($result.ContainsKey(\"ok\"))\n {\n \tWrite-Output \"Successfully added role(s) $MongoDBRoles to $MongoDBUsername in database $MongoDBDatabaseName.\"\n }\n else\n {\n \tWrite-Error \"Failed, $result\"\n }\n}\nelse\n{\n\tWrite-Error \"Unable to add role(s) to $MongoDBUsername, user does not exist in $MongoDBDatabaseName.\"\n}\n\n\n\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 $moduleParameters = @{\n Name = $PowerShellModuleName\n Path = $LocalModulesPath\n Force = $true\n }\n\n # Check the version of PowerShell\n if ($PSVersionTable.PSVersion.Major -lt 7)\n {\n # Add specific version of powershell module to use\n $moduleParameters.Add(\"MaximumVersion\", \"6.7.4\")\n }\n\n\t# Save the module in the temporary location\n Save-Module @moduleParameters\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\nfunction Get-DatabaseUserExists\n{\n\t# Define parameters\n param ($UserName)\n \n # Define working variables\n $userExists = $false\n \n\t# Get users for database\n $command = @\"\n{ usersInfo: 1 }\n\"@\n\n\t$results = Invoke-MdbcCommand -Command $command\n $users = $results[\"users\"]\n \n # Loop through returned results\n foreach ($user in $users)\n {\n \tif ($user[\"user\"] -eq $UserName)\n {\n \treturn $true\n }\n }\n \n return $false\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 = \"Mdbc\"\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 location\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Connect to mongodb instance\n$connectionUrl = \"mongodb://$($MongoDBAdminUsername):$($MogoDBAdminUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl $MongoDBDatabaseName\n\n# Get whether the database exits\nif ((Get-DatabaseUserExists -UserName $MongoDBUsername) -eq $true)\n{\n\t# Create user\n Write-Output \"Adding $MongoDBRoles to $MongoDBUsername.\"\n \n # Create Roles array for adding\n $roles = @()\n foreach ($MongoDBRole in $MongoDBRoles.Split(\",\"))\n {\n \t$roles += @{\n \trole = $MongoDBRole.Trim()\n db = $MongoDBDatabaseName\n }\n }\n\n # Define create user command\n $command = @\"\n{\n\tupdateUser: `\"$MongoDBUsername`\" \n roles: $(ConvertTo-Json $roles)\n}\n\"@\n\n\t# Create user account\n $result = Invoke-MdbcCommand -Command $command\n \n # Check to make sure it was created successfully\n if ($result.ContainsKey(\"ok\"))\n {\n \tWrite-Output \"Successfully added role(s) $MongoDBRoles to $MongoDBUsername in database $MongoDBDatabaseName.\"\n }\n else\n {\n \tWrite-Error \"Failed, $result\"\n }\n}\nelse\n{\n\tWrite-Error \"Unable to add role(s) to $MongoDBUsername, user does not exist in $MongoDBDatabaseName.\"\n}\n\n\n\n\n\n\n"
},
"Parameters": [
{
Expand Down Expand Up @@ -84,10 +84,10 @@
}
],
"$Meta": {
"ExportedAt": "2020-12-02T20:31:19.166Z",
"OctopusVersion": "2020.5.0",
"ExportedAt": "2024-10-28T21:13:02.226Z",
"OctopusVersion": "2024.3.12899",
"Type": "ActionTemplate"
},
"LastModifiedBy": "twerthi",
"Category": "mongodb"
}
}
12 changes: 6 additions & 6 deletions step-templates/mongodb-create-database.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"Name": "MongoDB - Create Database if not exists",
"Description": "Creates a new database on a MongoDB server with an initial collection.",
"ActionType": "Octopus.Script",
"Version": 3,
"Version": 4,
"Author": "twerthi",
"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\nfunction Get-DatabaseExists\n{\n\t# Define parameters\n param ($DatabaseName)\n \n\t# Execute query\n $mongodbDatabases = Get-MdbcDatabase\n \n return $mongodbDatabases.DatabaseNamespace -contains $DatabaseName\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 = \"Mdbc\"\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 location\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Connect to mongodb instance\n$connectionUrl = \"mongodb://$($MongoDBUsername):$($MogoDBUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl \"admin\"\n\n# Get whether the database exits\nif ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName) -ne $true)\n{\n\t# Create database\n Write-Output \"Database $MongoDBDatabaseName doesn't exist.\"\n Connect-Mdbc $connectionUrl \"$MongoDBDatabaseName\"\n \n # Databases don't get created unless some data has been added\n Add-MdbcCollection $MongoDBInitialCollection\n \n # Check to make sure it was successful\n if ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName))\n {\n \t# Display success\n Write-Output \"$MongoDBDatabaseName created successfully.\" \n }\n else\n {\n \tWrite-Error \"Failed to create $MongoDBDatabaseName!\"\n }\n}\nelse\n{\n\tWrite-Output \"Database $MongoDBDatabaseName already exists.\"\n}\n\n\n\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 $moduleParameters = @{\n Name = $PowerShellModuleName\n Path = $LocalModulesPath\n Force = $true\n }\n\n # Check the version of PowerShell\n if ($PSVersionTable.PSVersion.Major -lt 7)\n {\n # Add specific version of powershell module to use\n $moduleParameters.Add(\"MaximumVersion\", \"6.7.4\")\n }\n\n\t# Save the module in the temporary location\n Save-Module @moduleParameters\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\nfunction Get-DatabaseExists\n{\n\t# Define parameters\n param ($DatabaseName)\n \n\t# Execute query\n $mongodbDatabases = Get-MdbcDatabase\n \n return $mongodbDatabases.DatabaseNamespace -contains $DatabaseName\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 = \"Mdbc\"\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 location\n $PowerShellModuleName = \"$LocalModules\\$PowerShellModuleName\"\n}\n\n# Import the module\nImport-Module -Name $PowerShellModuleName\n\n# Connect to mongodb instance\n$connectionUrl = \"mongodb://$($MongoDBUsername):$($MogoDBUserpassword)@$($MongoDBServerName):$($MongoDBPort)\"\n\n# Connect to MongoDB server\nConnect-Mdbc $connectionUrl \"admin\"\n\n# Get whether the database exits\nif ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName) -ne $true)\n{\n\t# Create database\n Write-Output \"Database $MongoDBDatabaseName doesn't exist.\"\n Connect-Mdbc $connectionUrl \"$MongoDBDatabaseName\"\n \n # Databases don't get created unless some data has been added\n Add-MdbcCollection $MongoDBInitialCollection\n \n # Check to make sure it was successful\n if ((Get-DatabaseExists -DatabaseName $MongoDBDatabaseName))\n {\n \t# Display success\n Write-Output \"$MongoDBDatabaseName created successfully.\" \n }\n else\n {\n \tWrite-Error \"Failed to create $MongoDBDatabaseName!\"\n }\n}\nelse\n{\n\tWrite-Output \"Database $MongoDBDatabaseName already exists.\"\n}\n\n\n\n\n\n\n"
},
"Parameters": [
{
Expand Down Expand Up @@ -74,11 +74,11 @@
}
],
"$Meta": {
"ExportedAt": "2020-12-02T20:27:29.307Z",
"OctopusVersion": "2020.5.0",
"ExportedAt": "2024-10-28T21:07:02.249Z",
"OctopusVersion": "2024.3.12899",
"Type": "ActionTemplate"
},
"LastModifiedOn": "2021-07-26T16:50:00.000+00:00",
"LastModifiedBy": "bobjwalker",
"LastModifiedBy": "twerthi",
"Category": "mongodb"
}
}
Loading

0 comments on commit 5430023

Please sign in to comment.