Improve user experience when updating Icinga 2 with the NSIS installer

fixes #8116
This commit is contained in:
Gunnar Beutner 2015-05-12 13:49:52 +02:00
parent a02684651a
commit c9eccaac40
6 changed files with 348 additions and 34 deletions

View File

@ -80,6 +80,12 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ServiceStatus.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="ServiceStatus.Designer.cs">
<DependentUpon>ServiceStatus.cs</DependentUpon>
</Compile>
<Compile Include="SetupWizard.cs"> <Compile Include="SetupWizard.cs">
<SubType>Form</SubType> <SubType>Form</SubType>
</Compile> </Compile>
@ -94,6 +100,9 @@
</Compile> </Compile>
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="ServiceStatus.resx">
<DependentUpon>ServiceStatus.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="SetupWizard.resx"> <EmbeddedResource Include="SetupWizard.resx">
<DependentUpon>SetupWizard.cs</DependentUpon> <DependentUpon>SetupWizard.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>

View File

@ -1,10 +1,32 @@
using System; using System;
using System.IO;
using System.Windows.Forms; using System.Windows.Forms;
using Microsoft.Win32;
namespace Icinga namespace Icinga
{ {
static class Program static class Program
{ {
public static string Icinga2InstallDir
{
get
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Icinga Development Team\\ICINGA2");
if (rk == null)
return "";
return (string)rk.GetValue("");
}
}
public static void FatalError(Form owner, string message)
{
MessageBox.Show(owner, message, owner.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
@ -13,7 +35,20 @@ namespace Icinga
{ {
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SetupWizard());
string installDir = Program.Icinga2InstallDir;
if (installDir == "")
FatalError(null, "Icinga 2 does not seem to be installed properly.");
Form form;
if (File.Exists(installDir + "\\etc\\icinga2\\features-enabled\\api.conf"))
form = new ServiceStatus();
else
form = new SetupWizard();
Application.Run(form);
} }
} }
} }

View File

@ -0,0 +1,118 @@
namespace Icinga
{
partial class ServiceStatus
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServiceStatus));
this.picBanner = new System.Windows.Forms.PictureBox();
this.lblStatus = new System.Windows.Forms.Label();
this.txtStatus = new System.Windows.Forms.TextBox();
this.btnReconfigure = new System.Windows.Forms.Button();
this.btnOK = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.picBanner)).BeginInit();
this.SuspendLayout();
//
// picBanner
//
this.picBanner.Image = global::Icinga.Properties.Resources.icinga_banner;
this.picBanner.Location = new System.Drawing.Point(0, 0);
this.picBanner.Name = "picBanner";
this.picBanner.Size = new System.Drawing.Size(625, 77);
this.picBanner.TabIndex = 2;
this.picBanner.TabStop = false;
//
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Location = new System.Drawing.Point(12, 105);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(79, 13);
this.lblStatus.TabIndex = 3;
this.lblStatus.Text = "Service Status:";
//
// txtStatus
//
this.txtStatus.Location = new System.Drawing.Point(97, 102);
this.txtStatus.Name = "txtStatus";
this.txtStatus.ReadOnly = true;
this.txtStatus.Size = new System.Drawing.Size(278, 20);
this.txtStatus.TabIndex = 2;
//
// btnReconfigure
//
this.btnReconfigure.Location = new System.Drawing.Point(219, 143);
this.btnReconfigure.Name = "btnReconfigure";
this.btnReconfigure.Size = new System.Drawing.Size(75, 23);
this.btnReconfigure.TabIndex = 1;
this.btnReconfigure.Text = "Reconfigure";
this.btnReconfigure.UseVisualStyleBackColor = true;
this.btnReconfigure.Click += new System.EventHandler(this.btnReconfigure_Click);
//
// btnOK
//
this.btnOK.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnOK.Location = new System.Drawing.Point(300, 143);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(75, 23);
this.btnOK.TabIndex = 0;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// ServiceStatus
//
this.AcceptButton = this.btnOK;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.CancelButton = this.btnOK;
this.ClientSize = new System.Drawing.Size(391, 186);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.btnReconfigure);
this.Controls.Add(this.txtStatus);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.picBanner);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "ServiceStatus";
this.Text = "Icinga 2 Service Status";
((System.ComponentModel.ISupportInitialize)(this.picBanner)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.PictureBox picBanner;
private System.Windows.Forms.Label lblStatus;
private System.Windows.Forms.TextBox txtStatus;
private System.Windows.Forms.Button btnReconfigure;
private System.Windows.Forms.Button btnOK;
}
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
namespace Icinga
{
public partial class ServiceStatus : Form
{
public ServiceStatus()
{
InitializeComponent();
try {
ServiceController sc = new ServiceController("icinga2");
txtStatus.Text = sc.Status.ToString();
} catch (InvalidOperationException) {
txtStatus.Text = "Not Available";
}
}
private void btnReconfigure_Click(object sender, EventArgs e)
{
new SetupWizard().ShowDialog(this);
}
private void btnOK_Click(object sender, EventArgs e)
{
Close();
}
}
}

