Use the "agent setup" CLI command in the Windows wizard

fixes #7241
This commit is contained in:
Gunnar Beutner 2014-10-22 09:34:09 +02:00
parent 74830dd8dd
commit bed490829a
3 changed files with 181 additions and 98 deletions

View File

@ -55,6 +55,8 @@
this.rdoNewMaster = new System.Windows.Forms.RadioButton();
this.tbcPages = new System.Windows.Forms.TabControl();
this.tabRetrieveCertificate = new System.Windows.Forms.TabPage();
this.lblRetrieveCertificate = new System.Windows.Forms.Label();
this.prgRetrieveCertificate = new System.Windows.Forms.ProgressBar();
this.tabVerifyCertificate = new System.Windows.Forms.TabPage();
this.grpX509Fields = new System.Windows.Forms.GroupBox();
this.txtX509Field = new System.Windows.Forms.TextBox();
@ -66,9 +68,12 @@
this.lblX509Subject = new System.Windows.Forms.Label();
this.lblX509Issuer = new System.Windows.Forms.Label();
this.lblX509Prompt = new System.Windows.Forms.Label();
this.tabError = new System.Windows.Forms.TabPage();
this.picBanner = new System.Windows.Forms.PictureBox();
this.lblRetrieveCertificate = new System.Windows.Forms.Label();
this.prgRetrieveCertificate = new System.Windows.Forms.ProgressBar();
this.lblError = new System.Windows.Forms.Label();
this.txtError = new System.Windows.Forms.TextBox();
this.lblTicket = new System.Windows.Forms.Label();
this.txtTicket = new System.Windows.Forms.TextBox();
this.tabFinish.SuspendLayout();
this.tabConfigure.SuspendLayout();
this.tabParameters.SuspendLayout();
@ -78,6 +83,7 @@
this.tabRetrieveCertificate.SuspendLayout();
this.tabVerifyCertificate.SuspendLayout();
this.grpX509Fields.SuspendLayout();
this.tabError.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.picBanner)).BeginInit();
this.SuspendLayout();
//
@ -163,6 +169,8 @@
//
// tabParameters
//
this.tabParameters.Controls.Add(this.txtTicket);
this.tabParameters.Controls.Add(this.lblTicket);
this.tabParameters.Controls.Add(this.txtInstanceName);
this.tabParameters.Controls.Add(this.lblInstanceName);
this.tabParameters.Controls.Add(this.groupBox2);
@ -197,7 +205,7 @@
this.groupBox2.Controls.Add(this.txtListenerPort);
this.groupBox2.Controls.Add(this.lblListenerPort);
this.groupBox2.Controls.Add(this.rdoListener);
this.groupBox2.Location = new System.Drawing.Point(8, 330);
this.groupBox2.Location = new System.Drawing.Point(8, 359);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(601, 111);
this.groupBox2.TabIndex = 2;
@ -253,7 +261,7 @@
this.groupBox1.Controls.Add(this.lvwEndpoints);
this.groupBox1.Controls.Add(this.rdoNoMaster);
this.groupBox1.Controls.Add(this.rdoNewMaster);
this.groupBox1.Location = new System.Drawing.Point(8, 48);
this.groupBox1.Location = new System.Drawing.Point(8, 77);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(601, 276);
this.groupBox1.TabIndex = 1;
@ -337,6 +345,7 @@
this.tbcPages.Controls.Add(this.tabVerifyCertificate);
this.tbcPages.Controls.Add(this.tabConfigure);
this.tbcPages.Controls.Add(this.tabFinish);
this.tbcPages.Controls.Add(this.tabError);
this.tbcPages.ItemSize = new System.Drawing.Size(0, 1);
this.tbcPages.Location = new System.Drawing.Point(0, 80);
this.tbcPages.Margin = new System.Windows.Forms.Padding(0);
@ -359,6 +368,22 @@
this.tabRetrieveCertificate.Text = "Checking Certificate";
this.tabRetrieveCertificate.UseVisualStyleBackColor = true;
//
// lblRetrieveCertificate
//
this.lblRetrieveCertificate.AutoSize = true;
this.lblRetrieveCertificate.Location = new System.Drawing.Point(164, 229);
this.lblRetrieveCertificate.Name = "lblRetrieveCertificate";
this.lblRetrieveCertificate.Size = new System.Drawing.Size(110, 13);
this.lblRetrieveCertificate.TabIndex = 3;
this.lblRetrieveCertificate.Text = "Checking certificate...";
//
// prgRetrieveCertificate
//
this.prgRetrieveCertificate.Location = new System.Drawing.Point(164, 248);
this.prgRetrieveCertificate.Name = "prgRetrieveCertificate";
this.prgRetrieveCertificate.Size = new System.Drawing.Size(289, 23);
this.prgRetrieveCertificate.TabIndex = 2;
//
// tabVerifyCertificate
//
this.tabVerifyCertificate.Controls.Add(this.grpX509Fields);
@ -463,6 +488,18 @@
this.lblX509Prompt.TabIndex = 0;
this.lblX509Prompt.Text = "Please verify the master\'s SSL certificate:";
//
// tabError
//
this.tabError.Controls.Add(this.txtError);
this.tabError.Controls.Add(this.lblError);
this.tabError.Location = new System.Drawing.Point(4, 5);
this.tabError.Name = "tabError";
this.tabError.Padding = new System.Windows.Forms.Padding(3);
this.tabError.Size = new System.Drawing.Size(617, 500);
this.tabError.TabIndex = 8;
this.tabError.Text = "Error";
this.tabError.UseVisualStyleBackColor = true;
//
// picBanner
//
this.picBanner.Image = global::Icinga.Properties.Resources.icinga_banner;
@ -472,21 +509,41 @@
this.picBanner.TabIndex = 1;
this.picBanner.TabStop = false;
//
// lblRetrieveCertificate
// lblError
//
this.lblRetrieveCertificate.AutoSize = true;
this.lblRetrieveCertificate.Location = new System.Drawing.Point(164, 229);
this.lblRetrieveCertificate.Name = "lblRetrieveCertificate";
this.lblRetrieveCertificate.Size = new System.Drawing.Size(110, 13);
this.lblRetrieveCertificate.TabIndex = 3;
this.lblRetrieveCertificate.Text = "Checking certificate...";
this.lblError.AutoSize = true;
this.lblError.Location = new System.Drawing.Point(8, 12);
this.lblError.Name = "lblError";
this.lblError.Size = new System.Drawing.Size(209, 13);
this.lblError.TabIndex = 0;
this.lblError.Text = "An error occurred while setting up Icinga 2:";
//
// prgRetrieveCertificate
// txtError
//
this.prgRetrieveCertificate.Location = new System.Drawing.Point(164, 248);
this.prgRetrieveCertificate.Name = "prgRetrieveCertificate";
this.prgRetrieveCertificate.Size = new System.Drawing.Size(289, 23);
this.prgRetrieveCertificate.TabIndex = 2;
this.txtError.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtError.Location = new System.Drawing.Point(11, 38);
this.txtError.Multiline = true;
this.txtError.Name = "txtError";
this.txtError.ReadOnly = true;
this.txtError.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtError.Size = new System.Drawing.Size(598, 397);
this.txtError.TabIndex = 1;
//
// lblTicket
//
this.lblTicket.AutoSize = true;
this.lblTicket.Location = new System.Drawing.Point(9, 48);
this.lblTicket.Name = "lblTicket";
this.lblTicket.Size = new System.Drawing.Size(71, 13);
this.lblTicket.TabIndex = 4;
this.lblTicket.Text = "Agent Ticket:";
//
// txtTicket
//
this.txtTicket.Location = new System.Drawing.Point(98, 45);
this.txtTicket.Name = "txtTicket";
this.txtTicket.Size = new System.Drawing.Size(240, 20);
this.txtTicket.TabIndex = 5;
//
// AgentWizard
//
@ -523,6 +580,8 @@
this.tabVerifyCertificate.PerformLayout();
this.grpX509Fields.ResumeLayout(false);
this.grpX509Fields.PerformLayout();
this.tabError.ResumeLayout(false);
this.tabError.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.picBanner)).EndInit();
this.ResumeLayout(false);
@ -570,6 +629,11 @@
private System.Windows.Forms.TabPage tabRetrieveCertificate;
private System.Windows.Forms.Label lblRetrieveCertificate;
private System.Windows.Forms.ProgressBar prgRetrieveCertificate;
private System.Windows.Forms.TabPage tabError;
private System.Windows.Forms.TextBox txtError;
private System.Windows.Forms.Label lblError;
private System.Windows.Forms.TextBox txtTicket;
private System.Windows.Forms.Label lblTicket;
}
}

