-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmigrate_captures_dag.py
168 lines (149 loc) · 5.33 KB
/
migrate_captures_dag.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
163
164
165
166
167
168
from datetime import datetime, timedelta
from textwrap import dedent
from pprint import pprint
from airflow import DAG
from airflow.providers.postgres.operators.postgres import PostgresOperator
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.operators.bash import BashOperator
from airflow.operators.python import PythonOperator
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator
import psycopg2.extras
from airflow.models import Variable
# These args will get passed on to each operator
# You can override them on a per-task basis during operator initialization
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'email': ['[email protected]'],
'email_on_failure': True,
'email_on_retry': False,
'retries': 0,
'retry_delay': timedelta(minutes=5),
# 'queue': 'bash_queue',
# 'pool': 'backfill',
# 'priority_weight': 10,
# 'end_date': datetime(2016, 1, 1),
# 'wait_for_downstream': False,
# 'dag': dag,
# 'sla': timedelta(hours=2),
# 'execution_timeout': timedelta(seconds=300),
# 'on_failure_callback': some_function,
# 'on_success_callback': some_other_function,
# 'on_retry_callback': another_function,
# 'sla_miss_callback': yet_another_function,
# 'trigger_rule': 'all_success'
}
with DAG(
'migrate-captures',
default_args=default_args,
description='migrate legacy data to new domain db',
schedule_interval= "@daily",
start_date=datetime(2021, 1, 1),
catchup=False,
tags=['db','domain'],
) as dag:
t1 = BashOperator(
task_id='print_date',
bash_command='date',
)
postgresConnId = "postgres_default"
db = PostgresHook(postgres_conn_id=postgresConnId)
conn = db.get_uri()
# Airflow Variable DATABASE_PASSWORD is masked in the Airflow Variables UI and logs but not when actually used
# https://airflow.apache.org/docs/apache-airflow/stable/administration-and-deployment/security/secrets/mask-sensitive-values.html
# tested working in the https://github.com/Greenstand/treetracker-airflow-dags/blob/main/test1.py DAG
environments = {
'DATABASE_URL': "postgresql://" + Variable.get("DATABASE_LOGIN") + ":" + Variable.get("DATABASE_PASSWORD") + "@" + Variable.get("DATABASE"),
'NODE_TLS_REJECT_UNAUTHORIZED': '0',
}
image = 'greenstand/domain-migration-scripts:1.2.8'
namespace = 'airflow'
migrate_trees = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-trees"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_trees",
get_logs=True,
env_vars=environments
)
migrate_planter_info = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-planters"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_planter_info",
get_logs=True,
env_vars=environments
)
migrate_tags = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-tags"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_tags",
get_logs=True,
env_vars=environments
)
migrate_device_configurations = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-devices"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_device_configurations",
get_logs=True,
env_vars=environments
)
migrate_species = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-species"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_species",
get_logs=True,
env_vars=environments
)
migrate_raw_capture = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-raw-captures"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_raw_capture",
get_logs=True,
env_vars=environments
)
migrate_approved_capture = KubernetesPodOperator(
namespace=namespace,
image=image,
cmds=["sh", "-c", "npm run migrate-captures"],
name="airflow-k8s-pod",
do_xcom_push=False,
is_delete_operator_pod=True,
in_cluster=True,
task_id="k8s-pod-migrate_approved_capture",
get_logs=True,
env_vars=environments
)
migrate_planter_info >> migrate_raw_capture
migrate_device_configurations >> migrate_raw_capture
migrate_species >> migrate_raw_capture
migrate_tags >> migrate_raw_capture
migrate_raw_capture >> migrate_approved_capture >> migrate_trees >> t1