@intentius/behold 0.29.25 → 0.29.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,7 +18,7 @@ in the choudoufu repo), copied:
18
18
  the monolith does not, and team-b reads it through a data source filtered
19
19
  on team-a's marker tags, the cross-estate reference `live-check -json`
20
20
  states, drawn as an edge between the two boxes.
21
- - **monolith/carve.json.** The plan: team-a's five taggable resources move to
21
+ - **monolith/split.json.** The split plan: team-a's five taggable resources move to
22
22
  `tlmig-sample-team-a`. The scope panel shows each move with the
23
23
  `choudoufu live-mv` line to run and a link to the morph; behold never runs
24
24
  the write.
@@ -41,7 +41,7 @@ choudoufu live-mv -from-estate=tlmig-sample-monolith aws_iam_role.team_a aws_iam
41
41
 
42
42
  Reload the overlay: the role and its two followers are green in team-a's box,
43
43
  the monolith's card for it reads *owned by tlmig-sample-team-a*, and
44
- `/api/choudoufu/moves?plan=carve.json&receipt=1` says `moved`. Apply team-a
44
+ `/api/choudoufu/split?plan=split.json&receipt=1` says `moved`. Apply team-a
45
45
  afterwards to bring its VPC up, and team-b's read resolves.
46
46
 
47
47
  `bash scripts/choudoufu-down.sh` removes the container. The `.terraform/`
@@ -3,7 +3,7 @@
3
3
  # (the AWS emulator), provider schemas in each estate copy, and the terralith
4
4
  # applied — one estate with three teams' worth of IAM and log groups in it,
5
5
  # the shape the live-mv workbench splits. The team estates start empty: their
6
- # declared resources are the monolith's, and the plan in monolith/carve.json
6
+ # declared resources are the monolith's, and the plan in monolith/split.json
7
7
  # says which move where.
8
8
  #
9
9
  # Scratch discipline (behold's src/scratch.ts): the container is named
@@ -80,4 +80,4 @@ else
80
80
  echo "→ no 'aws' on PATH, so the adoptable card is skipped; everything else stands up"
81
81
  fi
82
82
 
83
- echo "choudoufu-estate demo: up. behold serves it next; the plan in monolith/carve.json moves team-a's resources out."
83
+ echo "choudoufu-estate demo: up. behold serves it next; the plan in monolith/split.json moves team-a's resources out."
@@ -0,0 +1,32 @@
1
+ # terralith: adopt and split, on a scratch floci
2
+
3
+ One Terraform root that holds everything, taken to four choudoufu estates in
4
+ two reviewed steps. [choudoufu](https://github.com/INTENTIUS/choudoufu) is the
5
+ OpenTofu fork for AWS and Kubernetes whose ownership lives on each resource as
6
+ two tags, `tofu-estate` and `tofu-address`.
7
+
8
+ ```sh
9
+ npx @intentius/behold demo terralith # Docker, curl and choudoufu 0.28.0 or newer
10
+ ```
11
+
12
+ The page is `http://localhost:4600/terralith`. It reads the files each phase
13
+ leaves in `snapshots/`, and shows the next phase's line for you to run here.
14
+
15
+ | Phase | You run | What happens |
16
+ |---|---|---|
17
+ | 1. Stock | `bash scripts/terralith-up.sh` (the demo runs it) | a floci on 127.0.0.1:4652, a state bucket, and `terralith/` applied the stock way: 23 resources in one state file |
18
+ | 2. Adopt | `bash scripts/terralith-step.sh adopt` | terragucci's `backends:` migration: the backend block becomes a live block (`phases/adopt/`), `choudoufu live-import` reads the state once and stamps the two tags on each resource it verifies |
19
+ | 3. Split | `bash scripts/terralith-step.sh split` | terragucci's `moves:` migration: three team roots (`phases/split/`), and `choudoufu live-mv -from-estate` moves each team's resources into the team's estate, one tag write each, recorded in `split.json` |
20
+
21
+ From adopt on, each card carries a tag. A filled tag means the resource wears the
22
+ marker; an outlined one means it has no tags argument and rides the role it
23
+ belongs to. The word beside it says how choudoufu's plan found the resource:
24
+ `marker`, by the tag sweep, or `derived`, by the name the code gives it. A
25
+ named resource reads `derived` and wears the marker too.
26
+
27
+ The state file stays in the bucket as it was after adopt. Each stamp and
28
+ each retag is its own tag write.
29
+
30
+ `bash scripts/terralith-down.sh` removes the container. Set
31
+ `TERRALITH_CONTAINER` and `TERRALITH_PORT` for another name and port, or
32
+ `CHOUDOUFU_BIN` for a choudoufu that is not on PATH.
@@ -0,0 +1,12 @@
1
+ # Adopt the terralith: its state moves onto the resources as markers.
2
+ # choudoufu live-import reads the old state once and stamps tofu-estate and
3
+ # tofu-address on every resource it verifies. The old state stays where it is.
4
+ backends:
5
+ - root: terralith
6
+ from:
7
+ backend: s3
8
+ config:
9
+ bucket: terralith-state
10
+ key: terralith/terraform.tfstate
11
+ region: us-east-1
12
+ use_lockfile: true
@@ -0,0 +1,138 @@
1
+ terraform {
2
+ required_providers {
3
+ aws = {
4
+ source = "hashicorp/aws"
5
+ version = "= 6.59.0"
6
+ }
7
+ }
8
+ live {
9
+ estate = "terralith"
10
+ }
11
+ }
12
+
13
+ provider "aws" {
14
+ region = "us-east-1"
15
+ }
16
+
17
+ # ---- network ----
18
+ resource "aws_vpc" "main" {
19
+ cidr_block = "10.42.0.0/16"
20
+ tags = { Name = "terralith-main" }
21
+ }
22
+
23
+ resource "aws_subnet" "app" {
24
+ vpc_id = aws_vpc.main.id
25
+ cidr_block = "10.42.1.0/24"
26
+ tags = { Name = "terralith-app" }
27
+ }
28
+
29
+ # ---- team-a ----
30
+ resource "aws_iam_role" "team_a" {
31
+ name = "terralith-team-a-role"
32
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
33
+ }
34
+
35
+ resource "aws_iam_role_policy" "team_a_inline" {
36
+ name = "terralith-team-a-inline"
37
+ role = aws_iam_role.team_a.id
38
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
39
+ }
40
+
41
+ resource "aws_iam_policy" "team_a" {
42
+ name = "terralith-team-a-policy"
43
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
44
+ }
45
+
46
+ resource "aws_iam_role_policy_attachment" "team_a" {
47
+ role = aws_iam_role.team_a.name
48
+ policy_arn = aws_iam_policy.team_a.arn
49
+ }
50
+
51
+ resource "aws_cloudwatch_log_group" "team_a_0" {
52
+ name = "/terralith/team-a/svc-0"
53
+ retention_in_days = 7
54
+ }
55
+
56
+ resource "aws_cloudwatch_log_group" "team_a_1" {
57
+ name = "/terralith/team-a/svc-1"
58
+ retention_in_days = 7
59
+ }
60
+
61
+ resource "aws_cloudwatch_log_group" "team_a_2" {
62
+ name = "/terralith/team-a/svc-2"
63
+ retention_in_days = 7
64
+ }
65
+
66
+ # ---- team-b ----
67
+ resource "aws_iam_role" "team_b" {
68
+ name = "terralith-team-b-role"
69
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
70
+ }
71
+
72
+ resource "aws_iam_role_policy" "team_b_inline" {
73
+ name = "terralith-team-b-inline"
74
+ role = aws_iam_role.team_b.id
75
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
76
+ }
77
+
78
+ resource "aws_iam_policy" "team_b" {
79
+ name = "terralith-team-b-policy"
80
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
81
+ }
82
+
83
+ resource "aws_iam_role_policy_attachment" "team_b" {
84
+ role = aws_iam_role.team_b.name
85
+ policy_arn = aws_iam_policy.team_b.arn
86
+ }
87
+
88
+ resource "aws_cloudwatch_log_group" "team_b_0" {
89
+ name = "/terralith/team-b/svc-0"
90
+ retention_in_days = 7
91
+ }
92
+
93
+ resource "aws_cloudwatch_log_group" "team_b_1" {
94
+ name = "/terralith/team-b/svc-1"
95
+ retention_in_days = 7
96
+ }
97
+
98
+ resource "aws_cloudwatch_log_group" "team_b_2" {
99
+ name = "/terralith/team-b/svc-2"
100
+ retention_in_days = 7
101
+ }
102
+
103
+ # ---- team-c ----
104
+ resource "aws_iam_role" "team_c" {
105
+ name = "terralith-team-c-role"
106
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
107
+ }
108
+
109
+ resource "aws_iam_role_policy" "team_c_inline" {
110
+ name = "terralith-team-c-inline"
111
+ role = aws_iam_role.team_c.id
112
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
113
+ }
114
+
115
+ resource "aws_iam_policy" "team_c" {
116
+ name = "terralith-team-c-policy"
117
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
118
+ }
119
+
120
+ resource "aws_iam_role_policy_attachment" "team_c" {
121
+ role = aws_iam_role.team_c.name
122
+ policy_arn = aws_iam_policy.team_c.arn
123
+ }
124
+
125
+ resource "aws_cloudwatch_log_group" "team_c_0" {
126
+ name = "/terralith/team-c/svc-0"
127
+ retention_in_days = 7
128
+ }
129
+
130
+ resource "aws_cloudwatch_log_group" "team_c_1" {
131
+ name = "/terralith/team-c/svc-1"
132
+ retention_in_days = 7
133
+ }
134
+
135
+ resource "aws_cloudwatch_log_group" "team_c_2" {
136
+ name = "/terralith/team-c/svc-2"
137
+ retention_in_days = 7
138
+ }
@@ -0,0 +1,29 @@
1
+ # Split the terralith: each team's resources move to the team's own estate.
2
+ # choudoufu live-mv -from-estate rewrites each resource's markers, run in
3
+ # the root it moves to. One tag write per resource; the untaggable children
4
+ # (inline policies, attachments) follow their role with no write of their own.
5
+ moves:
6
+ - from: terralith
7
+ to: team-a
8
+ addresses:
9
+ - aws_iam_role.team_a
10
+ - aws_iam_policy.team_a
11
+ - aws_cloudwatch_log_group.team_a_0
12
+ - aws_cloudwatch_log_group.team_a_1
13
+ - aws_cloudwatch_log_group.team_a_2
14
+ - from: terralith
15
+ to: team-b
16
+ addresses:
17
+ - aws_iam_role.team_b
18
+ - aws_iam_policy.team_b
19
+ - aws_cloudwatch_log_group.team_b_0
20
+ - aws_cloudwatch_log_group.team_b_1
21
+ - aws_cloudwatch_log_group.team_b_2
22
+ - from: terralith
23
+ to: team-c
24
+ addresses:
25
+ - aws_iam_role.team_c
26
+ - aws_iam_policy.team_c
27
+ - aws_cloudwatch_log_group.team_c_0
28
+ - aws_cloudwatch_log_group.team_c_1
29
+ - aws_cloudwatch_log_group.team_c_2
@@ -0,0 +1,52 @@
1
+ terraform {
2
+ required_providers {
3
+ aws = {
4
+ source = "hashicorp/aws"
5
+ version = "= 6.59.0"
6
+ }
7
+ }
8
+ live {
9
+ estate = "team-a"
10
+ }
11
+ }
12
+
13
+ provider "aws" {
14
+ region = "us-east-1"
15
+ }
16
+
17
+ # ---- team-a ----
18
+ resource "aws_iam_role" "team_a" {
19
+ name = "terralith-team-a-role"
20
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
21
+ }
22
+
23
+ resource "aws_iam_role_policy" "team_a_inline" {
24
+ name = "terralith-team-a-inline"
25
+ role = aws_iam_role.team_a.id
26
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
27
+ }
28
+
29
+ resource "aws_iam_policy" "team_a" {
30
+ name = "terralith-team-a-policy"
31
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
32
+ }
33
+
34
+ resource "aws_iam_role_policy_attachment" "team_a" {
35
+ role = aws_iam_role.team_a.name
36
+ policy_arn = aws_iam_policy.team_a.arn
37
+ }
38
+
39
+ resource "aws_cloudwatch_log_group" "team_a_0" {
40
+ name = "/terralith/team-a/svc-0"
41
+ retention_in_days = 7
42
+ }
43
+
44
+ resource "aws_cloudwatch_log_group" "team_a_1" {
45
+ name = "/terralith/team-a/svc-1"
46
+ retention_in_days = 7
47
+ }
48
+
49
+ resource "aws_cloudwatch_log_group" "team_a_2" {
50
+ name = "/terralith/team-a/svc-2"
51
+ retention_in_days = 7
52
+ }
@@ -0,0 +1,52 @@
1
+ terraform {
2
+ required_providers {
3
+ aws = {
4
+ source = "hashicorp/aws"
5
+ version = "= 6.59.0"
6
+ }
7
+ }
8
+ live {
9
+ estate = "team-b"
10
+ }
11
+ }
12
+
13
+ provider "aws" {
14
+ region = "us-east-1"
15
+ }
16
+
17
+ # ---- team-b ----
18
+ resource "aws_iam_role" "team_b" {
19
+ name = "terralith-team-b-role"
20
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
21
+ }
22
+
23
+ resource "aws_iam_role_policy" "team_b_inline" {
24
+ name = "terralith-team-b-inline"
25
+ role = aws_iam_role.team_b.id
26
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
27
+ }
28
+
29
+ resource "aws_iam_policy" "team_b" {
30
+ name = "terralith-team-b-policy"
31
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
32
+ }
33
+
34
+ resource "aws_iam_role_policy_attachment" "team_b" {
35
+ role = aws_iam_role.team_b.name
36
+ policy_arn = aws_iam_policy.team_b.arn
37
+ }
38
+
39
+ resource "aws_cloudwatch_log_group" "team_b_0" {
40
+ name = "/terralith/team-b/svc-0"
41
+ retention_in_days = 7
42
+ }
43
+
44
+ resource "aws_cloudwatch_log_group" "team_b_1" {
45
+ name = "/terralith/team-b/svc-1"
46
+ retention_in_days = 7
47
+ }
48
+
49
+ resource "aws_cloudwatch_log_group" "team_b_2" {
50
+ name = "/terralith/team-b/svc-2"
51
+ retention_in_days = 7
52
+ }
@@ -0,0 +1,52 @@
1
+ terraform {
2
+ required_providers {
3
+ aws = {
4
+ source = "hashicorp/aws"
5
+ version = "= 6.59.0"
6
+ }
7
+ }
8
+ live {
9
+ estate = "team-c"
10
+ }
11
+ }
12
+
13
+ provider "aws" {
14
+ region = "us-east-1"
15
+ }
16
+
17
+ # ---- team-c ----
18
+ resource "aws_iam_role" "team_c" {
19
+ name = "terralith-team-c-role"
20
+ assume_role_policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Principal = { Service = "ec2.amazonaws.com" }, Action = "sts:AssumeRole" }] })
21
+ }
22
+
23
+ resource "aws_iam_role_policy" "team_c_inline" {
24
+ name = "terralith-team-c-inline"
25
+ role = aws_iam_role.team_c.id
26
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:CreateLogStream"], Resource = "*" }] })
27
+ }
28
+
29
+ resource "aws_iam_policy" "team_c" {
30
+ name = "terralith-team-c-policy"
31
+ policy = jsonencode({ Version = "2012-10-17", Statement = [{ Effect = "Allow", Action = ["logs:PutLogEvents"], Resource = "*" }] })
32
+ }
33
+
34
+ resource "aws_iam_role_policy_attachment" "team_c" {
35
+ role = aws_iam_role.team_c.name
36
+ policy_arn = aws_iam_policy.team_c.arn
37
+ }
38
+
39
+ resource "aws_cloudwatch_log_group" "team_c_0" {
40
+ name = "/terralith/team-c/svc-0"
41
+ retention_in_days = 7
42
+ }
43
+
44
+ resource "aws_cloudwatch_log_group" "team_c_1" {
45
+ name = "/terralith/team-c/svc-1"
46
+ retention_in_days = 7
47
+ }
48
+
49
+ resource "aws_cloudwatch_log_group" "team_c_2" {
50
+ name = "/terralith/team-c/svc-2"
51
+ retention_in_days = 7
52
+ }
@@ -0,0 +1,27 @@
1
+ terraform {
2
+ required_providers {
3
+ aws = {
4
+ source = "hashicorp/aws"
5
+ version = "= 6.59.0"
6
+ }
7
+ }
8
+ live {
9
+ estate = "terralith"
10
+ }
11
+ }
12
+
13
+ provider "aws" {
14
+ region = "us-east-1"
15
+ }
16
+
17
+ # ---- network ----
18
+ resource "aws_vpc" "main" {
19
+ cidr_block = "10.42.0.0/16"
20
+ tags = { Name = "terralith-main" }
21
+ }
22
+
23
+ resource "aws_subnet" "app" {
24
+ vpc_id = aws_vpc.main.id
25
+ cidr_block = "10.42.1.0/24"
26
+ tags = { Name = "terralith-app" }
27
+ }
@@ -0,0 +1,64 @@
1
+ # Shared by terralith-up.sh and terralith-step.sh. Sourced, never run.
2
+ #
3
+ # Every phase ends in a snapshot: choudoufu's own documents for each root,
4
+ # written as files under snapshots/<n>-<phase>/, and phase.json written last.
5
+ # behold reads those files (src/terralith-demo.ts) and nothing else, so the
6
+ # walkthrough shows what the tool printed, and the same files can come from a
7
+ # pipeline instead of this script.
8
+
9
+ CONTAINER="${TERRALITH_CONTAINER:-behold-terralith-floci}"
10
+ PORT="${TERRALITH_PORT:-4652}"
11
+ # choudoufu's own pin (live/floci-image in the choudoufu repo).
12
+ IMAGE="ghcr.io/lex00/floci@sha256:ff46eb8d1b73a9727948d1a194a660972e44cc64164a486548da7cb468a09144"
13
+ CHOUDOUFU="${CHOUDOUFU_BIN:-choudoufu}"
14
+ BUCKET=terralith-state
15
+
16
+ export AWS_ENDPOINT_URL="http://127.0.0.1:${PORT}"
17
+ export AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test AWS_REGION=us-east-1
18
+ export TF_PLUGIN_CACHE_DIR="${TF_PLUGIN_CACHE_DIR:-$HOME/.terraform.d/plugin-cache}"
19
+ mkdir -p "$TF_PLUGIN_CACHE_DIR"
20
+
21
+ say() { printf '%s\n' "→ $*"; }
22
+ die() { printf 'terralith demo: %s\n' "$*" >&2; exit 1; }
23
+
24
+ need_choudoufu() {
25
+ command -v "$CHOUDOUFU" >/dev/null 2>&1 || die "${CHOUDOUFU} is not on PATH. Install choudoufu 0.28.0 or newer (https://github.com/INTENTIUS/choudoufu/releases), or set CHOUDOUFU_BIN."
26
+ }
27
+
28
+ # The estate a root's live block names, or nothing for a stock root.
29
+ estate_of() {
30
+ sed -n '/^ live {/,/^ }/s/^ *estate *= *"\(.*\)"/\1/p' "$1/main.tf" | head -1
31
+ }
32
+
33
+ # snap <phase dir> <root>: the root's documents, as choudoufu printed them.
34
+ snap() {
35
+ local dir="$1" root="$2" out estate
36
+ out="$PWD/$dir/$root"
37
+ mkdir -p "$out"
38
+ (cd "$root" && "$CHOUDOUFU" live-check -json >"$out/live-check.json" 2>"$out/live-check.err") || true
39
+ estate="$(estate_of "$root")"
40
+ if [ -z "$estate" ]; then
41
+ (cd "$root" && "$CHOUDOUFU" show -json -no-color >"$out/state.json" 2>"$out/state.err") || true
42
+ else
43
+ (cd "$root" && "$CHOUDOUFU" live-ls -estate="$estate" -consistent -json . >"$out/live-ls.json" 2>"$out/live-ls.err") || true
44
+ (cd "$root" && "$CHOUDOUFU" live-plan -json >"$out/live-plan.json" 2>"$out/live-plan.err") || true
45
+ fi
46
+ }
47
+
48
+ # finish <phase dir> <phase> <n> <roots...>: phase.json, written last, so a
49
+ # reader never sees half a phase.
50
+ finish() {
51
+ local dir="$1" phase="$2" n="$3"
52
+ shift 3
53
+ local roots="" r
54
+ for r in "$@"; do roots="${roots:+$roots, }\"$r\""; done
55
+ local version
56
+ version="$("$CHOUDOUFU" version 2>/dev/null | head -1 | sed 's/"/\\"/g')"
57
+ printf '{\n "phase": "%s",\n "n": %s,\n "at": "%s",\n "choudoufu": "%s",\n "roots": [%s]\n}\n' \
58
+ "$phase" "$n" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$version" "$roots" >"$PWD/$dir/phase.json.tmp"
59
+ mv "$PWD/$dir/phase.json.tmp" "$dir/phase.json"
60
+ }
61
+
62
+ init_root() {
63
+ (cd "$1" && "$CHOUDOUFU" init -input=false -no-color "${@:2}" >/dev/null) || die "choudoufu init failed in $1"
64
+ }
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ # Remove the terralith demo's scratch floci, by its name and no other. The
3
+ # copy and its snapshots stay; they are yours to read, and the next
4
+ # terralith-up.sh in a fresh copy starts over.
5
+ set -euo pipefail
6
+ cd "$(dirname "$0")/.."
7
+ . scripts/lib.sh
8
+ docker rm -f "$CONTAINER" >/dev/null 2>&1 || true
9
+ echo "terralith demo: ${CONTAINER} removed."
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env bash
2
+ # The writes of `behold demo terralith`, one phase at a time, run by you.
3
+ # behold shows these lines and reads what they leave; it never runs them.
4
+ #
5
+ # adopt the terragucci `backends:` migration: the root's backend block
6
+ # becomes a live block, and choudoufu live-import reads the old
7
+ # state once and stamps tofu-estate and tofu-address on every
8
+ # resource it verifies. The old state stays in the bucket.
9
+ # split the terragucci `moves:` migration: three team roots appear, and
10
+ # choudoufu live-mv -from-estate rewrites each team resource's
11
+ # markers, run in the root it moves to. One tag write per resource.
12
+ # The record is split.json.
13
+ #
14
+ # Each phase ends in a snapshot under snapshots/<n>-<phase>/.
15
+ set -euo pipefail
16
+ cd "$(dirname "$0")/.."
17
+ . scripts/lib.sh
18
+ need_choudoufu
19
+
20
+ phase="${1:-}"
21
+ WORK=.terralith
22
+ mkdir -p "$WORK"
23
+
24
+ case "$phase" in
25
+ adopt)
26
+ [ -f snapshots/1-stock/phase.json ] || die "phase 1 is not up yet. Run scripts/terralith-up.sh first."
27
+ [ -f snapshots/2-adopt/phase.json ] && { say "adopt already ran (snapshots/2-adopt)."; exit 0; }
28
+ say "the old state, read once from s3://${BUCKET}"
29
+ [ -s "$WORK/terralith.tfstate" ] || (cd terralith && "$CHOUDOUFU" state pull >"../$WORK/terralith.tfstate")
30
+ say "the code change: terralith's backend block becomes a live block, plus migrations/adopt.yml"
31
+ # The s3 backend's settings stay beside .terraform as a record; init under a
32
+ # live block configures no backend.
33
+ [ -f terralith/.terraform/terraform.tfstate ] && mv terralith/.terraform/terraform.tfstate "$WORK/terralith.backend.json"
34
+ mkdir -p migrations
35
+ cp phases/adopt/terralith/main.tf terralith/main.tf
36
+ cp phases/adopt/migrations/adopt.yml migrations/adopt.yml
37
+ init_root terralith
38
+ mkdir -p snapshots/2-adopt/terralith
39
+ say "live-import, read-only: what verifies against the live system"
40
+ (cd terralith && "$CHOUDOUFU" live-import -no-color "-state=$PWD/../$WORK/terralith.tfstate" -estate=terralith \
41
+ >../snapshots/2-adopt/terralith/live-import.txt 2>&1) || die "live-import could not verify the state (snapshots/2-adopt/terralith/live-import.txt)"
42
+ say "live-import -approve: one marker write per verified resource"
43
+ (cd terralith && "$CHOUDOUFU" live-import -no-color "-state=$PWD/../$WORK/terralith.tfstate" -estate=terralith -approve \
44
+ >../snapshots/2-adopt/terralith/live-import-approve.txt 2>&1) || die "live-import could not stamp (snapshots/2-adopt/terralith/live-import-approve.txt)"
45
+ tail -3 snapshots/2-adopt/terralith/live-import-approve.txt | head -1
46
+ say "snapshot: phase 2"
47
+ snap snapshots/2-adopt terralith
48
+ finish snapshots/2-adopt adopt 2 terralith
49
+ say "adopted. Next: bash scripts/terralith-step.sh split"
50
+ ;;
51
+ split)
52
+ [ -f snapshots/2-adopt/phase.json ] || die "adopt has not run yet. Run scripts/terralith-step.sh adopt first."
53
+ [ -f snapshots/3-split/phase.json ] && { say "split already ran (snapshots/3-split)."; exit 0; }
54
+ say "the code change: three team roots, the terralith keeps the network, plus migrations/split.yml"
55
+ for root in terralith team-a team-b team-c; do
56
+ mkdir -p "$root"
57
+ cp "phases/split/$root/main.tf" "$root/main.tf"
58
+ done
59
+ cp phases/split/migrations/split.yml migrations/split.yml
60
+ for root in team-a team-b team-c; do init_root "$root"; done
61
+ mkdir -p snapshots/3-split/live-mv
62
+ moves="" retags=""
63
+ for root in team-a team-b team-c; do
64
+ for address in $(sed -n "/to: ${root}\$/,/^ - /s/^ - //p" migrations/split.yml); do
65
+ doc="snapshots/3-split/live-mv/${root}--${address}.json"
66
+ (cd "$root" && "$CHOUDOUFU" live-mv -json -dry-run -from-estate=terralith "$address" "$address" >"../${doc%.json}.dry-run.json" 2>/dev/null) \
67
+ || die "live-mv -dry-run refused ${address} (${doc%.json}.dry-run.json)"
68
+ (cd "$root" && "$CHOUDOUFU" live-mv -json -from-estate=terralith "$address" "$address" >"../$doc" 2>/dev/null) \
69
+ || die "live-mv refused ${address} (${doc})"
70
+ say "retag ${address}: terralith → ${root}"
71
+ moves="${moves:+$moves,}
72
+ {\"address\": \"${address}\", \"from\": \"terralith\", \"to\": \"${root}\"}"
73
+ retags="${retags:+$retags,}
74
+ {\"address\": \"${address}\", \"from\": \"terralith\", \"to\": \"${root}\", \"from_estate\": \"terralith\", \"to_estate\": \"${root}\", \"document\": \"${doc}\"}"
75
+ done
76
+ done
77
+ # split.json: the plan behold reads (from, estates, moves), and where each
78
+ # retag's own live-mv document is.
79
+ printf '{\n "from": "terralith",\n "estates": ["team-a", "team-b", "team-c"],\n "migration": "migrations/split.yml",\n "moves": [%s\n ],\n "retags": [%s\n ]\n}\n' "$moves" "$retags" >split.json
80
+ say "snapshot: phase 3"
81
+ for root in terralith team-a team-b team-c; do snap snapshots/3-split "$root"; done
82
+ finish snapshots/3-split split 3 terralith team-a team-b team-c
83
+ say "split. Four estates, one tag write per resource."
84
+ ;;
85
+ *)
86
+ die "which phase? bash scripts/terralith-step.sh adopt | split"
87
+ ;;
88
+ esac
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env bash
2
+ # Phase 1 of `behold demo terralith`: a stock terralith.
3
+ #
4
+ # A scratch floci (the AWS emulator) on 127.0.0.1:4652, a state bucket in it,
5
+ # and one Terraform root applied the stock way: 23 resources (a network and
6
+ # three teams' IAM and log groups) in one state file on S3. No markers yet.
7
+ #
8
+ # Scratch discipline: the container is named behold-terralith-floci (or
9
+ # TERRALITH_CONTAINER), this script refuses to adopt one that already exists,
10
+ # and only terralith-down.sh stops it. The port is not the shared emulator's
11
+ # (:4566), so nothing here reaches a floci another tool runs.
12
+ set -euo pipefail
13
+ cd "$(dirname "$0")/.."
14
+ . scripts/lib.sh
15
+
16
+ docker info >/dev/null 2>&1 || die "Docker is not running. Start Docker and re-run."
17
+ need_choudoufu
18
+
19
+ if [ -f snapshots/1-stock/phase.json ]; then
20
+ say "phase 1 is already up (snapshots/1-stock). terralith-down.sh stops the emulator; a fresh copy starts over."
21
+ exit 0
22
+ fi
23
+ if docker ps -a --format '{{.Names}}' | grep -qx "$CONTAINER"; then
24
+ die "container ${CONTAINER} already exists from an earlier run. Run scripts/terralith-down.sh, or set TERRALITH_CONTAINER to a new name."
25
+ fi
26
+
27
+ say "floci on 127.0.0.1:${PORT} (${CONTAINER})"
28
+ docker run -d --name "$CONTAINER" -p "127.0.0.1:${PORT}:4566" "$IMAGE" >/dev/null
29
+ for _ in $(seq 1 40); do
30
+ curl -fs -o /dev/null "http://127.0.0.1:${PORT}/" && break
31
+ sleep 1
32
+ done
33
+
34
+ say "state bucket s3://${BUCKET}, versioned"
35
+ curl -fs -o /dev/null -X PUT "http://127.0.0.1:${PORT}/${BUCKET}"
36
+ curl -fs -o /dev/null -X PUT "http://127.0.0.1:${PORT}/${BUCKET}?versioning" -H 'content-type: application/xml' \
37
+ -d '<VersioningConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Status>Enabled</Status></VersioningConfiguration>'
38
+
39
+ say "choudoufu init in terralith (an OpenTofu fork; with no live block it runs stock)"
40
+ init_root terralith -backend-config=use_path_style=true
41
+
42
+ say "apply: the terralith, 23 resources in one state file"
43
+ (cd terralith && "$CHOUDOUFU" apply -auto-approve -input=false -no-color | tail -1)
44
+
45
+ say "snapshot: phase 1"
46
+ mkdir -p snapshots/1-stock
47
+ snap snapshots/1-stock terralith
48
+ finish snapshots/1-stock stock 1 terralith
49
+
50
+ say "phase 1 is up. Next: bash scripts/terralith-step.sh adopt"