DSCResources/cMongoDBDSC/cMongoDBDSC.schema.psm1
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
Configuration cMongoDBDSC { param( [Parameter(Mandatory = $True)] [ValidateSet('Present', 'Absent')] [string] $Ensure, [Parameter(Mandatory = $True)] [ValidateNotNullOrEmpty()] [System.IO.FileInfo] $MongoSourcePath, [ValidateNotNullOrEmpty()] [System.IO.FileInfo] $ConfigFile = "$env:ProgramFiles\MongoDb\Server\3.4\mongod.cfg", [ValidateNotNullOrEmpty()] [System.IO.FileInfo] $DBFolder = "d:\data\mongo-db", [ValidateNotNullOrEmpty()] [System.IO.FileInfo] $LogFolder = "d:\data\mongo-log", [ValidateNotNullOrEmpty()] [System.IO.FileInfo] $MongoExe = "$env:ProgramFiles\MongoDB\Server\3.4\bin\mongod.exe" ) Import-DscResource -ModuleName PSDesiredStateConfiguration Import-DscResource -ModuleName xPSDesiredStateConfiguration Package InstallMongoDb { Ensure = $Ensure Name = 'MongoDB 3.4.4 2008R2Plus (64 bit)' Path = $MongoSourcePath ProductId = '5582AB82-490A-4790-81F3-94D649925A1F' } File ConfigurationFile { Ensure = $Ensure Type = "File" DestinationPath = $ConfigFile #Do not change the format of Contents part, the format matters! Contents = " systemLog: destination: file path: $LogFolder\mongod.log storage: dbPath: $DBFolder " Force = $true } File DBDir { Ensure = $Ensure Type = "Directory" DestinationPath = $DBFolder Force = $true } File LogDir { Ensure = $Ensure Type = "Directory" DestinationPath = $LogFolder Force = $true } Script InstallMongoService { DependsOn = "[package]InstallMongoDb", "[file]ConfigurationFile", "[file]DBDir", "[file]LogDir" GetScript = { $instances = Get-Service mongoDB* $vals = @{ Installed = [boolean]$instances; } return $vals } TestScript = { $instances = Get-Service mongoDB* if ($instances) { Write-Verbose "MongoDB is already running as a service" } else { Write-Verbose "MongoDB is not running as a service" } return [boolean]$instances } SetScript = { if ($using:Ensure -eq 'Present' -and ) { $process = Start-Process -FilePath $using:mongoExe -ArgumentList "--config `"$using:configFile`" --install" -PassThru Start-Sleep -Seconds 10 if ($process.ExitCode -ne 0) { Write-Error "Mongo DB Service installation completed with errors (exit code $($process.ExitCode))" } else { Write-Verbose "Mongo DB Service installation completed successfully (exit code $($process.ExitCode))" } } elseif ($using:Ensure -eq 'Absent' -and (Test-Path $using:mongoExe)) { $process = Start-Process -FilePath $using:mongoExe -ArgumentList "--remove" -PassThru Start-Sleep -Seconds 10 if ($process.ExitCode -ne 0) { Write-Error "Mongo DB Service Removal completed with errors (exit code $($process.ExitCode))" } else { Write-Verbose "Mongo DB Service completed successfully (exit code $($process.ExitCode))" } } } } xService StartMongoService { Ensure = $Ensure DependsOn = "[script]InstallMongoService" StartupType = "Automatic" Name = "MongoDB" State = "Running" StartupTimeout = 6000000 } } |