tools/scripts/AksEdgeQuickStart/AksEdgeQuickStart.ps1 (202 lines of code) (raw):
<#
QuickStart script for setting up Azure for AKS Edge Essentials and deploying the same on the Windows device
#>
param(
[String] $SubscriptionId,
[String] $TenantId,
[String] $Location,
[Switch] $UseK8s,
[string] $Tag
)
#Requires -RunAsAdministrator
New-Variable -Name gAksEdgeQuickStartVersion -Value "1.0.250311.1500" -Option Constant -ErrorAction SilentlyContinue
New-Variable -Option Constant -ErrorAction SilentlyContinue -Name arcLocations -Value @(
"australiaeast","brazilsouth","canadacentral","canadaeast","centralindia","centralus","centraluseuap",
"eastasia","eastus","eastus2","eastus2euap","francecentral","germanywestcentral","israelcentral",
"italynorth","japaneast","koreacentral","northcentralus","northeurope","norwayeast","southafricanorth",
"southcentralus","southeastasia","southindia","swedencentral","switzerlandnorth","uaenorth","uksouth",
"ukwest","westcentralus","westeurope","westus","westus2","westus3"
)
if (! [Environment]::Is64BitProcess) {
Write-Host "Error: Run this in 64bit Powershell session" -ForegroundColor Red
exit -1
}
#Validate inputs
$skipAzureArc = $false
if ([string]::IsNullOrEmpty($SubscriptionId)) {
Write-Host "Warning: Require SubscriptionId for Azure Arc" -ForegroundColor Cyan
$skipAzureArc = $true
}
if ([string]::IsNullOrEmpty($TenantId)) {
Write-Host "Warning: Require TenantId for Azure Arc" -ForegroundColor Cyan
$skipAzureArc = $true
}
if ([string]::IsNullOrEmpty($Location)) {
Write-Host "Warning: Require Location for Azure Arc" -ForegroundColor Cyan
$skipAzureArc = $true
} elseif ($arcLocations -inotcontains $Location) {
Write-Host "Error: Location $Location is not supported for Azure Arc" -ForegroundColor Red
Write-Host "Supported Locations : $arcLocations"
exit -1
}
if ($skipAzureArc) {
Write-Host "Azure setup and Arc connection will be skipped as required details are not available" -ForegroundColor Yellow
}
$installDir = $((Get-Location).Path)
$productName = "AKS Edge Essentials - K3s"
$networkplugin = "flannel"
if ($UseK8s) {
$productName ="AKS Edge Essentials - K8s"
$networkplugin = "calico"
}
# Here string for the json content
$aideuserConfig = @"
{
"SchemaVersion": "1.3",
"Version": "1.0",
"AksEdgeProduct": "$productName",
"AksEdgeProductUrl": "",
"Azure": {
"SubscriptionName": "",
"SubscriptionId": "$SubscriptionId",
"TenantId": "$TenantId",
"ResourceGroupName": "aksedge-rg",
"ServicePrincipalName": "aksedge-sp",
"Location": "$Location",
"CustomLocationOID":"",
"Auth":{
"ServicePrincipalId":"",
"Password":""
},
"ConnectedMachineName": ""
},
"AksEdgeConfigFile": "aksedge-config.json"
}
"@
$aksedgeConfig = @"
{
"SchemaVersion": "1.15",
"Version": "1.0",
"DeploymentType": "SingleMachineCluster",
"Init": {
"ServiceIPRangeSize": 10,
"KmsPlugin":{
"Enable": true
}
},
"Network": {
"NetworkPlugin": "$networkplugin",
"InternetDisabled": false
},
"User": {
"AcceptEula": true,
"AcceptOptionalTelemetry": true
},
"Machines": [
{
"LinuxNode": {
"CpuCount": 4,
"MemoryInMB": 4096,
"DataSizeInGB": 20
}
}
]
}
"@
###
# Main
###
if (-not (Test-Path -Path $installDir)) {
Write-Host "Creating $installDir..."
New-Item -Path "$installDir" -ItemType Directory | Out-Null
}
$starttime = Get-Date
$starttimeString = $($starttime.ToString("yyMMdd-HHmm"))
$transcriptFile = "$installDir\aksedgedlog-$starttimeString.txt"
Start-Transcript -Path $transcriptFile
Set-ExecutionPolicy Bypass -Scope Process -Force
# Download the AksEdgeDeploy modules from Azure/AksEdge
$fork ="Azure"
$branch="main"
$url = "https://github.com/$fork/AKS-Edge/archive/$branch.zip"
$zipFile = "AKS-Edge-$branch.zip"
$workdir = "$installDir\AKS-Edge-$branch"
if (-Not [string]::IsNullOrEmpty($Tag)) {
$url = "https://github.com/Azure/AKS-Edge/archive/refs/tags/$Tag.zip"
$zipFile = "$Tag.zip"
$workdir = "$installDir\AKS-Edge-$tag"
}
Write-Host "Step 1 : Azure/AKS-Edge repo setup"
if (!(Test-Path -Path "$installDir\$zipFile")) {
try {
Invoke-WebRequest -Uri $url -OutFile $installDir\$zipFile -UseBasicParsing
} catch {
Write-Host "Error: Downloading Aide Powershell Modules failed" -ForegroundColor Red
Stop-Transcript | Out-Null
Pop-Location
exit -1
}
}
if (!(Test-Path -Path "$workdir")) {
Expand-Archive -Path $installDir\$zipFile -DestinationPath "$installDir" -Force
}
$aidejson = (Get-ChildItem -Path "$workdir" -Filter aide-userconfig.json -Recurse).FullName
Set-Content -Path $aidejson -Value $aideuserConfig -Force
$aksedgejson = (Get-ChildItem -Path "$workdir" -Filter aksedge-config.json -Recurse).FullName
Set-Content -Path $aksedgejson -Value $aksedgeConfig -Force
$aksedgeShell = (Get-ChildItem -Path "$workdir" -Filter AksEdgeShell.ps1 -Recurse).FullName
. $aksedgeShell
# Setup Azure
Write-Host "Step 2: Setup Azure Cloud for Arc connections"
$azcfg = (Get-AideUserConfig).Azure
if ($azcfg.Auth.Password) {
Write-Host "Password found in json spec. Skipping AksEdgeAzureSetup." -ForegroundColor Cyan
$skipAzureArc = $false
} elseif ($skipAzureArc) {
Write-Host ">> skipping step 2" -ForegroundColor Yellow
} else {
$aksedgeazuresetup = (Get-ChildItem -Path "$workdir" -Filter AksEdgeAzureSetup.ps1 -Recurse).FullName
& $aksedgeazuresetup -jsonFile $aidejson -spContributorRole -spCredReset
if ($LastExitCode -eq -1) {
Write-Host "Error in configuring Azure Cloud for Arc connection" -ForegroundColor Red
Stop-Transcript | Out-Null
Pop-Location
exit -1
}
}
# Download, install and deploy AKS EE
Write-Host "Step 3: Download, install and deploy AKS Edge Essentials"
# invoke the workflow, the json file already updated above.
$retval = Start-AideWorkflow -jsonFile $aidejson
if ($retval) {
Write-Host "Deployment Successful. "
} else {
Write-Error -Message "Deployment failed" -Category OperationStopped
Stop-Transcript | Out-Null
Pop-Location
exit -1
}
Write-Host "Step 4: Connect to Arc"
if ($skipAzureArc) {
Write-Host ">> skipping step 4" -ForegroundColor Yellow
} else {
Write-Host "Installing required Az Powershell modules"
$arcstatus = Initialize-AideArc
if ($arcstatus) {
Write-Host ">Connecting to Azure Arc"
if (Connect-AideArc) {
Write-Host "Azure Arc connections successful."
} else {
Write-Host "Error: Azure Arc connections failed" -ForegroundColor Red
Stop-Transcript | Out-Null
Pop-Location
exit -1
}
} else { Write-Host "Error: Arc Initialization failed. Skipping Arc Connection" -ForegroundColor Red }
}
$endtime = Get-Date
$duration = ($endtime - $starttime)
Write-Host "Duration: $($duration.Hours) hrs $($duration.Minutes) mins $($duration.Seconds) seconds"
Stop-Transcript | Out-Null
Pop-Location
exit 0