From 641f85cf6f8f714ce324c55a0da49582c67c9b6d Mon Sep 17 00:00:00 2001 From: Devdeep Singh Date: Thu, 9 Jan 2014 20:42:47 +0530 Subject: [PATCH] CLOUDSTACK-5657: Run the service as an account (hyper-v admin on host) so that migration can work across hosts. --- .../ServerResource/AgentShell/Program.cs | 87 +++++++++++++++++-- .../AgentShell/ProjectInstaller.Designer.cs | 31 +++++-- 2 files changed, 102 insertions(+), 16 deletions(-) diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs index 5e58211d60f..7545644fb70 100644 --- a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/Program.cs @@ -29,7 +29,13 @@ namespace CloudStack.Plugin.AgentShell static class Program { private static ILog logger = LogManager.GetLogger(typeof(Program)); - public static string serviceName = "CloudStack Hyper-V Agent"; + public const string serviceName = "CloudStack Hyper-V Agent"; + private static string option = null; + private static string user = null; + private static string password = null; + private const string install = "--install"; + private const string uninstall = "--uninstall"; + private const string console = "--console"; /// /// Application entry point allows service to run in console application or as a Windows service. @@ -43,22 +49,21 @@ namespace CloudStack.Plugin.AgentShell ServiceBase[] ServicesToRun = new ServiceBase[] { new AgentService() }; ServiceBase.Run(ServicesToRun); } - else if (args.Length == 1) + else if (ParseArguments(args)) { - logger.DebugFormat(serviceName + " arg is ", args[0]); - switch (args[0]) + switch (option) { - case "--install": + case install: logger.InfoFormat("Installing and running " + serviceName); InstallService(); StartService(); break; - case "--uninstall": + case uninstall: logger.InfoFormat("Stopping and uninstalling " + serviceName); StopService(); UninstallService(); break; - case "--console": + case console: logger.InfoFormat(serviceName + " is running as console application"); new AgentService().RunConsole(args); break; @@ -66,6 +71,74 @@ namespace CloudStack.Plugin.AgentShell throw new NotImplementedException(); } } + else + { + string argumentExample = "--(install/uninstall/console) [-u ] [-p ].\n" + + " For example:\n " + + " --install -u domain1\\user1 -p mypwd\n"+ + " --uninstall"; + logger.Error("Invalid arguments passed for installing\\uninstalling the service. \n" + argumentExample); + } + } + + static private Boolean ParseArguments(string[] args) + { + logger.DebugFormat(serviceName + " arg is ", args[0]); + int argIndex = 0; + while (argIndex < args.Length) + { + switch (args[argIndex]) + { + case install: + case uninstall: + case console: + option = args[argIndex]; + argIndex++; + break; + case "-u": + user = args[argIndex+1]; + argIndex+=2; + break; + case "-p": + password = args[argIndex+1]; + argIndex+=2; + break; + default: + argIndex++; + // Unrecognised argument. Do nothing; + break; + } + } + + // Validate arguments + return ValidateArguments(); + } + + private static Boolean ValidateArguments() + { + Boolean argsValid = false; + switch (option) + { + case uninstall: + case install: + case console: + argsValid = true; + break; + default: + break; + } + + return argsValid; + } + + public static string GetPassword() + { + return password; + } + + public static string GetUser() + { + return user; } private static bool IsInstalled() diff --git a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs index d858192f63a..2c6ffe24462 100644 --- a/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs +++ b/plugins/hypervisors/hyperv/DotNet/ServerResource/AgentShell/ProjectInstaller.Designer.cs @@ -46,22 +46,35 @@ namespace CloudStack.Plugin.AgentShell { this.serviceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller(); this.serviceInstaller = new System.ServiceProcess.ServiceInstaller(); - // + // // serviceProcessInstaller - // - this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; - this.serviceProcessInstaller.Password = null; - this.serviceProcessInstaller.Username = null; - // + // + string user = Program.GetUser(); + string password = Program.GetPassword(); + + if (string.IsNullOrEmpty(user)) + { + this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem; + this.serviceProcessInstaller.Password = null; + this.serviceProcessInstaller.Username = null; + } + else + { + this.serviceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.User; + this.serviceProcessInstaller.Password = password; + this.serviceProcessInstaller.Username = user; + } + + // // serviceInstaller - // + // this.serviceInstaller.Description = "CloudStack agent for managing a hyper-v host"; this.serviceInstaller.DisplayName = Program.serviceName; this.serviceInstaller.ServiceName = Program.serviceName; this.serviceInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic; - // + // // ProjectInstaller - // + // this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.serviceProcessInstaller, this.serviceInstaller});