2019-12-06 13:29:17 -05:00
function Get-IcingaPowerShellModuleArchive ( )
{
param (
[ string ] $DownloadUrl = '' ,
[ string ] $ModuleName = '' ,
2020-03-26 14:43:37 -04:00
[ string ] $Repository = '' ,
[ string ] $GitHubUser = 'Icinga' ,
[ bool ] $Stable = $FALSE ,
[ bool ] $Snapshot = $FALSE ,
[ bool ] $DryRun = $FALSE
2019-12-06 13:29:17 -05:00
) ;
$ProgressPreference = " SilentlyContinue " ;
$Tag = 'master' ;
2020-03-26 14:43:37 -04:00
[ bool ] $SkipRepo = $FALSE ;
if ( $Stable -Or $Snapshot ) {
$SkipRepo = $TRUE ;
}
2020-02-07 05:21:53 -05:00
# Fix TLS errors while connecting to GitHub with old PowerShell versions
[ Net.ServicePointManager ] :: SecurityProtocol = " tls12, tls11 " ;
2019-12-06 13:29:17 -05:00
if ( [ string ] :: IsNullOrEmpty ( $DownloadUrl ) ) {
2020-03-26 14:43:37 -04:00
if ( $SkipRepo -Or ( Get-IcingaAgentInstallerAnswerInput -Prompt ( [ string ] :: Format ( 'Do you provide a custom repository for "{0}"?' , $ModuleName ) ) -Default 'n' ) . result -eq 1 ) {
if ( $Stable -eq $FALSE -And $Snapshot -eq $FALSE ) {
$branch = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Which version to you want to install? (snapshot/stable)' -Default 'v' -DefaultInput 'stable' ) . answer ;
} elseif ( $Stable ) {
$branch = 'stable' ;
} else {
$branch = 'snapshot'
}
2019-12-06 13:29:17 -05:00
if ( $branch . ToLower ( ) -eq 'snapshot' ) {
2020-03-26 14:43:37 -04:00
$DownloadUrl = [ string ] :: Format ( 'https://github.com/{0}/{1}/archive/master.zip' , $GitHubUser , $Repository ) ;
2019-12-06 13:29:17 -05:00
} else {
2020-03-26 14:43:37 -04:00
try {
$LatestRelease = ( Invoke-WebRequest -Uri ( [ string ] :: Format ( 'https://github.com/{0}/{1}/releases/latest' , $GitHubUser , $Repository ) ) -UseBasicParsing ) . BaseResponse . ResponseUri . AbsoluteUri ;
$DownloadUrl = $LatestRelease . Replace ( '/releases/tag/' , '/archive/' ) ;
$Tag = $DownloadUrl . Split ( '/' ) [ -1 ] ;
} catch {
Write-Host 'Failed to fetch latest release from GitHub. Either the module or the GitHub account do not exist' ;
}
2019-12-06 13:29:17 -05:00
$DownloadUrl = [ string ] :: Format ( '{0}/{1}.zip' , $DownloadUrl , $Tag ) ;
$CurrentVersion = Get-IcingaPowerShellModuleVersion $Repository ;
if ( $null -ne $CurrentVersion -And $CurrentVersion -eq $Tag ) {
Write-Host ( [ string ] :: Format ( 'Your "{0}" is already up-to-date' , $ModuleName ) ) ;
2020-03-10 12:28:22 -04:00
return @ {
'DownloadUrl' = $DownloadUrl ;
'Version' = $Tag ;
'Directory' = '' ;
'Archive' = '' ;
'ModuleRoot' = ( Get-IcingaFrameworkRootPath ) ;
'Installed' = $FALSE ;
} ;
2019-12-06 13:29:17 -05:00
}
}
} else {
$DownloadUrl = ( Get-IcingaAgentInstallerAnswerInput -Prompt ( [ string ] :: Format ( 'Please enter the full Url to your "{0}" Zip-Archive' , $ModuleName ) ) -Default 'v' ) . answer ;
}
}
2020-03-26 14:43:37 -04:00
if ( $DryRun ) {
return @ {
'DownloadUrl' = $DownloadUrl ;
'Version' = $Tag ;
'Directory' = '' ;
'Archive' = '' ;
'ModuleRoot' = ( Get-IcingaFrameworkRootPath ) ;
'Installed' = $FALSE ;
} ;
}
2019-12-06 13:29:17 -05:00
try {
$DownloadDirectory = New-IcingaTemporaryDirectory ;
$DownloadDestination = ( Join-Path -Path $DownloadDirectory -ChildPath ( [ string ] :: Format ( '{0}.zip' , $Repository ) ) ) ;
Write-Host ( [ string ] :: Format ( 'Downloading "{0}" into "{1}"' , $ModuleName , $DownloadDirectory ) ) ;
Invoke-WebRequest -UseBasicParsing -Uri $DownloadUrl -OutFile $DownloadDestination ;
} catch {
Write-Host ( [ string ] :: Format ( 'Failed to download "{0}" into "{1}". Starting cleanup process' , $ModuleName , $DownloadDirectory ) ) ;
Start-Sleep -Seconds 2 ;
Remove-Item -Path $DownloadDirectory -Recurse -Force ;
Write-Host 'Starting to re-run the download wizard' ;
return Get-IcingaPowerShellModuleArchive -ModuleName $ModuleName -Repository $Repository ;
}
return @ {
'DownloadUrl' = $DownloadUrl ;
'Version' = $Tag ;
'Directory' = $DownloadDirectory ;
'Archive' = $DownloadDestination ;
'ModuleRoot' = ( Get-IcingaFrameworkRootPath ) ;
2020-03-10 12:28:22 -04:00
'Installed' = $TRUE ;
2019-12-06 13:29:17 -05:00
} ;
}