2020-04-28 08:06:43 -04:00
<#
. SYNOPSIS
Download a PowerShell Module from a custom source or from GitHub
by providing a repository and the user space
. DESCRIPTION
Download a PowerShell Module from a custom source or from GitHub
by providing a repository and the user space
. FUNCTIONALITY
Download and install a PowerShell module from a custom or GitHub source
. EXAMPLE
PS > Get-IcingaPowerShellModuleArchive -ModuleName 'Plugins' -Repository 'icinga-powershell-plugins' -Stable 1 ;
. EXAMPLE
PS > Get-IcingaPowerShellModuleArchive -ModuleName 'Plugins' -Repository 'icinga-powershell-plugins' -Stable 1 -DryRun 1 ;
. PARAMETER DownloadUrl
The Url to a ZIP-Archive to download from ( skips the wizard )
. PARAMETER ModuleName
The name which is used inside output messages
. PARAMETER Repository
The repository to download the ZIP-Archive from
. PARAMETER GitHubUser
The user from which a repository is downloaded from
. PARAMETER Stable
Download the latest stable release
. PARAMETER Snapshot
Download the latest package from the master branch
. PARAMETER DryRun
Only return the finished build Url including the version to install but
do not modify the system in any way
. INPUTS
System . String
. OUTPUTS
System . Hashtable
. LINK
https : / / github . com / Icinga / icinga-powershell -framework
#>
2019-12-06 13:29:17 -05:00
function Get-IcingaPowerShellModuleArchive ( )
{
param (
[ string ] $DownloadUrl = '' ,
[ string ] $ModuleName = '' ,
[ string ] $Repository = ''
) ;
$ProgressPreference = " SilentlyContinue " ;
$Tag = 'master' ;
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 ) ) {
if ( ( Get-IcingaAgentInstallerAnswerInput -Prompt ( [ string ] :: Format ( 'Do you provide a custom repository for "{0}"?' , $ModuleName ) ) -Default 'n' ) . result -eq 1 ) {
$branch = ( Get-IcingaAgentInstallerAnswerInput -Prompt 'Which version to you want to install? (snapshot/stable)' -Default 'v' -DefaultInput 'stable' ) . answer
if ( $branch . ToLower ( ) -eq 'snapshot' ) {
$DownloadUrl = [ string ] :: Format ( 'https://github.com/Icinga/{0}/archive/master.zip' , $Repository ) ;
} else {
2019-12-17 11:19:48 -05:00
$LatestRelease = ( Invoke-WebRequest -Uri ( [ string ] :: Format ( 'https://github.com/Icinga/{0}/releases/latest' , $Repository ) ) -UseBasicParsing ) . BaseResponse . ResponseUri . AbsoluteUri ;
2019-12-06 13:29:17 -05:00
$DownloadUrl = $LatestRelease . Replace ( '/releases/tag/' , '/archive/' ) ;
$Tag = $DownloadUrl . Split ( '/' ) [ -1 ] ;
$DownloadUrl = [ string ] :: Format ( '{0}/{1}.zip' , $DownloadUrl , $Tag ) ;
$CurrentVersion = Get-IcingaPowerShellModuleVersion $Repository ;
if ( $null -ne $CurrentVersion -And $CurrentVersion -eq $Tag ) {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice -Message 'Your "{0}" is already up-to-date' -Objects $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 ;
}
}
try {
$DownloadDirectory = New-IcingaTemporaryDirectory ;
$DownloadDestination = ( Join-Path -Path $DownloadDirectory -ChildPath ( [ string ] :: Format ( '{0}.zip' , $Repository ) ) ) ;
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice ( [ string ] :: Format ( 'Downloading "{0}" into "{1}"' , $ModuleName , $DownloadDirectory ) ) ;
2019-12-06 13:29:17 -05:00
Invoke-WebRequest -UseBasicParsing -Uri $DownloadUrl -OutFile $DownloadDestination ;
} catch {
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleError ( [ string ] :: Format ( 'Failed to download "{0}" into "{1}". Starting cleanup process' , $ModuleName , $DownloadDirectory ) ) ;
2019-12-06 13:29:17 -05:00
Start-Sleep -Seconds 2 ;
Remove-Item -Path $DownloadDirectory -Recurse -Force ;
2020-05-13 10:53:15 -04:00
Write-IcingaConsoleNotice 'Starting to re-run the download wizard' ;
2019-12-06 13:29:17 -05:00
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
} ;
}