Updated Branches: refs/heads/4.3 ecfd48693 -> b1c8efb0e
code to install and uninstall agent as windows service Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/b1c8efb0 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/b1c8efb0 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/b1c8efb0 Branch: refs/heads/4.3 Commit: b1c8efb0eaa11d5b17a568935daa0afbfbcaaea6 Parents: ecfd486 Author: Anshul Gangwar <[email protected]> Authored: Wed Nov 13 08:58:10 2013 -0800 Committer: Rajesh Battala <[email protected]> Committed: Tue Dec 3 12:01:23 2013 +0530 ---------------------------------------------------------------------- .../ServerResource/AgentShell/AgentShell.csproj | 9 +- .../DotNet/ServerResource/AgentShell/Program.cs | 174 +++++++++++++++++-- .../AgentShell/ProjectInstaller.Designer.cs | 60 +++++++ .../AgentShell/ProjectInstaller.cs | 19 ++ 4 files changed, 250 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b1c8efb0/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj index 39fef16..a4c6b1f 100644 --- a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj @@ -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> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b1c8efb0/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs index 1066370..e843500 100644 --- a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs @@ -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,24 +37,173 @@ namespace CloudStack.Plugin.AgentShell /// </summary> static void Main(params string[] args) { - string arg1 = string.Empty; + 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(); + } + } + } - if (args.Length > 0) + private static bool IsInstalled() + { + using (ServiceController controller = + new ServiceController(serviceName)) { - arg1 = args[0]; - logger.DebugFormat("CloudStack ServerResource arg is ", arg1); + try + { + ServiceControllerStatus status = controller.Status; + } + catch + { + return false; + } + return true; } + } - if (string.Compare(arg1, "--console", true) == 0) + private static bool IsRunning() + { + using (ServiceController controller = + new ServiceController(serviceName)) { - logger.InfoFormat("CloudStack ServerResource running as console app"); - new AgentService().RunConsole(args); + if (!IsInstalled()) return false; + return (controller.Status == ServiceControllerStatus.Running); } - else + } + + 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 { - logger.InfoFormat("CloudStack ServerResource running as Windows Service"); - ServiceBase[] ServicesToRun = new ServiceBase[] { new AgentService() }; - ServiceBase.Run(ServicesToRun); + 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; + } } } } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b1c8efb0/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs new file mode 100644 index 0000000..9b9dbb6 --- /dev/null +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs @@ -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; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/b1c8efb0/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.cs ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.cs b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.cs new file mode 100644 index 0000000..c3a225f --- /dev/null +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.cs @@ -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(); + } + } +}
