tf-project 0.1.0__tar.gz
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.
- tf_project-0.1.0/CHANGELOG.md +59 -0
- tf_project-0.1.0/LICENSE +21 -0
- tf_project-0.1.0/PKG-INFO +251 -0
- tf_project-0.1.0/README.md +228 -0
- tf_project-0.1.0/pyproject.toml +204 -0
- tf_project-0.1.0/src/tf_project/__init__.py +18 -0
- tf_project-0.1.0/src/tf_project/__main__.py +4 -0
- tf_project-0.1.0/src/tf_project/__version__.py +1 -0
- tf_project-0.1.0/src/tf_project/banner.py +140 -0
- tf_project-0.1.0/src/tf_project/cli.py +362 -0
- tf_project-0.1.0/src/tf_project/commands.py +290 -0
- tf_project-0.1.0/src/tf_project/config.py +189 -0
- tf_project-0.1.0/src/tf_project/py.typed +0 -0
- tf_project-0.1.0/src/tf_project/secrets.py +54 -0
- tf_project-0.1.0/src/tf_project/self_commands.py +384 -0
- tf_project-0.1.0/src/tf_project/state.py +61 -0
- tf_project-0.1.0/src/tf_project/terraform.py +117 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here.
|
|
4
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
- `tfp self lock status` and `tfp self lock break` — inspect or break the
|
|
11
|
+
Azure blob lease on the remote tfstate, for unsticking a state that was
|
|
12
|
+
hard-killed mid-operation. `status` also decodes the `terraformlockid`
|
|
13
|
+
blob metadata so the lock ID is visible without having to provoke a
|
|
14
|
+
failed terraform run; pair it with `tfp force-unlock <ID>` for the
|
|
15
|
+
polite release path. Shells out to `az storage blob`.
|
|
16
|
+
- `tfp status` — one-line summary of the current init state.
|
|
17
|
+
- `tfp self banner check <tfvars>` — validate a tfvars banner and print the
|
|
18
|
+
resolved `source_root` / `backend_config` / `env` without running terraform.
|
|
19
|
+
- Global `--verbose` and `--dry-run` flags. `--verbose` logs the terraform
|
|
20
|
+
argv before exec; `--dry-run` prints the argv and skips execution.
|
|
21
|
+
- `[tf_project.backend_config]` TOML table and matching `backend_config`
|
|
22
|
+
banner field for arbitrary `-backend-config key=value` passthrough
|
|
23
|
+
(resource_group_name, storage_account_name, etc.).
|
|
24
|
+
- Stale-tfplan guard: `tfp plan` writes a sidecar with the SHA-256 of the
|
|
25
|
+
decrypted tfvars; `tfp apply` refuses to apply if the tfvars changed.
|
|
26
|
+
Bypass with `tfp apply --force`.
|
|
27
|
+
- Shell-completion entry point (Typer's built-in `--install-completion`).
|
|
28
|
+
- Real-terraform end-to-end integration test covering init→plan→apply→destroy
|
|
29
|
+
against a `null_resource` fixture.
|
|
30
|
+
- Subprocess smoke test exercising `python -m tf_project --version`.
|
|
31
|
+
- `BannerError` for clean, file-pathed banner validation failures.
|
|
32
|
+
- `py.typed` marker — the package now ships type information explicitly.
|
|
33
|
+
|
|
34
|
+
### Changed
|
|
35
|
+
- Terraform exit codes propagate cleanly (no Python traceback when
|
|
36
|
+
terraform fails). `terraform` not found exits 127.
|
|
37
|
+
- SIGINT during a running terraform command no longer kills Python first;
|
|
38
|
+
terraform receives the signal via the shared process group and we wait
|
|
39
|
+
for it to finish.
|
|
40
|
+
- Passthrough invocations (`tfp <unknown-subcommand>`) now `os.execvpe` the
|
|
41
|
+
terraform process directly — native signal handling and exit code.
|
|
42
|
+
- `MyState.save` takes an exclusive file lock so parallel `tfp init` runs
|
|
43
|
+
can't clobber each other (POSIX only).
|
|
44
|
+
- Banner parsing moved into `tf_project.banner`; validation errors now
|
|
45
|
+
raise `BannerError` with the offending file path.
|
|
46
|
+
|
|
47
|
+
## [0.1.0] — 2026-05-14
|
|
48
|
+
|
|
49
|
+
### Added
|
|
50
|
+
- Initial release. `tf-project` (aka `tfp`) CLI wrapping `terraform`
|
|
51
|
+
init / plan / apply / refresh / destroy / fmt / output / state-mv with
|
|
52
|
+
per-tfvars backend keys, pluggable secrets preprocessor, and a
|
|
53
|
+
pyproject.toml-driven config.
|
|
54
|
+
- Unknown subcommands forwarded to `terraform` verbatim.
|
|
55
|
+
- `self` subcommand group: `init`, `config print|path`, `state show|clear`,
|
|
56
|
+
`doctor`.
|
|
57
|
+
- Banner JSON support in tfvars: `project` (subdir), `state_key` (full
|
|
58
|
+
remote-state key override), `env` (env vars merged into state).
|
|
59
|
+
- Configurable `terraform_binary` (resolved via `shutil.which` when unset).
|
tf_project-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ilja Orlovs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tf-project
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Custom Terraform project wrapper used to provision infrastructure.
|
|
5
|
+
Author-Email: Ilja Orlovs <ilja@release.art>
|
|
6
|
+
Maintainer-Email: Ilja Orlovs <ilja@release.art>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
16
|
+
Classifier: Topic :: System :: Systems Administration
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Project-URL: Homepage, https://github.com/release-art/tf-project
|
|
19
|
+
Project-URL: Repository, https://github.com/release-art/tf-project
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: typer>=0.21.2
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# tf-project
|
|
25
|
+
|
|
26
|
+
A thin, opinionated Terraform-project wrapper. Provides a single `tf-project`
|
|
27
|
+
(aka `tfp`) CLI that wraps `terraform init / plan / apply / refresh / destroy /
|
|
28
|
+
fmt / output / state mv` with:
|
|
29
|
+
|
|
30
|
+
- Per-tfvars remote-state backend keys (one state per tfvars file).
|
|
31
|
+
- Pluggable tfvars preprocessing — defaults to [1Password's `op inject`][op] so
|
|
32
|
+
you can keep `op://...` references in tfvars committed to git.
|
|
33
|
+
- A small JSON state file (`tmp/my_terraform_state.json`) capturing which
|
|
34
|
+
tfvars was last init'd, so subsequent `plan` / `apply` need no arguments.
|
|
35
|
+
|
|
36
|
+
[op]: https://developer.1password.com/docs/cli/secrets-template-syntax/
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
pip install tf-project
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The CLI is exposed as both `tf-project` and the shorter alias `tfp`.
|
|
45
|
+
|
|
46
|
+
## Configure
|
|
47
|
+
|
|
48
|
+
The fastest way to get a config is:
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
cd path/to/your-terraform-repo
|
|
52
|
+
tfp self init
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This drops a `tf_project.toml` at the repo root, or — if a `pyproject.toml`
|
|
56
|
+
is already present — appends a `[tool.tf_project]` section to it. It refuses
|
|
57
|
+
to overwrite an existing config.
|
|
58
|
+
|
|
59
|
+
You can also write the file yourself:
|
|
60
|
+
|
|
61
|
+
```toml
|
|
62
|
+
[tf_project]
|
|
63
|
+
terraform_dir = "terraform" # where your <project>/ subdirs live
|
|
64
|
+
tfvars_dir = "tfvars" # used by `tfp fmt`
|
|
65
|
+
tmp_dir = "tmp" # state file + tfplan land here
|
|
66
|
+
state_key_prefix = "terraform/azure/" # remote backend key prefix
|
|
67
|
+
|
|
68
|
+
# Optional. Defaults to `shutil.which("terraform")` at config-load time.
|
|
69
|
+
# A value with a path separator is resolved relative to the project root;
|
|
70
|
+
# a bare name (e.g. `"tofu"`) is left for `subprocess` to PATH-resolve.
|
|
71
|
+
# terraform_binary = "bin/terraform-1.7.5"
|
|
72
|
+
|
|
73
|
+
# Optional. Static `-backend-config` k/v pairs applied to every `tfp init`.
|
|
74
|
+
# Banner-level `backend_config` overrides individual keys.
|
|
75
|
+
[tf_project.backend_config]
|
|
76
|
+
# resource_group_name = "tfstate-rg"
|
|
77
|
+
# storage_account_name = "tfstate0001"
|
|
78
|
+
# container_name = "tfstate"
|
|
79
|
+
|
|
80
|
+
# Optional. Defaults to `op inject`. Set `command = []` to disable.
|
|
81
|
+
[tf_project.secrets]
|
|
82
|
+
command = ["op", "inject", "--in-file", "{in}", "--out-file", "{out}"]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Alternatively, place the same fields under `[tool.tf_project]` in your
|
|
86
|
+
`pyproject.toml`. `tf_project.toml` wins if both are present. The package
|
|
87
|
+
walks up from `cwd` to find either file.
|
|
88
|
+
|
|
89
|
+
Each tfvars file should carry a one-line JSON banner identifying its project:
|
|
90
|
+
|
|
91
|
+
```hcl
|
|
92
|
+
# {"header": "terraform", "project": "demo"}
|
|
93
|
+
foo = "bar"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
`tfp init <tfvars>` reads that banner to pick the `terraform_dir/<project>/`
|
|
97
|
+
subdirectory to operate on, and persists a state record so subsequent
|
|
98
|
+
commands take no arguments.
|
|
99
|
+
|
|
100
|
+
The banner also accepts these optional fields:
|
|
101
|
+
|
|
102
|
+
| Field | Purpose |
|
|
103
|
+
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
104
|
+
| `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Use to share state across files. |
|
|
105
|
+
| `env` | JSON object of `string → string` env vars. Merged into the saved state on top of any previously-captured environment. |
|
|
106
|
+
| `backend_config` | JSON object of extra `-backend-config k=v` pairs (e.g. `resource_group_name`, `storage_account_name`). Wins over the config-level `[tf_project.backend_config]` table. |
|
|
107
|
+
|
|
108
|
+
```hcl
|
|
109
|
+
# {"header":"terraform","project":"core","state_key":"shared/core.tfstate","env":{"ARM_SUBSCRIPTION_ID":"…"}}
|
|
110
|
+
foo = "bar"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Usage
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
tfp init tfvars/dev.tfvars # init backend for this tfvars
|
|
117
|
+
tfp plan # plan using last init'd tfvars
|
|
118
|
+
tfp plan -t module.foo.bar # targeted plan (repeatable)
|
|
119
|
+
tfp plan -r module.foo.bar # force-replace (repeatable)
|
|
120
|
+
tfp apply # apply the saved plan
|
|
121
|
+
tfp refresh # apply directly (no saved plan)
|
|
122
|
+
tfp destroy -t module.foo.bar # targeted destroy
|
|
123
|
+
tfp fmt # terraform fmt -recursive over terraform/ + tfvars/
|
|
124
|
+
tfp output # terraform output -json
|
|
125
|
+
tfp state-mv aws_x.a aws_x.b # terraform state mv
|
|
126
|
+
tfp status # one-line summary of the current init
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Global flags
|
|
130
|
+
|
|
131
|
+
- `--verbose` — echo the terraform argv to stderr before exec.
|
|
132
|
+
- `--dry-run` — print the argv and skip execution. Combine with any subcommand
|
|
133
|
+
(wrapped or passthrough) to preview what would be invoked.
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
tfp --dry-run plan -t module.foo
|
|
137
|
+
tfp --verbose apply
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Recovering from a stuck Azure tfstate lock
|
|
141
|
+
|
|
142
|
+
When terraform is killed mid-operation (Ctrl-C, OOM, network drop), the
|
|
143
|
+
azurerm backend leaves an infinite blob lease on the tfstate. Subsequent
|
|
144
|
+
runs fail with "state locked".
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
tfp self lock status # lease + lock metadata (exits 2 if locked)
|
|
148
|
+
tfp self lock break # break the blob lease (prompts for confirmation; -y to skip)
|
|
149
|
+
tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`tfp self lock status` reads the lock ID directly from the blob's
|
|
153
|
+
`terraformlockid` metadata (which the azurerm backend writes as
|
|
154
|
+
base64-encoded JSON), so you don't have to provoke a failed terraform run
|
|
155
|
+
to discover it:
|
|
156
|
+
|
|
157
|
+
```
|
|
158
|
+
locked = True
|
|
159
|
+
lease_state = leased
|
|
160
|
+
lease_duration = infinite
|
|
161
|
+
lock_id = 12345678-90ab-cdef-1234-567890abcdef
|
|
162
|
+
lock_who = user@host
|
|
163
|
+
lock_operation = OperationTypePlan
|
|
164
|
+
lock_created = 2026-05-14T12:00:00Z
|
|
165
|
+
|
|
166
|
+
To release via terraform: tfp force-unlock 12345678-90ab-cdef-1234-567890abcdef
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
`tfp self lock {status,break}` shells out to `az storage blob` and so
|
|
170
|
+
requires the Azure CLI to be installed and authenticated. It reads the
|
|
171
|
+
storage account / container / blob from the saved init state — so this only
|
|
172
|
+
works after `tfp init` has captured `[tf_project.backend_config]`
|
|
173
|
+
(`storage_account_name`, `container_name`) and the `key`.
|
|
174
|
+
|
|
175
|
+
Two options once you have the ID:
|
|
176
|
+
|
|
177
|
+
- **`tfp force-unlock <ID>`** — the polite version: terraform releases the
|
|
178
|
+
lease *and* deletes the lock metadata. Works for any backend.
|
|
179
|
+
- **`tfp self lock break`** — the blunt version: breaks the blob lease
|
|
180
|
+
without going through terraform. Useful when terraform itself can't
|
|
181
|
+
reach the backend or when you don't have the ID.
|
|
182
|
+
|
|
183
|
+
### Apply safety
|
|
184
|
+
|
|
185
|
+
`tfp plan` records a SHA-256 of the decrypted tfvars alongside the saved
|
|
186
|
+
tfplan (`<tfplan>.meta.json`). `tfp apply` refuses to run if the tfvars
|
|
187
|
+
content changed since the plan was generated. Pass `--force` to override.
|
|
188
|
+
|
|
189
|
+
### Passthrough to `terraform`
|
|
190
|
+
|
|
191
|
+
Any subcommand not in the wrapped list above is forwarded to `terraform`
|
|
192
|
+
verbatim, prefixed with `-chdir=<source_root>` and the environment from the
|
|
193
|
+
last `tfp init`. So the full Terraform CLI surface is reachable through `tfp`:
|
|
194
|
+
|
|
195
|
+
```sh
|
|
196
|
+
tfp validate # terraform -chdir=... validate
|
|
197
|
+
tfp validate -json # flags pass straight through
|
|
198
|
+
tfp workspace list # terraform -chdir=... workspace list
|
|
199
|
+
tfp taint module.foo.bar # terraform -chdir=... taint module.foo.bar
|
|
200
|
+
tfp providers schema -json # terraform -chdir=... providers schema -json
|
|
201
|
+
tfp version # works without init (no -chdir prepended)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The wrapped subcommands also accept extra terraform flags, which are appended
|
|
205
|
+
to the underlying invocation:
|
|
206
|
+
|
|
207
|
+
```sh
|
|
208
|
+
tfp plan -t module.foo -- -detailed-exitcode -compact-warnings
|
|
209
|
+
tfp apply -- -parallelism=20
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
(The `--` is optional — anything Typer doesn't recognise is forwarded — but
|
|
213
|
+
including it is the most readable way to signal "everything after this is raw
|
|
214
|
+
terraform flags".)
|
|
215
|
+
|
|
216
|
+
### Self-management
|
|
217
|
+
|
|
218
|
+
A group of `tfp self ...` commands manages the tool itself, not Terraform:
|
|
219
|
+
|
|
220
|
+
```sh
|
|
221
|
+
tfp self init # bootstrap tf_project.toml or [tool.tf_project]
|
|
222
|
+
tfp self config print # show effective config (--json for JSON)
|
|
223
|
+
tfp self config path # show which file the config came from
|
|
224
|
+
tfp self state show # pretty-print the saved init state
|
|
225
|
+
tfp self state clear # delete the saved state file
|
|
226
|
+
tfp self doctor # sanity-check the environment (PATH, dirs, ...)
|
|
227
|
+
tfp self banner check <tfvars> # validate a tfvars banner; print resolved fields
|
|
228
|
+
tfp self lock status # show Azure blob-lease state of the remote tfstate
|
|
229
|
+
tfp self lock break # break the lease after a hard-kill left state locked
|
|
230
|
+
tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Development
|
|
234
|
+
|
|
235
|
+
```sh
|
|
236
|
+
pdm install
|
|
237
|
+
pdm run ruff check src tests
|
|
238
|
+
pdm run pyright src
|
|
239
|
+
pdm run pytest # unit tests
|
|
240
|
+
pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
## Release
|
|
244
|
+
|
|
245
|
+
1. Merge to `main`. CI runs; on success the **Release** workflow triggers.
|
|
246
|
+
2. `Release` builds the wheel, publishes to TestPyPI then PyPI via OIDC
|
|
247
|
+
trusted publishing, then opens a `pdm bump patch` commit to prepare the
|
|
248
|
+
next version.
|
|
249
|
+
3. PyPI trusted-publishing setup is a one-time step on
|
|
250
|
+
`pypi.org → Manage project → Publishing`, tied to this repo and the
|
|
251
|
+
`release` GitHub Environment.
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
# tf-project
|
|
2
|
+
|
|
3
|
+
A thin, opinionated Terraform-project wrapper. Provides a single `tf-project`
|
|
4
|
+
(aka `tfp`) CLI that wraps `terraform init / plan / apply / refresh / destroy /
|
|
5
|
+
fmt / output / state mv` with:
|
|
6
|
+
|
|
7
|
+
- Per-tfvars remote-state backend keys (one state per tfvars file).
|
|
8
|
+
- Pluggable tfvars preprocessing — defaults to [1Password's `op inject`][op] so
|
|
9
|
+
you can keep `op://...` references in tfvars committed to git.
|
|
10
|
+
- A small JSON state file (`tmp/my_terraform_state.json`) capturing which
|
|
11
|
+
tfvars was last init'd, so subsequent `plan` / `apply` need no arguments.
|
|
12
|
+
|
|
13
|
+
[op]: https://developer.1password.com/docs/cli/secrets-template-syntax/
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pip install tf-project
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The CLI is exposed as both `tf-project` and the shorter alias `tfp`.
|
|
22
|
+
|
|
23
|
+
## Configure
|
|
24
|
+
|
|
25
|
+
The fastest way to get a config is:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
cd path/to/your-terraform-repo
|
|
29
|
+
tfp self init
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
This drops a `tf_project.toml` at the repo root, or — if a `pyproject.toml`
|
|
33
|
+
is already present — appends a `[tool.tf_project]` section to it. It refuses
|
|
34
|
+
to overwrite an existing config.
|
|
35
|
+
|
|
36
|
+
You can also write the file yourself:
|
|
37
|
+
|
|
38
|
+
```toml
|
|
39
|
+
[tf_project]
|
|
40
|
+
terraform_dir = "terraform" # where your <project>/ subdirs live
|
|
41
|
+
tfvars_dir = "tfvars" # used by `tfp fmt`
|
|
42
|
+
tmp_dir = "tmp" # state file + tfplan land here
|
|
43
|
+
state_key_prefix = "terraform/azure/" # remote backend key prefix
|
|
44
|
+
|
|
45
|
+
# Optional. Defaults to `shutil.which("terraform")` at config-load time.
|
|
46
|
+
# A value with a path separator is resolved relative to the project root;
|
|
47
|
+
# a bare name (e.g. `"tofu"`) is left for `subprocess` to PATH-resolve.
|
|
48
|
+
# terraform_binary = "bin/terraform-1.7.5"
|
|
49
|
+
|
|
50
|
+
# Optional. Static `-backend-config` k/v pairs applied to every `tfp init`.
|
|
51
|
+
# Banner-level `backend_config` overrides individual keys.
|
|
52
|
+
[tf_project.backend_config]
|
|
53
|
+
# resource_group_name = "tfstate-rg"
|
|
54
|
+
# storage_account_name = "tfstate0001"
|
|
55
|
+
# container_name = "tfstate"
|
|
56
|
+
|
|
57
|
+
# Optional. Defaults to `op inject`. Set `command = []` to disable.
|
|
58
|
+
[tf_project.secrets]
|
|
59
|
+
command = ["op", "inject", "--in-file", "{in}", "--out-file", "{out}"]
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Alternatively, place the same fields under `[tool.tf_project]` in your
|
|
63
|
+
`pyproject.toml`. `tf_project.toml` wins if both are present. The package
|
|
64
|
+
walks up from `cwd` to find either file.
|
|
65
|
+
|
|
66
|
+
Each tfvars file should carry a one-line JSON banner identifying its project:
|
|
67
|
+
|
|
68
|
+
```hcl
|
|
69
|
+
# {"header": "terraform", "project": "demo"}
|
|
70
|
+
foo = "bar"
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`tfp init <tfvars>` reads that banner to pick the `terraform_dir/<project>/`
|
|
74
|
+
subdirectory to operate on, and persists a state record so subsequent
|
|
75
|
+
commands take no arguments.
|
|
76
|
+
|
|
77
|
+
The banner also accepts these optional fields:
|
|
78
|
+
|
|
79
|
+
| Field | Purpose |
|
|
80
|
+
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------- |
|
|
81
|
+
| `state_key` | Full remote-state backend key. Overrides the default `<state_key_prefix><tfvars-stem>.tfstate`. Use to share state across files. |
|
|
82
|
+
| `env` | JSON object of `string → string` env vars. Merged into the saved state on top of any previously-captured environment. |
|
|
83
|
+
| `backend_config` | JSON object of extra `-backend-config k=v` pairs (e.g. `resource_group_name`, `storage_account_name`). Wins over the config-level `[tf_project.backend_config]` table. |
|
|
84
|
+
|
|
85
|
+
```hcl
|
|
86
|
+
# {"header":"terraform","project":"core","state_key":"shared/core.tfstate","env":{"ARM_SUBSCRIPTION_ID":"…"}}
|
|
87
|
+
foo = "bar"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Usage
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
tfp init tfvars/dev.tfvars # init backend for this tfvars
|
|
94
|
+
tfp plan # plan using last init'd tfvars
|
|
95
|
+
tfp plan -t module.foo.bar # targeted plan (repeatable)
|
|
96
|
+
tfp plan -r module.foo.bar # force-replace (repeatable)
|
|
97
|
+
tfp apply # apply the saved plan
|
|
98
|
+
tfp refresh # apply directly (no saved plan)
|
|
99
|
+
tfp destroy -t module.foo.bar # targeted destroy
|
|
100
|
+
tfp fmt # terraform fmt -recursive over terraform/ + tfvars/
|
|
101
|
+
tfp output # terraform output -json
|
|
102
|
+
tfp state-mv aws_x.a aws_x.b # terraform state mv
|
|
103
|
+
tfp status # one-line summary of the current init
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Global flags
|
|
107
|
+
|
|
108
|
+
- `--verbose` — echo the terraform argv to stderr before exec.
|
|
109
|
+
- `--dry-run` — print the argv and skip execution. Combine with any subcommand
|
|
110
|
+
(wrapped or passthrough) to preview what would be invoked.
|
|
111
|
+
|
|
112
|
+
```sh
|
|
113
|
+
tfp --dry-run plan -t module.foo
|
|
114
|
+
tfp --verbose apply
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Recovering from a stuck Azure tfstate lock
|
|
118
|
+
|
|
119
|
+
When terraform is killed mid-operation (Ctrl-C, OOM, network drop), the
|
|
120
|
+
azurerm backend leaves an infinite blob lease on the tfstate. Subsequent
|
|
121
|
+
runs fail with "state locked".
|
|
122
|
+
|
|
123
|
+
```sh
|
|
124
|
+
tfp self lock status # lease + lock metadata (exits 2 if locked)
|
|
125
|
+
tfp self lock break # break the blob lease (prompts for confirmation; -y to skip)
|
|
126
|
+
tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
`tfp self lock status` reads the lock ID directly from the blob's
|
|
130
|
+
`terraformlockid` metadata (which the azurerm backend writes as
|
|
131
|
+
base64-encoded JSON), so you don't have to provoke a failed terraform run
|
|
132
|
+
to discover it:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
locked = True
|
|
136
|
+
lease_state = leased
|
|
137
|
+
lease_duration = infinite
|
|
138
|
+
lock_id = 12345678-90ab-cdef-1234-567890abcdef
|
|
139
|
+
lock_who = user@host
|
|
140
|
+
lock_operation = OperationTypePlan
|
|
141
|
+
lock_created = 2026-05-14T12:00:00Z
|
|
142
|
+
|
|
143
|
+
To release via terraform: tfp force-unlock 12345678-90ab-cdef-1234-567890abcdef
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
`tfp self lock {status,break}` shells out to `az storage blob` and so
|
|
147
|
+
requires the Azure CLI to be installed and authenticated. It reads the
|
|
148
|
+
storage account / container / blob from the saved init state — so this only
|
|
149
|
+
works after `tfp init` has captured `[tf_project.backend_config]`
|
|
150
|
+
(`storage_account_name`, `container_name`) and the `key`.
|
|
151
|
+
|
|
152
|
+
Two options once you have the ID:
|
|
153
|
+
|
|
154
|
+
- **`tfp force-unlock <ID>`** — the polite version: terraform releases the
|
|
155
|
+
lease *and* deletes the lock metadata. Works for any backend.
|
|
156
|
+
- **`tfp self lock break`** — the blunt version: breaks the blob lease
|
|
157
|
+
without going through terraform. Useful when terraform itself can't
|
|
158
|
+
reach the backend or when you don't have the ID.
|
|
159
|
+
|
|
160
|
+
### Apply safety
|
|
161
|
+
|
|
162
|
+
`tfp plan` records a SHA-256 of the decrypted tfvars alongside the saved
|
|
163
|
+
tfplan (`<tfplan>.meta.json`). `tfp apply` refuses to run if the tfvars
|
|
164
|
+
content changed since the plan was generated. Pass `--force` to override.
|
|
165
|
+
|
|
166
|
+
### Passthrough to `terraform`
|
|
167
|
+
|
|
168
|
+
Any subcommand not in the wrapped list above is forwarded to `terraform`
|
|
169
|
+
verbatim, prefixed with `-chdir=<source_root>` and the environment from the
|
|
170
|
+
last `tfp init`. So the full Terraform CLI surface is reachable through `tfp`:
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
tfp validate # terraform -chdir=... validate
|
|
174
|
+
tfp validate -json # flags pass straight through
|
|
175
|
+
tfp workspace list # terraform -chdir=... workspace list
|
|
176
|
+
tfp taint module.foo.bar # terraform -chdir=... taint module.foo.bar
|
|
177
|
+
tfp providers schema -json # terraform -chdir=... providers schema -json
|
|
178
|
+
tfp version # works without init (no -chdir prepended)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The wrapped subcommands also accept extra terraform flags, which are appended
|
|
182
|
+
to the underlying invocation:
|
|
183
|
+
|
|
184
|
+
```sh
|
|
185
|
+
tfp plan -t module.foo -- -detailed-exitcode -compact-warnings
|
|
186
|
+
tfp apply -- -parallelism=20
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
(The `--` is optional — anything Typer doesn't recognise is forwarded — but
|
|
190
|
+
including it is the most readable way to signal "everything after this is raw
|
|
191
|
+
terraform flags".)
|
|
192
|
+
|
|
193
|
+
### Self-management
|
|
194
|
+
|
|
195
|
+
A group of `tfp self ...` commands manages the tool itself, not Terraform:
|
|
196
|
+
|
|
197
|
+
```sh
|
|
198
|
+
tfp self init # bootstrap tf_project.toml or [tool.tf_project]
|
|
199
|
+
tfp self config print # show effective config (--json for JSON)
|
|
200
|
+
tfp self config path # show which file the config came from
|
|
201
|
+
tfp self state show # pretty-print the saved init state
|
|
202
|
+
tfp self state clear # delete the saved state file
|
|
203
|
+
tfp self doctor # sanity-check the environment (PATH, dirs, ...)
|
|
204
|
+
tfp self banner check <tfvars> # validate a tfvars banner; print resolved fields
|
|
205
|
+
tfp self lock status # show Azure blob-lease state of the remote tfstate
|
|
206
|
+
tfp self lock break # break the lease after a hard-kill left state locked
|
|
207
|
+
tfp force-unlock <LOCK_ID> # backend-agnostic, via terraform passthrough
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Development
|
|
211
|
+
|
|
212
|
+
```sh
|
|
213
|
+
pdm install
|
|
214
|
+
pdm run ruff check src tests
|
|
215
|
+
pdm run pyright src
|
|
216
|
+
pdm run pytest # unit tests
|
|
217
|
+
pdm run pytest -m integration # smoke tests (need `terraform` on PATH)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Release
|
|
221
|
+
|
|
222
|
+
1. Merge to `main`. CI runs; on success the **Release** workflow triggers.
|
|
223
|
+
2. `Release` builds the wheel, publishes to TestPyPI then PyPI via OIDC
|
|
224
|
+
trusted publishing, then opens a `pdm bump patch` commit to prepare the
|
|
225
|
+
next version.
|
|
226
|
+
3. PyPI trusted-publishing setup is a one-time step on
|
|
227
|
+
`pypi.org → Manage project → Publishing`, tied to this repo and the
|
|
228
|
+
`release` GitHub Environment.
|