Public/Sync-VsoGitSmRunbooks.ps1

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    <#
    .SYNOPSIS
        Syncs all runbooks in a VSO git repository to an Azure Automation account.

    .DESCRIPTION
        Syncs all runbooks in a VSO git repository to an Azure Automation account starting with dependent (child)
        runbooks and followed by parent runbooks to an existing Automation Account. This runbook will recursively
        treat all sub directories within the VSORunbookFolderPath as dependent (child) runbooks and publish these
        first
    
        Requires a VSO Alternate Authentication Credential for connecting with VSO-Git repository, stored
        in a Automation credential asset.
    
        This runbook has a dependency on Azure-Connect, which you can download from
        http://gallery.technet.microsoft.com/scriptcenter/Connect-to-an-Azure-f27a81bb
        The Azure-Connect runbook must be published for this runbook to run correctly
        
    .PARAMETER VSOCredentialName
        Name of the credential asset containing the VSO Alternate Authentication Credential name
        and password configured from VSO Profile dialog.
    
    .PARAMETER VSOAccount
        Name of the account name for VSO Online. Ex. https://accountname.visualstudio.com

    .PARAMETER VSOProject
        Name of the VSO project that contains the repository

    .PARAMETER VSORepository
        Name of the repository that contains the runbook project

    .PARAMETER VSORunbookFolderPath
        Project path to the root where the runbooks are located. Ex. /Project1/ProjectRoot
        where ProjectRoot contains the parent runbooks

    .PARAMETER AutomationAccount
        Name of the Automation Account where the runbooks should be synced to

    .PARAMETER AzureConnectionName
        Name of the Azure connection asset that was created in the Automation service.
        This connection asset contains the subscription id and the name of the certificate
        setting that holds the management certificate.
    
    .PARAMETER VSOBranch
        Optional name of the Git branch to retrieve the runbooks from. Defaults to "master"

    .EXAMPLE
        Publish-From-GitVSO -VSOCredentialName "VSOCredentialAsset" -VSOAccount "AccountName"
            -VSOProject "Project" -VSORepository "Repository" -VSORunbookFolderPath "/Project1/ProjectRoot"
            -AutomationAccount "AccountName" -AzureConnectionName "ConnectionAssetName" -VSOBranch "master"

    #>

    function Sync-VsoGitSmRunbooks
    {
        [CmdletBinding()]    
        param (
               [Parameter(Mandatory=$True)]
               [string] $VSOCredentialName,

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

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

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

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

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

               [Parameter(Mandatory=$True)]
               [string] $TargetCredentialName,
       
               [Parameter(Mandatory=$True)]
               [string] $TargetSubscriptionId,

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

               [Parameter(Mandatory=$False)]
               [bool] $FlatFilesMode = $true
        )
    
        $psExtension = ".ps1"
        $grExtension = ".graphrunbook"
        $apiVersion = "1.0-preview"

        #Getting Credentail asset for VSO alternate authentication credentail
        $VSOCred = Get-AutomationPSCredential -Name $VSOCredentialName
        if ($VSOCred -eq $null)
        {
            throw "Could not retrieve '$VSOCredentialName' credential asset. Check that you created this asset in the Automation service."
        }    
        $VSOAuthUserName = $VSOCred.UserName
        $VSOAuthPassword = $VSOCred.GetNetworkCredential().Password
    
        #Creating authorization header using
        $basicAuth = ("{0}:{1}" -f $VSOAuthUserName,$VSOAuthPassword)
        $basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
        $basicAuth = [System.Convert]::ToBase64String($basicAuth)
        $headers = @{Authorization=("Basic {0}" -f $basicAuth)}

        #ex. "https://gkeong.visualstudio.com/defaultcollection/_apis/git/automation-git-test2-proj/repositories/automation-git-test2-proj/items?scopepath=/Project1/Project1/&recursionlevel=full&includecontentmetadata=true&versionType=branch&version=production&api-version=1.0-preview"
        $VSOURL = "https://" + $VSOAccount + ".visualstudio.com/defaultcollection/_apis/git/" + 
                $VSOProject + "/repositories/" + $VSORepository + "/items?scopepath=" + $VSORunbookFolderPath +  
                "&recursionlevel=full&includecontentmetadata=true&versionType=branch&version=" + $VSOBranch +  
                "&api-version=" + $apiVersion
        Write-Verbose("Connecting to VSO using URL: $VSOURL")
        $results = Invoke-RestMethod -Uri $VSOURL -Method Get -Headers $headers
    
        $VerbosePreference = "Continue"
        #$results | ConvertTo-Json | Write-Verbose
    
        $allRunbooks = [AzureAutomationVsoGitSync.Models.SortedRunbookCollection]@{}

        #grab folders & files
        $folderObj = @()
        foreach ($item in $results.value)
        {
            if ($item.gitObjectType -eq "tree")
            {
                $folderObj += $item
            }
            elseif (($item.gitObjectType -eq "blob") -and ($item.path -match $psExtension -or $item.path -match $grExtension))
            {
                # get runbook file name
                $path = $item.path; 
                $fileName = [AzureAutomationVsoGitSync.Models.Runbook]::GetRunbookFileName($path); 

                # local temp path for runbook
                $tempPath = Join-Path -Path $env:SystemDrive -ChildPath "temp"
                $outFile = Join-Path -Path $tempPath -ChildPath $fileName

                # download the runbook
                $fileUrl = $item.url
                Write-Verbose "`tGET $fileName"
                $VerbosePreference = "SilentlyContinue"
                Invoke-RestMethod -Uri $fileUrl -Method Get -Headers $headers -OutFile $outFile 
                $VerbosePreference = "Continue"

                $new = $allRunbooks.Add($outFile, $fileUrl)
            }
        }
    
        # Select the Azure Subscription
        $VerbosePreference = "SilentlyContinue"
        $azCred = Get-AutomationPSCredential -Name $TargetCredentialName
        $azAcct = Add-AzureAccount -Credential $azCred
        $azSub = Select-AzureSubscription -SubscriptionId $TargetSubscriptionId
        $VerbosePreference = "Continue"
        
        if ($FlatFilesMode)
        {
            $sysDrive = $env:SystemDrive
            
            # [SortedRunbookCollection].Result will be the topologically-sorted list of runbooks
            # (Leaf nodes first)
            
            $vsoApiVersion = $true

            $haveSynced = @{}
            $errorSync = @{}

            Write-Verbose "Publish Order (by dependency):"
            $sorted = $allRunbooks.Result
            $sorted | Select Name | ConvertTo-Json | Write-Verbose

            foreach($rb in $sorted)
            {
                $outFile = $rb.FilePath
                $runbookName = $rb.Name
                $rbType = $rb.Type
                
                try 
                {
                    # if not yet synced .. import & add to synced collection
                    if (!$haveSynced.ContainsKey($runbookName))
                    {
                        $fileName = $rb.FileName
                        $tempPath = Join-Path -Path $env:SystemDrive -ChildPath "temp"
                        $outFile = Join-Path -Path $tempPath -ChildPath $fileName
                        $runbookName = $rb.Name
                            
                        # if not yet synced .. import & add to synced collection
                        if (!$haveSynced.ContainsKey($runbookName))
                        {
                            #Import ps1 files into Automation, create one if doesn't exist
                            Write-Verbose("Importing runbook $runbookName into Automation Account...")
                            $arb = Get-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName -ErrorAction "SilentlyContinue"
                            if ($arb -eq $null) 
                            { 
                                Write-Verbose("`tRunbook $runbookName doesn't exist, creating it...") 
                                New-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName  
                            }

                            #Update the runbook, overwrite if existing
                            Write-Verbose("`tUpdating $runbookName ...") 
                            Set-AzureAutomationRunbookDefinition -AutomationAccountName $TargetAutomationAccount -Name $runbookName -Path $outFile -Overwrite 
                     
                            #Publish the updated runbook
                            Write-Verbose("`tPublishing $runbookName ...")
                            Publish-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName 
                        
                            $haveSynced.Add($runbookName, $rb.FileUrl)
                        }
                        else
                        {
                            Write-Verbose("Runbook $runbookName already synced. Duplicate?")
                        }

                        $haveSynced.Add($runbookName, $rb.FileUrl)
                    }
                    else
                    {
                        Write-Verbose("Runbook $runbookName already synced. Duplicate?")
                    }
                }
                catch [System.Exception] 
                {
                    $ex = ConvertTo-Json $_
                    if (!$errorSync.ContainsKey($runbookName))
                    {
                        $errorsync.add( $runbookname, $ex )
                    }
                    Write-Verbose $ex
                    Write-Error $_
                }
            }

            Write-Verbose "Done.`n"
                
            Write-Verbose "Synced $($haveSynced.Count) of $($sorted.Count)"
            #Write-Verbose "Errors $($errorSync.Count)"

            if ($errorSync.Count > 0)
            {
                Write-Verbose "Errors:"
                $errorSync | ConvertTo-Json | Write-Verbose
            }

            if ($haveSynced.Count -eq $sorted.Count)
            {
                Write-Verbose "All runbooks synced."
            }

        }
        else
        {
            $haveSynced = @{}
            $errorSync = @{}

            #recursively go through most inner child folders first, then their parents, grand-parents, etc.
            for ($i = $folderObj.count - 1; $i -ge 0; $i--)
            {
                Write-Verbose("Processing files in $folderObj[$i]")        
                $folderURL = "https://" + $VSOAccount + ".visualstudio.com/defaultcollection/_apis/git/" + 
                        $VSOProject + "/repositories/" + $VSORepository + "/items?scopepath=" + $folderObj[$i].path +  
                        "&recursionLevel=OneLevel&includecontentmetadata=true&versionType=branch&version=" + 
                        $VSOBranch + "&api-version=" + $apiVersion
                
                $results = Invoke-RestMethod -Uri $folderURL -Method Get -Headers $headers
        
                foreach ($item in $results.value)
                {
                    try 
                    {
                        $rb = $allRunbooks.FindByUrl($item.url)
                        if ($rb -ne $null)
                        {
                            $fileName = $rb.FileName
                            $tempPath = Join-Path -Path $env:SystemDrive -ChildPath "temp"
                            $outFile = Join-Path -Path $tempPath -ChildPath $fileName
                            $runbookName = $rb.Name
                            
                            # if not yet synced .. import & add to synced collection
                            if (!$haveSynced.ContainsKey($runbookName))
                            {
                                #Import ps1 files into Automation, create one if doesn't exist
                                Write-Verbose("Importing runbook $runbookName into Automation Account...")
                                $arb = Get-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName -ErrorAction "SilentlyContinue"
                                if ($arb -eq $null) 
                                { 
                                    Write-Verbose("`tRunbook $runbookName doesn't exist, creating it...") 
                                    New-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName 
                                }

                                #Update the runbook, overwrite if existing
                                Write-Verbose("`tUpdating $runbookName ...") 
                                Set-AzureAutomationRunbookDefinition -AutomationAccountName $TargetAutomationAccount -Name $runbookName -Path $outFile -Overwrite 
                     
                                #Publish the updated runbook
                                Write-Verbose("`tPublishing $runbookName ...")
                                Publish-AzureAutomationRunbook -AutomationAccountName $TargetAutomationAccount -Name $runbookName 
                        
                                $haveSynced.Add($runbookName, $rb.FileUrl)
                            }
                            else
                            {
                                Write-Verbose("Runbook $runbookName already synced. Duplicate?")
                            }
                        }

                    }
                    catch [System.Exception] 
                    {
                        $ex = ConvertTo-Json $_
                        if (!$errorSync.ContainsKey($runbookName))
                        {
                            $errorsync.add( $runbookname, $ex )
                        }
                        Write-Verbose $ex
                        Write-Error $_
                    }
                    
                }
            }

            
            Write-Verbose "Done.`n"
                
            Write-Verbose "Synced $($haveSynced.Count) of $($allRunbooks.Results.Count)"

            if ($errorSync.Count > 0)
            {
                Write-Verbose "Errors:"
                $errorSync | ConvertTo-Json | Write-Verbose
            }

            if ($haveSynced.Count -gt 0 -and $haveSynced.Count -eq $results.Count)
            {
                Write-Verbose "All runbooks synced."
            }
            
        }
    }