Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
48 changes: 26 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The [`tools/`](tools/) directory contains scripts and helpers to **publish these

## Uploading to StackGen

Uploads use the **StackGen CLI**. The script **`tools/upload_stackgen_modules.sh`** is a thin wrapper: for each module it runs **`stackgen upload custom-modules`** (with `--provider`, `--name`, and optional `--repo-url` / `--branch` / `--tag` / `--project`).
Uploads use the **StackGen CLI**. The script **`tools/upload_stackgen_modules.sh`** bulk-uploads modules by running **`stackgen upload custom-modules`** in parallel (with `--provider`, `--name`, and optional `--repo-url` / `--branch` / `--tag` / `--project`). See [`tools/README.md`](tools/README.md) for full documentation, flag reference, and usage examples.

Modules scanned are the immediate subdirectories of **`aws/`**, **`azurerm/`**, and **`gcp/`**, with optional `--templates` filtering.

Expand All @@ -51,36 +51,40 @@ Modules scanned are the immediate subdirectories of **`aws/`**, **`azurerm/`**,

### Options

| Flag | Required | Description |
|------|----------|-------------|
| `--token` | Yes | StackGen authentication token |
| `--url` | No | StackGen base URL (otherwise use your CLI default / env) |
| `--project` | No | Project ID to upload modules into |
| `--templates` | No | Comma-separated module folder names (e.g. `aws_ec2,aws_s3`) to upload only those |
| `--repo-url` | No | Repository URL for source tracking in StackGen |
| `--branch` | No* | Git branch name (use only one of `--branch` or `--tag`) |
| `--tag` | No* | Git tag name |
| Flag | Required | Default | Description |
|------|----------|---------|-------------|
| `--token` | Yes | — | StackGen authentication token |
| `--url` | No | CLI default | StackGen base URL (e.g., `https://seti.cloud.stackgen.com`) |
| `--project` | No | — | Project ID for auth context (modules are always org-wide) |
| `--provider` | No | All | Filter to a single provider: `aws`, `azurerm`, or `gcp` |
| `--templates` | No | All modules | Comma-separated module folder names (e.g. `aws_ec2,aws_s3`) |
| `--repo-url` | No | — | Repository URL for source tracking in StackGen |
| `--branch` | No | — | Git branch name (mutually exclusive with `--tag`; requires `--repo-url`) |
| `--tag` | No | — | Git tag name (mutually exclusive with `--branch`; requires `--repo-url`) |
| `--version` | No | `1.0` | Module version string |
| `--overwrite-version` | No | `false` | Overwrite an existing version instead of skipping |
| `--parallel` | No | `10` | Number of concurrent uploads |

### Behavior

- **Minimal input**: Only `--token` is strictly required by the script.
- **Batch upload**: Without `--templates`, all modules in those provider trees are uploaded (one CLI invocation per module).
- **Parallel uploads**: Modules are uploaded concurrently using `xargs -P` (default: 10 workers, tunable with `--parallel`).
- **Retry with backoff**: Each upload is retried up to 3 times with exponential backoff (1s → 2s → 4s) for transient failures.
- **Provider mapping**: `azurerm` modules are uploaded with StackGen provider **`azure`**.
- **Skip existing**: If the CLI reports that the version name already exists, that module is skipped and the script continues.
- **Errors**: Other failures stop the run with a non-zero exit code.
- **Skip existing**: If the CLI reports that the version name already exists, that module is skipped (use `--overwrite-version` to force).
- **Fail-at-end**: Failures are collected and reported in a summary after all uploads complete, rather than stopping on the first error.
- **Cross-platform**: Works on both macOS (BSD) and Linux (GNU).

## Tools

Utilities for **publishing** and **maintaining** discovery modules (upload flow above).
Utilities for **publishing** and **maintaining** discovery modules. See [`tools/README.md`](tools/README.md) for full documentation.

### `upload_stackgen_modules.sh`

