Skip to content

Instantly share code, notes, and snippets.

View droyad's full-sized avatar

Robert Wagner droyad

View GitHub Profile
services.AddAuthentication(options =>
{
// custom scheme defined in .AddPolicyScheme() below
options.DefaultScheme = "JwtOrApiKey";
options.DefaultChallengeScheme = "JwtOrApiKey";
})
.AddPolicyScheme("JwtOrApiKey", "JwtOrApiKey", options =>
{
// runs on each request
options.ForwardDefaultSelector = context =>

Stop Loss Kata (original source: https://gist.github.com/gregoryyoung/1500720) Developing solutions when time is involved is tricky, but testing when time is involved is yet another problem.

A trailing stop loss is a term used in financial trading. For a very in depth explanation you can read here (http://www.investopedia.com/articles/trading/03/080603.asp) and here (http://en.wikipedia.org/wiki/Order_(exchange)#Stop_orders), however we do not need a huge amount of background in order to do the kata as we are going to limit the problem a bit.

Say you buy into a stock at $10. You want it to automatically get sold if the stock goes below $9 to limit your exposure. This threshold is set by subtracting 10% from the original position. The term "trailing" means that if the price goes up to $11 then the sell point becomes $10, if it goes up to $15 then the sell point becomes $14, maintaining the original 10% margin of $1.

The kata is to create something that implements a trailing stop loss and to do it with TDD.

/// <summary>
/// Work around for https://github.com/dotnet/corefx/issues/27326
/// </summary>
public class NonFlushingStream : Stream
{
private readonly Stream _stream;
public NonFlushingStream(Stream stream)
{
_stream = stream;
using NuGet.Packaging;
using NuGet.Frameworks;
using (var reader = new PackageArchiveReader(stream, true))
{
var nuspecFile = reader.GetNuspecFile();
if (nuspecFile == null)
throw new Exception("Package does not contain a nuspec file");
using (var s = reader.GetStream(nuspecFile))
@droyad
droyad / ssh-health-check.sh
Created September 4, 2018 01:27
SSH Target Health Check
#!/bin/bash
# Users can write their own custom script via a machine policy, which is appended here via MachinePolicyCustomScript.
function perform_standardhealthcheck
{
# Mono is required if not using self-contained Calamari
requiresMono={{RequiresMono}}
# Ensure required commands are available
@droyad
droyad / TurnOnApplyToDynamicallyReferencedPackages.ps1
Created April 18, 2018 06:00
Turn ApplyToDynamicallyReferencedPackages on
Add-Type -Path 'C:\Program Files\Octopus Deploy\Octopus\Octopus.Client.dll'
$octopusURI = 'http://myserver' # Your server address
# Create using API key
# $endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI,'API-GHRWGHEWJGHWJEGHW' # Get this from your profile
# $repository = new-object Octopus.Client.OctopusRepository $endpoint
# OR create using username/password
$endpoint = new-object Octopus.Client.OctopusServerEndpoint $octopusURI
@droyad
droyad / gist:eb3f788d9ef03a8d81761e7bdb9d6f07
Created April 4, 2018 23:38
Octopus Health Check Script
# Users can write their own custom script via a machine policy, which is appended here via MachinePolicyCustomScript.
$hasErrors = $false
$details = @()
function Convert-ServiceMessageValue([string]$value)
{
$valueBytes = [System.Text.Encoding]::UTF8.GetBytes($value)
return [Convert]::ToBase64String($valueBytes)
}
@droyad
droyad / gist:b8923fbf4af5c50c2f41ca3a760763f8
Created January 31, 2018 22:04
Find Projects that use Library Variable Set
var endpoint = new OctopusServerEndpoint("http://localhost");
var repository = new OctopusRepository(endpoint);
repository.Users.SignIn("Admin", "foo");
var mySet = repository.LibraryVariableSets.FindByName("MySet");
var projectNames = repository.Projects.GetAll().Where(p => p.IncludedLibraryVariableSetIds.Contains(mySet.Id)).Select(p => p.Name);
void Main()
{
Halibut.Logging.LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());
var rt = new HalibutRuntime(new X509Certificate2());
rt.Poll(new Uri("http://example.com"), new ServiceEndPoint("http://example.com", ""));
Thread.Sleep(10000);
}
class ConsoleLogProvider : Halibut.Logging.ILogProvider
{
// Output
// 8
// 9
// 10
// 11
// 12
// 13
// 14
// 15
// 16