Пример #1
0
        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);
        }
        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);
        }