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

Commit

Permalink
feat: tentacle configuration part of install, command cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cruikshj committed Apr 4, 2016
1 parent afb10ae commit cae09a2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 55 deletions.
2 changes: 1 addition & 1 deletion nautilus/DeployCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Nautilus
[Verb("deploy", HelpText = "Creates deployments for the latest release of all projects related to the target machine by role and environment.")]
public class DeployCommand : CommandBase
{
[Option('n', "name", Required = false, HelpText = "The target machine name.")]
[Option('n', "name", Required = false, HelpText = "The target machine name. Defaults to the local machine name.")]
public string MachineName { get; set; }

protected override int Run(OctopusProxy octopus)
Expand Down
99 changes: 67 additions & 32 deletions nautilus/InstallCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,33 @@ namespace Nautilus
[Verb("install", HelpText = "Installs an Octopus tenticle on the local machine.")]
public class InstallCommand : CommandBase
{
[Option('l', "location", Required = false, HelpText = "The install directory of the Octopus Tenticle. Defaults to Program Files.")]
public string InstallLocation { get; set; }

[Option('h', "home", Required = false, HelpText = "The home directory of the Octopus Tenticle. Defaults to \"C:\\Octopus\"")]
public string HomeLocation { get; set; }

[Option('t', "thumbprint", Required = false, HelpText = "The Octopus Server thumbprint. Defaults to global certificate thumbprint.")]
public string Thumbprint { get; set; }

[Option('p', "port", Required = false, HelpText = "The port of the Octopus Tenticle. Defaults to 10933.")]
public int? Port { get; set; }

protected override int Run(OctopusProxy octopus)
{
{
var installLocation = InstallLocation ?? Path.Combine(Environment.GetEnvironmentVariable("PROGRAMFILES"), @"Octopus Deploy\Tentacle");

var homeLocation = HomeLocation ?? @"C:\Octopus";

var thumbprint = Thumbprint;
if (thumbprint == null)
{
var certicate = octopus.GetGlobalCertificate();
thumbprint = certicate.Thumbprint;
}

var port = Port ?? 10933;

var systemInfo = octopus.GetSystemInfo();
var downloadVersion = systemInfo.Version;
if (Environment.Is64BitOperatingSystem)
Expand All @@ -24,50 +49,60 @@ protected override int Run(OctopusProxy octopus)
using (var webClient = new WebClient())
{
webClient.DownloadFile(downloadUrl, filePath);
}
}

Console.WriteLine($"Installing tentacle from {filePath}");
var processStartInfo = new ProcessStartInfo
if (RunProcess("msiexec", $"INSTALLLOCATION=\"{installLocation}\" /i \"{filePath}\" /quiet"))
{
Console.WriteLine("Tentacle installation completed successfully");

Console.WriteLine("Configuring tentacle");
var tentacleExe = installLocation + @"\Tentacle.exe";

if (RunProcess(tentacleExe, $"create-instance --instance \"Tentacle\" --config \"{homeLocation}\\Tentacle.config\" --console"))
if (RunProcess(tentacleExe, $"new-certificate --instance \"Tentacle\" --if-blank --console"))
if (RunProcess(tentacleExe, $"configure --instance \"Tentacle\" --reset-trust --console"))
if (RunProcess(tentacleExe, $"configure --instance \"Tentacle\" --home \"{homeLocation}\" --app \"{homeLocation}\\Applications\" --port \"{port}\" --console"))
if (RunProcess(tentacleExe, $"configure --instance \"Tentacle\" --trust \"{thumbprint}\" --console"))
if (RunProcess("netsh", $"advfirewall firewall add rule \"name=Octopus Deploy Tentacle\" dir=in action=allow protocol=TCP localport={port}"))
if (RunProcess(tentacleExe, $"service --instance \"Tentacle\" --install --start --console"))
{
Console.WriteLine("Tentacle configuration completed successfully");
return 0;
}
}

return 1;
}

private static bool RunProcess(string fileName, string arguments)
{
var startInfo = new ProcessStartInfo
{
FileName = "msiexec",
Arguments = $"/i \"{filePath}\" /quiet",
UseShellExecute = true
FileName = fileName,
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(processStartInfo))

using (var process = Process.Start(startInfo))
{
var timeout = 120000;
const int timeout = 120000;
if (!process.WaitForExit(timeout))
{
Console.WriteLine($"Error: Operation timed out ({timeout} milliseconds) while waiting for tentacle installation to complete");
return 1;
Console.WriteLine($"Error: \"{fileName} {arguments}\" operation timed out ({timeout} milliseconds)");
return false;
}

if (process.ExitCode != 0)
{
Console.WriteLine($"Tentacle installation failed and exited with code {process.ExitCode}");
return 1;
Console.WriteLine($"Error: \"{fileName} {arguments}\" operation failed and exited with code {process.ExitCode}");
return false;
}

return true;
}

Console.WriteLine("Tentacle installation completed successfully");

Console.WriteLine("Configuring tentacle");

//todo: configure

// cd "C:\Program Files\Octopus Deploy\Tentacle"


// Tentacle.exe create-instance --instance "Tentacle" --config "C:\Octopus\Tentacle.config" --console
// Tentacle.exe new-certificate --instance "Tentacle" --if-blank --console
// Tentacle.exe configure --instance "Tentacle" --reset-trust --console
// Tentacle.exe configure --instance "Tentacle" --home "C:\Octopus" --app "C:\Octopus\Applications" --port "10933" --console
// Tentacle.exe configure --instance "Tentacle" --trust "YOUR_OCTOPUS_THUMBPRINT" --console
// "netsh" advfirewall firewall add rule "name=Octopus Deploy Tentacle" dir=in action=allow protocol=TCP localport=10933
// Tentacle.exe register-with --instance "Tentacle" --server "http://YOUR_OCTOPUS" --apiKey="API-YOUR_API_KEY" --role "web-server" --environment "Staging" --comms-style TentaclePassive --console
// Tentacle.exe service --instance "Tentacle" --install --start --console

return 0;
}
}
}
70 changes: 52 additions & 18 deletions nautilus/RegisterCommand.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,90 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using CommandLine;
using Microsoft.Win32;