View File

@ -18,6 +18,8 @@ namespace Icinga
{
public partial class AgentWizard : Form
{
private string _TrustedFile;
public AgentWizard()
{
InitializeComponent();
@ -94,92 +96,113 @@ namespace Icinga
lblConfigStatus.Text = message;
}
private void ShowErrorText(string text)
{
if (InvokeRequired) {
Invoke((MethodInvoker)delegate { ShowErrorText(text); });
return;
}
txtError.Text = text;
tbcPages.SelectedTab = tabError;
}
private bool RunProcess(string filename, string arguments, out string output)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = filename;
psi.Arguments = arguments;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
String result = "";
using (Process proc = Process.Start(psi)) {
proc.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs args) {
result += args.Data + "\r\n";
};
proc.OutputDataReceived += delegate(object sender, DataReceivedEventArgs args) {
result += args.Data + "\r\n";
};
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
output = result;
if (proc.ExitCode != 0)
return false;
}
return true;
}
private void VerifyCertificate(string host, string port)
{
SetRetrievalStatus(25);
string pathPrefix = Icinga2InstallDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
ProcessStartInfo psi;
string output;
if (!File.Exists(pathPrefix + ".crt")) {
psi = new ProcessStartInfo();
psi.FileName = Icinga2InstallDir + "\\sbin\\icinga2.exe";
psi.Arguments = "pki new-cert --cn \"" + txtInstanceName.Text + "\" --keyfile \"" + pathPrefix + ".key\" --certfile \"" + pathPrefix + ".crt\"";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
using (Process proc = Process.Start(psi)) {
proc.WaitForExit();
if (proc.ExitCode != 0) {
Invoke((MethodInvoker)delegate { FatalError("The Windows service could not be installed."); });
return;
}
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
"pki new-cert --cn \"" + txtInstanceName.Text + "\" --keyfile \"" + pathPrefix + ".key\" --certfile \"" + pathPrefix + ".crt\"",
out output)) {
ShowErrorText(output);
return;
}
}
SetRetrievalStatus(50);
string trustedfile = Path.GetTempFileName();
_TrustedFile = Path.GetTempFileName();
psi = new ProcessStartInfo();
psi.FileName = Icinga2InstallDir + "\\sbin\\icinga2.exe";
psi.Arguments = "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --keyfile \"" + pathPrefix + ".key\" --certfile \"" + pathPrefix + ".crt\" --trustedfile \"" + trustedfile + "\"";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
using (Process proc = Process.Start(psi)) {
proc.WaitForExit();
if (proc.ExitCode != 0) {
Invoke((MethodInvoker)delegate { FatalError("Could not retrieve the master's X509 certificate."); });
return;
}
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
"pki save-cert --host \"" + host + "\" --port \"" + port + "\" --keyfile \"" + pathPrefix + ".key\" --certfile \"" + pathPrefix + ".crt\" --trustedfile \"" + _TrustedFile + "\"",
out output)) {
ShowErrorText(output);
return;
}
SetRetrievalStatus(100);
X509Certificate2 cert = new X509Certificate2(trustedfile);
X509Certificate2 cert = new X509Certificate2(_TrustedFile);
Invoke((MethodInvoker)delegate { ShowCertificatePrompt(cert); });
}
private void ConfigureService()
{
SetConfigureStatus(0, "Updating configuration files...");
using (FileStream fp = File.Open(Icinga2InstallDir + "\\etc\\icinga2\\features-available\\agent.conf", FileMode.Create)) {
using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) {
sw.Write(
"/**\n" +
" * The agent listener accepts checks from agents.\n" +
" */\n" +
"\n" +
"library \"agent\"\n" +
"\n" +
"object AgentListener \"agent\" {\n" +
" cert_path = SysconfDir + \"/icinga2/pki/agent/agent.crt\"\n" +
" key_path = SysconfDir + \"/icinga2/pki/agent/agent.key\"\n" +
" ca_path = SysconfDir + \"/icinga2/pki/agent/ca.crt\"\n"
);
/*if (rdoNoMaster.Checked)
sw.Write(" upstream_name = \"{0}\"\n", txtMasterInstance.Text);*/
string output;
if (rdoListener.Checked)
sw.Write(" bind_port = \"{0}\"\n", txtListenerPort.Text);
string args = "";
/*if (rdoConnect.Checked)
sw.Write(
" upstream_host = \"{0}\"\n" +
" upstream_port = \"{1}\"\n", txtPeerHost.Text, txtPeerPort.Text
);*/
if (rdoNewMaster.Checked)
args += " --master";
sw.Write("}\n");
Invoke((MethodInvoker)delegate {
foreach (ListViewItem lvi in lvwEndpoints.Items) {
args += " --endpoint " + lvi.SubItems[0].Text + "," + lvi.SubItems[1].Text;
}
}
});
EnableFeature("api");
EnableFeature("checker");
if (rdoListener.Checked)
args += " --listen ::," + txtListenerPort.Text;
args += " --ticket " + txtTicket.Text;
args += " --trustedcert " + _TrustedFile;
args += " --cn " + txtInstanceName.Text;
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
"agent setup" + args,
out output)) {
ShowErrorText(output);
return;
}
SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
DirectoryInfo di = new DirectoryInfo(Icinga2InstallDir);
@ -192,29 +215,15 @@ namespace Icinga
SetConfigureStatus(75, "Installing the Icinga 2 service...");
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Icinga2InstallDir + "\\sbin\\icinga2.exe";
psi.Arguments = "--scm-uninstall";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
"--scm-uninstall",
out output);
using (Process proc = Process.Start(psi)) {
proc.WaitForExit();
}
psi = new ProcessStartInfo();
psi.FileName = Icinga2InstallDir + "\\sbin\\icinga2.exe";
psi.Arguments = "--scm-install daemon";
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
using (Process proc = Process.Start(psi)) {
proc.WaitForExit();
if (proc.ExitCode != 0) {
Invoke((MethodInvoker)delegate { FatalError("The Windows service could not be installed."); });
return;
}
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe",
"--scm-install daemon",
out output)) {
ShowErrorText(output);
return;
}
SetConfigureStatus(100, "Finished.");
@ -256,6 +265,11 @@ namespace Icinga
private void btnBack_Click(object sender, EventArgs e)
{
if (tbcPages.SelectedTab == tabError) {
tbcPages.SelectedIndex = 0;
return;
}
int offset = 1;
if (tbcPages.SelectedTab == tabVerifyCertificate)
@ -272,6 +286,11 @@ namespace Icinga
return;
}
if (txtTicket.Text.Length == 0) {
Warning("Please enter an agent ticket.");
return;
}
if (rdoNoMaster.Checked && lvwEndpoints.Items.Count == 0) {
Warning("You need to add at least one master endpoint.");
return;
@ -283,7 +302,7 @@ namespace Icinga
}
}
if (tbcPages.SelectedTab == tabFinish)
if (tbcPages.SelectedTab == tabFinish || tbcPages.SelectedTab == tabError)
Application.Exit();
tbcPages.SelectedIndex++;
@ -298,7 +317,7 @@ namespace Icinga
{
Refresh();
btnBack.Enabled = (tbcPages.SelectedTab == tabVerifyCertificate);
btnBack.Enabled = (tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabError);
btnNext.Enabled = (tbcPages.SelectedTab == tabParameters || tbcPages.SelectedTab == tabVerifyCertificate || tbcPages.SelectedTab == tabFinish);
if (tbcPages.SelectedTab == tabFinish) {

View File

@ -57,7 +57,7 @@ void AgentSetupCommand::InitParameters(boost::program_options::options_descripti
("ticket", po::value<std::string>(), "Generated ticket number for this request")
("trustedcert", po::value<std::string>(), "Trusted master certificate file")
("cn", po::value<std::string>(), "The certificate's common name")
("master", po::value<std::string>(), "Use setup for a master instance");
("master", "Use setup for a master instance");
}
std::vector<String> AgentSetupCommand::GetArgumentSuggestions(const String& argument, const String& word) const