基於 YAML 檔案的 OpenResty Edge 配置映象

我們提供了一個命令列工具,用以支援基於本地 YAML 檔案配置更新 OpenResty Edge 中的配置。配置按照分割槽劃分,每組配置對應 OpenResty Edge 中的一個分割槽。可以使用 Git 等程式對本地配置進行版本控制。

以下文件內容解釋瞭如何使用該工具。

建立 API 令牌

  1. 首先登入到 OpenResty Edge 管理控制檯。
  2. 然後點選右上角的使用者名稱:USERNAME > API 令牌 > 建立 API 令牌 > 複製 API 令牌。

更新或新增配置到 OpenResty Edge

我們將處理配置的命令列工具和初始配置分開到不同的包中,以防後續釋出的新包中的初始配置將您更新過的配置覆蓋了。

  • OpenResty 下載中心下載 openresty-edge-cmd-tools-VERSION.tar.gzopenresty-edge-cmd-configs-VERSION.tar.gzopenresty-edge-cmd-tools-VERSION.tar.gz 包含命令列工具。openresty-edge-cmd-configs-VERSION.tar.gz 包含配置。如果您正從其他平臺遷移到 OpenResty Edge,我們可以對您的配置進行轉換併為您提供示例。

  • openresty-edge-cmd-tools-VERSION.tar.gzopenresty-edge-cmd-configs-VERSION.tar.gz 包上傳到已安裝 Python3 並能夠訪問 OpenResty Edge 管理控制檯的機器上。

  • openresty-edge-cmd-configs-VERSION.tar.gz 解壓得到以下目錄:

    - openresty-edge-cmd-configs-VERSION/
    |-- configs/
      |-- global_lua_modules/
        |-- util.lua
        |-- uuid.lua
        |-- ...
      |-- upstreams/
        |-- upstream-a.yaml
        |-- upstream-b.yaml
        |-- ...
      |-- page_rules
        |-- 000-core-rules.yaml
        |-- other-rules.yaml
        |-- ...
      |-- basic_auth_groups
        |-- group1.yaml
      |-- global_configs.yaml
    

    configs/ 目錄包含了某個分割槽的所有配置。global_lua_modules/ 目錄儲存了全域性 Lua 模組,每個 Lua 檔案代表一個 Lua 模組。upstreams/ 目錄包含了上游伺服器配置,您可以將上游分割成多個檔案以便於管理。page_rules 目錄中包含頁面規則,這些規則將被新增到與分割槽相關的 HTTP 應用中,規則的順序根據檔名稱字母序和檔案內的規則順序決定。basic_auth_groups 目錄中是 Basic 認證使用者組及使用者配置,當使用 Basic 認證相關動作時,需要用到。global_configs.yaml 檔案包括全域性配置,如全域性使用者變數。

  • openresty-edge-cmd-tools-VERSION.tar.gz 解壓得到以下目錄:

    - openresty-edge-cmd-tools-VERSION/
    |-- edge-config.py
    |-- gen-cert.sh
    

    edge-config.py 是您可以執行以管理分割槽配置的命令列工具。 您可以透過命令列工具的 -i 選項指定配置路徑;當不指定選項 -i 選項時,預設使用與 edge-config.py 同目錄的 configs/ 目錄。

    您可以參考我們提供的配置檔案和目錄結構為其他環境生成配置。如果遇到任何問題,或者命令列工具需要支援額外的配置,請隨時與我們聯絡,我們將非常樂意為您提供必要的支援。

  • 安裝命令列工具所需的依賴

    pip3 install openresty-edge-sdk ruamel.yaml
    

    openresty-edge-sdk 的版本應該大於或等於 1.2.52

  • 執行命令列工具

    如果您想看到這個命令列工具的使用方法,您可以執行以下命令:

    python3 edge-config.py -h
    

    這個命令的輸出看起來像這樣:

    usage: edge-config.py [-h] -t API_TOKEN -u ADMIN_URL [-s] [-c] [-i CONFIGS_PATH] [-l LOCATION]
                          [-d DOMAIN] (-p PARTITION_ID | -n PARTITION_NAME)
    
    Update or add OpenResty Edge configuration.
    
    optional arguments:
      -h, --help            show this help message and exit
      -t API_TOKEN, --api-token API_TOKEN
                            specify the API Token for sending the request
      -u ADMIN_URL, --admin-url ADMIN_URL
                            specify the URL of the OpenResty Edge Admin. For example,
                            https://admin.com:443
      -s, --disable-ssl-verify
                            turn off SSL verify for requests to access OpenResty Edge Admin
      -c, --cleanup         based on the location. This option allows for the cleanup of page rules,
                            application upstreams, global user variables, and resetting the access log
                            format for partitions. It can also be used independently of the location
      -i CONFIGS_PATH, --configs-path CONFIGS_PATH
                            specify the path to the configuration file; if not specified, the default is
                            the 'configs' directory in the same directory as the current script
      -l LOCATION, --location LOCATION
                            specify the configuration name that needs to be updated, supported:
                            page_rules, upstreams, global_lua_modules, global_configs
      -d DOMAIN, --domain DOMAIN
                            specify a domain name. When an HTTP application containing this domain exists,
                            it will be updated; otherwise, a new application will be created
      -p PARTITION_ID, --partition-id PARTITION_ID
                            specify the id of the partition where you want to add or update the
                            configuration
      -n PARTITION_NAME, --partition-name PARTITION_NAME
                            specify the name of the partition where you want to add or update the
                            configuration
    

    透過指定 -p PARTITION_ID-n PARTITION_NAME 選項,可以將配置更新到指定的分割槽。

    示例:

    python3 edge-config.py -t 3ed00172-280e-4e43-a4ef-3c2fae5669ca -u https://192.168.1.1 -s -i /path/of/configs -n default -d test.com
    

    此命令將更新配置到預設分割槽(名稱為 default,id 為 1),如果 test.com 應用不存在,則會建立它。

