CLOUDSTACK-5657: Run the service as an account (hyper-v admin on host) so

that migration can work across hosts.
This commit is contained in:
Devdeep Singh 2014-01-09 20:42:47 +05:30
parent db2b025608
commit 641f85cf6f
2 changed files with 102 additions and 16 deletions

View File

@ -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";
/// <summary>
/// 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 <hyper-v admin user>] [-p <hyper-v admin password>].\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()

View File

@ -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});