Skip to content

Commit

Permalink
Allow overriding INSTALLDIR (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
amitkanfer authored Feb 10, 2024
1 parent 0f861f5 commit 88ab634
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ Update version in `Directory.Build.props` in the branch for the related minor ve
- Remove the daily schedule for the previous minor branch
ex: https://github.com/elastic/elastic-stack-installers/pull/156 and https://github.com/elastic/elastic-stack-installers/pull/172

---
## Installing to a custom location
The default target folder for the MSI is typically something like `c:\Program Files\Elastic\Beats\<version>\<beat>` eg. `c:\Program Files\Elastic\Beats\8.12.0\winlogbeat`.

Starting version 8.13 It's also possible to override the installation folder by executing the MSI from command line as such:
`msiexec /i "<full path to msi file>" INSTALLDIR="<path of custom folder>"`

Few important notes:
- For security reasons, it's recommended to keep the installation folder "Program Files"
- An empty folder will always be created at the default location `...\Beats\<version>\<beat>` to a known limitation as explained [here](https://github.com/oleg-shilo/wixsharp/issues/790).
- Custom installation folder is not applicable for Agent MSI (only beats)

---
## Agent

Expand Down
2 changes: 1 addition & 1 deletion src/build/ElastiBuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="commandlineparser" Version="2.7.82" />
<PackageReference Include="DotNetZip" Version="1.13.7" />
<PackageReference Include="Humanizer.Core" Version="2.8.2" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SimpleExec" Version="6.2.0" />
<PackageReference Include="SharpYaml" Version="1.6.6" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
Expand Down
6 changes: 2 additions & 4 deletions src/installer/BeatPackageCompiler/AgentCustomAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public static ActionResult InstallAction(Session session)
else
session.Log("No INSTALLARGS detected");

string install_folder = Path.Combine(session["INSTALLDIR"], session["exe_folder"]);

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = Path.Combine(install_folder, "elastic-agent.exe");
process.StartInfo.FileName = Path.Combine(session["INSTALLDIR"], "elastic-agent.exe");
process.StartInfo.Arguments = "install -f " + install_args;
StartProcess(session, process);

Expand All @@ -31,7 +29,7 @@ public static ActionResult InstallAction(Session session)
if (process.ExitCode == 0)
{
// If agent got installed properly, we can go ahead and remove all the files installed by the MSI (best effort)
RemoveFolder(session, install_folder);
RemoveFolder(session, session["INSTALLDIR"]);
}

return process.ExitCode == 0 ? ActionResult.Success : ActionResult.Failure;
Expand Down
20 changes: 6 additions & 14 deletions src/installer/BeatPackageCompiler/BeatPackageCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static void Main(string[] args)
if (pc.IsWindowsService)
{
service = new WixSharp.File(Path.Combine(opts.PackageInDir, exeName));
string installedPath = ("[INSTALLDIR]" + Path.Combine(ap.Version, ap.CanonicalTargetName));
string installedPath = ("[INSTALLDIR]");

// TODO: CNDL1150 : ServiceConfig functionality is documented in the Windows Installer SDK to
// "not [work] as expected." Consider replacing ServiceConfig with the
Expand Down Expand Up @@ -179,8 +179,6 @@ static void Main(string[] args)
project.LaunchConditions.Add(new LaunchCondition("Privileged", "Elastic Agent MSI must run as an administrator"));
project.AddProperty(new Property("MSIUSEREALADMINDETECTION", "1"));

// Passing the agent executable path to the action handler which will run it post installation
project.AddProperty(new Property("exe_folder", Path.Combine(ap.Version, ap.CanonicalTargetName)));
project.AddAction(new ManagedAction(AgentCustomAction.InstallAction, Return.check, When.After, Step.InstallExecute, Condition.NOT_Installed));

// https://stackoverflow.com/questions/320921/how-to-add-a-wix-custom-action-that-happens-only-on-uninstall-via-msi
Expand Down Expand Up @@ -216,26 +214,20 @@ static void Main(string[] args)
System.IO.File.WriteAllText(cliShimScriptPath, Resources.GenericCliShim);

var beatsInstallPath =
$"[ProgramFiles{(ap.Is64Bit ? "64" : string.Empty)}Folder]" +
Path.Combine(companyName, productSetName);
$"[ProgramFiles{(ap.Is64Bit ? "64" : string.Empty)}Folder]\\" +
Path.Combine(companyName, productSetName, ap.Version, ap.CanonicalTargetName);

var l = packageContents.ToArray().Combine(new WixSharp.File(cliShimScriptPath));
project.Dirs = new[]
{
// Binaries
new InstallDir(
// Wix# directory parsing needs forward slash
beatsInstallPath.Replace("Folder]", "Folder]\\"),
new Dir(
ap.Version,
new Dir(ap.CanonicalTargetName, packageContents.ToArray()),
new WixSharp.File(cliShimScriptPath))),

new InstallDir(beatsInstallPath, l)
};

if (!pc.IsAgent)
{
// CLI Shim path (In agent MSI te 'elastic-agent install' takes care of the PATH)
project.Add(new EnvironmentVariable("PATH", Path.Combine(beatsInstallPath, ap.Version))
project.Add(new EnvironmentVariable("PATH", beatsInstallPath)
{
Part = EnvVarPart.last
});
Expand Down
10 changes: 5 additions & 5 deletions src/installer/BeatPackageCompiler/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
{
"profiles": {
"BeatPackageCompiler.netcore": {
"commandName": "Project",
"commandLineArgs": "--package=winlogbeat-8.11.1-windows-x86_64 -v --keep-temp-files",
"workingDirectory": "$(SolutionDir)"
},
"Filebeat": {
"commandName": "Project",
"commandLineArgs": "--package=filebeat-8.11.0-windows-x86_64 -v --keep-temp-files",
Expand All @@ -29,6 +24,11 @@
"commandName": "Project",
"commandLineArgs": "--package=elastic-agent-8.11.4-windows-x86_64 -v --keep-temp-files",
"workingDirectory": "$(SolutionDir)"
},
"winlogbeat 8.12.1": {
"commandName": "Project",
"commandLineArgs": "--package=winlogbeat-8.12.1-windows-x86_64 -v --keep-temp-files",
"workingDirectory": "$(SolutionDir)"
}
}
}

0 comments on commit 88ab634

Please sign in to comment.