Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure preprovision script is running in the proper context and fix azd env set error #1961

Closed
wants to merge 6 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 45 additions & 25 deletions azure_jumpstart_ag/scripts/preprovision.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
########################################################################
# Connect to Azure
########################################################################

Write-Host "Connecting to Azure..."

# Install Azure module if not already installed
if (-not (Get-Command -Name Get-AzContext)) {
Write-Host "Installing Azure module..."
Install-Module -Name Az -AllowClobber -Scope CurrentUser -ErrorAction Stop
}

# If not signed in, run the Connect-AzAccount cmdlet
if (-not (Get-AzContext)) {
Write-Host "Logging in to Azure..."
If (-not (Connect-AzAccount -SubscriptionId $env:AZURE_SUBSCRIPTION_ID -ErrorAction Stop)){
Throw "Unable to login to Azure. Please check your credentials and try again."
}
}

# Write-Host "Getting Azure Tenant Id..."
$tenantId = (Get-AzSubscription -SubscriptionId $env:AZURE_SUBSCRIPTION_ID).TenantId

# Write-Host "Setting Azure context..."
$context = Set-AzContext -SubscriptionId $env:AZURE_SUBSCRIPTION_ID -Tenant $tenantId -ErrorAction Stop

# Write-Host "Setting az subscription..."
$azLogin = az account set --subscription $env:AZURE_SUBSCRIPTION_ID


########################################################################
# Check for available capacity in region
Expand Down Expand Up @@ -91,12 +120,7 @@ $JS_WINDOWS_ADMIN_USERNAME = 'arcdemo'
if ($promptOutput = Read-Host "Enter the Windows Admin Username [$JS_WINDOWS_ADMIN_USERNAME]") { $JS_WINDOWS_ADMIN_USERNAME = $promptOutput }

# set the env variable
azd env set JS_WINDOWS_ADMIN_USERNAME $JS_WINDOWS_ADMIN_USERNAME

# The user will be prompted for this by azd so we can maintain the security of the password.
# $JS_WINDOWS_ADMIN_PASSWORD = Read-Host "Enter the Windows Admin Password (hint: ArcPassword123!! - 12 character minimum)" -AsSecureString

# azd env set JS_WINDOWS_ADMIN_PASSWORD $JS_WINDOWS_ADMIN_PASSWORD
azd env set JS_WINDOWS_ADMIN_USERNAME -- $JS_WINDOWS_ADMIN_USERNAME


########################################################################
Expand All @@ -117,21 +141,27 @@ azd env set JS_RDP_PORT $JS_RDP_PORT
########################################################################
$JS_GITHUB_USER = $env:JS_GITHUB_USER

if ($promptOutput = Read-Host "Enter your GitHub user name [$JS_GITHUB_USER]") { $JS_GITHUB_USER = $promptOutput }
$defaultGhUser = ""
If ($JS_GITHUB_USER) { $defaultGhUser = " [$JS_GITHUB_USER]"}

if ($promptOutput = Read-Host "Enter your GitHub user name$defaultGhUser") { $JS_GITHUB_USER = $promptOutput }

# set the env variable
azd env set JS_GITHUB_USER $JS_GITHUB_USER
azd env set JS_GITHUB_USER -- $JS_GITHUB_USER


########################################################################
# GitHub Personal Access Token
########################################################################
$JS_GITHUB_PAT = $env:JS_GITHUB_PAT

if ($promptOutput = Read-Host "Enter your GitHub Personal Access Token (PAT) [$JS_GITHUB_PAT]") { $JS_GITHUB_PAT = $promptOutput }
$defaultPAT = ""
If ($JS_GITHUB_PAT) { $defaultPAT = " [$JS_GITHUB_PAT]"}

if ($promptOutput = Read-Host "Enter your GitHub Personal Access Token (PAT)$defaultPAT") { $JS_GITHUB_PAT = $promptOutput }

# set the env variable
azd env set JS_GITHUB_PAT $JS_GITHUB_PAT
azd env set JS_GITHUB_PAT -- $JS_GITHUB_PAT


########################################################################
Expand All @@ -151,25 +181,15 @@ $JS_SSH_RSA_PUBLIC_KEY = get-content "$file.pub"
$JS_SSH_RSA_PUBLIC_KEY = $JS_SSH_RSA_PUBLIC_KEY.Replace("\", "\\")

# set the env variable
azd env set JS_SSH_RSA_PUBLIC_KEY $JS_SSH_RSA_PUBLIC_KEY
azd env set JS_SSH_RSA_PUBLIC_KEY -- $JS_SSH_RSA_PUBLIC_KEY


########################################################################
# Create Azure Service Principal
########################################################################
Write-Host "Creating Azure Service Principal..."
# Install Azure module if not already installed
if (-not (Get-Command -Name Get-AzContext)) {
Write-Host "Installing Azure module..."
Install-Module -Name Az -AllowClobber -Scope CurrentUser
}

# If not signed in, run the Connect-AzAccount cmdlet
if (-not (Get-AzContext)) {
Connect-AzAccount
}

$user = (get-azcontext).Account.Id.split("@")[0]
$user = $context.Account.Id.split("@")[0]
$uniqueSpnName = "$user-jumpstart-spn-$(Get-Random -Minimum 1000 -Maximum 9999)"
try {
$spn = New-AzADServicePrincipal -DisplayName $uniqueSpnName -Role "Owner" -Scope "/subscriptions/$($env:AZURE_SUBSCRIPTION_ID)" -ErrorAction Stop
Expand All @@ -191,7 +211,7 @@ $SPN_CLIENT_SECRET = $spn.PasswordCredentials.SecretText
$SPN_TENANT_ID = (Get-AzContext).Tenant.Id

# Set environment variables
azd env set SPN_CLIENT_ID $SPN_CLIENT_ID
azd env set SPN_CLIENT_SECRET $SPN_CLIENT_SECRET
azd env set SPN_TENANT_ID $SPN_TENANT_ID
azd env set SPN_CLIENT_ID -- $SPN_CLIENT_ID
azd env set SPN_CLIENT_SECRET -- $SPN_CLIENT_SECRET
azd env set SPN_TENANT_ID -- $SPN_TENANT_ID