VersionInfrastructure.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function Update-InfrastructureVersion {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Medium')]
    param(
        [Parameter(Mandatory = $True)]
        [string]
        $infraToolsFunctionName,

        [Parameter(Mandatory = $True)]
        [string]
        $infraToolsTableName,

        [Parameter(Mandatory = $True)]
        [string]
        $deploymentStage
    )
    Write-Output "Updating version..."

    # getting the name of IaC script file
    $fullScriptName = $MyInvocation.PSCommandPath
    $sourceFile = Split-Path $fullScriptName -leaf
    Write-Output "source file: $sourceFile"

    # get latest version defined in the IaC file
    $latestVersion = $(getUpFunctionTotal -parentFile $fullScriptName)
    Write-Output "latest version: $latestVersion"

    $currentVersion = 0
    $couldNotReachInfraTools = $false
    try {
        # this lets me call Invoke-RestMethod to my azure function by allowing TLS, TLS 1.1 and TLS1.2
        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12
        $currentVersion = $(Invoke-RestMethod "https://$infraToolsFunctionName.azurewebsites.net/api/InfraVersionRetriever?tablename=$infraToolsTableName&stage=$deploymentStage&infraname=$sourceFile")
        Write-Output "Current version is: $currentVersion"
    }
    catch {
        Write-Output "Could not reach infra tools function. Set current version to: $currentVersion"
        $couldNotReachInfraTools = $true
    }
    
    if ($currentVersion -eq $latestVersion) {
        Write-Output "Environment is up to date, no change."
    }
    Write-Output ""
    # call all the Up functions needed to get to the latest version
    for ($methodIndex=($currentVersion+1); $methodIndex -le $latestVersion; $methodIndex++)
    {
        # execute the up function
        Write-Output "Executing $($methodIndex)_Up()"
        & "$($methodIndex)_Up"
        Write-Output ""

        # update infrastructure version by 1
        Write-Output "Registering new version of infrastructure..."
        try {
            $updateResponse = $(Invoke-RestMethod "https://$infraToolsFunctionName.azurewebsites.net/api/InfraVersionUpdater?tablename=$infraToolsTableName&stage=$deploymentStage&infraname=$sourceFile")
        }
        catch {
            if ($couldNotReachInfraTools -eq $true) {
                Write-Output "Could not reach infra tools, not updating version"
            }
            else {
                Write-Error "Could not reach infra tools: $PSItem"
            }
        }
        Write-Output "Done registering new infrastructure version, response: $updateResponse"
        Write-Output ""
    }
}


function getUpFunctionTotal {
    param($parentFile)

    $tokens = $errors = $null
    $ast = [System.Management.Automation.Language.Parser]::ParseFile(
    $parentFile,
    [ref]$tokens,
    [ref]$errors)

    # Get only function definition ASTs
    $functionDefinitions = $ast.FindAll({
        param([System.Management.Automation.Language.Ast] $Ast)

        $Ast -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
        # Class methods have a FunctionDefinitionAst under them as well, but we don't want them.
        ($PSVersionTable.PSVersion.Major -lt 5 -or
        $Ast.Parent -isnot [System.Management.Automation.Language.FunctionMemberAst])

    }, $true)

    # calculating latest version by finding the number of #_Up functions
    $latestVersion = 0;
    $functionDefinitions | ForEach-Object {
        if ($_.Name -match '\d_Up') {
            $latestVersion++
        }
    }
    return $latestVersion
}