升級檢查腳本

取得 Airflow 升級檢查套件

Apache Airflow 在 PyPI 中以 apache-airflow 套件發布。升級檢查腳本是獨立 Python 套件的一部分,因為它與核心 Apache Airflow 套件分開,並且僅在一段時間內需要,特別是僅用於從 Airflow 1.10 版本升級到 Airflow 2.0。

雖然在盡可能簡化此升級方面已投入大量工作,但 Airflow 1.10 和 Airflow 2.0 之間仍存在一些不相容的變更。為了盡可能簡化導航,我們建議人們首先升級到 1.10 系列中的最新版本(在撰寫本文時:1.10.15),然後下載此套件並按照以下詳細說明執行腳本。

注意

目前僅正式支援 pip 安裝。

雖然使用其他工具(如 poetrypip-tools)取得了一些成功,但它們與 pip 的工作流程不同,尤其是在約束與需求管理方面。目前不支援透過 Poetrypip-tools 安裝。

使用 bazel 安裝 Airflow 時,已知存在可能導致循環依賴的問題。如果遇到此類問題,請切換至 pipBazel 社群正在努力在 此 PR 中修復此問題,因此較新版本的 bazel 可能會處理它。

如果您希望使用這些工具安裝 airflow,則應使用約束檔案並將其轉換為您的工具所需的適當格式和工作流程。

pip install apache-airflow-upgrade-check

這將安裝最新版本的 Airflow 升級檢查套件。

執行 Airflow 升級檢查套件

airflow upgrade_check

這將在螢幕上列印出在將 Airflow 版本升級到 2.0.0 或更高版本之前應採取的許多動作。

如果執行命令時未發現問題,則命令的結束代碼將為 0(成功),如果執行檢查時遇到問題,則為 1。

以下顯示成功執行升級檢查的範例輸出。

============================================== STATUS ============================================

Check for latest versions of apache-airflow and checker..................................SUCCESS
Remove airflow.AirflowMacroPlugin class..................................................SUCCESS
Chain between DAG and operator not allowed...............................................SUCCESS
Connection.conn_id is not unique.........................................................SUCCESS
Connection.conn_type is not nullable.....................................................SUCCESS
Fernet is enabled by default.............................................................FAIL
GCP service account key deprecation......................................................SUCCESS
Changes in import paths of hooks, operators, sensors and others..........................FAIL
Users must delete deprecated configs for KubernetesExecutor..............................FAIL
Legacy UI is deprecated by default.......................................................SUCCESS
Logging configuration has been moved to new section......................................FAIL
Removal of Mesos Executor................................................................SUCCESS
Users must set a kubernetes.pod_template_file value......................................FAIL
SendGrid email uses old airflow.contrib module...........................................SUCCESS
Changes in import path of remote task handlers...........................................SUCCESS
Jinja Template Variables cannot be undefined.............................................FAIL
Found 7 problems.

========================================== RECOMMENDATIONS ========================================

Fernet is enabled by default
----------------------------
The fernet mechanism is enabled by default to increase the security of the default installation.

Problems:

1.  fernet_key in airflow.cfg must be explicitly set empty as fernet mechanism is enabledby default. This means that the apache-airflow[crypto] extra-packages are always installed.However, this requires that your operating system has libffi-dev installed.

Changes in import paths of hooks, operators, sensors and others
---------------------------------------------------------------
Many hooks, operators and other classes has been renamed and moved. Those changes were part of unifying names and imports paths as described in AIP-21.
The contrib folder has been replaced by providers directory and packages:
https://github.com/apache/airflow#backport-packages

Problems:

1.  Please install ``apache-airflow-backport-providers-presto``
2.  Using ``airflow.hooks.presto_hook.PrestoHook`` will be replaced by ``airflow.providers.presto.hooks.presto.PrestoHook``. Affected file:

以下章節描述了正在執行的操作以及如何套用上面顯示的建議。請注意,上面顯示的結果僅是部分集合,其中僅顯示了七個已識別問題中的前兩個。實際上,所有問題都會顯示在螢幕上。

了解正在檢查的內容

升級檢查會檢查來自 airflow.cfg 的組態資料、來自 Airflow 資料庫的中繼資料,以及已在目前 Airflow 環境中設定的 DAG。

以上述結果為例,已識別出兩個特定問題。

第一個問題在組態檔案 airflow.cfg 中識別,其中 fernet_key 的目前組態選項不再可接受,需要變更。這是因為從 Airflow 2.0 開始,fernet_key 不能留空,而需要具有定義的值。檢查有問題的 airflow.cfg 並搜尋 fernet_key 條目將顯示以下內容

fernet_key =

第二個問題是在其中一個 DAG 中識別出來的。在這種情況下,需要變更 PrestoHook 的此匯入語句,因為它在 Airflow 2.0 中的位置不同。檢查 DAG 檔案可能會顯示以下內容

from airflow.hooks.presto_hook import PrestoHook

我們將在下一節討論如何修復這些問題並使其與 Airflow 2.0 相容。

套用建議

在大多數情況下,升級檢查的「建議」結果部分包含足夠的資訊來進行變更。

對於上面識別出的與 fernet_key 相關的第一個問題,解決方案是在 Airflow 組態檔案 airflow.cfg 中為 fernet_key 輸入有效值。

對於第二個問題,您需要安裝 Presto backport provider。然後您可以使用包含 hook 更新版本的 provider 匯入。

from airflow.providers.presto.hooks.presto import PrestoHook

關閉檢查

進階 Airflow 使用者或具有多個 Airflow 部署的使用者可能希望透過關閉某些不適用於他們的檢查來自訂升級檢查。一個範例是未使用 KubernetesPodOperator 的 Airflow 部署使用者可能希望關閉與 KubernetesPodOperator 相關的升級檢查。

這可以透過建立 YAML 格式的「升級組態檔案」來完成,如下所示

ignored_rules:
    - PodTemplateFileRule

若要在執行升級檢查腳本時使用此組態檔案(在本範例中命名為 upgrade-configuration.yaml),請使用以下命令語法

airflow upgrade_check --config=./upgrade-configuration.yaml

新增自訂檢查

進階 Airflow 使用者或具有多個 Airflow 部署的使用者也可能希望為其環境中的特定元素(無論是 DAG 還是組態相關)新增額外的升級檢查。

這些額外檢查應在 Python 類別中定義,並在「升級組態檔案」中新增為 custom_rules,如下所示

custom_rules:
    - path.CustomCheckClass1
    - path.CustomCheckClass2

現在,使用此組態檔案叫用升級檢查腳本,如下所示(在本範例中,組態檔案名為 upgrade-configuration.yaml

airflow upgrade_check --config=./upgrade-configuration.yaml

此條目是否有幫助?