-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate_delft3dflow_operational.py
178 lines (159 loc) · 8.24 KB
/
simulate_delft3dflow_operational.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
169
170
171
172
173
174
175
176
177
178
import json
from datetime import timedelta, datetime
from airflow.operators.bash import BashOperator
from airflow.operators.python_operator import PythonOperator
from airflow.models import Variable
from airflow.utils.dates import days_ago
from functions.email import report_failure
from functions.simulate import (get_last_sunday, get_end_date, get_today, get_restart, number_of_cores,
cache_simulation_data, format_simulation_directory, process_event_notifications)
from airflow import DAG
def create_dag(dag_id, parameters):
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': days_ago(2),
'email': ['james.runnalls@eawag.ch'],
'email_on_failure': False,
'email_on_retry': False,
'queue': 'simulation',
'retries': 1,
'retry_delay': timedelta(minutes=30),
# 'pool': 'backfill',
'priority_weight': 10,
# 'end_date': datetime(2016, 1, 1),
# 'wait_for_downstream': False,
# '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'
}
dag = DAG(
dag_id,
default_args=default_args,
description='Operational Delft3D-Flow simulation.',
schedule_interval=parameters["schedule_interval"],
catchup=False,
tags=['simulation', 'operational'],
user_defined_macros={'filesystem': '/opt/airflow/filesystem',
'FILESYSTEM': Variable.get("FILESYSTEM"),
'model': 'delft3d-flow/' + parameters["simulation_id"],
'docker': parameters["docker"],
'simulation_folder_prefix': format_simulation_directory(parameters["docker"]),
'start': get_last_sunday,
'end': get_end_date,
'today': get_today,
'restart': get_restart,
'bucket': 'alplakes-eawag',
'api': "http://eaw-alplakes2:8000",
'cores': parameters["cores"],
'id': parameters["simulation_id"],
'simulation_repo_name': "alplakes-simulations",
'simulation_repo_https': "https://github.com/eawag-surface-waters-research/alplakes-simulations.git",
'api_user': "alplakes",
'api_server': 'eaw-alplakes2',
'API_PASSWORD': Variable.get("API_PASSWORD"),
'api_server_folder': "/nfsmount/filesystem/media/simulations/delft3d-flow/results/{}".format(
parameters["simulation_id"]),
'number_of_cores': number_of_cores}
)
prepare_simulation_files = BashOperator(
task_id='prepare_simulation_files',
bash_command="mkdir -p {{ filesystem }}/git;"
"cd {{ filesystem }}/git;"
"git clone {{ simulation_repo_https }} && cd {{ simulation_repo_name }} || cd {{ simulation_repo_name }} && git stash && git pull;"
"python src/main.py -m {{ model }} -d {{ docker }} -t {{ today(ds) }} -s {{ start(ds) }} -e {{ end(ds) }} -a {{ api }}",
on_failure_callback=report_failure,
dag=dag,
)
run_simulation = BashOperator(
task_id='run_simulation',
bash_command='docker run '
'-e AWS_ID={{ params.AWS_ID }} '
'-e AWS_KEY={{ params.AWS_KEY }} '
'-v {{ FILESYSTEM }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }}:/job '
'--rm '
'{{ docker }} '
'-p {{ number_of_cores(task_instance, cores) }} '
'-r "{{ params.restart }}{{ restart(ds) }}.000000"',
params={
'restart': 's3://alplakes-eawag/simulations/delft3d-flow/restart-files/{}/tri-rst.Simulation_Web_rst.'.format(
parameters["simulation_id"]),
'AWS_ID': Variable.get("AWS_ACCESS_KEY_ID"),
'AWS_KEY': Variable.get("AWS_SECRET_ACCESS_KEY")},
on_failure_callback=report_failure,
retries=2,
retry_delay=timedelta(minutes=2),
dag=dag,
)
postprocess_simulation_output = BashOperator(
task_id='postprocess_simulation_output',
bash_command="cd {{ filesystem }}/git/{{ simulation_repo_name }};"
"python src/postprocess.py -f {{ filesystem }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }} -d {{ docker }}",
on_failure_callback=report_failure,
dag=dag,
)
events_simulation_output = BashOperator(
task_id='events_simulation_output',
bash_command="cd {{ filesystem }}/git/{{ simulation_repo_name }};"
"python src/events.py -f {{ filesystem }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }} -d {{ docker }}",
on_failure_callback=report_failure,
dag=dag,
)
event_notifications = PythonOperator(
task_id='event_notifications',
python_callable=process_event_notifications,
op_kwargs={"lake": parameters["simulation_id"],
"name": parameters["name"],
"model": "delft3d-flow",
"email_list": ["james.runnalls@eawag.ch", "damien.bouffard@eawag.ch", "anne.leroquais@eawag.ch"],
"bucket": "https://alplakes-eawag.s3.eu-central-1.amazonaws.com",
"folder": "{{ filesystem }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }}",
'AWS_ID': Variable.get("AWS_ACCESS_KEY_ID"),
'AWS_KEY': Variable.get("AWS_SECRET_ACCESS_KEY")},
on_failure_callback=report_failure,
dag=dag,
)
send_results = BashOperator(
task_id='send_results',
bash_command="sshpass -p {{ API_PASSWORD }} scp -r "
"-o StrictHostKeyChecking=no "
"{{ filesystem }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }}/postprocess/* "
"{{ api_user }}@{{ api_server }}:{{ api_server_folder }}",
on_failure_callback=report_failure,
dag=dag,
)
remove_results = BashOperator(
task_id='remove_results',
bash_command="rm -rf {{ filesystem }}/git/{{ simulation_repo_name }}/runs/{{ simulation_folder_prefix }}_{{ id }}_{{ start(ds) }}_{{ end(ds) }}",
on_failure_callback=report_failure,
dag=dag,
)
cache_data = PythonOperator(
task_id='cache_data',
python_callable=cache_simulation_data,
op_kwargs={"lake": parameters["simulation_id"],
"model": "delft3d-flow",
"bucket": "https://alplakes-eawag.s3.eu-central-1.amazonaws.com",
"api": "https://alplakes-api.eawag.ch",
'AWS_ID': Variable.get("AWS_ACCESS_KEY_ID"),
'AWS_KEY': Variable.get("AWS_SECRET_ACCESS_KEY")},
on_failure_callback=report_failure,
dag=dag,
)
update_datalakes = BashOperator(
task_id='update_datalakes',
bash_command="curl https://api.datalakes-eawag.ch/externaldata/update/alplakes",
on_failure_callback=report_failure,
dag=dag,
)
prepare_simulation_files >> run_simulation >> postprocess_simulation_output >> events_simulation_output >> event_notifications >> send_results >> remove_results >> cache_data >> update_datalakes
return dag
with open('dags/simulate_delft3dflow_operational.json') as f:
simulations = json.load(f)
for simulation in simulations:
dag_id = "simulate_delft3dflow_operational_" + simulation["simulation_id"]
globals()[dag_id] = create_dag(dag_id, simulation)