-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update bicep and powershell for managing pipeline logs kusto (#7624)
* Update scripts for deploying kusto resources * Update kusto schema from prod database * Update kusto ingestion deployment templates
- Loading branch information
Showing
35 changed files
with
332 additions
and
260 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
tools/pipeline-witness/infrastructure/Extract-KustoScripts.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<# | ||
.SYNOPSIS | ||
Downloads Kusto schema from a Kusto cluster and saves it to the file system. | ||
#> | ||
|
||
param( | ||
[string]$ClusterUri = "https://azsdkengsys.westus2.kusto.windows.net", | ||
[string]$DatabaseName = "Pipelines", | ||
[string]$OutputPath = (Join-Path $PSScriptRoot "kusto") | ||
) | ||
|
||
$accessToken = az account get-access-token --resource "https://api.kusto.windows.net" --query "accessToken" --output tsv | ||
$headers = @{ Authorization="Bearer $accessToken" } | ||
$nl = [System.Environment]::NewLine | ||
|
||
function InvokeKustoCommand($command) { | ||
$response = Invoke-RestMethod -Uri "$ClusterUri/v1/rest/mgmt" -Headers $headers -Method Post -Body (@{csl=$command; db=$DatabaseName} | ConvertTo-Json) -ContentType "application/json" | ||
$columns = $response.Tables[0].Columns | ||
$rows = $response.Tables[0].Rows | ||
|
||
$results = @() | ||
foreach ($row in $rows) { | ||
$obj = @{} | ||
for ($i = 0; $i -lt $columns.Length; $i++) { | ||
$obj[$columns[$i].ColumnName] = $row[$i] | ||
} | ||
$results += [PSCustomObject]$obj | ||
} | ||
return $results | ||
} | ||
|
||
function ToCamelCase($str) { | ||
return $str.Substring(0, 1).ToLower() + $str.Substring(1) | ||
} | ||
|
||
function Quote($str, $quoteChar = '"') { | ||
$str ??= "" | ||
return $quoteChar + $str.Replace('\', '\\').Replace($quoteChar, "\" + $quoteChar) + $quoteChar | ||
} | ||
|
||
function WriteKustoFile($kustoObject, $folder, $fileContents) { | ||
$parentFolder = Join-Path $OutputPath $folder | ||
|
||
if($kustoObject.Folder) { | ||
$parentFolder = Join-Path $parentFolder $kustoObject.Folder | ||
} | ||
|
||
if (-not (Test-Path $parentFolder -PathType Container)) { | ||
New-Item -ItemType Directory -Path $parentFolder | Out-Null | ||
} | ||
|
||
$filePath = Join-Path $parentFolder "$($kustoObject.Name).kql" | ||
|
||
Set-Content -Path $filePath -Value $fileContents -Encoding ASCII | ||
} | ||
|
||
function ExtractTables { | ||
$clusterSchema = (InvokeKustoCommand '.show database schema as json').DatabaseSchema | ConvertFrom-Json | ||
$tables = $clusterSchema.Databases.$DatabaseName.Tables.PSObject.Properties.Value | ||
|
||
foreach ($table in $tables) { | ||
$fileContents = ( | ||
".create-merge table $($table.Name) (", | ||
(($table.OrderedColumns | ForEach-Object { " $($_.Name): $($_.CslType)" }) -join ",$nl"), | ||
") with (folder=$(Quote $table.Folder "'"), docstring=$(Quote $table.DocString "'"))", | ||
"", | ||
".create-or-alter table $($table.Name) ingestion json mapping '$($table.Name)`_mapping' ``````[", | ||
(($table.OrderedColumns | ForEach-Object { " { `"column`": `"$($_.Name)`", `"path`": `"$['$(ToCamelCase $_.Name)']`" }" }) -join ",$nl"), | ||
"]``````" | ||
) -join $nl | ||
|
||
WriteKustoFile $table "tables" $fileContents | ||
} | ||
} | ||
|
||
function ExtractFunctions { | ||
$functions = InvokeKustoCommand '.show functions' | ||
foreach ($function in $functions) { | ||
$fileContents = ".create-or-alter function with (folder=$(Quote $function.Folder "'"), docstring=$(Quote $function.DocString "'")) $($function.Name)$($function.Parameters)$nl$($function.Body)" | ||
WriteKustoFile $function "functions" $fileContents | ||
} | ||
} | ||
|
||
function ExtractViews { | ||
$materializedViews = InvokeKustoCommand '.show materialized-views' | ||
foreach ($view in $materializedViews) { | ||
$fileContents = ( | ||
".create-or-alter materialized-view with (folder=$(Quote $view.Folder "'"), docstring=$(Quote $view.DocString "'")) $($view.Name) on table $($view.SourceTable)", | ||
"{", | ||
$view.Query, | ||
"}" | ||
) -join $nl | ||
|
||
WriteKustoFile $view "views" $fileContents | ||
} | ||
} | ||
|
||
ExtractTables | ||
ExtractFunctions | ||
ExtractViews |
37 changes: 37 additions & 0 deletions
37
tools/pipeline-witness/infrastructure/Merge-KustoScripts.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<# | ||
.SYNOPSIS | ||
Merges Kusto scripts into a single file. | ||
#> | ||
param ( | ||
[Parameter(Mandatory)] | ||
[string]$OutputPath | ||
) | ||
|
||
function ReadFiles([IO.FileInfo[]] $files) { | ||
foreach ($file in $files) { | ||
Write-Output '/////////////////////////////////////////////////////////////////////////////////////////' | ||
Write-Output "// Imported from $(Resolve-Path $file.FullName -Relative)" | ||
Write-Output '' | ||
Get-Content $file | ||
Write-Output '' | ||
Write-Output '' | ||
} | ||
} | ||
|
||
$outputFolder = Split-Path $OutputPath -Parent | ||
if (-not (Test-Path $outputFolder)) { | ||
New-Item -ItemType Directory -Force -Path $outputFolder | Out-Null | ||
} | ||
|
||
$lines = @() | ||
|
||
Push-Location "$PSScriptRoot/kusto" | ||
try { | ||
$lines += ReadFiles (Get-ChildItem -Path "./tables/" -Include "*.kql" -Recurse) | ||
$lines += ReadFiles (Get-ChildItem -Path "./views/" -Include "*.kql" -Recurse) | ||
$lines += ReadFiles (Get-ChildItem -Path "./functions/" -Include "*.kql" -Recurse) | ||
} finally { | ||
Pop-Location | ||
} | ||
|
||
$lines | Set-Content $OutputPath |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
tools/pipeline-witness/infrastructure/bicep/pipelinelogs.test.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#", | ||
"contentVersion": "1.0.0.0", | ||
"parameters": { | ||
"resourceGroupName": { | ||
"value": "pipelinelogs-test" | ||
}, | ||
"location": { | ||
"value": "westus2" | ||
}, | ||
"storageAccountName": { | ||
"value": "pipelinelogstest" | ||
}, | ||
"kustoClusterName": { | ||
"value": "azsdkengsystest" | ||
}, | ||
"kustoDatabaseName": { | ||
"value": "test" | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tools/pipeline-witness/infrastructure/bicep/resourceGroup.bicep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
targetScope='subscription' | ||
|
||
param resourceGroupName string | ||
param location string | ||
param storageAccountName string | ||
param kustoClusterName string | ||
param kustoDatabaseName string | ||
param deploymentName string = deployment().name | ||
|
||
resource resourceGroup 'Microsoft.Resources/resourceGroups@2022-09-01' = { | ||
name: resourceGroupName | ||
location: location | ||
} | ||
|
||
module resources 'resources.bicep' = { | ||
name: deploymentName | ||
scope: resourceGroup | ||
params: { | ||
storageAccountName: storageAccountName | ||
kustoClusterName: kustoClusterName | ||
kustoDatabaseName: kustoDatabaseName | ||
location: location | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<# | ||
.SYNOPSIS | ||
Validates the bicep file build. | ||
#> | ||
|
||
Push-Location $PSScriptRoot | ||
try { | ||
./Merge-KustoScripts.ps1 -OutputPath "./artifacts/merged.kql" | ||
if($?) { | ||
Write-Host "Merged KQL files" | ||
} else { | ||
Write-Error "Failed to merge KQL files" | ||
exit 1 | ||
} | ||
|
||
az bicep build --file "./bicep/resourceGroup.bicep" --outdir "./artifacts" | ||
if($?) { | ||
Write-Host "Built Bicep files" | ||
} else { | ||
Write-Error "Failed to build Bicep files" | ||
exit 1 | ||
} | ||
} | ||
finally { | ||
Pop-Location | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<# | ||
.SYNOPSIS | ||
Deploys the kusto cluster, storage account and associated ingestion queue resources to the specified environment. | ||
#> | ||
param( | ||
[Parameter(Mandatory)] | ||
[validateSet('production', 'staging', 'test')] | ||
[string]$target | ||
) | ||
|
||
Push-Location $PSScriptRoot | ||
try { | ||
$deploymentName = "pipelinelogs-$target-$(Get-Date -Format 'yyyyMMddHHmm')" | ||
$parametersFile = "./bicep/pipelinelogs.$target.json" | ||
|
||
$subscription = az account show --query name -o tsv | ||
|
||
$parameters = (Get-Content -Path $parametersFile -Raw | ConvertFrom-Json).parameters | ||
$location = $parameters.location.value | ||
$resourceGroupName = $parameters.resourceGroupName.value | ||
|
||
./Merge-KustoScripts.ps1 -OutputPath "./artifacts/merged.kql" | ||
if($?) { | ||
Write-Host "Merged KQL files" | ||
} else { | ||
Write-Error "Failed to merge KQL files" | ||
exit 1 | ||
} | ||
|
||
Write-Host "Deploying resources to:`n" + ` | ||
" Subscription: $subscription`n" + ` | ||
" Resource Group: $resourceGroupName`n" + ` | ||
" Location: $location`n" | ||
|
||
Write-Host "> az deployment sub create --template-file './bicep/resourceGroup.bicep' --parameters $parametersFile --location $location --name $deploymentName" | ||
az deployment sub create --template-file './bicep/resourceGroup.bicep' --parameters $parametersFile --location $location --name $deploymentName | ||
} finally { | ||
Pop-Location | ||
} |
23 changes: 0 additions & 23 deletions
23
tools/pipeline-witness/infrastructure/kusto/Merge-KustoScripts.ps1
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
tools/pipeline-witness/infrastructure/kusto/functions/DashboardQuery.kql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.