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
2020-05-29 08:02:43 -04:00
PS > Get-IcingaPowerShellModuleArchive -ModuleName 'Plugins' -Repository 'icinga-powershell-plugins' -Release 1 ;
2020-04-28 08:06:43 -04:00
. EXAMPLE
2020-05-29 08:02:43 -04:00
PS > Get-IcingaPowerShellModuleArchive -ModuleName 'Plugins' -Repository 'icinga-powershell-plugins' -Release 1 -DryRun 1 ;
2020-04-28 08:06:43 -04:00
. 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
2020-05-29 08:02:43 -04:00
. PARAMETER Release
Download the latest release
2020-04-28 08:06:43 -04:00
. 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 = '' ,
2020-03-26 14:43:37 -04:00
[ string ] $Repository = '' ,
[ string ] $GitHubUser = 'Icinga' ,
2020-05-29 08:02:43 -04:00
[ bool ] $Release = $FALSE ,
2020-03-26 14:43:37 -04:00
[ 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 ;
2020-05-29 08:02:43 -04:00
if ( $Release -Or $Snapshot ) {
2020-03-26 14:43:37 -04:00
$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 ) {
2020-05-29 08:02:43 -04:00
if ( $Release -eq $FALSE -And $Snapshot -eq $FALSE ) {
2020-05-29 08:49:34 -04:00
$branch = ( Get-IcingaAgentInstallerAnswerInput -Prompt ( [ string ] :: Format ( 'Which version of the "{0}" do you want to install? (release/snapshot)' , $ModuleName ) ) -Default 'v' -DefaultInput 'release' ) . answer ;
2020-05-29 08:02:43 -04:00
} elseif ( $Release ) {
$branch = 'release' ;
2020-03-26 14:43:37 -04:00
} 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 {
2020-05-29 08:49:34 -04:00
Write-IcingaConsoleError -Message 'Failed to fetch latest release for "{0}" from GitHub. Either the module or the GitHub account do not exist' -Objects $ModuleName ;
2020-03-26 14:43:37 -04:00
}
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 ) {
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 {
2020-05-29 08:49:34 -04:00
$DownloadUrl = ( Get-IcingaAgentInstallerAnswerInput -Prompt ( [ string ] :: Format ( 'Please enter the full path of the custom repository for the "{0}" (location of zip file)' , $ModuleName ) ) -Default 'v' ) . answer ;
2019-12-06 13:29:17 -05:00
}
}
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 ) ) ) ;
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
} ;
}