Skip to content

Instantly share code, notes, and snippets.

@jayhilden
Created February 19, 2015 15:33
Show Gist options
  • Save jayhilden/e60b26a94cdc7599a0ab to your computer and use it in GitHub Desktop.
Save jayhilden/e60b26a94cdc7599a0ab to your computer and use it in GitHub Desktop.
RabbitMQ setup script
#region setup values
$apiURL = "http://localhost:15672/api";
$apiUsername = "username";
$apiPassword = "password";
$envName = "int";
$clientName = "sales";
$clientUsername = "sales";
$clientPassword = "sales";
#endregion
#region internal setup
$vhost = $envName + "." + $clientName
$clientUsername = $envName + "." + $clientUsername
$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($apiUsername+":"+$apiPassword ))
$headers = @{Authorization = "Basic "+$encoded}
$vhostURL = $apiURL + "/vhosts/" + $vhost;
$exchangeName = "AuthComplete";
$queueNameEPCIS = "AuthComplete.EPCIS";
$queueNameES = "AuthComplete.ElasticSearch";
$exchangeNameDeadLetter = "DeadLetter";
$queueNameDeadLetter = "DeadLetter.All";
#endregion
#step 0: verify connection
$overviewURL = $apiURL + "/overview";
Try {
$Response = Invoke-WebRequest -Uri $overviewURL -Headers $headers
}
Catch {
[console]::Writeline("Unable to connect to {0}, exiting.", $overviewURL);
exit;
}
#step 1 create vhost
Try
{
$response = Invoke-WebRequest -Uri $vhostURL -Headers $headers
[console]::Writeline("vhost {0} already exists", $vhost);
}
Catch
{
$response = Invoke-WebRequest -Uri $vhostURL -Headers $headers -Method Put -ContentType "application/json"
[console]::Writeline("vhost {0} created", $vhost);
}
#step 2: create user
$userURL = $apiURL + "/users/" + $clientUsername;
Try
{
$response = Invoke-WebRequest -Uri $userURL -Headers $headers
[console]::Writeline("user {0} already exists", $clientUsername);
}
Catch
{
$body = "{""password"":""" + $clientPassword + """, ""tags"":""""}";
$response = Invoke-WebRequest -Uri $userURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("user {0} created with password {1}", $clientUsername, $clientPassword);
}
#step 3: assign user to vhost
$permissionsURL = $apiURL + "/permissions/" + $vhost + "/" + $clientUsername
$body = "{""scope"":""client"",""configure"":"""",""write"":"".*"",""read"":"".*""}";
$response = Invoke-WebRequest -Uri $permissionsURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("user {0} given permissions on vhost {1}", $clientUsername, $vhost);
#step 3b: assign the admin user to the vhost
$permissionsURL = $apiURL + "/permissions/" + $vhost + "/" + $apiUsername
$body = "{""scope"":""client"",""configure"":"".*"",""write"":"".*"",""read"":"".*""}";
$response = Invoke-WebRequest -Uri $permissionsURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("user {0} given permissions on vhost {1} - including configuration", $apiUsername, $vhost);
#step 4: create AuthComplete exchange on the vhost
$exchangeURL = $apiURL + "/exchanges/" + $vhost + "/" + $exchangeName;
$body = "{""type"":""fanout"",""auto_delete"":false,""durable"":true,""arguments"":[]}"
$response = Invoke-WebRequest -Uri $exchangeURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("exchange {0} created (if it didn't already exist) on vhost {1}", $exchangeName, $vhost);
#step 5: create queue AuthComplete.EPCIS
$queueURL = $apiURL + "/queues/" + $vhost + "/" + $queueNameEPCIS;
$body = "{""auto_delete"":false,""durable"":true,""arguments"":[]}"
$response = Invoke-WebRequest -Uri $queueURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("queue {0} created (if it didn't already exist) on vhost {1}", $queueNameEPCIS, $vhost);
#step 6: create queue AuthComplete.ElasticSearch
$queueURL = $apiURL + "/queues/" + $vhost + "/" + $queueNameES;
$body = "{""auto_delete"":false,""durable"":true,""arguments"":[]}"
$response = Invoke-WebRequest -Uri $queueURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("queue {0} created (if it didn't already exist) on vhost {1}", $queueNameES, $vhost);
#step 7: bind exchange AuthComplete to queue AuthComplete.EPCIS
$bindURL = [string]::Format("{0}/bindings/{1}/e/{2}/q/{3}", $apiURL, $vhost, $exchangeName, $queueNameEPCIS);
#echo $bindURL;
$body = "{""routing_key"":"""",""arguments"":[]}";
$response = Invoke-WebRequest -Uri $bindURL -Headers $headers -Method Post -ContentType "application/json" -Body $body
[console]::Writeline("exchange {0} bound to {1} on vhost {2}", $exchangeName, $queueNameEPCIS, $vhost);
#step 8: bind exchange AuthComplete to queue AuthComplete.ElasticSearch
$bindURL = [string]::Format("{0}/bindings/{1}/e/{2}/q/{3}", $apiURL, $vhost, $exchangeName, $queueNameES);
#echo $bindURL;
$body = "{""routing_key"":"""",""arguments"":[]}";
$response = Invoke-WebRequest -Uri $bindURL -Headers $headers -Method Post -ContentType "application/json" -Body $body
[console]::Writeline("exchange {0} bound to {1} on vhost {2}", $exchangeName, $queueNameES, $vhost);
#step 9: create dead letter FANOUT exchange
$exchangeURL = $apiURL + "/exchanges/" + $vhost + "/" + $exchangeNameDeadLetter;
$body = "{""type"":""fanout"",""auto_delete"":false,""durable"":true,""arguments"":[]}"
$response = Invoke-WebRequest -Uri $exchangeURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("exchange {0} created (if it didn't already exist) on vhost {1}", $exchangeNameDeadLetter, $vhost);
#step 10: create dead letter queue
$queueURL = $apiURL + "/queues/" + $vhost + "/" + $queueNameDeadLetter;
$body = "{""auto_delete"":false,""durable"":true,""arguments"":[]}"
$response = Invoke-WebRequest -Uri $queueURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("queue {0} created (if it didn't already exist) on vhost {1}", $queueNameDeadLetter, $vhost);
#step 11: bind dead letter queue to dead letter exchange
$bindURL = [string]::Format("{0}/bindings/{1}/e/{2}/q/{3}", $apiURL, $vhost, $exchangeNameDeadLetter, $queueNameDeadLetter);
#echo $bindURL;
$body = "{""routing_key"":"""",""arguments"":[]}";
$response = Invoke-WebRequest -Uri $bindURL -Headers $headers -Method Post -ContentType "application/json" -Body $body
[console]::Writeline("exchange {0} bound to {1} on vhost {2}", $exchangeNameDeadLetter, $queueNameDeadLetter, $vhost);
#step 12: set policy on vhost so that the dead letter exchange is bound everywhere
$policyURL = [string]::Format("{0}/policies/{1}/DLX", $apiURL, $vhost);
$body = @"
{
"pattern":".*",
"definition": {
"dead-letter-exchange":"$exchangeNameDeadLetter"
},
"priority":0,
"apply-to": "all"
}
"@;
$response = Invoke-WebRequest -Uri $policyURL -Headers $headers -Method Put -ContentType "application/json" -Body $body
[console]::Writeline("policy DLX set on vhost {0}", $vhost);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment