Skip to content

Commit b1c8efb

Browse files
anshulgangwarrajesh-battala
authored andcommitted
code to install and uninstall agent as windows service
1 parent ecfd486 commit b1c8efb

4 files changed

Lines changed: 250 additions & 12 deletions

File tree

‎plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/AgentShell.csproj‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<HintPath>..\packages\NSubstitute.1.6.1.0\lib\NET40\NSubstitute.dll</HintPath>
7171
</Reference>
7272
<Reference Include="System" />
73+
<Reference Include="System.Configuration.Install" />
7374
<Reference Include="System.Core" />
7475
<Reference Include="System.Management" />
7576
<Reference Include="System.Net.Http" />
@@ -101,6 +102,12 @@
101102
</Compile>
102103
<Compile Include="AgentShellException.cs" />
103104
<Compile Include="Program.cs" />
105+
<Compile Include="ProjectInstaller.cs">
106+
<SubType>Component</SubType>
107+
</Compile>
108+
<Compile Include="ProjectInstaller.Designer.cs">
109+
<DependentUpon>ProjectInstaller.cs</DependentUpon>
110+
</Compile>
104111
<Compile Include="Properties\AssemblyInfo.cs" />
105112
<Compile Include="AgentSettings.Designer.cs">
106113
<AutoGen>True</AutoGen>
@@ -137,4 +144,4 @@
137144
<Target Name="AfterBuild">
138145
</Target>
139146
-->
140-
</Project>
147+
</Project>

‎plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs‎

Lines changed: 163 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,189 @@
2121
using System.ServiceProcess;
2222
using System.Text;
2323
using System.Threading.Tasks;
24+
using System.Configuration.Install;
25+
using System.Collections;
2426

2527
namespace CloudStack.Plugin.AgentShell
2628
{
2729
static class Program
2830
{
2931
private static ILog logger = LogManager.GetLogger(typeof(Program));
32+
private static string serviceName = "CloudStack ServerResource";
3033

3134
/// <summary>
3235
/// Application entry point allows service to run in console application or as a Windows service.
3336
/// Add '--console' to the commandline for the former, the latter is the default.
3437
/// </summary>
3538
static void Main(params string[] args)
3639
{
37-
string arg1 = string.Empty;
40+
if (args.Length == 0)
41+
{
42+
logger.InfoFormat("CloudStack ServerResource running as Windows Service");
43+
ServiceBase[] ServicesToRun = new ServiceBase[] { new AgentService() };
44+
ServiceBase.Run(ServicesToRun);
45+
}
46+
else if (args.Length == 1)
47+
{
48+
logger.DebugFormat("CloudStack ServerResource arg is ", args[0]);
49+
switch (args[0])
50+
{
51+
case "--install":
52+
logger.InfoFormat("Installing and running CloudStack ServerResource ");
53+
InstallService();
54+
StartService();
55+
break;
56+
case "--uninstall":
57+
logger.InfoFormat("stopping and uninstalling CloudStack ServerResource ");
58+
StopService();
59+
UninstallService();
60+
break;
61+
case "--console":
62+
logger.InfoFormat("CloudStack ServerResource running as console app");
63+
new AgentService().RunConsole(args);
64+
break;
65+
default:
66+
throw new NotImplementedException();
67+
}
68+
}
69+
}
3870

39-
if (args.Length > 0)
71+
private static bool IsInstalled()
72+
{
73+
using (ServiceController controller =
74+
new ServiceController(serviceName))
4075
{
41-
arg1 = args[0];
42-
logger.DebugFormat("CloudStack ServerResource arg is ", arg1);
76+
try
77+
{
78+
ServiceControllerStatus status = controller.Status;
79+
}
80+
catch
81+
{
82+
return false;
83+
}
84+
return true;
4385
}
86+
}
4487

45-
if (string.Compare(arg1, "--console", true) == 0)
88+
private static bool IsRunning()
89+
{
90+
using (ServiceController controller =
91+
new ServiceController(serviceName))
4692
{
47-
logger.InfoFormat("CloudStack ServerResource running as console app");
48-
new AgentService().RunConsole(args);
93+
if (!IsInstalled()) return false;
94+
return (controller.Status == ServiceControllerStatus.Running);
4995
}
50-
else
96+
}
97+
98+
private static AssemblyInstaller GetInstaller()
99+
{
100+
AssemblyInstaller installer = new AssemblyInstaller(
101+
typeof(Program).Assembly, null);
102+
installer.UseNewContext = true;
103+
return installer;
104+
}
105+
106+
private static void InstallService()
107+
{
108+
if (IsInstalled()) return;
109+
110+
try
51111
{
52-
logger.InfoFormat("CloudStack ServerResource running as Windows Service");
53-
ServiceBase[] ServicesToRun = new ServiceBase[] { new AgentService() };
54-
ServiceBase.Run(ServicesToRun);
112+
using (AssemblyInstaller installer = GetInstaller())
113+
{
114+
IDictionary state = new Hashtable();
115+
try
116+
{
117+
installer.Install(state);
118+
installer.Commit(state);
119+
}
120+
catch
121+
{
122+
try
123+
{
124+
installer.Rollback(state);
125+
}
126+
catch { }
127+
throw;
128+
}
129+
}
130+
}
131+
catch (Exception ex)
132+
{
133+
logger.ErrorFormat(" Error occured in installing service " + ex.Message);
134+
throw;
135+
}
136+
}
137+
138+
private static void UninstallService()
139+
{
140+
if (!IsInstalled()) return;
141+
try
142+
{
143+
using (AssemblyInstaller installer = GetInstaller())
144+
{
145+
IDictionary state = new Hashtable();
146+
try
147+
{
148+
installer.Uninstall(state);
149+
}
150+
catch
151+
{
152+
throw;
153+
}
154+
}
155+
}
156+
catch (Exception ex)
157+
{
158+
logger.ErrorFormat(" Error occured in uninstalling service " + ex.Message);
159+
throw;
160+
}
161+
}
162+
163+
private static void StartService()
164+
{
165+
if (!IsInstalled()) return;
166+
167+
using (ServiceController controller =
168+
new ServiceController(serviceName))
169+
{
170+
try
171+
{
172+
if (controller.Status != ServiceControllerStatus.Running)
173+
{
174+
controller.Start();
175+
controller.WaitForStatus(ServiceControllerStatus.Running,
176+
TimeSpan.FromSeconds(10));
177+
}
178+
}
179+
catch (Exception ex)
180+
{
181+
logger.ErrorFormat(" Error occured in starting service " + ex.Message);
182+
throw;
183+
}
184+
}
185+
}
186+
187+
private static void StopService()
188+
{
189+
if (!IsInstalled()) return;
190+
using (ServiceController controller =
191+
new ServiceController(serviceName))
192+
{
193+
try
194+
{
195+
if (controller.Status != ServiceControllerStatus.Stopped)
196+
{
197+
controller.Stop();
198+
controller.WaitForStatus(ServiceControllerStatus.Stopped,
199+
TimeSpan.FromSeconds(10));
200+
}
201+
}
202+
catch (Exception ex)
203+
{
204+
logger.ErrorFormat(" Error occured in stopping service " + ex.Message);
205+
throw;
206+
}
55207
}
56208
}
57209
}

‎plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs‎

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Configuration.Install;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace CloudStack.Plugin.AgentShell
10+
{
11+
[RunInstaller(true)]
12+
public partial class ProjectInstaller : System.Configuration.Install.Installer
13+
{
14+
public ProjectInstaller()
15+
{
16+
InitializeComponent();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)