From cc66f930b0635e1abfcf07acbc088509db26fe2e Mon Sep 17 00:00:00 2001 From: OG Date: Wed, 20 Nov 2024 00:21:22 +0100 Subject: [PATCH] feat: add terramate --- terramate/README.md | 100 ++++++++++++++++++++++++++++++++++++++++++ terramate/install.ps1 | 56 +++++++++++++++++++++++ terramate/install.sh | 47 ++++++++++++++++++++ terramate/releases.js | 26 +++++++++++ 4 files changed, 229 insertions(+) create mode 100644 terramate/README.md create mode 100644 terramate/install.ps1 create mode 100644 terramate/install.sh create mode 100644 terramate/releases.js diff --git a/terramate/README.md b/terramate/README.md new file mode 100644 index 0000000..97c0451 --- /dev/null +++ b/terramate/README.md @@ -0,0 +1,100 @@ +--- +title: Terramate +homepage: https://github.com/terramate-io/terramate +tagline: | + Terramate simplifies managing large-scale Terraform codebases with a focus on automation and scalability. +--- + +To update or switch versions, run `webi terramate@stable` (or `@v1.0.0`, +`@beta`, etc). + +## Cheat Sheet + +The information in this section is a copy of the preflight requirements and +common command-line arguments from Terramate +(https://github.com/terramate-io/terramate). + +> `Terramate` enables scalable automation for Terraform by providing a robust +> framework for managing multiple stacks, generating code, and executing +> targeted workflows. + + +### **1. Create a New Project** +```bash +git init -b main terramate-quickstart +cd terramate-quickstart +git commit --allow-empty -m "Initial empty commit" +``` + +--- + +### **2. Create a Stack** +```bash +terramate create \ + --name "StackName" \ + --description "Description of the stack" \ + stacks/stackname + +git add stacks/stackname/stack.tm.hcl +git commit -m "Create a stack" +``` + +--- + +### **3. List Stacks** +```bash +terramate list +``` + +--- + +### **4. Detect Changes** +```bash +terramate list --changed +``` + +--- + +### **5. Generate Code** +1. Create a `.tm.hcl` file for code generation: + ```bash + cat <stacks/backend.tm.hcl + generate_hcl "backend.tf" { + content { + terraform { + backend "local" {} + } + } + } + EOF + ``` + +2. Run the generation command: + ```bash + terramate generate + ``` + +--- + +### **6. Run Terraform Commands** +- **Initialize stacks:** + ```bash + terramate run terraform init + ``` + +- **Plan changes:** + ```bash + terramate run terraform plan + ``` + +- **Apply changes:** + ```bash + terramate run terraform apply -auto-approve + ``` + +- **Run commands only on changed stacks:** + ```bash + terramate run --changed terraform init + terramate run --changed terraform plan + terramate run --changed terraform apply -auto-approve + ``` diff --git a/terramate/install.ps1 b/terramate/install.ps1 new file mode 100644 index 0000000..ca38f9c --- /dev/null +++ b/terramate/install.ps1 @@ -0,0 +1,56 @@ +#!/usr/bin/env pwsh + +############### +# Install terramate # +############### + +# Every package should define these variables +$pkg_cmd_name = "terramate" + +$pkg_dst_cmd = "$Env:USERPROFILE\.local\bin\terramate.exe" +$pkg_dst = "$pkg_dst_cmd" + +$pkg_src_cmd = "$Env:USERPROFILE\.local\opt\terramate-v$Env:WEBI_VERSION\bin\terramate.exe" +$pkg_src_bin = "$Env:USERPROFILE\.local\opt\terramate-v$Env:WEBI_VERSION\bin" +$pkg_src_dir = "$Env:USERPROFILE\.local\opt\terramate-v$Env:WEBI_VERSION" +$pkg_src = "$pkg_src_cmd" + +New-Item "$Env:USERPROFILE\Downloads\webi" -ItemType Directory -Force | Out-Null +$pkg_download = "$Env:USERPROFILE\Downloads\webi\$Env:WEBI_PKG_FILE" + +# Fetch archive +IF (!(Test-Path -Path "$Env:USERPROFILE\Downloads\webi\$Env:WEBI_PKG_FILE")) { + Write-Output "Downloading terramate from $Env:WEBI_PKG_URL to $pkg_download" + & curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part" + & Move-Item "$pkg_download.part" "$pkg_download" +} + +IF (!(Test-Path -Path "$pkg_src_cmd")) { + Write-Output "Installing terramate" + + # TODO: create package-specific temp directory + # Enter tmp + Push-Location .local\tmp + + # Remove any leftover tmp cruft + Remove-Item -Path ".\terramate-v*" -Recurse -ErrorAction Ignore + Remove-Item -Path ".\terramate.exe" -Recurse -ErrorAction Ignore + + # Unpack archive file into this temporary directory + # Windows BSD-tar handles zip. Imagine that. + Write-Output "Unpacking $pkg_download" + & tar xf "$pkg_download" + + # Settle unpacked archive into place + Write-Output "Install Location: $pkg_src_cmd" + New-Item "$pkg_src_bin" -ItemType Directory -Force | Out-Null + Move-Item -Path "terramate.exe" -Destination "$pkg_src_bin" + + # Exit tmp + Pop-Location +} + +Write-Output "Copying into '$pkg_dst_cmd' from '$pkg_src_cmd'" +Remove-Item -Path "$pkg_dst_cmd" -Recurse -ErrorAction Ignore | Out-Null +Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse +Remove-Item -Path "$pkg_src" -Recurse -ErrorAction Ignore diff --git a/terramate/install.sh b/terramate/install.sh new file mode 100644 index 0000000..294e00b --- /dev/null +++ b/terramate/install.sh @@ -0,0 +1,47 @@ +#!/bin/sh +set -e +set -u + +__init_terramate() { + + ##################### + # Install terramate # + ##################### + + # Every package should define these 6 variables + pkg_cmd_name="terramate" + + pkg_dst_cmd="$HOME/.local/bin/terramate" + pkg_dst="$pkg_dst_cmd" + + pkg_src_cmd="$HOME/.local/opt/terramate-v$WEBI_VERSION/bin/terramate" + pkg_src_dir="$HOME/.local/opt/terramate-v$WEBI_VERSION" + pkg_src="$pkg_src_cmd" + + # pkg_install must be defined by every package + pkg_install() { + # ~/.local/opt/terramate-v0.99.9/bin + mkdir -p "$(dirname "$pkg_src_cmd")" + + # mv ./terramate-*/terramate ~/.local/opt/terramate-v0.99.9/bin/terramate + mv terramate "$pkg_src_cmd" + } + + # pkg_get_current_version is recommended, but (soon) not required + pkg_get_current_version() { + # 'terramate version' has output in this format: + + # 0.10.4 + + # Your version of Terramate is out of date! The latest version + # is 0.11.1 (released on Wed Oct 30 15:37:00 UTC 2024). + # You can update by downloading from https://github.com/mineiros-io/terramate/releases/tag/v0.11.1 + + # This trims it down to just the version number: + # 0.10.4 + terramate version | grep '^[0-9]\+\.[0-9]\+\.[0-9]\+$' + } + +} + +__init_terramate diff --git a/terramate/releases.js b/terramate/releases.js new file mode 100644 index 0000000..91d2e2b --- /dev/null +++ b/terramate/releases.js @@ -0,0 +1,26 @@ +'use strict'; + +var github = require('../_common/github.js'); +var owner = 'terramate-io'; +var repo = 'terramate'; + +module.exports = function () { + return github(null, owner, repo).then(function (all) { + all.releases = all.releases.filter( + (release) => !['checksums.txt', 'cosign.pub'].includes(release.name), + ); + return all; + }); +}; + +if (module === require.main) { + module.exports().then(function (all) { + all = require('../_webi/normalize.js')(all); + // just select the first 5 for demonstration + // all.releases = all.releases.slice(0, 5); + // all.releases = all.releases.filter( + // release => !["checksums.txt.sig", "cosign.pub","terramate_0.9.0_windows_x86_64.zip"].includes(release.name) + // ); + console.info(JSON.stringify(all, null, 2)); + }); +}