-
Notifications
You must be signed in to change notification settings - Fork 3
/
migrate_state_resources.py
executable file
·67 lines (56 loc) · 1.51 KB
/
migrate_state_resources.py
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
#!/usr/bin/env python3
import subprocess
import sys
FROM_STATE = "module.argo-registration."
TO_STATE = "module.argo-registration[0]."
if len(sys.argv) <= 1:
print(
f"Usage: {sys.argv[0]} <workspace_name> --yes ie {sys.argv[0]} Indico-Dev-us-east-2-dev-ci --yes"
)
sys.exit(1)
workspace = sys.argv[1]
dry_run = "-dry-run"
if len(sys.argv) > 2 and sys.argv[2] == "--yes":
dry_run = ""
backend_string = f"""
terraform {{
required_version = ">= 0.13.5"
cloud {{
organization = "indico"
workspaces {{
name = "{workspace}"
}}
}}
}}
"""
with open("backend.tf", "w") as f:
f.write(backend_string)
def run(args, stdout=subprocess.PIPE, shell=False):
return subprocess.run(args, stdout=stdout, shell=shell)
subprocess.run(["terraform", "init", "-upgrade"]) # TODO: remove comment
result = run(["terraform", "state", "list"])
for l in result.stdout.splitlines():
line = l.decode()
if line.startswith(FROM_STATE):
newpath = line.replace(FROM_STATE, TO_STATE)
if dry_run != "":
subprocess.run(
[
"terraform",
"state",
"mv",
dry_run,
line,
newpath,
]
)
else:
subprocess.run(
[
"terraform",
"state",
"mv",
line,
newpath,
]
)