-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Scaffolding for using OpenTofu
Deploys a small DigitalOcean project using a thin abstraction layer
- Loading branch information
1 parent
6bdb9f2
commit 87389ef
Showing
6 changed files
with
438 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Provision | ||
There's no way to do something like tofu.deploy() right now. | ||
|
||
This goes through two levels of abstractions?. JSII | ||
|
||
### CDKTF | ||
We write TerraformStack subclass and define our configration in init. It should something like this | ||
```python | ||
from cdktf import App, Fn, TerraformStack, TerraformVariable | ||
from cdktf_cdktf_provider_digitalocean.provider import DigitaloceanProvider | ||
from cdktf_cdktf_provider_digitalocean.vpc import Vpc | ||
from constructs import Construct | ||
|
||
|
||
class MyStack(TerraformStack): | ||
def __init__(self, scope: Construct, id: str): | ||
super().__init__(scope, id) | ||
do_token_variable = TerraformVariable(self,"do_token", type="string") | ||
DigitaloceanProvider(self, "digitalocean", token=do_token_variable.string_value) | ||
vpc = Vpc(self, "example_vpc", name="vpc-1", region="blr-1", ip_range="ip_range") | ||
|
||
``` | ||
|
||
Unfortunately, Pilot config isn't static. But good news is, this is actually implemented as following | ||
|
||
1. Define TerraformStack subclass, like we did before | ||
This is equivalent of writing a HCL file | ||
|
||
2. Define an app and call `synth` on it | ||
|
||
```python | ||
from cdktf import App, Fn, TerraformStack, TerraformVariable | ||
|
||
|
||
app = App() | ||
MyStack(app, "cdktf-demo") | ||
app.synth() | ||
``` | ||
|
||
|
||
3. Apply generated plan | ||
`cdktf deploy` | ||
We can open up the implemntation and see what happens underneath. | ||
|
||
1. If the implementation is complicated then we can run `cdktf deploy` ourselves | ||
|
||
|
||
--- | ||
|
||
We need to put some dynamic logic on Step 1. | ||
We can't write a class everytime. | ||
Generating Python code is basically the same as writing HCL | ||
What we can do is build a class implementation and app implementation on the fly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
We want to do as much work in Python / JSON as possible | ||
|
||
Luckily Tofu helps out. | ||
|
||
Every plan / state can be stored as JSON or converted to JSON. | ||
|
||
Here's the rough idea | ||
|
||
```mermaid | ||
stateDiagram | ||
code: Python Declaration | ||
plan: Plan | ||
json: Synthesized JSON | ||
infra: Infrastructure | ||
code --> json: app.synth() | ||
json --> plan: tofu plan | ||
plan --> infra: tofu deploy | ||
``` | ||
|
||
|
||
Same thing in words | ||
|
||
1. Write Python code in `TerraformStack.__init__()` that describes the infra we need | ||
2. "Synthesize" this TerraformStack object `app.synth()` | ||
|
||
The synth actually executes the `__init__` so we can do whatever we want in Python (loops, conditionals etc) | ||
|
||
`synth` generates a `cdktf.out/stacks/<stack-name>/cdktf.json` file in the working directory (this is `frappe-bench/sites`) | ||
|
||
We will use `sites/<site>/stacks` for now. So the state moves with the site without any special handling. | ||
|
||
TODO: Include this directory in the file backups. | ||
|
||
3. Store this synthesized JSON in some DocType Provision | ||
|
||
|
||
Note: Our Stack can have bugs or the code that defines what we need can have bugs. Have a way to prevent catastrophies at this stage. We need sanity checks in Production to guard against | ||
- Don't trigger anything that can cause data loss | ||
- Don't trigger massive changes ( More than n resources at a time) | ||
- Cross stack changes ?! (Don't delete someone other region?!) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.