|
21 | 21 | using System.ServiceProcess; |
22 | 22 | using System.Text; |
23 | 23 | using System.Threading.Tasks; |
| 24 | +using System.Configuration.Install; |
| 25 | +using System.Collections; |
24 | 26 |
|
25 | 27 | namespace CloudStack.Plugin.AgentShell |
26 | 28 | { |
27 | 29 | static class Program |
28 | 30 | { |
29 | 31 | private static ILog logger = LogManager.GetLogger(typeof(Program)); |
| 32 | + private static string serviceName = "CloudStack ServerResource"; |
30 | 33 |
|
31 | 34 | /// <summary> |
32 | 35 | /// Application entry point allows service to run in console application or as a Windows service. |
33 | 36 | /// Add '--console' to the commandline for the former, the latter is the default. |
34 | 37 | /// </summary> |
35 | 38 | static void Main(params string[] args) |
36 | 39 | { |
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 | + } |
38 | 70 |
|
39 | | - if (args.Length > 0) |
| 71 | + private static bool IsInstalled() |
| 72 | + { |
| 73 | + using (ServiceController controller = |
| 74 | + new ServiceController(serviceName)) |
40 | 75 | { |
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; |
43 | 85 | } |
| 86 | + } |
44 | 87 |
|
45 | | - if (string.Compare(arg1, "--console", true) == 0) |
| 88 | + private static bool IsRunning() |
| 89 | + { |
| 90 | + using (ServiceController controller = |
| 91 | + new ServiceController(serviceName)) |
46 | 92 | { |
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); |
49 | 95 | } |
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 |
51 | 111 | { |
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 | + } |
55 | 207 | } |
56 | 208 | } |
57 | 209 | } |
|
0 commit comments