通过命令行

时间:2018-04-26 10:30:31

标签: python command-line google-cloud-platform airflow

Airflow允许我们通过命令行airflow connections添加连接信息。这有助于通过ansible或其他开发工具自动部署气流装置。

目前尚不清楚如何通过命令行将与谷歌云平台(服务帐户)的连接添加到ariflow。

1 个答案:

答案 0 :(得分:3)

Pre airflow 1.9以下示例概述了如何使用DAG添加连接信息:https://gist.github.com/yu-iskw/42f9f0aa6f2ff0a2a375d43881e13b49

def add_gcp_connection(ds, **kwargs):
    """"Add a airflow connection for GCP"""
    new_conn = Connection(
        conn_id=<CONNECTION_ID>,
        conn_type='google_cloud_platform',
    )
    scopes = ['https://www.googleapis.com/auth/cloud-platform']
    conn_extra = {
        "extra__google_cloud_platform__scope": ",".join(scopes),
        "extra__google_cloud_platform__project":
            "<GCP_PROJECT_NAME>",
        "extra__google_cloud_platform__key_path":
            "<GCP_CREDENTIALS_ABSOLUTE_PATH.json>"
    }
    conn_extra_json = json.dumps(conn_extra)
    new_conn.set_extra(conn_extra_json)

    session = settings.Session()
    session.add(new_conn)
    session.commit()

从气流1.9向前可以:

airflow connections -a \
  --conn_id=<CONNECTION_ID> \
  --conn_type=google_cloud_platform \
  --conn_extra='{ "extra__google_cloud_platform__key_path":" '`
        `'<GCP_CREDENTIALS_ABSOLUTE_PATH.json>", '`
    `'"extra__google_cloud_platform__project": '`
        `'"<GCP_PROJECT_NAME>", '`
    `'"extra__google_cloud_platform__scope":  '`
        `'"https://www.googleapis.com/auth/cloud-platform"}'