For each module under **`aws/`**, **`azurerm/`**, and **`gcp/`**, this script invokes **`stackgen upload custom-modules`**. Usage, flags, and behavior are documented under [Uploading to StackGen](#uploading-to-stackgen).

### Other files

- **`stackgen_yaml_schema.json`** — JSON Schema for `.stackgen/stackgen.yaml` (validation and editor support).
- **`dummy.yaml`** — Sample / fixture input for development.
| Script | Description |
|--------|-------------|
| **`upload_stackgen_modules.sh`** | Bulk-uploads modules to StackGen with parallel execution, retries, and progress tracking. See [Uploading to StackGen](#uploading-to-stackgen). |
| **`bulk-tag-modules.sh`** | Creates `v1.0.0` Git tags for all module subdirectories (dry-run by default). Required by the `module-backfill.yml` workflow. |
| **`stackgen_yaml_schema.json`** | JSON Schema for `.stackgen/stackgen.yaml` (validation and editor support). |
| **`dummy.yaml`** | Sample / fixture input for development. |

## Versioning and provider compatibility

Expand Down
168 changes: 168 additions & 0 deletions tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# Tools

Helper scripts for managing and publishing StackGen discovery modules.

---

## upload_stackgen_modules.sh

Bulk-uploads Terraform module directories to the StackGen custom module registry using the `stackgen` CLI. Supports parallel execution for fast uploads of large module libraries.

### Prerequisites

- **`stackgen` CLI** installed and available on `$PATH`
- A valid **StackGen API token** (passed via `--token`)
- Modules organized under `aws/`, `azurerm/`, and/or `gcp/` provider directories

### Usage

```bash
./tools/upload_stackgen_modules.sh --token <token> [OPTIONS]
```

### Flags

| Flag | Required | Default | Description |
|------|----------|---------|-------------|
| `--token <token>` | **Yes** | — | StackGen API token for authentication |
| `--url <url>` | No | CLI default | StackGen instance URL (e.g., `https://seti.cloud.stackgen.com`) |
| `--project <id>` | No | — | Project ID for auth context (does **not** scope module visibility — modules are always org-wide) |
| `--provider <name>` | No | All providers | Filter to a single provider: `aws`, `azurerm`, or `gcp` |
| `--templates <list>` | No | All modules | Comma-separated list of module names to upload (e.g., `aws_s3_bucket,aws_iam_role`) |
| `--repo-url <url>` | No | — | Git repository URL for StackGen to reference (e.g., `https://github.com/org/repo`) |
| `--branch <branch>` | No | — | Git branch to reference. Mutually exclusive with `--tag`. Requires `--repo-url` |
| `--tag <tag>` | No | — | Git tag to reference. Mutually exclusive with `--branch`. Requires `--repo-url` |
| `--version <ver>` | No | `1.0` (CLI default) | Module version string |
| `--overwrite-version` | No | `false` | Overwrite an existing version instead of skipping |
| `--parallel <N>` | No | `10` | Number of concurrent uploads |

### Examples

#### Upload all modules (all providers)

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com"
```

#### Upload only AWS modules

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--provider aws
```

#### Upload specific modules by name

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--templates "aws_s3_bucket,aws_iam_role,aws_lambda_function"
```

#### Upload from a specific branch with repo reference

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--repo-url "https://github.com/stackgenhq/discovery-modules" \
--branch "main"
```

#### Overwrite existing module versions

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--overwrite-version
```

#### Bump to a new version

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--version "2.0" \
--provider aws
```

#### Increase parallelism for faster uploads

```bash
./tools/upload_stackgen_modules.sh \
--token "$STACKGEN_TOKEN" \
--url "https://seti.cloud.stackgen.com" \
--parallel 20
```

### How it works

1. **Module discovery** — Scans `aws/`, `azurerm/`, and `gcp/` directories (or a single provider if `--provider` is set) for subdirectories. Each subdirectory is treated as one module. If `--templates` is provided, only the named modules are uploaded.

2. **Parallel dispatch** — Modules are uploaded concurrently using `xargs -P`. The default concurrency is 10, tunable with `--parallel`.

3. **Retry with backoff** — Each module upload is retried up to 3 times with exponential backoff (1s → 2s → 4s) for transient API failures. "Version already exists" errors are detected immediately and skipped without retrying.

4. **Provider mapping** — The provider name is derived from the parent directory (`aws` → `aws`, `gcp` → `gcp`). The `azurerm` directory is mapped to `azure` since the StackGen CLI uses `azure` as the provider name.

5. **Progress tracking** — A thread-safe atomic counter (using `mkdir`-based spinlocking) provides real-time `[N/Total]` progress output, even across parallel subshells. Works on both macOS (BSD) and Linux (GNU).

6. **Summary report** — After all uploads complete, a structured summary shows succeeded, skipped (version exists), and failed counts. Failed module names and error messages are listed. The script exits non-zero only if there were failures.

### Output example

```
Uploading 817 module(s) (parallelism: 10)...
[1/817] ✓ aws/aws_s3_bucket
[2/817] ✓ aws/aws_iam_role
[3/817] ⊘ aws/aws_lambda_function (skipped: version exists)
[4/817] ✗ aws/aws_bad_module (FAILED after 3 attempts)
...

===== Upload Summary =====
Succeeded: 815
Skipped: 1 (version already exists)
Failed: 1
Total: 817

===== Failed Modules =====
aws/aws_bad_module: Error: invalid module configuration
```

---

## bulk-tag-modules.sh
Comment thread
asarkar157 marked this conversation as resolved.

Creates `v1.0.0` Git tags for all module subdirectories. These tags are required by the `module-backfill.yml` GitHub Actions workflow to discover and upload modules.

### Usage

```bash
./tools/bulk-tag-modules.sh # Dry run — shows what would be tagged
./tools/bulk-tag-modules.sh --apply # Creates tags locally
./tools/bulk-tag-modules.sh --apply --push # Creates tags and pushes to remote
```

### Tag format

```
<module-subdirectory-name>-v1.0.0
```

Examples: `aws_s3_bucket-v1.0.0`, `azurerm_resource_group-v1.0.0`, `google_compute_instance-v1.0.0`

---

## Other files

| File | Description |
|------|-------------|
| `stackgen_yaml_schema.json` | JSON Schema for `.stackgen/stackgen.yaml` files. Use for editor validation and CI linting. |
| `dummy.yaml` | Sample fixture input for development and testing. |
132 changes: 132 additions & 0 deletions tools/bulk-tag-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash
# bulk-tag-modules.sh
#
# Creates v1.0.0 tags for all module subdirectories in aws/, azurerm/, and gcp/.
# These tags are required for the module-backfill.yml workflow to discover and upload modules.
#
# Usage:
# ./tools/bulk-tag-modules.sh # Dry run (default) — shows what would be tagged
# ./tools/bulk-tag-modules.sh --apply # Creates tags locally
# ./tools/bulk-tag-modules.sh --apply --push # Creates tags and pushes to remote
#
# Tag format: <module-subdirectory-name>-v1.0.0
# Examples:
# aws_s3_bucket-v1.0.0
# azurerm_resource_group-v1.0.0
# google_compute_instance-v1.0.0

set -euo pipefail

# Parse flags
APPLY=false
PUSH=false
for arg in "$@"; do
case "$arg" in
--apply) APPLY=true ;;
--push) PUSH=true ;;
--help|-h)
echo "Usage: $0 [--apply] [--push]"
echo ""
echo " --apply Create tags locally (without this flag, dry run only)"
echo " --push Push tags to remote (implies --apply)"
exit 0
;;
esac
done

