private static LinuxServiceSettings CreateServiceSettings(HostSettings settings, string commandLine) { var currentAssembly = Assembly.GetEntryAssembly(); if (currentAssembly == null) { throw new InstallException(Resources.ServiceMustBeExecutableFile); } var serviceName = settings.Name; if (!string.IsNullOrEmpty(settings.InstanceName)) { serviceName += "@" + settings.InstanceName; } var result = new LinuxServiceSettings { ServiceName = serviceName, DisplayName = settings.DisplayName, Description = settings.Description, ServiceExe = currentAssembly.Location, ServiceArgs = commandLine }; var installSettings = settings as InstallHostSettings; if (installSettings != null) { result.Username = installSettings.Credentials?.Username; result.Dependencies = installSettings.Dependencies; } return result; }
private void CreateServiceFile(LinuxServiceSettings settings) { // Создание скрипта в '/etc/init.d' var serviceName = BuildServiceName(settings); var serviceFile = BuildServicePath(settings); var serviceScript = BuildServiceScript(settings, serviceName); File.WriteAllText(serviceFile, serviceScript); }
private static void DeleteServiceFile(LinuxServiceSettings settings) { // Удаление скрипта из '/etc/init.d' var serviceFile = BuildServicePath(settings); if (File.Exists(serviceFile)) { File.Delete(serviceFile); } }
private static void UnregisterServiceFile(LinuxServiceSettings settings) { // Выполнение команды: 'update-rc.d -f <serviceFile> remove' var serviceFile = Path.GetFileName(BuildServicePath(settings)); try { MonoHelper.ExecuteProcessSync("update-rc.d", $" -f {serviceFile} remove"); } catch (Exception error) { throw new InstallException(string.Format(Properties.Resources.CantUnregisterServiceFile, serviceFile), error); } }
private static string BuildServiceScript(LinuxServiceSettings settings, string serviceName) { return(new StringBuilder(Properties.Resources.LsbLinuxServiceScript) .Replace("<ServiceName>", serviceName) .Replace("<Dependencies>", BuildDependencies(settings)) .Replace("<DisplayName>", BuildDisplayName(settings)) .Replace("<Description>", BuildDescription(settings)) .Replace("<ServiceDir>", BuildServiceDirectory(settings)) .Replace("<ServiceExe>", BuildServiceExecutable(settings)) .Replace("<ServiceArgs>", BuildServiceArguments(settings)) .Replace("<ServiceUser>", BuildServiceUser(settings)) .Replace("<ServicePidDir>", "/var/run") .Replace("\r\n", "\n") .ToString()); }
private static string BuildDependencies(LinuxServiceSettings settings) { var dependencies = settings.Dependencies; if (dependencies != null && dependencies.Length > 0) { dependencies = settings.Dependencies.Where(d => !string.IsNullOrWhiteSpace(d)).ToArray(); } if (dependencies == null || dependencies.Length <= 0) { dependencies = new[] { "$local_fs", "$network", "$remote_fs", "$syslog" }; } return(string.Join(" ", dependencies)); }
public LsbLinuxHostInstaller(LinuxServiceSettings settings, Installer[] installers, LinuxServiceLogWriter logWriter) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _settings = settings; _installers = installers; _logWriter = logWriter; _installTransaction = new TransactionManager<LinuxServiceSettings>(_logWriter) .Stage(Properties.Resources.CreateServiceFileStage, CreateServiceFile, DeleteServiceFile) .Stage(Properties.Resources.SetServiceFileAsExecutableStage, SetServiceFileAsExecutable) .Stage(Properties.Resources.RegisterServiceFileStage, RegisterServiceFile, UnregisterServiceFile); }
public LsbLinuxHostInstaller(LinuxServiceSettings settings, Installer[] installers, LinuxServiceLogWriter logWriter) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _settings = settings; _installers = installers; _logWriter = logWriter; _installTransaction = new TransactionManager <LinuxServiceSettings>(_logWriter) .Stage(Properties.Resources.CreateServiceFileStage, CreateServiceFile, DeleteServiceFile) .Stage(Properties.Resources.SetServiceFileAsExecutableStage, SetServiceFileAsExecutable) .Stage(Properties.Resources.RegisterServiceFileStage, RegisterServiceFile, UnregisterServiceFile); }
private static void SetServiceFileAsExecutable(LinuxServiceSettings settings) { // Выполнение команды: 'chmod +x <serviceFile>' var serviceFile = BuildServicePath(settings); Stat fileStatus; // Определение состояния файла if (Syscall.stat(serviceFile, out fileStatus) != 0) { throw new InstallException(string.Format(Properties.Resources.CantRetrieveFileStatus, serviceFile)); } // Разрешение исполнения файла if (Syscall.chmod(serviceFile, fileStatus.st_mode | FilePermissions.S_IXUSR | FilePermissions.S_IXGRP | FilePermissions.S_IXOTH) != 0) { throw new InstallException(string.Format(Properties.Resources.CantSetFileAsExecutable, serviceFile)); } }
private static string BuildServiceName(LinuxServiceSettings settings) { return settings.ServiceName; }
private static string BuildServiceUser(LinuxServiceSettings settings) { return(string.IsNullOrWhiteSpace(settings.Username) ? Environment.UserName : settings.Username.Trim()); }
private static string BuildServiceExecutable(LinuxServiceSettings settings) { return(Path.GetFileName(settings.ServiceExe)); }
private static string BuildDescription(LinuxServiceSettings settings) { return(string.IsNullOrWhiteSpace(settings.Description) ? BuildDisplayName(settings) : settings.Description); }
private static string BuildDisplayName(LinuxServiceSettings settings) { return(string.IsNullOrWhiteSpace(settings.DisplayName) ? BuildServiceName(settings) : settings.DisplayName); }
private static string BuildServiceName(LinuxServiceSettings settings) { return(settings.ServiceName); }
private static string BuildServiceExecutable(LinuxServiceSettings settings) { return Path.GetFileName(settings.ServiceExe); }
private static string BuildServiceDirectory(LinuxServiceSettings settings) { return Path.GetDirectoryName(settings.ServiceExe); }
private static string BuildDescription(LinuxServiceSettings settings) { return string.IsNullOrWhiteSpace(settings.Description) ? BuildDisplayName(settings) : settings.Description; }
private static string BuildDisplayName(LinuxServiceSettings settings) { return string.IsNullOrWhiteSpace(settings.DisplayName) ? BuildServiceName(settings) : settings.DisplayName; }
private static string BuildServicePath(LinuxServiceSettings settings) { var serviceName = BuildServiceName(settings); return Path.Combine("/etc/init.d", serviceName); }
public LsbLinuxHostInstaller(LinuxServiceSettings settings, Installer[] installers) : this(settings, installers, LinuxServiceLogWriter.Null) { }
private static string BuildServiceArguments(LinuxServiceSettings settings) { return string.IsNullOrWhiteSpace(settings.ServiceArgs) ? string.Empty : settings.ServiceArgs.Trim(); }
private static string BuildServiceUser(LinuxServiceSettings settings) { return string.IsNullOrWhiteSpace(settings.Username) ? Environment.UserName : settings.Username.Trim(); }
private static string BuildServicePath(LinuxServiceSettings settings) { var serviceName = BuildServiceName(settings); return(Path.Combine("/etc/init.d", serviceName)); }
private static string BuildDependencies(LinuxServiceSettings settings) { var dependencies = settings.Dependencies; if (dependencies != null && dependencies.Length > 0) { dependencies = settings.Dependencies.Where(d => !string.IsNullOrWhiteSpace(d)).ToArray(); } if (dependencies == null || dependencies.Length <= 0) { dependencies = new[] { "$local_fs", "$network", "$remote_fs", "$syslog" }; } return string.Join(" ", dependencies); }
public LsbLinuxHostInstaller(LinuxServiceSettings settings) : this(settings, null, LinuxServiceLogWriter.Null) { }
private static string BuildServiceDirectory(LinuxServiceSettings settings) { return(Path.GetDirectoryName(settings.ServiceExe)); }
private static string BuildServiceArguments(LinuxServiceSettings settings) { return(string.IsNullOrWhiteSpace(settings.ServiceArgs) ? string.Empty : settings.ServiceArgs.Trim()); }
private static string BuildServiceScript(LinuxServiceSettings settings, string serviceName) { return new StringBuilder(Properties.Resources.LsbLinuxServiceScript) .Replace("<ServiceName>", serviceName) .Replace("<Dependencies>", BuildDependencies(settings)) .Replace("<DisplayName>", BuildDisplayName(settings)) .Replace("<Description>", BuildDescription(settings)) .Replace("<ServiceDir>", BuildServiceDirectory(settings)) .Replace("<ServiceExe>", BuildServiceExecutable(settings)) .Replace("<ServiceArgs>", BuildServiceArguments(settings)) .Replace("<ServiceUser>", BuildServiceUser(settings)) .Replace("<ServicePidDir>", "/var/run") .Replace("\r\n", "\n") .ToString(); }