-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathinstall_terraform.py
More file actions
executable file
·135 lines (107 loc) · 4.54 KB
/
install_terraform.py
File metadata and controls
executable file
·135 lines (107 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Script to set up terraform and databricks terraform provider in a local directory:
- Download terraform.
- Download databricks provider.
- Write a .terraformrc config file that uses this directory.
- The config file contains env vars that need to be set so that databricks CLI uses this terraform and provider.
"""
import os
import platform
import zipfile
import argparse
import json
from pathlib import Path
from urllib.request import urlretrieve
os_name = platform.system().lower()
arch = platform.machine().lower()
arch = {
"x86_64": "amd64",
"aarch64": "arm64",
}.get(arch, arch)
if os_name == "windows" and arch not in ("386", "amd64"):
# terraform 1.5.5 only has builds for these two.
arch = "amd64"
terraform_version = "1.5.5"
terraform_file = f"terraform_{terraform_version}_{os_name}_{arch}.zip"
terraform_url = f"https://releases.hashicorp.com/terraform/{terraform_version}/{terraform_file}"
terraform_binary = "terraform.exe" if os_name == "windows" else "terraform"
def retrieve(url, path):
if not path.exists():
print(f"Downloading {url} -> {path}")
urlretrieve(url, path)
def read_version(path):
for line in path.open():
if "ProviderVersion" in line:
# Expecting 'const ProviderVersion = "1.64.1"'
items = line.strip().split()
assert len(items) >= 3, items
assert items[-3:-1] == ["ProviderVersion", "="], items
version = items[-1].strip('"')
assert version, items
return version
raise SystemExit(f"Could not find ProviderVersion in {path}")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--targetdir", default="build", type=Path)
parser.add_argument("--provider-version")
args = parser.parse_args()
target = args.targetdir
if not args.provider_version:
version_file = Path(__file__).parent.parent / "bundle/internal/tf/codegen/schema/version.go"
assert version_file.exists(), version_file
terraform_provider_version = read_version(version_file)
print(f"Read version {terraform_provider_version} from {version_file}")
else:
terraform_provider_version = args.provider_version
terraform_provider_file = f"terraform-provider-databricks_{terraform_provider_version}_{os_name}_{arch}.zip"
terraform_provider_url = f"https://github.com/databricks/terraform-provider-databricks/releases/download/v{terraform_provider_version}/{terraform_provider_file}"
target.mkdir(exist_ok=True, parents=True)
zip_path = target / terraform_file
terraform_path = target / terraform_binary
terraform_provider_path = target / terraform_provider_file
retrieve(terraform_url, zip_path)
retrieve(terraform_provider_url, terraform_provider_path)
if not terraform_path.exists():
print(f"Extracting {zip_path} -> {terraform_path}")
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(target)
terraform_path.chmod(0o755)
tfplugins_path = target / "tfplugins"
provider_dir = Path(
tfplugins_path / f"registry.terraform.io/databricks/databricks/{terraform_provider_version}/{os_name}_{arch}"
)
if not provider_dir.exists():
print(f"Extracting {terraform_provider_path} -> {provider_dir}")
os.makedirs(provider_dir, exist_ok=True)
with zipfile.ZipFile(terraform_provider_path, "r") as zip_ref:
zip_ref.extractall(provider_dir)
files = list(provider_dir.iterdir())
assert files, provider_dir
for f in files:
f.chmod(0o755)
terraformrc_path = target / ".terraformrc"
if not terraformrc_path.exists():
path = json.dumps(str(tfplugins_path.absolute()))
# Check for dev override environment variable
dev_override_path = os.environ.get("DATABRICKS_TF_PROVIDER_DEV_OVERRIDE")
dev_overrides_section = ""
if dev_override_path:
dev_overrides_section = f""" dev_overrides {{
"databricks/databricks" = "{dev_override_path}"
}}
"""
text = f"""# Set these env variables before running databricks cli:
# export DATABRICKS_TF_CLI_CONFIG_FILE={terraformrc_path.absolute()}
# export DATABRICKS_TF_EXEC_PATH={terraform_path.absolute()}
provider_installation {{
{dev_overrides_section} filesystem_mirror {{
path = {path}
include = ["registry.terraform.io/databricks/databricks"]
}}
}}
"""
print(f"Writing {terraformrc_path}:\n{text}")
terraformrc_path.write_text(text)
if __name__ == "__main__":
main()