private static bool AutoInstallService(string path) { if (!File.Exists(path)) { return(false); } var root = PackageManagerSettings.CoAppRootDirectory; var coappBinDirectory = Path.Combine(root, "bin"); if (!Directory.Exists(coappBinDirectory)) { Directory.CreateDirectory(coappBinDirectory); } var canonicalServiceExePath = Path.Combine(coappBinDirectory, "coapp.service.exe"); if (Symlink.IsSymlink(path)) { // we found a symlink, if (!File.Exists(Symlink.GetActualPath(path))) { // it is invalid anyway. trash it, try again. Symlink.DeleteSymlink(path); } return(false); } try { Symlink.MakeFileLink(canonicalServiceExePath, path); // hey we found one where it's supposed to be! var processStartInfo = new ProcessStartInfo(canonicalServiceExePath) { Arguments = "--start", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, }; var process = Process.Start(processStartInfo); process.WaitForExit(); // after it exits, lets see if we've got an installed service if (EngineServiceManager.IsServiceInstalled) { // YAY!. We're outta here! EngineServiceManager.TryToStartService(); return(true); } } catch { // hmm. not working... } return(false); }
public static int AutoInstall() { if (EngineServiceManager.IsServiceInstalled) { EngineServiceManager.TryToStartService(); return(0); } var serviceExe = EngineServiceManager.CoAppServiceExecutablePath; if (serviceExe != null) { if (AutoInstallService(serviceExe)) { return(0); } } return(1); }
private int main(IEnumerable <string> args) { try { Environment.CurrentDirectory = Environment.GetEnvironmentVariable("tmp"); var options = args.Switches(); var parameters = args.Parameters(); Console.CancelKeyPress += (x, y) => { Console.WriteLine("Stopping CoAppService."); Engine.RequestStop(); }; #region Parse Options foreach (var arg in from arg in options.Keys select arg) { var argumentParameters = options[arg]; switch (arg) { case "load-config": break; case "auto-install": RequiresAdmin("--auto-install"); Environment.Exit(CoAppService.AutoInstall()); break; case "start": _start = true; _install = true; break; case "restart": _stop = true; _start = true; _install = true; break; case "stop": _stop = true; break; case "install": _install = true; break; case "uninstall": _stop = true; _uninstall = true; break; case "username": UseUserAccount = true; _username = argumentParameters.LastOrDefault(); break; case "password": _password = argumentParameters.LastOrDefault(); break; case "status": _status = true; break; case "interactive": if (EngineServiceManager.IsServiceRunning) { Console.WriteLine("Shutting down running assembly."); EngineServiceManager.TryToStopService(); while (EngineServiceManager.IsServiceRunning) { Console.Write("."); Thread.Sleep(100); } } foreach (var proc in Process.GetProcessesByName("coapp.service").Where(each => each.Id != Process.GetCurrentProcess().Id).ToArray()) { try { Console.WriteLine("Killing Process... {0}", proc.Id); proc.Kill(); } catch { } } _interactive = true; break; case "help": return(Help()); default: Fail("Unrecognized switch [--{0}]", arg); return(Help()); } } #endregion Logo(); if (_interactive) { RequiresAdmin("--interactive"); if (EngineServiceManager.IsServiceRunning) { throw new ConsoleException( "The CoApp Service can not be running.\r\nYou must stop it with --stop before using the service interactively."); } Console.WriteLine("Launching CoApp Service interactively.\r\nUse ctrl-c to stop."); var task = Engine.Start(true); Console.WriteLine("[CoApp Interactive -- Press escape to stop.]"); // wait for user to cancel task, or when it's actually closed while (!task.Wait(1000)) { Console.Write("."); while (Console.KeyAvailable) { if (Console.ReadKey(true).Key == ConsoleKey.Escape) { Engine.RequestStop(); } } } return(0); } if (_stop) { RequiresAdmin("--stop"); Console.Write("Stopping service:"); if (EngineServiceManager.IsServiceInstalled) { EngineServiceManager.TryToStopService(); } while (EngineServiceManager.IsServiceRunning) { Console.Write("."); Thread.Sleep(100); } Console.WriteLine(" [Stopped]"); } if (_uninstall) { RequiresAdmin("--uninstall"); CoAppService.Uninstall(); return(0); } if (_install) { RequiresAdmin("--install"); CoAppService.Install(_username, _password); } if (_start) { RequiresAdmin("--start"); if (EngineServiceManager.IsServiceInstalled) { Console.Write("Starting service:"); EngineServiceManager.TryToStartService(); while (!EngineServiceManager.IsServiceRunning) { Console.Write("."); Thread.Sleep(100); } Console.WriteLine(" [Started]"); } else { throw new ConsoleException("CoApp.Service is not installed."); } } if (!options.Any() && EngineServiceManager.IsServiceInstalled && parameters.FirstOrDefault() == null) { // this lets us run the service ServiceBase.Run(new CoAppService()); return(0); } if (_status) { Console.WriteLine("Service installed: {0}", EngineServiceManager.IsServiceInstalled); Console.WriteLine("Service running: {0}", EngineServiceManager.IsServiceRunning); return(0); } if (!options.Any()) { throw new ConsoleException("Missing CoApp.Service command. Use --help for information"); } } catch (ConsoleException e) { return(Fail(e.Message)); } catch (Exception ex) { return(Fail("{0}\r\n{1}", ex.Message, ex.StackTrace)); } return(0); }