namespace Nautilus
{
[Verb("register", HelpText = "Registers the target machine with Octopus.")]
[Verb("register", HelpText = "Registers the local machine with Octopus.")]
public class RegisterCommand : CommandBase
{
[Option('n', "name", Required = false, HelpText = "The target machine name.")]
[Option('n', "name", Required = false, HelpText = "The machine name. Defaults to the local machine name.")]
public string MachineName { get; set; }

[Option('t', "thumbprint", Required = false, HelpText = "The Octopus server certificate thumbprint. Defaults to global certificate.")]
[Option('t', "thumbprint", Required = false, HelpText = "The Octopus Tentacle thumbprint. Defaults to the local Tentacle thumbprint.")]
public string Thumbprint { get; set; }

[Option('h', "host", Required = false, HelpText = "The target machine tentacle host name.")]
[Option('h', "host", Required = false, HelpText = "The Tentacle host name. Defaults to the local machine name.")]
public string HostName { get; set; }

[Option('p', "port", Required = false, HelpText = "The target machine tentacle port.")]
[Option('p', "port", Required = false, HelpText = "The Tentacle port. Defaults to 10933.")]
public int? Port { get; set; }

[Option('e', "environments", Required = true, HelpText = "The environment names of the target machine.")]
[Option('e', "environments", Required = true, HelpText = "The environment names of the machine.")]
public IList<string> Environments { get; set; }

[Option('r', "roles", Required = true, HelpText = "The roles of the target machine.")]
[Option('r', "roles", Required = true, HelpText = "The roles of the machine.")]
public IList<string> Roles { get; set; }

protected override int Run(OctopusProxy octopus)
{
var machineName = MachineName ?? Environment.MachineName;
var machineName = MachineName ?? Environment.MachineName;
var hostName = HostName ?? Environment.MachineName;
var port = Port ?? 10933;

var thumbprint = Thumbprint;
if (thumbprint == null)
{
thumbprint = GetTentacleThumbprint();
}

var machine = octopus.GetMachine(machineName);
if (machine != null)
{
Console.WriteLine($"The target machine ({machineName}) is already registered with Octopus ({OctopusServerAddress})");
Console.WriteLine($"The machine ({machineName}) is already registered with Octopus ({OctopusServerAddress})");
return 0;
}

var hostName = HostName ?? Environment.MachineName;
var port = Port ?? 10933;
machine = octopus.CreateMachine(machineName, thumbprint, hostName, port, Environments, Roles);

var thumbprint = Thumbprint;
if (thumbprint == null)
Console.WriteLine($"The machine ({machine.Name}) was registered successfully");

return 0;
}

private static string GetTentacleThumbprint()
{
var installLocation = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Octopus\Tentacle", "InstallLocation", null) as string;
if (installLocation == null)
{
var certicate = octopus.GetGlobalCertificate();
thumbprint = certicate.Thumbprint;
return null;
}

machine = octopus.CreateMachine(machineName, thumbprint, hostName, port, Environments, Roles);
var processStartInfo = new ProcessStartInfo
{
FileName = Path.Combine(installLocation, "Tentacle.exe"),
Arguments = "show-thumbprint --nologo",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};

Console.WriteLine($"Target machine ({machine.Name}) registered successfully");
string thumbprint;
using (var process = Process.Start(processStartInfo))
{
thumbprint = process.StandardOutput.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
return null;
}

thumbprint = thumbprint.Substring(36, 40);
}

return 0;
return thumbprint;
}
}
}
8 changes: 4 additions & 4 deletions nautilus/UnregisterCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace Nautilus
{
[Verb("unregister", HelpText = "Unregisters the target machine from Octopus.")]
[Verb("unregister", HelpText = "Unregisters the local machine from Octopus.")]
public class UnregisterCommand : CommandBase
{
[Option('n', "name", Required = false, HelpText = "The target machine name.")]
[Option('n', "name", Required = false, HelpText = "The machine name. Defaults to the local machine name.")]
public string MachineName { get; set; }

protected override int Run(OctopusProxy octopus)
Expand All @@ -16,13 +16,13 @@ protected override int Run(OctopusProxy octopus)
var machine = octopus.GetMachine(machineName);
if (machine == null)
{
Console.WriteLine($"The target machine ({machineName}) is not registered with Octopus ({OctopusServerAddress})");
Console.WriteLine($"The machine ({machineName}) is not registered with Octopus ({OctopusServerAddress})");
return 0;
}

octopus.DeleteMachine(machine);

Console.WriteLine($"Target machine ({machine.Name}) unregistered successfully");
Console.WriteLine($"The machine ({machine.Name}) was unregistered successfully");

return 0;
}
Expand Down

0 comments on commit cae09a2

Please sign in to comment.