private static int CreateServiceHandler(bool user, ParseResult result) { bool systemUnit = !user; var commandOptions = new ArgumentsDictionary(result.CommandResult.Children); if (!Prerequisite.RequiredOption(commandOptions, "name", out string unitName) || (systemUnit && !Prerequisite.RunningAsRoot())) { return(1); } var substitutions = new Dictionary <string, string>(); substitutions.Add("%unitname%", unitName); string unitFileContent = UnitFileHelper.BuildUnitFile(ConfigurationOptions, commandOptions, substitutions); if (!UnitFileHelper.TryCreateNewUnitFile(systemUnit, $"{unitName}.socket", unitFileContent, out string unitFilePath)) { return(1); } string userOption = user ? " --user" : string.Empty; string sudoPrefix = user ? string.Empty : "sudo "; Console.WriteLine($"Created socket file at: {unitFilePath}"); Console.WriteLine(); Console.WriteLine("The following commands may be handy:"); Console.WriteLine($"{sudoPrefix}systemctl{userOption} daemon-reload # Notify systemd a new service file exists"); return(0); }
public static bool RequiredOption(ArgumentsDictionary commandOptions, string name, out string value) { if (!commandOptions.TryGetValue(name.ToLowerInvariant(), out value)) { Console.WriteLine($"Missing required option: --{name}"); return(false); } return(true); }
private static int CreateServiceHandler(bool user, ParseResult result) { bool systemUnit = !user; var commandOptions = new ArgumentsDictionary(result.CommandResult.Children); if (!Prerequisite.RequiredOption(commandOptions, "name", out string unitName) || !Prerequisite.RequiredOption(commandOptions, "execstart", out string execStartUser) || (systemUnit && !Prerequisite.RunningAsRoot()) || !Prerequisite.ResolveApplication(execStartUser, systemUnit, out string execStart, out string execStartDir)) { return(1); } var substitutions = new Dictionary <string, string>(); substitutions.Add("%unitname%", unitName); substitutions.Add("%execstartdir%", execStartDir); // Replace user execstart with resolved application execstart commandOptions.SetValue("execstart", execStart); string unitFileContent = UnitFileHelper.BuildUnitFile(ConfigurationOptions, commandOptions, substitutions); if (!UnitFileHelper.TryCreateNewUnitFile(systemUnit, $"{unitName}.service", unitFileContent, out string unitFilePath)) { return(1); } string userOption = user ? " --user" : string.Empty; string sudoPrefix = user ? string.Empty : "sudo "; Console.WriteLine($"Created service file at: {unitFilePath}"); Console.WriteLine(); Console.WriteLine("The following commands may be handy:"); Console.WriteLine($"{sudoPrefix}systemctl{userOption} daemon-reload # Notify systemd a new service file exists"); Console.WriteLine($"{sudoPrefix}systemctl{userOption} start {unitName} # Start the service"); Console.WriteLine($"{sudoPrefix}systemctl{userOption} status {unitName} # Check the service status"); Console.WriteLine($"{sudoPrefix}systemctl{userOption} enable {unitName} # Automatically start the service"); return(0); }
public static string BuildUnitFile(ConfigurationOption[] options, ArgumentsDictionary optionValues, Dictionary <string, string> substitutions) { var sb = new StringBuilder(); string currentSection = null; foreach (var option in options) { IReadOnlyCollection <string> value; if (!optionValues.TryGetValue(option.Name.ToLowerInvariant(), out value)) { string defaultValue = option.Default; if (defaultValue != null) { value = new[] { defaultValue }; } } if (value != null && value.Count > 0 && !string.IsNullOrEmpty(value.First())) { // Start a new section if (currentSection != option.Section) { if (currentSection != null) { sb.AppendLine(); } sb.AppendLine($"[{option.Section}]"); currentSection = option.Section; } foreach (var val in value) { string substitutedValue = val; foreach (var subsitution in substitutions) { substitutedValue = substitutedValue.Replace(subsitution.Key, subsitution.Value); } sb.AppendLine($"{option.Name}={substitutedValue}"); } } } return(sb.ToString()); }