如果您繼續遇到 No module named 'MODULE_NAME' 錯誤,請使用以下命令安裝依賴:

pip3 install MODULE_NAME

請將命令中的 MODULE_NAME 更改為實際的模組名稱。

更多示例:

# 刪除:lua 模組、全域性使用者變數、應用
# 重置:訪問日誌格式
python3 edge-config.py -t 6cc1cf09-1a39-4c17-8ed6-c11838edac12 \
    -u https://192.168.1.1 -s -i /path/of/configs -n default -d test.com -c

# 從應用 "test.com" 中移除所有頁面規則
python3 edge-config.py -t 6cc1cf09-1a39-4c17-8ed6-c11838edac12 \
    -u https://192.168.1.1 -s -i /path/of/configs -n default -l page_rules -d test.com -c

# 從分割槽 1(預設)中移除所有 Lua 模組
python3 edge-config.py -t 6cc1cf09-1a39-4c17-8ed6-c11838edac12 \
    -u https://192.168.1.1 -s -i /path/of/configs -n default -l global_lua_modules -c

# 將所有配置更新/插入到分割槽 1(預設)
python3 edge-config.py -t 6cc1cf09-1a39-4c17-8ed6-c11838edac12 \
    -u https://192.168.1.1 -s -i /path/of/configs -n default -d test.com

# 將頁面規則更新/插入到應用 "test.com"
python3 edge-config.py -t 6cc1cf09-1a39-4c17-8ed6-c11838edac12 \
    -u https://192.168.1.1 -s -i /path/of/configs -n default -l page_rules -d test.com

配置示例

更多示例見基於 YAML 檔案的 OpenResty Edge 配置示例

- enable_rule: true
  actions:
    set-access-log-off: {}
    print:
      content_type: text/html
      msg: favicon.ico 和 robots.txt
    exit:
      code: 200
  conditions:
  - op: eq
    var: uri
    vals:
      - /favicon.ico
      - /robots.txt
  order: 1
  comment: ''
- enable_rule: true
  actions:
    "user-code":
      el: |-
        {
        my Str $transid;

        true =>
            foreign-call(module: "example", func: "go"),
            $transid = ctx-var("lm_transid"),
            $or_global_user_variable_uuid = $transid,
            done;
        }
  comment: ''

頁面規則是有序的,更新到 Edge Admin 時將保持配置檔案中的順序。此示例包括兩條頁面規則。第一條規則判斷 URI 是否為 /favicon.ico/robots.txt,如果是,則禁用訪問日誌並輸出指定內容。第二條規則無條件執行一段 Edgelang 程式碼,該程式碼設定全域性使用者變數 $or_global_user_variable_uuid

所有頁面規則組成一個陣列,每條頁面規則都是陣列的一個元素。

錯誤訊息示例

對於配置檔案錯誤,命令列工具將嘗試提供提示,指出錯誤發生的具體行號。

[!] Error: invalid port in upstream upstream_name_foo: 70000, file: foo.yaml, line: 6.

還有其他一些錯誤,例如下面的錯誤表明輸入的配置路徑不正確,命令列工具找不到這個路徑。

[!] Error: bad configs path: /bad/path.