[英]Execute gcloud commands with python subprocess in Airflow task
我想構建使用多個 gcloud 命令的 Airflow 任務。 一個簡單的例子:
def worker(**kwargs) :
exe = subprocess.run(["gcloud", "compute", "instances", "list"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(exe.returncode)
for line in exe.stdout.splitlines() :
print(line.decode())
exe = subprocess.run(["gcloud", "compute", "ssh", "user@host", "--command=pwd"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
print(exe.returncode)
for line in exe.stdout.splitlines() :
print(line.decode())
dag = DAG("TEST", default_args=default_args, schedule_interval=None)
worker_task = PythonOperator(task_id='sample-task', python_callable=worker, provide_context = True, dag=dag)
worker_task
我有這個錯誤:
ERROR: gcloud crashed (AttributeError): 'NoneType' object has no attribute 'isatty'
除了 airflow 之外,這些命令都可以正常工作。
我已經嘗試使用“--quiet”禁用 gcloud 交互模式,但這無濟於事。
我不想使用 airflow 中的 GcloudOperator 運算符,因為這些命令必須集成在自定義運算符中。
預先感謝您的幫助
也許使用 BashOperator?
worker_task = BashOperator(task_id="sample-task",bash_command='gcloud compute instances list', dag=dag)
如我所見,您的兩個命令是獨立的,因此您可以在操作員BashOperator
的兩個單獨任務中運行它們,如果您想訪問命令的 output ,每個命令的 output 將作為xcom
提供,您可以使用ti.xcom_pull(task_ids='<the task id>')
閱讀它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.