From 6514c338a281c689419a5950cf8fa720adba3179 Mon Sep 17 00:00:00 2001 From: Rob Ruma Date: Fri, 6 Jul 2018 23:18:12 -0400 Subject: [PATCH] Adding ability to ignore SSL certificate validation errors, cleanup parameters and usage Signed-off-by: Rob Ruma --- tools/scripts/request_tower_configuration.ps1 | 88 ++++++++++++++----- 1 file changed, 68 insertions(+), 20 deletions(-) diff --git a/tools/scripts/request_tower_configuration.ps1 b/tools/scripts/request_tower_configuration.ps1 index db18bfa6bd..72d5263cab 100644 --- a/tools/scripts/request_tower_configuration.ps1 +++ b/tools/scripts/request_tower_configuration.ps1 @@ -1,27 +1,60 @@ -Param( - [string]$tower_url, - [string]$host_config_key, - [string]$job_template_id, - [string]$extra_vars +# Define parameters +[cmdletbinding()] +Param( + [Alias("k")] + [switch] $insecure = $false, + + [Alias("h")] + [switch] $help, + + [Alias("s")] + [string] $tower_url, + + [Alias("c")] + [string] $host_config_key, + + [Alias("t")] + [string] $job_template_id, + + [Alias("e")] + [string] $extra_vars ) +# Initialize variables Set-StrictMode -Version 2 $ErrorActionPreference = "Stop" +$retry_attempts = 10 +$attempt = 0 +$usage = @" +Request server configuration from Ansible Tower -If(-not $tower_url -or -not $host_config_key -or -not $job_template_id) -{ - Write-Host "Requests server configuration from Ansible Tower" - Write-Host "Usage: $($MyInvocation.MyCommand.Name) https://[:server port] " - Write-Host "Example: $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38" - Write-Host "Example with extra_vars: $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38 '{ key: value, dict: { key: value }}'" +Usage: + Execution using positional parameters: + $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38 + + Ignore self-signed certificates using named parameters: + $($MyInvocation.MyCommand.Name) -k -s https://example.towerhost.local -c 44d7507f2ead49af5fca80aa18fd24bc -t 38 + + Execution using optional extra_vars: + $($MyInvocation.MyCommand.Name) https://example.towerhost.net 44d7507f2ead49af5fca80aa18fd24bc 38 '{ key: value, dict: { key: value }}' + +Options: + -help, -h Show this message + -tower_url, -s Tower server (e.g. https://[:server port]) (required) + -insecure, -k Allow insecure SSL connections and transfers + -host_config_key, -c Host config key (required) + -job_template_id, -t Job template ID (required) + -extra_vars, -e [] Extra variables +"@ + +# Validate required arguments +If (-not $tower_url -or -not $host_config_key -or -not $job_template_id -or $help) { + Write-Host $usage Exit 1 } -$retry_attempts = 10 -$attempt = 0 - -If(-not $extra_vars) -{ +# Create Invoke-WebRequest JSON data hash tables +If (-not $extra_vars) { $data = @{ host_config_key=$host_config_key } @@ -32,22 +65,37 @@ If(-not $extra_vars) } } +# Success on any 2xx status received, failure on only 404 status received, retry any other status every min for up to 10 min While ($attempt -lt $retry_attempts) { Try { + If ($insecure) { + [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} + } $resp = Invoke-WebRequest -ContentType application/json -Method POST -Body (ConvertTo-Json $data) -Uri $tower_url/api/v2/job_templates/$job_template_id/callback/ - If ($resp.StatusCode -match '^2[0-9]+$') { Exit 0 - } ElseIf ($resp.StatusCode -eq 404) { - Write-Host "$resp.StatusCode received... encountered problem, halting" - Exit 1 } } + Catch [System.Security.Authentication.AuthenticationException] { + Write-Host $_ + Exit 1 + } Catch { $ex = $_ $attempt++ + If ($([int]$ex.Exception.Response.StatusCode) -eq 404) { + Write-Host "$([int]$ex.Exception.Response.StatusCode) received... encountered problem, halting" + Exit 1 + } Write-Host "$([int]$ex.Exception.Response.StatusCode) received... retrying in 1 minute (Attempt $attempt)" } + Finally { + If ($insecure) { + $sp = [System.Net.ServicePointManager]::FindServicePoint($tower_url) + $sp.CloseConnectionGroup("") > $null + [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null + } + } Start-Sleep -Seconds 60 } Exit 1