-
Notifications
You must be signed in to change notification settings - Fork 7
/
update_profile_with_prod.py
162 lines (138 loc) · 5.41 KB
/
update_profile_with_prod.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
workflow_jobs = {
'run_dbt.yml': 'run_dbt',
'run_dbt_force.yml': 'run_dbt',
'run_dbt_on_cron.yml': 'dbt_scheduled_run',
'run_incremental_dbt_on_merge.yml': 'dbt_run_on_merge_incremental',
'run_dbt_on_pr.yml' : 'dbt_run_on_pr',
'run_dbt_cleanup.yml' : 'dbt_scheduled_cleanup_run'
}
# Load the profiles.yml file
with open('project_goes_here/profiles.yml', 'r') as f:
profiles = yaml.load(f)
# get the profile name from dbt_project.yml
with open('project_goes_here/dbt_project.yml', 'r') as f:
dbt_project = yaml.load(f)
profile_name = dbt_project['profile']
# Get the type of datawarehouse we are using
dwh_type = sys.argv[1]
print("dwh_type: ", dwh_type)
# Add a new target called "prod" based on the type of datawarehouse
if dwh_type == 'postgres':
profiles[profile_name]['outputs']['prod'] = {
'type': 'postgres',
'host': '{{ env_var("HOST") }}',
'user': '{{ env_var("USERNAME") }}',
'password': '{{ env_var("PASSWORD") }}',
'port': '{{ env_var("PORT") | int }}',
'dbname': '{{ env_var("DATABASE") }}',
'schema': '{{ env_var("SCHEMA") }}',
'threads': 8
}
# update the gh actions jobs
for file, job in workflow_jobs.items():
with open('.github/workflows/'+file, 'r') as f:
data = yaml.load(f)
# edit the env block for all the jobs
data['jobs'][job]['env'] = {
'HOST': '${{ secrets.HOST }}',
'DATABASE': '${{ secrets.DATABASE }}',
'USERNAME': '${{ secrets.USERNAME }}',
'PASSWORD': '${{ secrets.PASSWORD }}',
'PORT': '${{ secrets.PORT }}',
'SCHEMA': '${{ secrets.SCHEMA }}'
}
with open('.github/workflows/'+file, 'w') as f:
yaml.dump(data, f)
elif dwh_type == 'snowflake':
profiles[profile_name]['outputs']['prod'] = {
'type': 'snowflake',
'account': '{{ env_var("SNOWFLAKE_ACCOUNT") }}',
'role': '{{ env_var("SNOWFLAKE_ROLE") }}',
'user': '{{ env_var("USERNAME") }}',
'password': '{{ env_var("PASSWORD") }}',
'database': '{{ env_var("DATABASE") }}',
'schema': '{{ env_var("SCHEMA") }}',
'warehouse': '{{ env_var("WAREHOUSE") }}',
'threads': 8
}
# update the gh actions jobs
for file, job in workflow_jobs.items():
with open('.github/workflows/'+file, 'r') as f:
data = yaml.load(f)
# edit the env block for all the jobs
data['jobs'][job]['env'] = {
'SNOWFLAKE_ACCOUNT': '${{ secrets.SNOWFLAKE_ACCOUNT }}',
'DATABASE': '${{ secrets.DATABASE }}',
'SNOWFLAKE_ROLE': '${{ secrets.SNOWFLAKE_ROLE }}',
'USERNAME': '${{ secrets.USERNAME }}',
'PASSWORD': '${{ secrets.PASSWORD }}',
'SCHEMA': '${{ secrets.SCHEMA }}',
'WAREHOUSE': '${{ secrets.WAREHOUSE }}',
}
with open('.github/workflows/'+file, 'w') as f:
yaml.dump(data, f)
elif dwh_type == 'redshift':
profiles[profile_name]['outputs']['prod'] = {
'type': 'redshift',
'host': '{{ env_var("HOST") }}',
'user': '{{ env_var("USERNAME") }}',
'password': '{{ env_var("PASSWORD") }}',
'port': '{{ env_var("PORT") | int }}',
'dbname': '{{ env_var("DATABASE") }}',
'schema': '{{ env_var("SCHEMA") }}',
'threads': 8
}
# update the gh actions jobs
for file, job in workflow_jobs.items():
with open('.github/workflows/'+file, 'r') as f:
data = yaml.load(f)
# edit the env block for all the jobs
data['jobs'][job]['env'] = {
'HOST': '${{ secrets.HOST }}',
'USERNAME': '${{ secrets.USERNAME }}',
'PASSWORD': '${{ secrets.PASSWORD }}',
'PORT': '${{ secrets.PORT }}',
'DATABASE': '${{ secrets.DATABASE }}',
'SCHEMA': '${{ secrets.SCHEMA }}',
}
with open('.github/workflows/'+file, 'w') as f:
yaml.dump(data, f)
elif dwh_type == 'bigquery':
profiles[profile_name]['outputs']['prod'] = {
'type': 'bigquery',
'method': 'oauth',
'project': '{{ env_var("PROJECT_NAME") }}',
'dataset': '{{ env_var("DATASET") }}',
'threads': 8
}
# update the gh actions jobs
for file, job in workflow_jobs.items():
with open('.github/workflows/'+file, 'r') as f:
data = yaml.load(f)
# edit the env block for all the jobs
data['jobs'][job]['env'] = {
'PROJECT_NAME': '${{ secrets.PROJECT_NAME }}',
'DATASET': '${{ secrets.DATASET }}',
}
with open('.github/workflows/'+file, 'w') as f:
yaml.dump(data, f)
# Save the updated profiles.yml file
with open('project_goes_here/profiles.yml', 'w') as f:
yaml.dump(profiles, f)
# update requirements.txt with the dbt adapter depending on the type of datawarehouse
if dwh_type == 'postgres':
with open('requirements.txt', 'a') as f:
f.write("\ndbt-postgres==1.5.2")
elif dwh_type == 'snowflake':
with open('requirements.txt', 'a') as f:
f.write("\ndbt-snowflake==1.5.3")
elif dwh_type == 'redshift':
with open('requirements.txt', 'a') as f:
f.write("\ndbt-redshift==1.6.1")
elif dwh_type == 'bigquery':
with open('requirements.txt', 'a') as f:
f.write("\ndbt-bigquery==1.6.3")