This repository has been archived by the owner on Mar 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tentacle configuration part of install, command cleanup
- Loading branch information
Showing
4 changed files
with
124 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters