Using Github, Jekyll and Poole

Just trying out different blog engines for the fun of it.

Powershell script to set Pagerduty in maintenance mode for x minutes via API

After the previous quick post on notifying NewRelic of a deployment using a powershell script I setup a second script to inform Pagerduty to set certain alerting services into maintenance mode. The idea is the same; an easy on the fly changeable powershell script which can be executed from batch deploy scripts and will perform an HTTP POST to Pagerduty REST Api using some command-line parameters.

The steps:
1. Get the powershell script from github gist (https://gist.github.com/henkmeulekamp/3441f937caf991d6500b) 2. Get your pagerduty API token, which is described here 3. Get a requersterId, which is a userId. You can find these by going into your Pagerduty account and open a user from the user tab. The requesterId is the last part of the path after users/ 4. Open the Pagerduty services from the services tab. The serviceIds are the last part of the path after after services. You need to get the id from each service which need to be set to maintenance mode. 5. Determine your needed maintenance window in minutes, from the moment your script is executed. 6. Set all these param values in the first param script block of the script. 7. The previous step is optional when you want to run the script using command-line params. 8. Then run script by: - Powershell.exe -executionpolicy remotesigned -File pagerduty-maintenance.ps1 - OR - Powershell.exe -executionpolicy remotesigned -File pagerduty-maintenance.ps1 -minutes 3 -requesterId ABC1234 -apiKey ABC123 -description "New version deployed" -serviceIDs "'serviceId1', 'serviceId2', 'serviceId..'"

Thats all!

Powershell script to inform NewRelic of deployment

For our current script based deployment process we wanted to automaticly inform Newrelic of the deployment. Newrelic will then make the change in performance and resource usage metrics visible for that deployment, which is a really cool feature.

Our current production deployment scripts are batch file based. The Newrelic API deployment call requires a HTTP POST with custom HTTP authorization header, so options are limited to make something simple work, which can be changed on the fly if needed. I settled for powershell, which I still dislike in general, but a great fit for this scenario.

Setting up Newrelic deployment notifications via their API can be done in few simple steps:
- Get my powershell from github/gist: (https://gist.github.com/henkmeulekamp/78ac923abad62c62adaa)
- Setup Newrelic to allow API calls on your account
- Get your NewRelic API Key
- Get Application ID and name, within newrelic click on applications, then on the application name; The app id is the last number in the url
- Then run by:
- Put those 3 in the script in the first three param value, then run script by executing:
- Powershell.exe -executionpolicy remotesigned -File newrelicdeploy.ps1
- OR
- Set them runtime by calling the script as
- Powershell.exe -executionpolicy remotesigned -File newrelicdeploy.ps1 -appname yourAppName -appid yourAppId -apikey yourapiKey

Thats all!

Reference, the powershell script:

  
param (
    [string]$appname = "your-newrelic-appname",
    [string]$appid = "your-newrelic-appid",
    [string]$apiKey = "your-newrelic-api-key"
 )

function Execute-HTTPPostCommand() {
    param(
        [string] $target = $null,
	[string] $postParam,
	[string] $key
    )
    $webRequest = [System.Net.WebRequest]::Create($target)
    $PostStr = [System.Text.Encoding]::UTF8.GetBytes($postParam)
    $webrequest.ContentLength = $PostStr.Length
    $webRequest.ServicePoint.Expect100Continue = $false
    $webRequest.Headers.Add("x-api-key", $key);
    $webRequest.Method = "POST"
	  
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($PostStr, 0,$PostStr.length)
    $requestStream.Close()

	[System.Net.WebResponse] $resp = $webRequest.GetResponse();
	#response check
	if ([int]$resp.StatusCode -eq 201) {
		Write-Host "NewRelic Deploy API called succeeded."
		$rs = $resp.GetResponseStream();
	    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
	    [string] $results = $sr.ReadToEnd();
	    return $results;
	} else {
		Write-Host "NewRelic Deploy API called failed."
		Write-Host $resp.StatusCode.ToString() + " " $resp.StatusDescription		
	}
	return "Failed";
}

Setting UserAgent in ServiceStack JsonServiceClient

Quick one, ran into an issue with one of our services which require a non empty UserAgent header and wanted to use the ServiceStack.ServiceClient.Web.JsonServiceClient from Servicestack.

Was almost going to implement it myself in the ServiceClientBase class in Servicestack and push my first pull request. But going through the code there was already an extension point, the HttpWebRequestFilter (there is an HttpWebResponseFilter also). You can setup an action there which will be called before the request is done and gives you access to the HttpWebRequest instance.

I’m using it for setting the UserAgent, but basically you can set any header (even setup the authentication headers):

var client = new JsonServiceClient

{
       LocalHttpWebRequestFilter = request => request.UserAgent = "MyUserAgent"
};

Love servicestack, every time you think you found a problem there is already an extension point..