📜 ⬆️ ⬇️

Automatic installation and configuration of PostgreSQL using Wix #

Hi Habr!

Due to the complexity of the project I'm working on, there is a need to deploy and configure PostgreSQL on each client machine. Our company has a lot of customers, so it was decided to automate the process of setting up PostgreSQL and create an MSI installer.

Recently, at Habré, I read the translated article about Wix #, and it will be discussed.
')


As already mentioned in the article , Wix # is part of the open source engine that allows you to execute C # scripts.
After downloading Wix #, you can find many examples in the archive, starting with simple file copying and creating shortcuts on your desktop and ending with your own installer dialogs on WPF.

The task is as follows :

We will not dwell on the first two paragraphs; everything is in the examples. To configure the DBMS, you need to replace the pg_hba.conf file, import the database from the saved copy. All this needs to be done without the participation of the user running the installer.

First of all we create a simple installer to deploy the project. Then I will tell you how to make such an installer using the example of Visual Studio, although this is not necessary at all - you can use any text editor.

We create a simple console application in C #,
and install for the WixSharp solution through the NuGet project manager.

Before installation, the user is usually offered several components to choose from, in Wix # there is a Feature object for this.

Create components to install,

Feature fSoftware = new Feature("< >"); Feature fPostgreSQL = new Feature(" PostgreSQL"); //       , // ,      ,   fSoftware.Description = "  "; fPostgreSQL.Description = "   PostgreSQL"; 


In Wix # there are different objects for building projects, for example, Project or ManagedProject. The latter is different in that you can add your own C # code to it, which will be executed during the installation process.

Define the directory where the files included in the installer package will be located.

 var filesPath =Path.Combine( Path.GetFullPath( Path.Combine(System.Reflection.Assembly .GetExecutingAssembly().Location, @"..\..\..\")), "Files"); 


Create a project object and describe the necessary files for installation.

 var project = new ManagedProject("  ", // Id  ,    //   Custom Actions   new Dir(new Id("INSTALL_DIR"), @"%ProgramFiles%\< >", //         new Files(fSoftware, Path.Combine( filesPath, @"<    >\*.*"))), // PostgreSQL     :\PostgreSQL // (   ) //          new Dir("C:\PostgreSQL", //   new WixSharp.File(fServer, Path.Combine(filesPath, @"postgresqlwin.exe")), //   new WixSharp.File(fServer, Path.Combine(filesPath, @"database.backup")), //   new WixSharp.File(fServer, Path.Combine(filesPath, @"pg_hba.conf")), // BAT- ( ) new WixSharp.File(fServer, Path.Combine(filesPath, @"installDB.bat"))) ); 


In order to perform various actions after or during installation in Wix # there are rich possibilities - for example, if you need to install a ReportViewer, then you should add to the project body:

 new InstalledFileAction("ReportViewer.exe", "/q", Return.asyncNoWait, When.After, Step.InstallFinalize, Condition.NOT_Installed) 


To install and configure PostgreSQL, you can also use InstalledFileAction and others, but because of the many commands that need to be executed, it is decided to make the InstallDB.bat BAT file:

 #       PostgreSQL SET PGPASSWORD=123456 #    C:\PostgreSQL\bin\createdb.exe --host localhost --port 5432 --username "postgres" --no-password <  > #   C:\PostgreSQL\bin\pg_restore.exe --host localhost --port 5432 --username "postgres" --dbname "  " --role "postgres" --no-password --verbose "C:\PostgreSQL\database.backup" 


Next, the project must be configured:

  project.ControlPanelInfo.Manufacturer = "<  >"; project.GUID = new Guid("{F1CC4E21-0326-4107-BB5C-A834EAEF6DAE}"); project.LicenceFile = Path.Combine(filesPath, @"License.rtf"); project.UI = WUI.WixUI_Mondo; project.Language = "ru-RU"; project.OutFileName = "SetUp"; //   .Net Framework 4.0 project.SetNetFxPrerequisite("NETFRAMEWORK40FULL >= '#1'", ",  .NET Framework 4.0."); project.PreserveTempFiles = true; project.DefaultFeature = fSoftware; project.Version = new Version("1.0.0.1"); project.AfterInstall += AfterInstall; Compiler.BuildMsi(project); 


Now for the AfterInstall event, you can write a Custom Action to start the installation and configuration of PostgreSQL:

 private static void AfterInstall(SetupEventArgs e) { try { if ((e.Mode == SetupEventArgs.SetupMode.Installing) || (e.Mode == SetupEventArgs.SetupMode.Modifying)) { if (System.IO.File.Exists(@"C:\PostgreSQL\postgresqlwin.exe")) { var process = new Process(); process.StartInfo = new ProcessStartInfo(@"C:\PostgreSQL\postgresqlwin.exe", @"--prefix C:\PostgreSQL --mode unattended --datadir C:\PostgreSQL\data --superpassword 123456 --install_runtimes 0"); process.Start(); process.WaitForExit(); System.IO.File.Delete(@"C:\PostgreSQL\postgresqlwin.exe"); } if (System.IO.File.Exists(@"C:\PostgreSQL\pg_hba.conf")) { System.IO.File.Copy(@"C:\PostgreSQL\pg_hba.conf", @"C:\PostgreSQL\data\pg_hba.conf", true); System.IO.File.Delete(@"C:\PostgreSQL\pg_hba.conf"); } if (System.IO.File.Exists(@"C:\PostgreSQL\database.backup")) { if (System.IO.File.Exists(@"C:\PostgreSQL\installDB.bat")) { var pgProcess = new Process(); pgProcess.StartInfo = new ProcessStartInfo(@"C:\PostgreSQL\installDB.bat", "") { CreateNoWindow = true, UseShellExecute = false }; pgProcess.Start(); pgProcess.WaitForExit(); System.IO.File.Delete(@"C:\PostgreSQL\installDB.bat"); } else return; System.IO.File.Delete(@"C:\PostgreSQL\database.backup"); } } } catch (Exception ex) { System.Windows.MessageBox.Show("  : " + ex.ToString()); } } 


Alternatively, one could copy the PostgreSQL installer to a temporary folder and start the installation from it.

With these simple steps, PostgreSQL will automatically be configured for your software.

However, there are moments that in Wix # I didn’t like:
1) Support only .NET Framework 3.5
2) Generally in Wix - it is impossible to start the MSI installer from your installer, for this you need to use WIX extensions, which complicates the task. That is why ReportViewer had to be installed after the installation was completed.
3) Why is there a problem with this code:

 new Dir(@"C:\Test1", ..), new Dir(@"C:\Test2", ..) 

Not going, an error about an invalid path is displayed. If you leave only one folder, then everything works.

The following code works, but partially:
 new Dir(@"C:\Test", new Dir(@"Test1",   feature1), new Dir(@"Test2",   feature2), ) 


The idea is that the files Test1 and Test2 should be copied to the files, which is what happens if during installation you select feature1 and feature2.
And if during installation you select only feature1 and then add feature2 (Change installation), then the Test2 folder does not appear - instead it will be ABSOLUTEPATH folder with files for feature2.
Surely in my script something was not taken into account.

In general, I liked Wix #, especially if you need to quickly make an installer without diving into XML for WIX.

Sources:
  1. Translation article about Wix #
  2. Step-by-step instructions for setting up various development tools for Wix #

Source: https://habr.com/ru/post/276175/


All Articles