use super::Elevation;
use super::steps::{Note, Step};
use crate::host::Arch;
const VERSION: &str = "1.0.24";
fn url(file: &str) -> String {
format!("https://releases.hashicorp.com/vagrant-vmware-utility/{VERSION}/{file}")
}
fn mac_image(arch: Arch) -> (String, &'static str) {
let arch = match arch {
Arch::Amd64 => "amd64",
Arch::Arm64 => "arm64",
};
(
format!("vagrant-vmware-utility_{VERSION}_darwin_{arch}.dmg"),
"5f42cb017dd07270409eaee7c3b4a164ffa7c0f21d85c65840c4f81aab21d457",
)
}
const HASHICORP_TEAM: &str = "D38WU7D763";
fn mac_root_script(sha256: &str) -> String {
format!(
r#"set -e
d="$(mktemp -d /private/tmp/isothrum-vmware-utility.XXXXXX)"
m=""
trap '[ -z "$m" ] || hdiutil detach -quiet "$m"; rm -rf "$d"' EXIT
cp "$1" "$d/utility.dmg"
[ "$(shasum -a 256 "$d/utility.dmg" | awk '{{print $1}}')" = "{sha256}" ] || {{ echo "The download isn't HashiCorp's Vagrant VMware Utility {VERSION} (its SHA-256 doesn't match). Nothing was installed." >&2; exit 1; }}
m="$(hdiutil attach -nobrowse -noautoopen -readonly -mountrandom "$d" "$d/utility.dmg" </dev/null | awk -F'\t' '$NF ~ /^\// {{print $NF; exit}}')"
p="$m/VagrantVMwareUtility.pkg"
pkgutil --check-signature "$p" | grep -qF "Developer ID Installer: Hashicorp, Inc. ({HASHICORP_TEAM})" || {{ echo "The package isn't signed by HashiCorp. Nothing was installed." >&2; exit 1; }}
spctl --assess --type install "$p" || {{ echo "macOS doesn't accept the package (not notarized). Nothing was installed." >&2; exit 1; }}
installer -pkg "$p" -target /
i=0
until nc -z 127.0.0.1 {port} 2>/dev/null; do
i=$((i + 1))
[ "$i" -le 60 ] || {{ echo "The Vagrant VMware Utility is installed, but its service doesn't answer on port {port}." >&2; exit 1; }}
sleep 1
done
echo "The Vagrant VMware Utility's service answers.""#,
port = crate::host::VMWARE_UTILITY_PORT
)
}
fn mac_script(file: &str, sha256: &str, elevation: Elevation) -> String {
let url = url(file);
let prompt = super::mac_image::admin_prompt("install the Vagrant VMware Utility", "install its service");
let elevated = super::mac_image::as_administrator(elevation, "Downloaded and checked. Installing it", &prompt, &[r#""$d/utility.dmg""#]);
format!(
r#"set -e
d="$(mktemp -d)"
trap 'rm -rf "$d"' EXIT
curl -fsSL --proto '=https' --tlsv1.2 "{url}" -o "$d/utility.dmg"
[ "$(shasum -a 256 "$d/utility.dmg" | awk '{{print $1}}')" = "{sha256}" ] || {{ echo "The download isn't HashiCorp's Vagrant VMware Utility {VERSION} (its SHA-256 doesn't match). Nothing was installed." >&2; exit 1; }}
{elevated}"#
)
}
#[must_use]
pub(crate) fn mac_install(arch: Arch, elevation: Elevation) -> Step {
let (file, sha256) = mac_image(arch);
let mut step = super::steps::step("sh", &["-c", &mac_script(&file, sha256, elevation), "sh", &mac_root_script(sha256)]);
step.note = Some(Note::DownloadingVmwareUtility { version: VERSION });
step
}
const WINDOWS_MSI: (&str, &str) = (
"vagrant-vmware-utility_1.0.24_windows_amd64.msi",
"8ee7bea062d7552a484b7d9ade369744e5cd9ab1b55eed8b7bd35a1b006666d6",
);
const WINDOWS_PRODUCT_CODE: &str = "{DE47C4D6-3BD9-4286-A93D-1DCA1CBADDA7}";
const WORKSTATION_32_BIT_VIEW: &str = r"$vmware64 = 'HKLM:\SOFTWARE\VMware, Inc.'; $vmware32 = 'HKLM:\SOFTWARE\WOW6432Node\VMware, Inc.'
if ((Test-Path ($vmware64 + '\VMware Workstation')) -and -not (Test-Path ($vmware32 + '\VMware Workstation'))) {
& reg.exe copy 'HKLM\SOFTWARE\VMware, Inc.' 'HKLM\SOFTWARE\WOW6432Node\VMware, Inc.' /s /f | Out-Null
if ($LASTEXITCODE -ne 0) { exit 4 }
New-ItemProperty -Path $vmware32 -Name 'CopiedByIsothrum' -Value '1' -PropertyType String -Force | Out-Null
}";
fn windows_elevated(sha256: &str) -> String {
format!(
r#"$ErrorActionPreference = 'Stop'
{WORKSTATION_32_BIT_VIEW}
if (Get-Service -Name 'vagrant-vmware-utility' -ErrorAction SilentlyContinue) {{ Restart-Service -Name 'vagrant-vmware-utility'; exit 0 }}
$d = Join-Path $env:SystemRoot ('Temp\isothrum-' + [guid]::NewGuid())
New-Item -ItemType Directory -Path $d | Out-Null
try {{
$copy = Join-Path $d 'vagrant-vmware-utility.msi'
Copy-Item -LiteralPath $msi -Destination $copy
if ((Get-FileHash -Algorithm SHA256 -LiteralPath $copy).Hash -ne '{sha256}') {{ exit 2 }}
$sig = Get-AuthenticodeSignature -LiteralPath $copy
if ($sig.Status -ne 'Valid' -or $sig.SignerCertificate.GetNameInfo('SimpleName', $false) -ne 'HashiCorp, Inc.') {{ exit 3 }}
$p = Start-Process -FilePath msiexec.exe -ArgumentList '/i', ('"' + $copy + '"'), '/qn', '/norestart' -Wait -PassThru
if ($p.ExitCode -eq 3010) {{ exit 0 }}
exit $p.ExitCode
}} finally {{ Remove-Item -LiteralPath $d -Recurse -Force -ErrorAction SilentlyContinue }}"#
)
}
fn windows_script(file: &str, sha256: &str) -> String {
let url = url(file);
let elevated = ps_quote(&windows_elevated(sha256));
format!(
r"$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$d = Join-Path ([IO.Path]::GetTempPath()) ('isothrum-' + [guid]::NewGuid())
New-Item -ItemType Directory -Path $d | Out-Null
try {{
$msi = Join-Path $d 'vagrant-vmware-utility.msi'
Invoke-WebRequest -UseBasicParsing -Uri '{url}' -OutFile $msi
if ((Get-FileHash -Algorithm SHA256 -LiteralPath $msi).Hash -ne '{sha256}') {{ [Console]::Error.WriteLine('The download isn''t HashiCorp''s Vagrant VMware Utility {VERSION} (its SHA-256 doesn''t match). Nothing was installed.'); exit 1 }}
Write-Output 'Downloaded and checked. Installing it: Windows asks for an administrator''s approval.'
$script = '$msi = ' + ([char]39) + $msi.Replace([string][char]39, [string][char]39 + [char]39) + ([char]39) + [Environment]::NewLine + {elevated}
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($script))
try {{ $p = Start-Process -FilePath powershell.exe -ArgumentList '-NoProfile', '-NonInteractive', '-EncodedCommand', $encoded -Verb RunAs -Wait -PassThru }} catch {{ exit 1223 }}
if ($p.ExitCode -eq 2) {{ [Console]::Error.WriteLine('The installer changed after it was checked. Nothing was installed.') }}
if ($p.ExitCode -eq 3) {{ [Console]::Error.WriteLine('The installer isn''t signed by HashiCorp. Nothing was installed.') }}
if ($p.ExitCode -eq 4) {{ [Console]::Error.WriteLine('VMware Workstation''s settings couldn''t be made visible to the utility. Nothing was installed.') }}
exit $p.ExitCode
}} finally {{ Remove-Item -LiteralPath $d -Recurse -Force -ErrorAction SilentlyContinue }}"
)
}
#[must_use]
pub(crate) fn windows_install() -> Step {
let (file, sha256) = WINDOWS_MSI;
let mut step = super::steps::step("powershell", &["-NoProfile", "-NonInteractive", "-Command", &windows_script(file, sha256)]);
step.note = Some(Note::DownloadingVmwareUtility { version: VERSION });
step
}
#[must_use]
pub(crate) fn windows_uninstall() -> Step {
let removal = format!(
"try {{ $p = Start-Process -FilePath msiexec.exe -ArgumentList '/x', '{WINDOWS_PRODUCT_CODE}', '/qn', '/norestart' -Verb RunAs -Wait -PassThru; if ($p.ExitCode -eq 3010) {{ exit 0 }}; exit $p.ExitCode }} catch {{ exit 1223 }}"
);
let mut step = super::steps::step("powershell", &["-NoProfile", "-NonInteractive", "-Command", &removal]);
step.note = Some(Note::Removing(super::Dependency::VmwareUtility));
step
}
fn ps_quote(s: &str) -> String {
format!("'{}'", s.replace('\'', "''"))
}
#[cfg(test)]
mod tests;