简体   繁体   English

通过命令行将 GCP 凭据添加到 airflow

[英]Add GCP credentials to airflow via command line

Airflow allows us to add connection information via command-line airflow connections . Airflow 允许我们通过命令行airflow connections添加连接信息。 This can help with automated deployment of airflow installations via ansible or other dev-ops tools.这有助于通过 ansible 或其他开发操作工具自动部署 airflow 安装。

It is unclear how connections to google cloud platform (service accounts) can be added to ariflow via command line.目前尚不清楚如何通过命令行将与谷歌云平台(服务帐户)的连接添加到 ariflow。

Pre airflow 1.9 the following example outlines how to use a DAG to add connection information: https://gist.github.com/yu-iskw/42f9f0aa6f2ff0a2a375d43881e13b49 在气流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()

From airflow 1.9 forward one can: 从气流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"}'

Since this was posted, documentation has been added for Google Cloud connections: https://airflow.apache.org/docs/apache-airflow-providers-google/stable/connections/gcp.html自发布以来,已为 Google Cloud 连接添加了文档: https : //airflow.apache.org/docs/apache-airflow-providers-google/stable/connections/gcp.html

As an alternative to using --conn_extra , which can can make it challenging to substitute values into the json string, you can use the conn_uri .作为使用--conn_extra的替代方法,这可能会使将值替换为 json 字符串具有挑战性,您可以使用conn_uri For example:例如:

# AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT='google-cloud-platform://'
# GCP_PROJECT='<your-gcp-project>'
airflow connections \
  --add \
  --conn_id '<CONNECTION_ID>' \
  --conn_uri "${AIRFLOW_CONN_GOOGLE_CLOUD_DEFAULT}?extra__google_cloud_platform__project=${GCP_PROJECT}"

Where you can add as many additional uri params as needed, separated by &s.您可以根据需要添加尽可能多的额外 uri 参数,以 &s 分隔。

Another more readable example which works in Airflow 2.2.5.另一个更具可读性的示例,适用于 Airflow 2.2.5。
Here the JSON file test.json:这里是 JSON 文件 test.json:

{"test2_gcp_connection": {
"conn_type": "google_cloud_platform",
"description": null,
"host": null,
"login": null,
"password": null,
"schema": null,
"port": null,
"extra": "{ \"extra__google_cloud_platform__key_path\": \"/path/to/key.json\", \"extra__google_cloud_platform__project\": \"project_name\", \"extra__google_cloud_platform__scope\": \"https://www.googleapis.com/auth/cloud-platform\"}"}}

You can then use the command:然后你可以使用命令:

airflow connections import test.json

And your GCP connection will be created.您的 GCP 连接将被创建。

In a similar way, you can export an already created airflow connection in a file named test.json using the command:以类似的方式,您可以使用以下命令将已创建的 airflow 连接导出到名为 test.json 的文件中:

airflow connections export test.json

Another way to write it in a similar but more simplified way than the first answer:另一种以与第一个答案类似但更简化的方式编写它的方法:

airflow connections add 'test_gcp_connection' --conn-type=google_cloud_platform --conn-extra='{ "extra__google_cloud_platform__key_path": "path/to/key.json", "extra__google_cloud_platform__project": "projectname", "extra__google_cloud_platform__scope": "https://www.googleapis.com/auth/cloud-platform"}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM