forked from Scalr/terraform-scalr-migrate-tfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
113 lines (103 loc) · 3.02 KB
/
main.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
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
from argparse import ArgumentParser
import json
import os
from migrator import Migrator
def parse_args():
parser = ArgumentParser()
parser.add_argument(
"--workspace-regex",
required=True,
help="Regex to match workspaces to migrate",
type=str,
)
parser.add_argument(
"--cdktf-path",
required=True,
help="Path to the CDKTF repository",
type=str,
)
parser.add_argument(
"--scalr-account-id",
required=True,
help="Scalr account ID",
type=str,
)
parser.add_argument(
"--vcs-id",
required=True,
help="Scalr VCS ID",
type=str,
)
parser.add_argument(
"--scalr-hostname",
required=True,
help="Scalr hostname",
type=str,
)
parser.add_argument(
"--tf-hostname",
help="Terraform Cloud hostname",
default="app.terraform.io",
type=str,
)
parser.add_argument(
"--tf-organization",
required=True,
help="Terraform Cloud organization",
type=str,
)
parser.add_argument(
"--aws-profile",
required=False,
help="AWS profile to use for fetching secrets from SSM. Defaults to the AWS_PROFILE environment variable.",
type=str,
default=os.getenv("AWS_PROFILE", "missing"),
)
parser.add_argument(
"--aws-region",
required=False,
help="AWS region to use for fetching secrets from SSM. Defaults to the AWS_REGION environment variable.",
default=os.getenv("AWS_REGION", "missing"),
)
parser.add_argument(
"--aws-ssm-prefix",
type=str,
required=True,
help="AWS SSM parameter prefix. Secrets are assumed to be stored under {prefix}/{workspace_name}/{key}",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
tf_organization = args.tf_organization
scalr_account_id = args.scalr_account_id
vcs_id = args.vcs_id
scalr_hostname = args.scalr_hostname
tf_hostname = args.tf_hostname
cdktf_path = args.cdktf_path
workspace_regex = args.workspace_regex
aws_profile = args.aws_profile
aws_region = args.aws_region
with open(os.path.expanduser("~/.terraform.d/credentials.tfrc.json"), "r") as f:
credentials = json.load(f)
tfe_token = credentials["credentials"][tf_hostname]["token"]
scalr_token = credentials["credentials"][scalr_hostname]["token"]
migrator = Migrator(
lock=True,
scalr_token=scalr_token,
tf_token=tfe_token,
tf_hostname=tf_hostname,
scalr_hostname=scalr_hostname,
tf_organization=tf_organization,
account_id=scalr_account_id,
skip_workspace_creation=False,
skip_backend_secrets=False,
cdktf_path=cdktf_path,
workspace_regex=workspace_regex,
vcs_id=vcs_id,
aws_profile=aws_profile,
aws_region=aws_region,
)
migrator.migrate_workspaces()
if __name__ == "__main__":
main()