Exemplo n.º 1
0
        /// <summary>
        /// Removes all tables from the configuration database.
        /// </summary>
        private void ClearBase()
        {
            transferControl.ThrowIfCancellationRequested();
            transferControl.WriteLine();
            transferControl.WriteMessage(ExtensionPhrases.ClearBase);
            NpgsqlTransaction trans = null;

            try
            {
                trans = conn.BeginTransaction();
                progressTracker.SubtaskCount = project.ConfigDatabase.AllTables.Length;

                foreach (IBaseTable baseTable in project.ConfigDatabase.AllTables)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.DeleteTable, baseTable.Name));

                    string sql = $"DROP TABLE IF EXISTS {GetBaseTableName(baseTable)} CASCADE";
                    new NpgsqlCommand(sql, conn, trans).ExecuteNonQuery();
                    progressTracker.SubtaskIndex++;
                }

                trans.Commit();
                progressTracker.TaskIndex++;
            }
            catch
            {
                trans?.Rollback();
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Downloads the configuration database.
        /// </summary>
        private void DownloadBase()
        {
            transferControl.ThrowIfCancellationRequested();
            transferControl.WriteLine();
            transferControl.WriteMessage(AdminPhrases.DownloadBase);

            GetTempFileName(out string tempFileName, out string extractDir);
            agentClient.DownloadConfig(tempFileName, TopFolder.Base);
            ExtractArchive(tempFileName, extractDir, false);

            foreach (IBaseTable baseTable in project.ConfigDatabase.AllTables)
            {
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(string.Format(ExtensionPhrases.ImportTable, baseTable.Name));
                string datFileName = Path.Combine(extractDir, "BaseDAT", baseTable.FileNameDat);

                if (File.Exists(datFileName))
                {
                    BaseTableAdapter baseAdapter = new() { FileName = datFileName };
                    IBaseTable       srcTable    = BaseTableFactory.GetBaseTable(baseTable);
                    baseAdapter.Fill(srcTable);
                    baseTable.Modified = true;

                    foreach (object item in srcTable.EnumerateItems())
                    {
                        baseTable.AddObject(item);
                    }
                }
            }

            progressTracker.TaskIndex++;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Downloads the configuration database.
        /// </summary>
        private void DownloadBase()
        {
            transferControl.ThrowIfCancellationRequested();
            transferControl.WriteLine();
            transferControl.WriteMessage(AdminPhrases.DownloadBase);
            progressTracker.SubtaskCount = project.ConfigDatabase.AllTables.Length;

            foreach (IBaseTable baseTable in project.ConfigDatabase.AllTables)
            {
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(string.Format(ExtensionPhrases.DownloadTable, baseTable.Name));
                ReadBaseTable(baseTable, conn);
                progressTracker.SubtaskIndex++;
            }

            progressTracker.TaskIndex++;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Restarts the services.
        /// </summary>
        public void RestartServices()
        {
            bool restartServer = instance.ServerApp.Enabled && uploadOptions.RestartServer &&
                                 (uploadOptions.IncludeBase || uploadOptions.IncludeServer);
            bool restartComm = instance.CommApp.Enabled && uploadOptions.RestartComm &&
                               (uploadOptions.IncludeBase || uploadOptions.IncludeComm);
            bool smoothRestartWeb = instance.WebApp.Enabled && uploadOptions.RestartWeb &&
                                    !uploadOptions.IncludeWeb && (uploadOptions.IncludeBase || uploadOptions.IncludeView);
            bool fullRestartWeb = instance.WebApp.Enabled && uploadOptions.RestartWeb && uploadOptions.IncludeWeb;

            if (restartServer || restartComm || smoothRestartWeb || fullRestartWeb)
            {
                transferControl.WriteLine();

                if (restartComm)
                {
                    ControlService(ServiceApp.Comm, ServiceCommand.Stop);
                }

                if (fullRestartWeb)
                {
                    ControlService(ServiceApp.Web, ServiceCommand.Stop);
                }

                if (restartServer)
                {
                    ControlService(ServiceApp.Server, ServiceCommand.Restart);
                }

                if (restartComm)
                {
                    ControlService(ServiceApp.Comm, ServiceCommand.Start);
                }

                if (smoothRestartWeb)
                {
                    ControlService(ServiceApp.Web, ServiceCommand.Restart);
                }

                if (fullRestartWeb)
                {
                    ControlService(ServiceApp.Web, ServiceCommand.Start);
                }
            }

            progressTracker.TaskIndex++;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Compresses the configuration into the specified file.
        /// </summary>
        private void CompressConfig(string destFileName)
        {
            transferControl.ThrowIfCancellationRequested();
            transferControl.WriteLine();
            transferControl.WriteMessage(ExtensionPhrases.CompressConfig);

            // archive path separator is '/'
            FileStream fileStream = null;
            ZipArchive zipArchive = null;

            try
            {
                fileStream = new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
                zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create);

                bool ignoreRegKeys = uploadOptions.IgnoreRegKeys;
                bool filterByObj   = uploadOptions.ObjectFilter.Count > 0;

                // archive the configuration database
                if (uploadOptions.IncludeBase)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(ExtensionPhrases.CompressBase);

                    foreach (IBaseTable srcTable in project.ConfigDatabase.AllTables)
                    {
                        transferControl.ThrowIfCancellationRequested();

                        // filter source table by objects if needed
                        IBaseTable baseTable = srcTable;

                        if (filterByObj)
                        {
                            if (srcTable.ItemType == typeof(Cnl))
                            {
                                baseTable = GetFilteredTable <Cnl>(srcTable, uploadOptions.ObjectFilter);
                            }
                            else if (srcTable.ItemType == typeof(View))
                            {
                                baseTable = GetFilteredTable <View>(srcTable, uploadOptions.ObjectFilter);
                            }
                        }

                        // convert table to DAT format
                        using Stream entryStream =
                                  zipArchive.CreateEntry("BaseDAT/" + srcTable.FileNameDat, CompressionLevel.Fastest).Open();
                        BaseTableAdapter baseAdapter = new() { Stream = entryStream };
                        baseAdapter.Update(baseTable);
                    }
                }

                // archive views
                if (uploadOptions.IncludeView)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(ExtensionPhrases.CompressViews);

                    if (filterByObj)
                    {
                        PackFiles(zipArchive, project.Views.ViewDir, GetFilteredViews(project.ConfigDatabase.ViewTable,
                                                                                      project.Views.ViewDir, uploadOptions.ObjectFilter), "Views/");
                    }
                    else
                    {
                        PackDirectory(zipArchive, project.Views.ViewDir, "Views/", ignoreRegKeys);
                    }
                }

                // archive Server configuration
                if (uploadOptions.IncludeServer && instance.ServerApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.ServerAppName));
                    PackDirectory(zipArchive, instance.ServerApp.AppDir, "ScadaServer/", ignoreRegKeys);
                }

                // archive Communicator configuration
                if (uploadOptions.IncludeComm && instance.CommApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.CommAppName));
                    PackDirectory(zipArchive, instance.CommApp.AppDir, "ScadaComm/", ignoreRegKeys);
                }

                // archive Webstation configuration
                if (uploadOptions.IncludeWeb && instance.WebApp.Enabled)
                {
                    transferControl.ThrowIfCancellationRequested();
                    transferControl.WriteMessage(string.Format(ExtensionPhrases.CompressAppConfig,
                                                               CommonPhrases.WebAppName));
                    PackDirectory(zipArchive, instance.WebApp.AppDir, "ScadaWeb/", ignoreRegKeys);
                }

                // add project information
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(ExtensionPhrases.AddProjectInfo);

                using (Stream entryStream =
                           zipArchive.CreateEntry(AgentConst.ProjectInfoEntry, CompressionLevel.Fastest).Open())
                {
                    using StreamWriter writer = new(entryStream, Encoding.UTF8);
                    writer.Write(project.GetInfo());
                }

                // add transfer options
                transferControl.ThrowIfCancellationRequested();
                transferControl.WriteMessage(ExtensionPhrases.AddTransferOptions);

                using (Stream entryStream =
                           zipArchive.CreateEntry(AgentConst.UploadOptionsEntry, CompressionLevel.Fastest).Open())
                {
                    uploadOptions.Save(entryStream);
                }

                progressTracker.TaskIndex++;
            }
            finally
            {
                zipArchive?.Dispose();
                fileStream?.Dispose();
            }
        }