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

Fix user-assigned identity example and other small fixes #18

Merged
merged 9 commits into from
Nov 17, 2023
8 changes: 5 additions & 3 deletions .spacelift/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
version: 1
module_version: 0.0.7
module_version: 0.1.0

tests:
- name: System-assigned identity
project_root: examples/system-assigned-identity

- name: Password authentication
project_root: examples/password-authentication
# This test case is currently disabled because the destroy is failing when trying to delete
# the KeyVault. It looks like it may be related to this issue: https://github.com/hashicorp/terraform-provider-azurerm/issues/19322
# - name: Password authentication
# project_root: examples/password-authentication
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ image before trying to use the module.
```hcl
terraform {
required_providers {
google = {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 2.68.0"
version = "=3.61.0"
}
}
}

module "azure-worker" {
source = "github.com/spacelift-io/terraform-azure-spacelift-workerpool?ref=v0.0.6"
source = "github.com/spacelift-io/terraform-azure-spacelift-workerpool?ref=v0.1.0"

admin_password = "Super Secret Password!"

Expand Down
2 changes: 1 addition & 1 deletion examples/bastion/keyvault.tf
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ resource "azurerm_key_vault_secret" "ssh_private_key" {

resource "azurerm_key_vault_secret" "worker_pool_config" {
name = "worker-pool-config"
value = base64encode(var.worker_pool_config)
value = var.worker_pool_config
key_vault_id = azurerm_key_vault.this.id
}

Expand Down
4 changes: 2 additions & 2 deletions examples/bastion/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ module "azure-worker" {
--vault-name "${azurerm_key_vault.this.name}" \
--file "/tmp/worker-pool-private-key" 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log

export SPACELIFT_TOKEN=$(cat /tmp/worker-pool-config | base64 --decode)
export SPACELIFT_POOL_PRIVATE_KEY=$(cat /tmp/worker-pool-private-key | base64 --decode)
export SPACELIFT_TOKEN=$(cat /tmp/worker-pool-config)
export SPACELIFT_POOL_PRIVATE_KEY=$(cat /tmp/worker-pool-private-key)

rm /tmp/worker-pool-config
rm /tmp/worker-pool-private-key
Expand Down
2 changes: 1 addition & 1 deletion examples/user-assigned-identity/keyvault.tf
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ resource "azurerm_key_vault" "this" {

resource "azurerm_key_vault_secret" "worker_pool_config" {
name = "worker-pool-config"
value = base64encode(var.worker_pool_config)
value = var.worker_pool_config
key_vault_id = azurerm_key_vault.this.id
}

Expand Down
6 changes: 3 additions & 3 deletions examples/user-assigned-identity/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module "azure-worker" {
# from KeyVault, and then configures the environment variables the Spacelift worker will
# read them from.
configuration = <<-EOT
az login --identity
az login --identity --username "${azurerm_user_assigned_identity.vmss.id}" 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log

echo "Downloading worker pool credentials from KeyVault" >> /var/log/spacelift/info.log
az keyvault secret download --name "${azurerm_key_vault_secret.worker_pool_config.name}" \
Expand All @@ -38,8 +38,8 @@ module "azure-worker" {
--vault-name "${azurerm_key_vault.this.name}" \
--file "/tmp/worker-pool-private-key" 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log

export SPACELIFT_TOKEN=$(cat /tmp/worker-pool-config | base64 --decode)
export SPACELIFT_POOL_PRIVATE_KEY=$(cat /tmp/worker-pool-private-key | base64 --decode)
export SPACELIFT_TOKEN=$(cat /tmp/worker-pool-config)
export SPACELIFT_POOL_PRIVATE_KEY=$(cat /tmp/worker-pool-private-key)

rm /tmp/worker-pool-config
rm /tmp/worker-pool-private-key
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ variable "process_exit_behavior" {
}
}

variable "perform_unattended_upgrade_on_boot" {
type = bool
description = "Indicates whether unattended-upgrade should be run on startup to ensure the latest security updates are installed. Defaults to true."
default = true
}

locals {
namespace = "${var.name_prefix}-${var.worker_pool_id}"
}
18 changes: 13 additions & 5 deletions vmss.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ ${local.exit_command_map[var.process_exit_behavior].command}
#!/bin/bash
spacelift () {(
set -e

# Ensure the Spacelift log directory exists in case it hasn't been provisioned on the VM image
mkdir -p /var/log/spacelift
EOF

worker_script_tail = <<EOF
Expand All @@ -29,8 +32,11 @@ binaryURL=$(printf "%s-%s" "$baseURL" "$currentArch")
shaSumURL=$(printf "%s-%s_%s" "$baseURL" "$currentArch" "SHA256SUMS")
shaSumSigURL=$(printf "%s-%s_%s" "$baseURL" "$currentArch" "SHA256SUMS.sig")

echo "Updating packages" >> /var/log/spacelift/info.log
unattended-upgrade -d 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log
if [[ "${var.perform_unattended_upgrade_on_boot}" == "true" ]]; then
echo "Updating packages" >> /var/log/spacelift/info.log
apt-get update 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log
unattended-upgrade -d 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log
fi

echo "Downloading Spacelift launcher" >> /var/log/spacelift/info.log
curl "$binaryURL" --output /usr/bin/spacelift-launcher 2>>/var/log/spacelift/error.log
Expand All @@ -43,7 +49,7 @@ echo "Verifying checksum signature..." >> /var/log/spacelift/info.log
gpg --verify spacelift-launcher_SHA256SUMS.sig 1>>/var/log/spacelift/info.log 2>>/var/log/spacelift/error.log
retStatus=$?
if [ $retStatus -eq 0 ]; then
echo "OK\!" >> /var/log/spacelift/info.log
echo "OK!" >> /var/log/spacelift/info.log
else
return $retStatus
fi
Expand All @@ -52,7 +58,7 @@ rm spacelift-launcher_SHA256SUMS spacelift-launcher_SHA256SUMS.sig
LAUNCHER_SHA=$(sha256sum /usr/bin/spacelift-launcher | cut -f 1 -d ' ')
echo "Verifying launcher binary..." >> /var/log/spacelift/info.log
if [[ "$CHECKSUM" == "$LAUNCHER_SHA" ]]; then
echo "OK\!" >> /var/log/spacelift/info.log
echo "OK!" >> /var/log/spacelift/info.log
else
echo "Checksum and launcher binary hash did not match" >> /var/log/spacelift/error.log
return 1
Expand Down Expand Up @@ -165,7 +171,9 @@ resource "azurerm_linux_virtual_machine_scale_set" "this" {

custom_data = base64encode(local.user_data)

scale_in_policy = "OldestVM"
scale_in {
rule = "OldestVM"
}

tags = merge(var.tags,
{
Expand Down