public void StopService(ServiceStatus serviceStatus, ProgressReportsModel report, IProgress <ProgressReportsModel> progress = null)
        {
            try
            {
                var serviceName = serviceStatus.ServiceName;
                using (var sc = new ServiceController(serviceName))
                {
                    try
                    {
                        report.UpdateProgress(serviceName, ProgressState.InProgress);
                        progress?.Report(report);

                        var svcStatus = sc.Status;
                        while (svcStatus != ServiceControllerStatus.Stopped)
                        {
                            sc.Refresh();

                            svcStatus = sc.Status;

                            this._LogService.Info($"Service status of {serviceStatus.ServiceName} is {svcStatus}");

                            switch (svcStatus)
                            {
                            case ServiceControllerStatus.Paused:
                                sc.Continue();
                                break;

                            case ServiceControllerStatus.Running:
                                sc.Stop();
                                break;

                            case ServiceControllerStatus.ContinuePending:
                            case ServiceControllerStatus.PausePending:
                            case ServiceControllerStatus.StartPending:
                            case ServiceControllerStatus.StopPending:
                                break;
                            }

                            svcStatus = sc.Status;

                            Thread.Sleep(this._ServiceStatusCheckTimeWait);
                        }

                        report.UpdateProgress(serviceName, ProgressState.Complete);
                        report.AddErrorMessage(serviceName,
                                               string.Format(Properties.Resources.Format_SucceedDoService, Properties.Resources.Word_Stop));
                    }
                    catch (Exception ex)
                    {
                        report.UpdateProgress(serviceName, ProgressState.Failed);
                        report.AddErrorMessage(serviceName,
                                               string.Format(Properties.Resources.Format_FailedDoService, Properties.Resources.Word_Stop));

                        this._LogService.Error($"Failed to stop '{serviceName}'. Exception is {ex.Message}");
                    }

                    progress?.Report(report);
                }
            }
            catch (Exception e)
            {
                this._LogService.Error(e.Message);
                throw;
            }
        }
Esempio n. 2
0
        public void Restore(BitnamiRedmineStack stack, BackupConfiguration configuration, string path, IProgress <ProgressReportsModel> progress = null)
        {
            if (!Directory.Exists(path))
            {
                throw new DirectoryNotFoundException($"{path} は存在しません。");
            }

            var databaseName     = Resources.Word_Database;
            var pluginName       = Resources.Word_Plugin;
            var themeName        = Resources.Word_Theme;
            var attachedFileName = Resources.Word_AttachedFile;

            var report = new ProgressReportsModel(this._DispatcherService, new[]
            {
                new ProgressItemModel {
                    Key = databaseName, Progress = ProgressState.NotStart
                },
                new ProgressItemModel {
                    Key = pluginName, Progress = ProgressState.NotStart
                },
                new ProgressItemModel {
                    Key = themeName, Progress = ProgressState.NotStart
                },
                new ProgressItemModel {
                    Key = attachedFileName, Progress = ProgressState.NotStart
                },
            });

            // データベースの復元
            if (configuration.Database)
            {
                report.UpdateProgress(databaseName, ProgressState.InProgress);
                progress?.Report(report);

                var databaseConfigurations = this._DatabaseConfigurationService.GetDatabaseConfiguration(stack).ToArray();
                foreach (var databaseConfiguration in databaseConfigurations)
                {
                    var sqlFileName = $"{databaseConfiguration.Mode}.sql";
                    var sqlFilePath = Path.Combine(path, sqlFileName);
                    if (!File.Exists(sqlFilePath))
                    {
                        continue;
                    }

                    this._DatabaseService.Restore(stack, databaseConfiguration, sqlFilePath);
                }

                report.UpdateProgress(databaseName, ProgressState.Complete);
                progress?.Report(report);
                report.AddErrorMessage(databaseName, Resources.Msg_RestoreComplete);
            }
            else
            {
                report.UpdateProgress(databaseName, ProgressState.NotRequire);
                report.AddErrorMessage(databaseName, Resources.Msg_RestoreSkip);

                this._LogService.Info("Database is skipped");
            }

            // プラグイン、テーマ、添付ファイルの復元
            var rules = new[]
            {
                new
                {
                    Condition   = configuration.Plugins,
                    Target      = BackupConfiguration.PluginsPath,
                    Source      = PluginsDirectoryName,
                    CheckAction = new Action <ProgressState>(state =>
                    {
                        report.UpdateProgress(pluginName, state);
                        progress?.Report(report);

                        switch (state)
                        {
                        case ProgressState.Complete:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreComplete);
                            break;

                        case ProgressState.NotRequire:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreSkip);
                            break;

                        case ProgressState.Failed:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreFailed);
                            break;
                        }
                    })
                },
                new
                {
                    Condition   = configuration.Themes,
                    Target      = BackupConfiguration.ThemesePath,
                    Source      = ThemeseDirectoryName,
                    CheckAction = new Action <ProgressState>(state =>
                    {
                        report.UpdateProgress(themeName, state);
                        progress?.Report(report);

                        switch (state)
                        {
                        case ProgressState.Complete:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreComplete);
                            break;

                        case ProgressState.NotRequire:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreSkip);
                            break;

                        case ProgressState.Failed:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreFailed);
                            break;
                        }
                    })
                },
                new
                {
                    Condition   = configuration.Files,
                    Target      = BackupConfiguration.FilesPath,
                    Source      = FilesDirectoryName,
                    CheckAction = new Action <ProgressState>(state =>
                    {
                        report.UpdateProgress(attachedFileName, state);
                        progress?.Report(report);

                        switch (state)
                        {
                        case ProgressState.Complete:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreComplete);
                            break;

                        case ProgressState.NotRequire:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreSkip);
                            break;

                        case ProgressState.Failed:
                            report.AddErrorMessage(databaseName, Resources.Msg_RestoreFailed);
                            break;
                        }
                    })
                }
            };

            foreach (var rule in rules)
            {
                if (!rule.Condition)
                {
                    rule.CheckAction(ProgressState.NotRequire);
                    continue;
                }

                var sourceDir = Path.Combine(path, rule.Source);
                var targetDir = Path.Combine(stack.InstallLocation, rule.Target);
                var condition = Directory.Exists(sourceDir);
                if (!condition)
                {
                    rule.CheckAction(ProgressState.Failed);
                    continue;
                }

                rule.CheckAction(ProgressState.InProgress);

                this.CopyDirectory(sourceDir, targetDir);

                rule.CheckAction(ProgressState.Complete);
            }
        }