Skip to content

Commit 355b535

Browse files
committed
New Python template without pipenv
Closes hashicorp#327
1 parent d62599d commit 355b535

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { execSync } = require('child_process');
2+
const { chmodSync } = require('fs');
3+
const { readFileSync, writeFileSync } = require('fs');
4+
5+
const cli = require.resolve('../../bin/cdktf');
6+
7+
exports.pre = () => {
8+
try {
9+
execSync('which pip')
10+
} catch {
11+
console.error(`Unable to find "pip". Install from https://github.com/pyenv/pyenv-virtualenv`)
12+
process.exit(1);
13+
}
14+
};
15+
16+
exports.post = options => {
17+
// Terraform Cloud configuration settings if the organization name and workspace is set.
18+
if (options.OrganizationName != '') {
19+
console.log(`\nGenerating Terraform Cloud configuration for '${options.OrganizationName}' organization and '${options.WorkspaceName}' workspace.....`)
20+
terraformCloudConfig(options.$base, options.OrganizationName, options.WorkspaceName)
21+
}
22+
23+
const pypi_cdktf = options.pypi_cdktf;
24+
if (!pypi_cdktf) {
25+
throw new Error(`missing context "pypi_cdktf"`);
26+
}
27+
28+
writeFileSync('requirements.txt', pypi_cdktf, 'utf-8');
29+
execSync('pip install -r requirements.txt', { stdio: 'inherit' });
30+
chmodSync('main.py', '700');
31+
32+
execSync(`${cli} get`, { stdio: 'inherit' });
33+
execSync(`python3 ./main.py`);
34+
35+
console.log(readFileSync('./help', 'utf-8'));
36+
};
37+
38+
function terraformCloudConfig(baseName, organizationName, workspaceName) {
39+
template = readFileSync('./main.py', 'utf-8');
40+
41+
const result = template.replace(`MyStack(app, "${baseName}")`, `stack = MyStack(app, "${baseName}")
42+
stack.add_override('terraform.backend', {
43+
'remote': {
44+
'hostname': 'app.terraform.io',
45+
'organization': '${organizationName}',
46+
'workspaces': {
47+
'name': '${workspaceName}'
48+
}
49+
}
50+
})`);
51+
52+
writeFileSync('./main.py', result, 'utf-8');
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"language": "python",
3+
"app": "python3 ./main.py",
4+
"terraformProviders": ["aws@~> 2.0"],
5+
"codeMakerOutput": "imports"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
========================================================================================================
2+
3+
Your cdktf Python project is ready!
4+
5+
cat help Prints this message
6+
7+
Compile:
8+
python3 ./main.py Compile and run the python code.
9+
10+
Synthesize:
11+
cdktf synth Synthesize Terraform resources to cdktf.out/
12+
13+
Diff:
14+
cdktf diff Perform a diff (terraform plan) for the given stack
15+
16+
Deploy:
17+
cdktf deploy Deploy the given stack
18+
19+
Destroy:
20+
cdktf destroy Destroy the given stack
21+
22+
========================================================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
from constructs import Construct
3+
from cdktf import App, TerraformStack
4+
5+
6+
class MyStack(TerraformStack):
7+
def __init__(self, scope: Construct, ns: str):
8+
super().__init__(scope, ns)
9+
10+
# define resources here
11+
12+
13+
app = App()
14+
MyStack(app, "{{ $base }}")
15+
16+
app.synth()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cdktf>=0.0.16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dist/
2+
imports/*
3+
!imports/__init__.py
4+
.terraform
5+
cdktf.out
6+
terraform.tfstate*

0 commit comments

Comments
 (0)