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;
        }
示例#2
0
        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);
        }
示例#3
0
        private static void DeleteServiceFile(LinuxServiceSettings settings)
        {
            // Удаление скрипта из '/etc/init.d'

            var serviceFile = BuildServicePath(settings);

            if (File.Exists(serviceFile))
            {
                File.Delete(serviceFile);
            }
        }
示例#4
0
        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);
            }
        }
示例#5
0
 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());
 }
示例#6
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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;
 }
示例#11
0
 private static string BuildServiceUser(LinuxServiceSettings settings)
 {
     return(string.IsNullOrWhiteSpace(settings.Username) ? Environment.UserName : settings.Username.Trim());
 }
示例#12
0
 private static string BuildServiceExecutable(LinuxServiceSettings settings)
 {
     return(Path.GetFileName(settings.ServiceExe));
 }
示例#13
0
 private static string BuildDescription(LinuxServiceSettings settings)
 {
     return(string.IsNullOrWhiteSpace(settings.Description)
         ? BuildDisplayName(settings) : settings.Description);
 }
示例#14
0
 private static string BuildDisplayName(LinuxServiceSettings settings)
 {
     return(string.IsNullOrWhiteSpace(settings.DisplayName)
         ? BuildServiceName(settings) : settings.DisplayName);
 }
示例#15
0
 private static string BuildServiceName(LinuxServiceSettings settings)
 {
     return(settings.ServiceName);
 }
        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 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);
 }
示例#22
0
 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 void DeleteServiceFile(LinuxServiceSettings settings)
        {
            // Удаление скрипта из '/etc/init.d'

            var serviceFile = BuildServicePath(settings);

            if (File.Exists(serviceFile))
            {
                File.Delete(serviceFile);
            }
        }
 private static string BuildServiceUser(LinuxServiceSettings settings)
 {
     return string.IsNullOrWhiteSpace(settings.Username) ? Environment.UserName : settings.Username.Trim();
 }
示例#26
0
        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 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);
            }
        }
示例#30
0
 private static string BuildServiceDirectory(LinuxServiceSettings settings)
 {
     return(Path.GetDirectoryName(settings.ServiceExe));
 }
示例#31
0
 public LsbLinuxHostInstaller(LinuxServiceSettings settings)
     : this(settings, null, LinuxServiceLogWriter.Null)
 {
 }
示例#32
0
 private static string BuildServiceArguments(LinuxServiceSettings settings)
 {
     return(string.IsNullOrWhiteSpace(settings.ServiceArgs) ? string.Empty : settings.ServiceArgs.Trim());
 }
 public LsbLinuxHostInstaller(LinuxServiceSettings settings, Installer[] installers)
     : this(settings, installers, LinuxServiceLogWriter.Null)
 {
 }
        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 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();
 }