mattmcspirit / azurestack

Azure Stack Resources
80 stars 41 forks source link

Offline download script doesn't set default variable for $branch (correctly) #57

Closed gijs007 closed 5 years ago

gijs007 commented 5 years ago

The offline download scripts fails, when not specifying the branch. As the default variable for $branch is empty.

Steps to reproduce \ConfigASDKdependencies.ps1 -downloadPath "C:\ASDKdependencies" -ISOPath "C:\WS2016EVAL.iso"

The workaround is to specify the branch manually: .\ConfigASDKdependencies.ps1 -downloadPath "C:\ASDKdependencies" -ISOPath "C:\WS2016EVAL.iso" -branch master

As we can see in the error there is a double // instead of /master/.

`VERBOSE: [15:28]::[VALIDATION]:: Validating if running under Admin Privileges VERBOSE: GET https://raw.githubusercontent.com/mattmcspirit/azurestack//README.md with 0-byte payload Accessing https://raw.githubusercontent.com/mattmcspirit/azurestack//README.md - Status Code is 404 - URL is invalid If you're not sure, don't specify a branch, and 'master' will be used. Error details:

Invalid Github branch specified. You tried to access https://raw.githubusercontent.com/mattmcspirit/azurestack//README.md, which doesn't exist. Status Code: 404 - exiting process At C:\ConfigASDKfiles\ConfigASDKdependencies.ps1:131 char:5

mattmcspirit commented 5 years ago

This works fine for me - there is a script that catches this:

try {
    if ($null -eq $branch) {
        $branch = "master"
    }
    $urlToTest = "https://raw.githubusercontent.com/mattmcspirit/azurestack/$branch/README.md"
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    $statusCode = Invoke-WebRequest "$urlToTest" -UseBasicParsing -ErrorAction SilentlyContinue | ForEach-Object {$_.StatusCode} -ErrorAction SilentlyContinue
    if ($statusCode -eq 200) {
        Write-Host "Accessing $urlToTest - Status Code is 200 - URL is valid" -ForegroundColor Green
    }
}
catch {
    $statusCode = [int]$_.Exception.Response.StatusCode
    Write-Host "Accessing $urlToTest - Status Code is $statusCode - URL is invalid" -ForegroundColor Red
    Write-Host "If you're not sure, don't specify a branch, and 'master' will be used. Error details: `r`n" -ForegroundColor Red
    throw "Invalid Github branch specified. You tried to access $urlToTest, which doesn't exist. Status Code: $statusCode - exiting process"
}

If no $branch is provided, it automatically sets to "master" - not sure when that's not working for you.

I'll check again, and see if the same thing happens.

gijs007 commented 5 years ago

The output for the $branch variable remains empty. I've tested it by running $branch after the script errors.

Perhaps the if condition is somehow wrong?

mattmcspirit commented 5 years ago

All variables would be empty after the script errors, unless you .\ (dot source) the initial launch command, but nonetheless, you're right. Give me a bit of time and I'll get this fixed. I just need a more reliable way of checking the variable exists/doesn't exist and then act on it from there.

Thanks! Matt

mattmcspirit commented 5 years ago

Should be fixed now. In the main ConfigASDK script, i used:

        if (!$branch) {
            $branch = "master"
        }

and in the offline downloader script, used

    if ($null -eq $branch) {
        $branch = "master"
    }

I've replaced the $null version with the top one and re-tested, works fine now. Feel free to edit your version, or download the new master version.

Thanks for the catch.

gijs007 commented 5 years ago

You're welcome :) Thanks for the quick fix.

Good to know that variables are empty after a script exits, unless doing .\ (dot source).