Skip to content

Instantly share code, notes, and snippets.

@RichardSlater
Created January 16, 2014 17:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save RichardSlater/8459579 to your computer and use it in GitHub Desktop.
Save RichardSlater/8459579 to your computer and use it in GitHub Desktop.
Desired State Configuration to Install MongoDB on Windows Server 2013
configuration MongoDB {
param (
[string[]]$ComputerName = $env:ComputerName
)
node $ComputerName {
File SetupFolder {
Type = 'Directory'
DestinationPath = "C:\setup"
Ensure = 'Present'
}
Script DownloadMongoBinaries
{
SetScript = {
Invoke-WebRequest 'http://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.4.9.zip' -OutFile "C:\setup\mongodb-win32-x86_64-2008plus-2.4.9.zip"
}
TestScript = { Test-Path "C:\setup\mongodb-win32-x86_64-2008plus-2.4.9.zip" }
GetScript = { @{MongoBinariesDownloaded = $(Test-Path "C:\setup\mongodb-win32-x86_64-2008plus-2.4.9.zip") } }
DependsOn = '[File]SetupFolder'
}
Archive MongoBinaries {
Path = "C:\setup\mongodb-win32-x86_64-2008plus-2.4.9.zip"
Destination = "C:\setup"
Ensure = 'Present'
DependsOn = '[Script]DownloadMongoBinaries'
}
File MongoDBFolder {
Type = 'Directory'
Recurse = $true
DestinationPath = "C:\mongo"
SourcePath = "c:\setup\mongodb-win32-x86_64-2008plus-2.4.9"
Ensure = 'Present'
DependsOn = '[Archive]MongoBinaries'
}
File MongoDataFolder {
Type = 'Directory'
DestinationPath = "C:\mongo\data"
Ensure = 'Present'
DependsOn = '[File]MongoDBFolder'
}
File MongoLogsFolder {
Type = 'Directory'
DestinationPath = "C:\mongo\logs"
Ensure = 'Present'
DependsOn = '[File]MongoDBFolder'
}
File MongoConfigFolder {
Type = 'Directory'
DestinationPath = "C:\mongo\config"
Ensure = 'Present'
DependsOn = '[File]MongoDBFolder'
}
Script MongoConfigurationFile
{
SetScript = {
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false);
$sw = New-Object System.IO.StreamWriter('C:\mongo\config\mongod.cfg', $false, $utf8WithoutBom)
$sw.WriteLine('logpath=C:\mongo\logs\mongo.log')
$sw.WriteLine('dbpath=C:\mongo\data')
$sw.WriteLine('replSet=rs')
$sw.WriteLine('oplogSize=700')
$sw.WriteLine('port=21701')
$sw.WriteLine('#auth=true')
$sw.WriteLine('#keyFile=C:\mongo\config\keyfile.txt')
$sw.Close()
}
TestScript = { Test-Path 'C:\mongo\config\mongod.cfg' }
GetScript = { @{MongoConfigured = (Test-Path 'C:\mongo\config\mongod.cfg')} }
DependsOn = '[File]MongoConfigFolder'
}
Script MongoKeyFile
{
SetScript = {
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false);
$sw = New-Object System.IO.StreamWriter('C:\mongo\config\keyfile.txt', $false, $utf8WithoutBom)
$sw.Write('hEGYEYqqgUBDC0Qy8P0xC4lUmOFpmIUlVMN5c1BZJ6vAM3XQS4v73YPNbrSWJ8IY')
$sw.Close()
}
TestScript = { Test-Path 'C:\mongo\config\keyfile.txt' }
GetScript = { @{MongoConfigured = (Test-Path 'C:\mongo\config\keyfile.txt')} }
DependsOn = '[File]MongoConfigFolder'
}
Script InstallMongoService
{
SetScript = {
C:\mongo\bin\mongod.exe --config C:\mongo\config\mongod.cfg --install
}
TestScript = { (Get-Service | Where-Object { $_.Name -eq 'MongoDB' }).Count -gt 0 }
GetScript = { @{MongoServiceInstalled = ((Get-Service | Where-Object { $_.Name -eq 'MongoDB' }).Count -gt 0) } }
DependsOn = '[Script]MongoConfigurationFile','[script]MongoKeyFile'
}
Service ConfigureMongoService
{
Name = 'MongoDB'
StartupType = 'Automatic'
DependsOn = '[Script]InstallMongoService','[File]MongoLogsFolder','[File]MongoDataFolder'
}
Script ConfigureFirewall
{
SetScript = { New-NetFirewallRule -Name Allow_MongoDB -DisplayName 'Allow MongoDB' -Description 'Allow inbound MongoDB connections' -Direction Inbound -Program 'C:\mongo\bin\mongod.exe' -Action Allow }
TestScript = { (Get-NetFirewallRule | Where-Object { $_.Name -eq 'Allow_MongoDB' }) -ne $null }
GetScript = { @{FirewallConfiguredForMongo = ((Get-NetFirewallRule | Where-Object { $_.Name -eq 'Allow_MongoDB' }).Count -gt 0) } }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment