Exemplo n.º 1
0
 private void UninstallService(ParameterHandlerInfo parameterHandlerInfo)
 {
     try
     {
         var serviceManager = new Win32ServiceManager();
         serviceManager.DeleteService(ServiceName);
         Core.Log.Warning($"The Service \"{ServiceName}\" was uninstalled successfully.");
     }
     catch (Exception ex)
     {
         Core.Log.Error(ex.Message);
     }
 }
Exemplo n.º 2
0
        private void RemoveService(ParameterHandlerInfo parameterHandlerInfo)
        {
            try
            {
                // Environment.GetCommandLineArgs() includes the current DLL from a "dotnet my.dll --register-service" call, which is not passed to Main()

                /*
                 * var remainingArgs = Environment.GetCommandLineArgs()
                 *  .Where(arg => arg != "/service-uninstall")
                 *  .Select(EscapeCommandLineArgument)
                 *  .ToArray();
                 *
                 * var host = Process.GetCurrentProcess().MainModule.FileName;
                 * if (!host.EndsWith("dotnet", StringComparison.OrdinalIgnoreCase))
                 * {
                 *  // For self-contained apps, skip the dll path
                 *  remainingArgs = remainingArgs.Skip(1).ToArray();
                 * }*/

                _settings.ServiceName = _settings.ServiceName ?? Core.ApplicationName;

                var withInstall = true;
                var servicePath = "/etc/systemd/system/";
                if (!Directory.Exists(servicePath))
                {
                    servicePath = "./";
                    withInstall = false;
                }

                var serviceName = _settings.ServiceName?.ToLowerInvariant().Replace(" ", "-") + ".service";
                servicePath = Path.Combine(servicePath, serviceName);

                if (File.Exists(servicePath))
                {
                    File.Delete(servicePath);

                    Core.Log.Warning(withInstall
                        ? $"The file {serviceName}, was deleted from /etc/systemd/system/ path."
                        : $"The file {serviceName}, was deleted from the current path.");
                }
                else
                {
                    Core.Log.InfoDetail("Nothing to do.");
                }
            }
            catch (Exception ex)
            {
                Core.Log.Error(ex.Message);
            }
        }
Exemplo n.º 3
0
        private void CreateService(ParameterHandlerInfo parameterHandlerInfo)
        {
            try
            {
                // Environment.GetCommandLineArgs() includes the current DLL from a "dotnet my.dll --register-service" call, which is not passed to Main()
                var remainingArgs = Environment.GetCommandLineArgs()
                                    .Where(arg => arg != "/service-create")
                                    .Select(EscapeCommandLineArgument)
                                    .ToArray();

                var    host = Process.GetCurrentProcess().MainModule.FileName;
                string directory;
                if (!host.EndsWith("dotnet", StringComparison.OrdinalIgnoreCase))
                {
                    // For self-contained apps, skip the dll path
                    remainingArgs = remainingArgs.Skip(1).ToArray();
                    directory     = Path.GetDirectoryName(host);
                }
                else
                {
                    directory = Path.GetDirectoryName(remainingArgs.First().Replace("\"", string.Empty).Trim());
                }

                var fullServiceCommand = host + " " + string.Join(" ", remainingArgs);
                _settings.ServiceName = _settings.ServiceName ?? Core.ApplicationName;
                _settings.Description = _settings.Description ?? Core.ApplicationDisplayName;

                if (string.IsNullOrWhiteSpace(_settings.User))
                {
                    string user;
                    while (true)
                    {
                        Console.WriteLine("Please enter the user to run the service or press enter [{0}]:", Environment.UserName);
                        user = Console.ReadLine();
                        if (string.IsNullOrEmpty(user))
                        {
                            user = Environment.UserName;
                        }
                        if (!string.IsNullOrWhiteSpace(user))
                        {
                            break;
                        }
                    }
                    _settings.User = user;
                }

                var withInstall = true;
                var servicePath = "/etc/systemd/system/";
                if (!Directory.Exists(servicePath))
                {
                    Core.Log.Error("The systemd path can't be found: {0}, copying on the same folder.", servicePath);
                    servicePath = "./";
                    withInstall = false;
                }

                var serviceName = _settings.ServiceName?.ToLowerInvariant().Replace(" ", "-") + ".service";
                servicePath = Path.Combine(servicePath, serviceName);
                var res = typeof(LinuxServiceContainer).Assembly.GetResourceString("SystemdServicePattern.service");
                res = res.Replace("{{DESCRIPTION}}", _settings.Description);
                res = res.Replace("{{USER}}", _settings.User);
                res = res.Replace("{{WORKINGDIRECTORY}}", directory);
                res = res.Replace("{{EXECUTIONPATH}}", fullServiceCommand);
                using (var fStream = File.Open(servicePath, FileMode.Create, FileAccess.Write))
                    using (var sWriter = new StreamWriter(fStream))
                        sWriter.WriteLine(res);

                Core.Log.Warning(withInstall
                    ? $"The file {serviceName}, was copied to /etc/systemd/system/ path."
                    : $"The file {serviceName}, was copied to the current path.");
            }
            catch (Exception ex)
            {
                Core.Log.Error(ex.Message);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// OnHandler Method
 /// </summary>
 /// <param name="info">Parameter handler info</param>
 protected abstract void OnHandler(ParameterHandlerInfo info);
Exemplo n.º 5
0
 /// <summary>
 /// OnHandler Method
 /// </summary>
 /// <param name="info">Parameter handler info</param>
 private void OnHandler(ParameterHandlerInfo info)
 => OnHandlerAsync(info).WaitAsync();
Exemplo n.º 6
0
 /// <summary>
 /// OnHandler Method
 /// </summary>
 /// <param name="info">Parameter handler info</param>
 protected abstract Task OnHandlerAsync(ParameterHandlerInfo info);
Exemplo n.º 7
0
        private void InstallService(ParameterHandlerInfo parameterHandlerInfo)
        {
            try
            {
                // Environment.GetCommandLineArgs() includes the current DLL from a "dotnet my.dll --register-service" call, which is not passed to Main()
                var remainingArgs = Environment.GetCommandLineArgs()
                                    .Where(arg => arg != "/service-install")
                                    .Select(EscapeCommandLineArgument)
                                    .Append("/service-run");

                var host = Process.GetCurrentProcess().MainModule.FileName;
                if (!host.EndsWith("dotnet.exe", StringComparison.OrdinalIgnoreCase))
                {
                    // For self-contained apps, skip the dll path
                    remainingArgs = remainingArgs.Skip(1);
                }
                var fullServiceCommand = host + " " + string.Join(" ", remainingArgs);


                _settings.DisplayName = _settings.DisplayName ?? Core.ApplicationDisplayName;
                _settings.Description = _settings.Description ?? Core.ApplicationDisplayName;

                Win32ServiceCredentials credentials;
                switch (_settings.Credentials)
                {
                case ServiceCredentials.LocalSystem:
                    credentials = Win32ServiceCredentials.LocalSystem;
                    break;

                case ServiceCredentials.LocalService:
                    credentials = Win32ServiceCredentials.LocalService;
                    break;

                case ServiceCredentials.NetworkService:
                    credentials = Win32ServiceCredentials.NetworkService;
                    break;

                case ServiceCredentials.Custom:
                    credentials = new Win32ServiceCredentials(_settings.Username, _settings.Password);
                    break;

                default:
                    throw new ArgumentException("The Credentials enum has a wrong value.");
                }


                var serviceManager = new Win32ServiceManager();
                serviceManager.CreateOrUpdateService(
                    ServiceName,
                    _settings.DisplayName,
                    _settings.Description,
                    fullServiceCommand,
                    credentials,
                    _settings.AutoStart
                    );

                Core.Log.Warning($"The Service \"{ServiceName}\" was installed successfully.");
            }
            catch (Exception ex)
            {
                Core.Log.Error(ex.Message);
            }
        }