ps/Modules/Alkami.PowerShell.ServerManagement/Public/Install-FileBeats.ps1
2023-05-30 22:51:22 -07:00

132 lines
5.1 KiB
PowerShell

function Install-FileBeats {
<#
.SYNOPSIS
Installs FileBeats. May download a version if not present already.
.PARAMETER ChocoSource
[Optional] The chocolatey feed to look for the FileBeat package at if not locally present. Defaults to the SRETools repo as preferred.
.PARAMETER OutputFolder
The location to download the file to if not found.
.PARAMETER TargetFolder
[Optional] The location where this service is found. Can be located with Get-FileBeatsPath (which may return one or more paths, however). Defaults to "C:\Tools\Beats\FileBeat"
.PARAMETER Port
The port this server runs against for reporting. Valid values approx 5046-5048 varying.
.PARAMETER FileBeatServiceName
[Optional] The name of the service being installed. Defaults to "Filebeat (Haystack)" but could also be "Filebeat_os (Haystack)" etc.
.PARAMETER ServerGroupName
Configure the server group this server reports for. Ex: production pod information
.PARAMETER ServerRoleName
Configure the role usage of this server. Examples: ORB, TI, etc.
.PARAMETER FileBeatVersion
[Optional] Specify the preferred version to be installed. Defaults to 6.8.13
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[Alias("Source")]
[string]$ChocoSource = "https://packagerepo.orb.alkamitech.com/nuget/SRETools/",
[Parameter(Mandatory = $false)]
[Alias("DownloadPath")]
[string]$OutputFolder,
[Parameter(Mandatory = $false)]
[Alias("InstallFolder")]
[string]$TargetFolder = "C:\Tools\Beats\FileBeat_os",
[Parameter(Mandatory = $false)]
[Alias("LogstashPort")]
[int]$Port,
[Parameter(Mandatory = $false)]
[Alias("ServiceName")]
[string]$FileBeatServiceName = "Filebeat_os (Haystack)",
[Parameter(Mandatory = $false)]
[Alias("ServerGroup")]
[string]$ServerGroupName,
[Parameter(Mandatory = $false)]
[Alias("ServerRole")]
[string]$ServerRoleName,
[Parameter(Mandatory = $false)]
$FileBeatVersion = "6.8.13"
)
$logLead = (Get-LogLeadName);
$fileBeatService = Get-Service -Name $FileBeatServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
#Set $outputFolder default
if ([string]::IsNullOrEmpty($OutputFolder)) {
$chocoInstallPath = Get-ChocolateyInstallPath
$OutputFolder = Join-Path $chocoInstallPath "lib\filebeat"
}
if ($null -ne $fileBeatService -and $fileBeatService.CanStop) {
Write-Output ("$logLead : Stopping the $FileBeatServiceName Service")
$fileBeatService.Stop() | Out-Null
}
# TODO: Wrap this in an invokable function so we can unit-test this file
choco install filebeat -y --source $ChocoSource --version $FileBeatVersion
$toolsFolder = Get-ChildItem $outputFolder | Where-Object { $_.PsIsContainer } | Select-Object -First 1
if (Test-Path $TargetFolder) {
Write-Host "$logLead : Using Existing FileBeat Folder at [$TargetFolder]"
} else {
New-Item $TargetFolder -ItemType Directory -Force | Out-Null
}
Write-Host "$logLead : Copying $($toolsFolder.Name) to $TargetFolder"
# For some reason I'm getting file locks so whatever, I'm copying it
Copy-Item -Recurse $toolsFolder.FullName $TargetFolder -Force
$installDir = (Join-Path $TargetFolder ($toolsFolder.Name))
$fileBeatService = Get-Service -Name $FileBeatServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
if ($null -eq $fileBeatService) {
Write-Host "$logLead : Installing $FileBeatServiceName Service"
$exePath = (Join-Path -Path $installDir -ChildPath "filebeat.exe")
$ymlPath = (Join-Path -Path $installDir -ChildPath "filebeat.yml")
$dataPath = (Join-Path -Path $installDir -ChildPath "data")
$logsPath = (Join-Path -Path $installDir -ChildPath "logs")
$binaryPathNameField = "'$exePath' -c '$ymlPath' -path.home '$installDir' -path.data '$dataPath' -path.logs '$logsPath'"
# Convert the single quotes above to double quotes
$binaryPathNameField = $binaryPathNameField -replace "'",'"'
$newServiceSplat = @{
Name = $FileBeatServiceName
DisplayName = $FileBeatServiceName
BinaryPathName = $binaryPathNameField
}
(New-Service @newServiceSplat) | Out-Null
}
# Should not error out, because we just installed it if it wasn't installed.
$fileBeatService = (Get-Service -Name $FileBeatServiceName -ErrorAction SilentlyContinue -WarningAction SilentlyContinue)
if ($null -ne $fileBeatService) {
if (($null -ne $Port) -and ($Port -gt 0)) {
$configPath = (Get-ChildItem $targetFolder -Recurse -Include "filebeat.yml") | Select-Object -First 1
Set-FileBeats -LogstashPort $Port -TargetConfigPath $configPath.FullName -ServerGroup $ServerGroupName -ServerRole $ServerRoleName
}
Write-Host ("$logLead : Starting $FileBeatServiceName Service")
Start-FileBeatsService -ServiceName $FileBeatServiceName
} else {
Write-Warning ("$logLead : Could not Find the $FileBeatServiceName Service")
return
}
}