Skip to content

Instantly share code, notes, and snippets.

@gbiellem
Last active April 12, 2018 07:21
Show Gist options
  • Save gbiellem/b3e2138d5211853eadc2ec129f5eac00 to your computer and use it in GitHub Desktop.
Save gbiellem/b3e2138d5211853eadc2ec129f5eac00 to your computer and use it in GitHub Desktop.
Setup 2 instances of CouchDB locally (dev and test)
#Requires -RunAsAdministrator
#Requires -Version 5.1
if (-not(Get-Module IniManager -ErrorAction SilentlyContinue)) {
Install-Module -Name IniManager -Force
}
Import-Module -Name IniManager
$webBinary = 'https://dl.bintray.com/apache/couchdb/win/2.1.1/couchdb-2.1.1.msi'
$msi = "~\Downloads\couchdb-2.1.1.msi"
$freshCouchInstall = $false
$freshCouchTestInstall = $false
$targetpath = 'C:\CouchDBTest'
$serviceName = 'Apache CouchDB Test'
$admin = @{
Name = 'admin'
Password = 'Welcome1'
}
function Purge {
param (
[Parameter(Mandatory)]
[String] $path
)
if (Test-Path $path -PathType Container)
{
Remove-item $path -Force -Recurse
}
if (Test-Path $path -PathType Leaf)
{
Remove-item $path -Force
}
}
if (Get-Service $serviceName -ErrorAction SilentlyContinue) {
"$serviceName is already installed. Exiting..."
return
}
if (-not (Get-Service 'Apache CouchDB' -ErrorAction SilentlyContinue))
{
if (-not (Test-Path -PathType Leaf -Path $msi)) {
Invoke-WebRequest -Uri $webBinary -OutFile $msi
}
$msifullpath= (Get-Item $msi).FullName
Start-Process -wait msiexec.exe "/i $msifullpath /qb TARGET=C:\CouchDB"
$freshCouchInstall = $true
}
$couchbin = Get-ItemPropertyValue -Path 'Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Apache CouchDB\Parameters' -Name 'AppDirectory'
$couchbasedir = (Get-Item $couchbin).Parent.FullName
cp $couchbasedir $targetpath -Recurse -Force
### configure the Couch Test instance
# Remove any copied data and logs
Purge "$targetpath\couch.log"
Purge "$targetpath\data"
# Change the default ports
$localini = "$targetpath\etc\local.ini"
Set-IniKey -Path $localini -Section 'httpd' -Key 'port' -Value 7986
Set-IniKey -Path $localini -Section 'chttpd' -Key 'port' -Value 7984
# Update the instance name
$vmargs = "$targetpath\etc\vm.args"
$updatedVMArgsContent = Get-content $vmargs | % {
if ($_ -match "^-name") {
'-name [email protected]'
} else {
$_
}
}
Set-Content -Path $vmargs -Value $updatedVMArgsContent
# Register the Test service
if (-not (Get-Service 'Apache CouchDB Test' -ErrorAction SilentlyContinue)) {
Start-Process -wait "$targetpath\bin\nssm.exe" "install ""$serviceName"" $targetpath\bin\couchdb.cmd"
}
# Start the Test service
Start-Service $serviceName
if ($freshCouchInstall) {
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/httpd/enable_cors -Body ( ConvertTo-Json "true" )
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/cors/credentials -Body ( ConvertTo-Json "true" )
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/cors/origins -Body ( ConvertTo-Json "*" )
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/cors/headers -Body ( ConvertTo-Json "accept, authorization, content-type, origin, referer" )
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/cors/methods -Body ( ConvertTo-Json "GET, PUT, POST, HEAD, DELETE" )
$resp = IRM -Method PUT -Uri http://localhost:5986/_config/log/level -Body ( ConvertTo-Json "error" )
$resp = IRM -Method PUT -Uri http://localhost:5984/_global_changes
$resp = IRM -Method Put -Uri http://localhost:5984/_replicator
$resp = IRM -Method Put -Uri http://localhost:5984/_users
$resp = IRM -Method Post http://localhost:5984/_cluster_setup -ContentType "application/json" -Body (ConvertTo-Json @{ action = "enable_cluster"; bind_address = "0.0.0.0"; username=$admin.Name; password=$admin.Password; node_count = "1"})
}
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/httpd/enable_cors -Body ( ConvertTo-Json "true" )
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/cors/credentials -Body ( ConvertTo-Json "true" )
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/cors/origins -Body ( ConvertTo-Json "*" )
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/cors/headers -Body ( ConvertTo-Json "accept, authorization, content-type, origin, referer" )
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/cors/methods -Body ( ConvertTo-Json "GET, PUT, POST, HEAD, DELETE" )
$resp = IRM -Method PUT -Uri http://localhost:7986/_config/log/level -Body ( ConvertTo-Json "error" )
$resp = IRM -Method PUT -Uri http://localhost:7984/_global_changes
$resp = IRM -Method Put -Uri http://localhost:7984/_replicator
$resp = IRM -Method Put -Uri http://localhost:7984/_users
#set the second instance to a different uuid
$uuid = [GUID]::NewGuid().ToString("n")
$resp = IRM -Method Put -Uri http://localhost:7986/_config/couchdb/uuid -Body (ConvertTo-Json $uuid ) -WebSession $couchsession
$resp = IRM -Method Post http://localhost:7984/_cluster_setup -ContentType "application/json" -Body (ConvertTo-Json @{ action = "enable_cluster"; bind_address = "0.0.0.0"; username=$admin.Name; password=$admin.Password; node_count = "1"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment