Skip to content
This repository has been archived by the owner on Mar 23, 2018. It is now read-only.

Commit

Permalink
feat: deploy nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
cruikshj committed Apr 12, 2016
1 parent 8cecefe commit 19049de
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions nautilus/DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class DeployCommand : CommandBase
[Option('f', "force", Required = false, HelpText = "Specifies whether to force redeployment of releases to this machine.")]
public bool Force { get; set; }

[Option("nonce", Required = false, HelpText = "An arbritrary value to ensure that a deploy is only run once. If the specified value matches a value previously used, this deploy will be prevented. The value is stored in an environment variable (NAUTILUS_NONCE) on the local machine.")]
public int? Nonce { get; set; }

protected override int Run(OctopusProxy octopus)
{
var machineName = MachineName ?? Environment.MachineName;
Expand All @@ -30,6 +33,11 @@ protected override int Run(OctopusProxy octopus)
return 1;
}

if (Nonce.HasValue && CheckAndUpdateNonces(Nonce.Value))
{
WriteLine($"Preventing repeat deploy based on the specified nonce value ({Nonce.Value})");
}

var successExpression = new Regex($"Success: {machine.Name}{Environment.NewLine}");

var environments = octopus.GetEnvironments().ToDictionary(i => i.Id);
Expand Down Expand Up @@ -126,5 +134,32 @@ private static string GetEnvironmentName(string environmentId, IDictionary<strin
}
return environmentId;
}

private static bool CheckAndUpdateNonces(int nonce)
{
var nonces = new HashSet<int>();
var ev = Environment.GetEnvironmentVariable("NAUTILUS_NONCE", EnvironmentVariableTarget.Machine);
if (!String.IsNullOrEmpty(ev))
{
foreach (var ns in ev.Split(','))
{
int n;
if (int.TryParse(ns, out n))
{
nonces.Add(n);
}
}
}

if (nonces.Contains(nonce))
{
return true;
}

nonces.Add(nonce);
Environment.SetEnvironmentVariable("NAUTILUS_NONCE", String.Join(",", nonces), EnvironmentVariableTarget.Machine);

return false;
}
}
}

0 comments on commit 19049de

Please sign in to comment.