code to install and uninstall agent as windows service

This commit is contained in:
Anshul Gangwar 2013-11-13 08:58:10 -08:00 committed by Rajesh Battala
parent ecfd486938
commit b1c8efb0ea
4 changed files with 253 additions and 15 deletions

View File

@ -70,6 +70,7 @@
<HintPath>..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Net.Http" />
@ -101,6 +102,12 @@
</Compile>
<Compile Include="AgentShellException.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProjectInstaller.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="ProjectInstaller.Designer.cs">
<DependentUpon>ProjectInstaller.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="AgentSettings.Designer.cs">
<AutoGen>True</AutoGen>
@ -137,4 +144,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>

View File

@ -21,12 +21,15 @@ using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Configuration.Install;
using System.Collections;
namespace CloudStack.Plugin.AgentShell
{
static class Program
{
private static ILog logger = LogManager.GetLogger(typeof(Program));
private static string serviceName = "CloudStack ServerResource";
/// <summary>
/// Application entry point allows service to run in console application or as a Windows service.
@ -34,25 +37,174 @@ namespace CloudStack.Plugin.AgentShell
/// </summary>
static void Main(params string[] args)
{
string arg1 = string.Empty;
if (args.Length > 0)
{
arg1 = args[0];
logger.DebugFormat("CloudStack ServerResource arg is ", arg1);
}
if (string.Compare(arg1, "--console", true) == 0)
{
logger.InfoFormat("CloudStack ServerResource running as console app");
new AgentService().RunConsole(args);
}
else
if (args.Length == 0)
{
logger.InfoFormat("CloudStack ServerResource running as Windows Service");
ServiceBase[] ServicesToRun = new ServiceBase[] { new AgentService() };
ServiceBase.Run(ServicesToRun);
}
else if (args.Length == 1)
{
logger.DebugFormat("CloudStack ServerResource arg is ", args[0]);
switch (args[0])
{
case "--install":
logger.InfoFormat("Installing and running CloudStack ServerResource ");
InstallService();
StartService();
break;
case "--uninstall":
logger.InfoFormat("stopping and uninstalling CloudStack ServerResource ");
StopService();
UninstallService();
break;
case "--console":
logger.InfoFormat("CloudStack ServerResource running as console app");
new AgentService().RunConsole(args);
break;
default:
throw new NotImplementedException();
}
}
}
private static bool IsInstalled()
{
using (ServiceController controller =
new ServiceController(serviceName))
{
try
{
ServiceControllerStatus status = controller.Status;
}
catch
{
return false;
}
return true;
}
}
private static bool IsRunning()
{
using (ServiceController controller =
new ServiceController(serviceName))
{
if (!IsInstalled()) return false;
return (controller.Status == ServiceControllerStatus.Running);
}
}
private static AssemblyInstaller GetInstaller()
{
AssemblyInstaller installer = new AssemblyInstaller(
typeof(Program).Assembly, null);
installer.UseNewContext = true;
return installer;
}
private static void InstallService()
{
if (IsInstalled()) return;
try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Install(state);
installer.Commit(state);
}
catch
{
try
{
installer.Rollback(state);
}
catch { }
throw;
}
}
}
catch (Exception ex)
{
logger.ErrorFormat(" Error occured in installing service " + ex.Message);
throw;
}
}
private static void UninstallService()
{
if (!IsInstalled()) return;
try
{
using (AssemblyInstaller installer = GetInstaller())
{
IDictionary state = new Hashtable();
try
{
installer.Uninstall(state);
}
catch
{
throw;
}
}
}
catch (Exception ex)
{
logger.ErrorFormat(" Error occured in uninstalling service " + ex.Message);
throw;
}
}
private static void StartService()
{
if (!IsInstalled()) return;
using (ServiceController controller =
new ServiceController(serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Running)
{
controller.Start();
controller.WaitForStatus(ServiceControllerStatus.Running,
TimeSpan.FromSeconds(10));
}
}
catch (Exception ex)
{
logger.ErrorFormat(" Error occured in starting service " + ex.Message);
throw;
}
}
}
private static void StopService()
{
if (!IsInstalled()) return;
using (ServiceController controller =
new ServiceController(serviceName))
{
try
{
if (controller.Status != ServiceControllerStatus.Stopped)
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped,
TimeSpan.FromSeconds(10));
}
}
catch (Exception ex)
{
logger.ErrorFormat(" Error occured in stopping service " + ex.Message);
throw;
}
}
}
}
}

View File

@ -0,0 +1,60 @@
namespace CloudStack.Plugin.AgentShell
{
partial class ProjectInstaller
{
/// <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 Component 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()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "CloudStack Agent";
this.serviceInstaller1.DisplayName = "CloudStack ServerResource";
this.serviceInstaller1.ServiceName = "CloudStack ServerResource";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Threading.Tasks;
namespace CloudStack.Plugin.AgentShell
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
}
}