if [ "$PUSH" = true ]; then
APPLY=true
fi

# Ensure we're in the repo root
REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || echo "")"
if [ -z "$REPO_ROOT" ]; then
echo "❌ Not in a git repository"
exit 1
fi
cd "$REPO_ROOT"

VERSION="v1.0.0"
CREATED=0
SKIPPED=0
TOTAL=0
TAGS_TO_PUSH=()

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 BULK MODULE TAGGER"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
if [ "$APPLY" = false ]; then
echo "🔍 DRY RUN MODE — no tags will be created"
echo " Run with --apply to create tags"
echo ""
fi

for PROVIDER_DIR in aws azurerm gcp; do
if [ ! -d "$PROVIDER_DIR" ]; then
echo "⚠️ Directory not found: $PROVIDER_DIR — skipping"
continue
fi

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📂 Processing: $PROVIDER_DIR/"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

DIR_COUNT=0
for MODULE_DIR in "$PROVIDER_DIR"/*/; do
# Strip trailing slash to get directory name
MODULE_DIR="${MODULE_DIR%/}"
MODULE_NAME="$(basename "$MODULE_DIR")"
TAG_NAME="${MODULE_NAME}-${VERSION}"

TOTAL=$((TOTAL + 1))
DIR_COUNT=$((DIR_COUNT + 1))

# Check if tag already exists
if git rev-parse "$TAG_NAME" >/dev/null 2>&1; then
echo " ⏭ $TAG_NAME (already exists)"
SKIPPED=$((SKIPPED + 1))
continue
fi

if [ "$APPLY" = true ]; then
git tag "$TAG_NAME"
echo " ✅ $TAG_NAME"
TAGS_TO_PUSH+=("$TAG_NAME")
else
echo " 🔍 $TAG_NAME (would create)"
fi
CREATED=$((CREATED + 1))
done

echo " → $DIR_COUNT modules in $PROVIDER_DIR/"
echo ""
done

# Summary
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 SUMMARY"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " Total modules: $TOTAL"
echo " Tags to create: $CREATED"
echo " Already tagged: $SKIPPED"
if [ "$APPLY" = false ]; then
echo " Mode: DRY RUN"
echo ""
echo "👉 Run with --apply to create tags locally"
echo "👉 Run with --apply --push to create and push tags"
else
echo " Mode: APPLIED"
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Push if requested
if [ "$PUSH" = true ] && [ ${#TAGS_TO_PUSH[@]} -gt 0 ]; then
echo ""
echo "📤 Pushing ${#TAGS_TO_PUSH[@]} tags to origin..."
git push origin "${TAGS_TO_PUSH[@]}"
echo "✅ All tags pushed"
elif [ "$PUSH" = true ] && [ ${#TAGS_TO_PUSH[@]} -eq 0 ]; then
echo ""
echo "ℹ️ No new tags to push"
fi
Loading