View File

@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<assembly alias="System.Drawing" name="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
AAABAAEAICAQAAAABADoAgAAFgAAACgAAAAgAAAAQAAAAAEABAAAAAAAAAIAAAAAAAAAAAAAEAAAABAA
AAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgACAgAAAgICAAMDAwAAAAP8AAP8AAAD//wD/AAAA/wD/AP//
AAD///8AAAAAAAAAAAAAAAAAAAAAAAAHAAAAAAAAAAAAAAAAcAAAcAB3AAAAAAAAAAAAAAcABwB///cA
AAAAAAAAAAAAcAAA////AAAAAAAAAAAAAAAAAP///3AAAAAAAAAAAAAAAAD///9wAAAAAAAAAAAAAAAA
j//4AAAAB3AAAAAAAAAAAAj/jwAAAA/4AAAAAAAAAAAAAAiAAAAP9wAAAAAAAAAAAAAH9wAAf3AAAAAA
AAAAAAAAAH93d/cAAAAAAAAAAAAAAAB////3AAAAAAAAAAAAAAAA/////wAAAAAAAAAAAAAAB/////9w
AAAAAAAAAAAAAAf/////gAAAAAAAAAAAAAAH//////h3AAAAAAAAAAAAB/////+I//h3eHAAAAAAAAD/
////AAB3j//3AAAAAAB4////9wAAAAf/+AAAAAf/+AB4iPAAAAAAj/cAAAAH//AAAAD3AAAAAAcAAAAA
B/+AAAAAjwAAAAAAAAAAAAB3AAAAAA9wAAAAAAAAAAAAAAAAAAAIh3AAAAAAAAAAAAAAAAAAD//wAAAA
AAAAAAAAAAAAAH//9wAAAAAAAAAAAAAAAAB///cAAAAAAAcAAAAAAAAACP+AAAAAAHAAcAAAAAAAAAB3
AAAAAAcAAAcAAAAAAAAAAAAAAABwAAAAAAAAAAAAAAAAAAAAAAD/////4AAAB8AAAAOAAAABgAAAAYAA
AAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAYAA
AAGAAAABgAAAAYAAAAGAAAABgAAAAYAAAAGAAAABgAAAAcAAAAPgAAAH/////w==
</value>
</data>
</root>

View File

@ -8,7 +8,6 @@ using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading; using System.Threading;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using Microsoft.Win32;
using System.IO.Compression; using System.IO.Compression;
using System.Diagnostics; using System.Diagnostics;
using System.ServiceProcess; using System.ServiceProcess;
@ -27,30 +26,11 @@ namespace Icinga
txtInstanceName.Text = Icinga2InstanceName; txtInstanceName.Text = Icinga2InstanceName;
} }
private void FatalError(string message)
{
MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
private void Warning(string message) private void Warning(string message)
{ {
MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning); MessageBox.Show(this, message, Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
} }
private string Icinga2InstallDir
{
get
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Icinga Development Team\\ICINGA2");
if (rk == null)
return "";
return (string)rk.GetValue("");
}
}
private string Icinga2InstanceName private string Icinga2InstanceName
{ {
get get
@ -85,7 +65,7 @@ namespace Icinga
{ {
FileStream fp = null; FileStream fp = null;
try { try {
fp = File.Open(Icinga2InstallDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create); fp = File.Open(Program.Icinga2InstallDir + String.Format("\\etc\\icinga2\\features-enabled\\{0}.conf", feature), FileMode.Create);
using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) { using (StreamWriter sw = new StreamWriter(fp, Encoding.ASCII)) {
fp = null; fp = null;
sw.Write(String.Format("include \"../features-available/{0}.conf\"\n", feature)); sw.Write(String.Format("include \"../features-available/{0}.conf\"\n", feature));
@ -164,12 +144,12 @@ namespace Icinga
{ {
SetRetrievalStatus(25); SetRetrievalStatus(25);
string pathPrefix = Icinga2InstallDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text; string pathPrefix = Program.Icinga2InstallDir + "\\etc\\icinga2\\pki\\" + txtInstanceName.Text;
string output; string output;
if (!File.Exists(pathPrefix + ".crt")) { if (!File.Exists(pathPrefix + ".crt")) {
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"", "pki new-cert --cn \"" + txtInstanceName.Text + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\"",
out output)) { out output)) {
ShowErrorText(output); ShowErrorText(output);
@ -181,7 +161,7 @@ namespace Icinga
_TrustedFile = Path.GetTempFileName(); _TrustedFile = Path.GetTempFileName();
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"", "pki save-cert --host \"" + host + "\" --port \"" + port + "\" --key \"" + pathPrefix + ".key\" --cert \"" + pathPrefix + ".crt\" --trustedcert \"" + _TrustedFile + "\"",
out output)) { out output)) {
ShowErrorText(output); ShowErrorText(output);
@ -233,7 +213,7 @@ namespace Icinga
args += " --cn " + txtInstanceName.Text; args += " --cn " + txtInstanceName.Text;
args += " --zone " + txtInstanceName.Text; args += " --zone " + txtInstanceName.Text;
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"node setup" + args, "node setup" + args,
out output)) { out output)) {
ShowErrorText(output); ShowErrorText(output);
@ -241,7 +221,7 @@ namespace Icinga
} }
SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory..."); SetConfigureStatus(50, "Setting ACLs for the Icinga 2 directory...");
DirectoryInfo di = new DirectoryInfo(Icinga2InstallDir); DirectoryInfo di = new DirectoryInfo(Program.Icinga2InstallDir);
DirectorySecurity ds = di.GetAccessControl(); DirectorySecurity ds = di.GetAccessControl();
FileSystemAccessRule rule = new FileSystemAccessRule("NT AUTHORITY\\NetworkService", FileSystemAccessRule rule = new FileSystemAccessRule("NT AUTHORITY\\NetworkService",
FileSystemRights.Modify, FileSystemRights.Modify,
@ -251,18 +231,18 @@ namespace Icinga
SetConfigureStatus(75, "Installing the Icinga 2 service..."); SetConfigureStatus(75, "Installing the Icinga 2 service...");
RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"--scm-uninstall", "--scm-uninstall",
out output); out output);
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"daemon --validate", "daemon --validate",
out output)) { out output)) {
ShowErrorText(output); ShowErrorText(output);
return; return;
} }
if (!RunProcess(Icinga2InstallDir + "\\sbin\\icinga2.exe", if (!RunProcess(Program.Icinga2InstallDir + "\\sbin\\icinga2.exe",
"--scm-install daemon", "--scm-install daemon",
out output)) { out output)) {
ShowErrorText(output); ShowErrorText(output);
@ -297,10 +277,7 @@ namespace Icinga
private void AgentWizard_Shown(object sender, EventArgs e) private void AgentWizard_Shown(object sender, EventArgs e)
{ {
string installDir = Icinga2InstallDir; string installDir = Program.Icinga2InstallDir;
if (installDir == "")
FatalError("Icinga 2 does not seem to be installed properly.");
/* TODO: This is something the NSIS installer should do */ /* TODO: This is something the NSIS installer should do */
Directory.CreateDirectory(installDir + "\\etc\\icinga2\\pki"); Directory.CreateDirectory(installDir + "\\etc\\icinga2\\pki");