public static ServiceInfo GetEasyService(string nameOrIndex) { var infoList = GetEasyServices(); if (int.TryParse(nameOrIndex, out int i)) { if (i < 1 || i > infoList.Count) { Libs.Abort($"Index \"{i}\" out of boundary 1~\"{infoList.Count}\""); } return(infoList[i - 1]); } foreach (var info in infoList) { if (info.Sc.ServiceName == nameOrIndex) { return(info); } } Libs.Abort($"EasyService \"{nameOrIndex}\" not exists"); return(null); }
public static void InstallService(Conf conf) { var depend = conf.Dependencies.AddUniq(Consts.NECESSARY_DEPENDENCY, '/'); // add "[svc]" to DisplayName to avoid "ServiceName == DisplayName" (which cases error in win7) var scArgs = $"create \"{conf.ServiceName}\" binPath= \"{Libs.BinPath}\" start= auto " + $"DisplayName= \"[svc] {conf.DisplayName}\" depend= \"{depend}\""; if (conf.User.Length > 0) { var obj = (conf.Domain.Length > 0) ? $"{conf.Domain}\\{conf.User}" : conf.User; scArgs = $"{scArgs} obj= \"{obj}\" password= \"{conf.Password}\""; } Libs.Exec("sc", scArgs); var sc = GetServiceController(conf.ServiceName); if (sc == null) { Libs.Abort($"Failed to install Service \"{conf.ServiceName}\""); } var msg = $"Installed Service \"{conf.ServiceName}\""; Console.WriteLine(msg); AddLog("INFO", msg, false); AddLog("INFO", ""); SetSvcDescription(conf); sc.StartService(); }
private static void SetSvcDescription(Conf conf) { var description = $"{conf.Description} @<{Libs.GetCwd()}>"; var scArgs = $"description \"{conf.ServiceName}\" \"{description}\""; string err; Libs.Exec("sc", scArgs); try { var mngObj = GetServiceManagementObjectByName(conf.ServiceName); if (mngObj != null && mngObj["Description"].ToString() == description) { return; } err = "unknown reason"; } catch (Exception ex) { err = ex.ToString(); } err = $"Failed to set description for Service \"{conf.ServiceName}\": {err}"; AddLog("ERROR", err); Libs.Abort($"{err}\r\nPlease run `svc remove` to remove the Service"); }
public void Cd() { if (!Directory.Exists(ConfDir)) { Libs.Abort($"Service directory \"{ConfDir}\" not exists"); } Libs.SetCwd(ConfDir); }
private static void CreateProject(string arg1) { var err = Conf.CheckServiceName(arg1); if (err != null) { Libs.Abort($"[svc.critical] Bad project name `{arg1}`: {err}"); } Libs.CopyDir($"{Libs.BinDir}..\\samples\\csharp-version", arg1, ".log"); Libs.ReplaceStringInFile($"{arg1}\\svc.conf", "easy-service", arg1); Console.WriteLine($"Create an Easy-Service project in {arg1}"); }
private static void ManageOneBySvrIdentity(string op, string arg1) { var info = SvcUtils.GetEasyService(arg1); var sc = info.Sc; info.Cd(); if (op == "log") { Libs.Abort("`log` command has been deprecated since v1.0.10"); } // op = start|stop|restart|remove sc.Operate(op); }
private static void LoggingSvc(Conf conf, string status) { if (status != "running") { Libs.Abort($"Service \"{conf.ServiceName}\" is not running"); } if (conf.OutFileDir == null) { Libs.Abort("Error: OutFileDir must not be $NULL"); } Libs.NewThread(() => Libs.MonitorFile(conf.LastLineFile)); Console.ReadLine(); }
public static void RemoveService(this ServiceController sc) { var name = sc.ServiceName; sc.StopService(); Libs.Exec("sc", $"delete \"{name}\""); if (GetServiceController(name) != null) { Libs.Abort($"Failed to remove Service \"{name}\""); } var msg = $"Removed Service \"{name}\""; AddLog("INFO", ""); AddLog("INFO", msg); Console.WriteLine(msg); }
private static List <ServiceInfo> GetEasyServices() { Console.Write("Getting all EasyServices, please wait..."); Console.Out.Flush(); var hasCircle = false; var infoList = new List <ServiceInfo>(); foreach (var sc in ServiceController.GetServices()) { var name = sc.ServiceName; var mngObj = GetServiceManagementObjectByName(name); var path = mngObj["PathName"].ToString(); if (path != Libs.BinPath) { continue; } var description = mngObj["Description"].ToString(); var confDir = GetCwdInDescription(description); var info = new ServiceInfo(sc, confDir); if (!Libs.InsertInto(infoList, info, ServiceInfo.IsDepend)) { Console.WriteLine("\r\n[ERROR] Circle dependencies detected"); infoList.Add(info); hasCircle = true; } } if (!hasCircle) { Console.Write($"\r{"".PadRight(60)}\r"); Console.Out.Flush(); } if (infoList.Count == 0) { Libs.Abort($"No EasyService found"); } return(infoList); }
private static void ManageOneByConfFile(string op) { var conf = new Conf(); var serviceName = conf.ServiceName; var sc = SvcUtils.GetServiceController(serviceName); var status = (sc == null) ? "not installed" : sc.Status.ToString().ToLower(); if (op == "check" || op == "status") { conf.ShowConfig(); Console.WriteLine($"\r\nService status: {status}"); return; } if (op == "install") { if (sc != null) { Libs.Abort($"Service \"{serviceName}\" is already installed!"); } SvcUtils.InstallService(conf); return; } if (op == "log") { LoggingSvc(conf, status); return; } // op: start|stop|restart|remove if (sc == null) { Libs.Abort($"Service \"{serviceName}\" is not installed!"); } sc.Operate(op); }
public static int Main(string[] args) { if (args.Length == 0) { SimpleService.RunService(); return(0); } var op = args[0]; var opr = $"|{op}|"; var argc = args.Length - 1; var arg1 = argc == 1 ? args[1] : null; if (op == "version" || op == "--version" || op == "-v") { if (argc != 0) { Libs.Abort(USAGE); } Console.WriteLine(VERSION); return(0); } if (op == "create") { if (argc != 1) { Libs.Abort(USAGE); } CreateProject(arg1); return(0); } if (op == "list" || op == "ls") { if (argc != 0) { Libs.Abort(USAGE); } SvcUtils.ListAllEasyServices(); return(0); } var commands = "|check|status|test-worker|install|stop|start|remove|restart|log|"; if (!commands.Contains(opr) || argc > 1) { Libs.Abort(USAGE); } if (arg1 != null && (arg1.Contains('/') || arg1.Contains('\\'))) { if (!Directory.Exists(arg1)) { Libs.Abort($"Directory \"{arg1}\" not exists"); } Libs.SetCwd(arg1); arg1 = null; } if (op == "test-worker") { if (arg1 != null && arg1 != "--popup") { Libs.Abort($"Directory argument \"{arg1}\" should contain '/' or '\\'"); } TestWorker(arg1 == "--popup"); return(0); } if ("|check|status|install|".Contains(opr) && arg1 != null) { Libs.Abort($"Directory argument \"{arg1}\" should contain '/' or '\\'"); } if ("|restart|log|".Contains(opr) && arg1 == "all") { Libs.Abort(USAGE); } if (arg1 == null) { ManageOneByConfFile(op); } else if (arg1 != "all") { ManageOneBySvrIdentity(op, arg1); } else { SvcUtils.ManageAll(op); } return(0); }