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

Simplify CircleCI config further #19

Merged
merged 6 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rpi: Support custom resource group prefix
This will allow persistent environments between CI builds as the
resource group name becomes predictable.

Also output what resoure group is being deleted in teardown.
  • Loading branch information
seriema committed May 1, 2020
commit 05f851e27e77c27e89f7ec7d1d350f33021b5e00
15 changes: 12 additions & 3 deletions raspberry-pi/create-vm.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Take a parameter for prefixing the resource group name. It default to the current date to be unique
# yet findable. Useful values could be the build number during CI, or the users unique machine name.
param (
[string]$rgPrefix = [NullString]::Value
)

# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-powershell

# Abort on error
Expand Down Expand Up @@ -26,8 +32,11 @@ function ProgressHelper {
}

# Shared variables
$prefix = Get-Date -Format "yyyy-MM-dd__HH.mm.ss__"
$rg = "$($prefix)retro-cloud"
# If no prefix was passed as a parameter to the script, default to the current date.
if ([String]::IsNullOrEmpty($rgPrefix)) {
$rgPrefix = Get-Date -Format "yyyy-MM-dd__HH.mm.ss__"
}
$rg = "$($rgPrefix)__retro-cloud"
$loc = "EastUS"
$envVarFile="$HOME/.retro-cloud.env"

Expand Down Expand Up @@ -139,7 +148,7 @@ $nic | Format-Table
$currentActivity = "Create the storage account (for scraping cache and boot diagnostics)"

# Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.
$storageAccountName = ("$($prefix)storage" -replace '[^A-Za-z0-9]+', '').ToLower()
$storageAccountName = ("$($rgPrefix)storage" -replace '[^A-Za-z0-9]+', '').ToLower()

ProgressHelper $currentActivity "Creating the storage account"
$storageAccount = New-AzStorageAccount `
Expand Down
8 changes: 6 additions & 2 deletions raspberry-pi/download-and-run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash
# Script takes optional parameter for branch name or commit hash.
# Script takes optional parameters:
# 1: Branch name or commit hash. Useful for getting any development branch and still test as the user.
# 2: The prefix to use for the resource group in Azure. It default to the current date to be unique
# yet findable. Useful values could be the build number during CI, or the users unique machine name.
branch=${1:-master}
rgPrefix=${2:-''}

# Abort on error, error if variable is unset, and error if any pipeline element fails
set -euo pipefail
Expand Down Expand Up @@ -29,6 +33,6 @@ curl -fL -o "dev/run-tests.sh" "https://raw.githubusercontent.com/seriema/retro-
curl -fL -o "dev/test-az-share.ps1" "https://raw.githubusercontent.com/seriema/retro-cloud/$branch/raspberry-pi/dev/test-az-share.ps1"

echo "SETUP: Run setup.sh"
bash setup.sh
bash setup.sh "$rgPrefix"

echo "SETUP: Done!"
8 changes: 7 additions & 1 deletion raspberry-pi/setup-az.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Take a parameter for prefixing the Azure resource group name. It default to the current date to be unique
# yet findable. Useful values could be the build number during CI, or the users unique machine name.
param (
[string]$rgPrefix = [NullString]::Value
)

# Abort on error
$ErrorActionPreference = "Stop"

./install-az-module.ps1

./create-vm.ps1
./create-vm.ps1 $rgPrefix
5 changes: 4 additions & 1 deletion raspberry-pi/setup.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/bin/bash
# Take a parameter for prefixing the Azure resource group name. It default to the current date to be unique
# yet findable. Useful values could be the build number during CI, or the users unique machine name.
rgPrefix=${1:-''}

# Abort on error, error if variable is unset, and error if any pipeline element fails
set -euo pipefail
Expand All @@ -19,7 +22,7 @@ echo "SETUP: Install PowerShell"
bash install-ps.sh

echo "SETUP: Run PowerShell scripts to create the Azure resources"
pwsh -executionpolicy bypass -File ".\setup-az.ps1"
pwsh -executionpolicy bypass -File ".\setup-az.ps1" -rgPrefix "$rgPrefix"

echo "SETUP: Mount remote files"
# Run this in interactive mode, otherwise bash won't load the variables set in ~/.bashrc by the create-vm-share.sh script above.
Expand Down
23 changes: 20 additions & 3 deletions raspberry-pi/teardown-az.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Take a parameter for prefixing the Azure resource group name. This is useful when you want to
# destroy a resource group that was created outside of the scripts during development, or in CI
# when the resource group prefix is predictable and the teardown can happen in a context (such as a
# separate container) without '.retro-cloud.env' available.
param (
[string]$rgPrefix = [NullString]::Value
)

# Can run without prompting if the following environment variables are set:
# * AZURE_SERVICE_PRINCIPAL_SECRET

Expand All @@ -7,7 +15,16 @@ $ErrorActionPreference = "Stop"
# Enable debug output
$DebugPreference = "Continue"

'Delete resources from Azure ...'
# If no parameter was given, use what should be available during a normal installation.
if ([String]::IsNullOrEmpty($rgPrefix)) {
$resourceGroup = $env:RETROCLOUD_AZ_RESOURCE_GROUP
}
# When a parameter is given, use the same naming convention as create-vm.ps1
else {
$resourceGroup = "$($rgPrefix)__retro-cloud"
}

"Delete Azure resource group $resourceGroup ..."
if (!$env:AZURE_SERVICE_PRINCIPAL_SECRET) {
'... Note: This operation takes a very long time (15-20 min) so it will only send a command to Azure and not wait for the removal to be complete.'

Expand All @@ -16,11 +33,11 @@ if (!$env:AZURE_SERVICE_PRINCIPAL_SECRET) {
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Remove-AzResourceGroup -Name $env:RETROCLOUD_AZ_RESOURCE_GROUP -AsJob
Remove-AzResourceGroup -Name $resourceGroup -AsJob
} else {
Write-Host 'Cancelled.'
}
} else {
'... assuming automation with Service Principle. Do not wait for resources to be deleted.'
Remove-AzResourceGroup -Name $env:RETROCLOUD_AZ_RESOURCE_GROUP -Force -AsJob
Remove-AzResourceGroup -Name $resourceGroup -Force -AsJob
}
7 changes: 6 additions & 1 deletion raspberry-pi/teardown.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash
# Take a parameter for prefixing the Azure resource group name. This is useful when you want to
# destroy a resource group that was created outside of the scripts during development, or in CI
# when the resource group prefix is predictable and the teardown can happen in a context (such as a
# separate container) without '.retro-cloud.env' available.
rgPrefix=${1:-''}

# Abort on error, error if variable is unset, and error if any pipeline element fails
set -euo pipefail
Expand All @@ -8,6 +13,6 @@ set -euo pipefail
source "$HOME/.retro-cloud.env"

echo "TEARDOWN: Run PowerShell scripts to remove the Azure resources"
pwsh -executionpolicy bypass -File ".\teardown-az.ps1"
pwsh -executionpolicy bypass -File ".\teardown-az.ps1" -resourceGroup "$rgPrefix"

echo "TEARDOWN: Done!"