dag14_crick_seqrun_transfer.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. from datetime import timedelta
  2. from airflow.models import DAG,Variable
  3. from airflow.utils.dates import days_ago
  4. from airflow.contrib.operators.ssh_operator import SSHOperator
  5. from airflow.contrib.hooks.ssh_hook import SSHHook
  6. from airflow.operators.bash_operator import BashOperator
  7. from airflow.operators.python_operator import PythonOperator
  8. from airflow.contrib.operators.sftp_operator import SFTPOperator
  9. #from igf_airflow.utils.dag14_crick_seqrun_transfer_utils import check_and_transfer_run_func
  10. #from igf_airflow.utils.dag14_crick_seqrun_transfer_utils import extract_tar_file_func
  11. FTP_SEQRUN_SERVER = Variable.get('crick_ftp_seqrun_hostname')
  12. FTP_CONFIG_FILE = Variable.get('crick_ftp_config_file_wells')
  13. SEQRUN_BASE_PATH = Variable.get('seqrun_base_path')
  14. HPC_SEQRUN_BASE_PATH = Variable.get('hpc_seqrun_path')
  15. args = {
  16. 'owner': 'airflow',
  17. 'start_date': days_ago(2),
  18. 'retries': 1,
  19. 'retry_delay': timedelta(minutes=5),
  20. 'provide_context': True,
  21. 'email_on_failure': False,
  22. 'email_on_retry': False,
  23. 'catchup': False,
  24. 'max_active_runs': 1,
  25. }
  26. ## SSH HOOK
  27. #orwell_ssh_hook = \
  28. # SSHHook(
  29. # key_file=Variable.get('hpc_ssh_key_file'),
  30. # username=Variable.get('hpc_user'),
  31. # remote_host=Variable.get('orwell_server_hostname'))
  32. dag = \
  33. DAG(
  34. dag_id='dag14_crick_seqrun_transfer',
  35. schedule_interval=None,
  36. default_args=args,
  37. tags=['ftp', 'hpc', 'orwell', 'wells'])
  38. with dag:
  39. ## TASK
  40. # not working on HPC
  41. #check_and_transfer_run = \
  42. # PythonOperator(
  43. # task_id='check_and_transfer_run',
  44. # dag=dag,
  45. # pool='crick_ftp_pool',
  46. # queue='hpc_4G',
  47. # params={'ftp_seqrun_server': FTP_SEQRUN_SERVER,
  48. # 'hpc_seqrun_base_path': HPC_SEQRUN_BASE_PATH,
  49. # 'ftp_config_file': FTP_CONFIG_FILE},
  50. # python_callable=check_and_transfer_run_func)
  51. ## TASK
  52. #extract_tar_file = \
  53. # PythonOperator(
  54. # task_id='extract_tar_file',
  55. # dag=dag,
  56. # queue='hpc_4G',
  57. # params={'hpc_seqrun_base_path': HPC_SEQRUN_BASE_PATH},
  58. # python_callable=extract_tar_file_func)
  59. #check_and_transfer_run = \
  60. # BashOperator(
  61. # task_id='check_and_transfer_run',
  62. # dag=dag,
  63. # pool='crick_ftp_pool',
  64. # do_xcom_push=False,
  65. # queue='wells',
  66. # params={'ftp_seqrun_server': FTP_SEQRUN_SERVER,
  67. # 'seqrun_base_path': HPC_SEQRUN_BASE_PATH,
  68. # 'ftp_config_file': FTP_CONFIG_FILE},
  69. # bash_command="""
  70. # python /rds/general/user/igf/home/data2/airflow_test/github/data-management-python/scripts/ftp_seqrun_transfer/transfer_seqrun_from_crick.py \
  71. # -f {{ params.ftp_seqrun_server }} \
  72. # -s {{ dag_run.conf["seqrun_id"] }} \
  73. # -d {{ params.seqrun_base_path }} \
  74. # -c {{ params.ftp_config_file }}
  75. # """)
  76. check_and_transfer_run = \
  77. SFTPOperator(
  78. task_id='check_and_transfer_run',
  79. dag=dag,
  80. pool='crick_ftp_pool',
  81. queue='wells',
  82. ssh_conn_id="crick_sftp_conn",
  83. local_filepath="/rds/general/project/genomics-facility-archive-2019/ephemeral/{{ dag_run.conf['seqrun_id'] }}.tar.gz",
  84. remote_filepath="/users/dattaa/runs/{{ dag_run.conf['seqrun_id'] }}.tar.gz",
  85. operation='get'
  86. )
  87. # TASK
  88. extract_tar_file = \
  89. BashOperator(
  90. task_id='extract_tar_file',
  91. dag=dag,
  92. do_xcom_push=False,
  93. queue='wells',
  94. params={'seqrun_base_path': HPC_SEQRUN_BASE_PATH},
  95. bash_command="""
  96. cd {{ params.seqrun_base_path }};
  97. if [ -d temp_{{ dag_run.conf["seqrun_id"] }} ];
  98. then
  99. echo "Seqrun dir exists";
  100. exit 1;
  101. else
  102. mkdir -p temp_{{ dag_run.conf["seqrun_id"] }};
  103. tar \
  104. --no-same-owner \
  105. --no-same-permissions \
  106. --owner=igf \
  107. -xzf {{ dag_run.conf["seqrun_id"] }}.tar.gz \
  108. -C temp_{{ dag_run.conf["seqrun_id"] }};
  109. find temp_{{ dag_run.conf["seqrun_id"] }} \
  110. -type d \
  111. -exec chmod 700 {} \;
  112. chmod -R u+r temp_{{ dag_run.conf["seqrun_id"] }};
  113. chmod -R u+w temp_{{ dag_run.conf["seqrun_id"] }};
  114. fi
  115. """
  116. )
  117. ## TASK
  118. move_seqrun_dir = \
  119. BashOperator(
  120. task_id='move_seqrun_dir',
  121. dag=dag,
  122. do_xcom_push=False,
  123. queue='hpc_4G',
  124. params={'seqrun_base_path': SEQRUN_BASE_PATH},
  125. bash_command="""
  126. cd {{ params.seqrun_base_path }};
  127. if [ -d {{ dag_run.conf["seqrun_id"] }} ];
  128. then
  129. echo "Seqrun dir exists";
  130. exit 1;
  131. fi
  132. ls temp_{{ dag_run.conf["seqrun_id"] }}/camp/stp/sequencing/inputs/instruments/sequencers/{{ dag_run.conf["seqrun_id"] }};
  133. move temp_{{ dag_run.conf["seqrun_id"] }}/camp/stp/sequencing/inputs/instruments/sequencers/{{ dag_run.conf["seqrun_id"] }} {{ params.seqrun_base_path }}/{{ dag_run.conf["seqrun_id"] }};
  134. """
  135. )
  136. ## PIPELINE
  137. check_and_transfer_run >> extract_tar_file >> move_seqrun_dir