更新组件配置 - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

更新组件配置

组件配置是定义每个组件参数的 JSON 对象。每个组件的配方都定义了其默认配置,当您将组件部署到核心设备时,您可以修改这些配置。

创建部署时,可以指定要应用于每个组件的配置更新。配置更新是补丁操作,这意味着更新会修改核心设备上存在的组件配置。如果核心设备没有该组件,则配置更新会修改并应用该部署的默认配置。

配置更新定义了重置更新和合并更新。重置更新定义了要将哪些配置值重置为默认值或移除哪些配置值。合并更新定义要为组件设置的新配置值。部署配置更新时, Amazon IoT Greengrass Core 软件会在合并更新之前运行重置更新。

组件可以验证您部署的配置更新。当部署更改其配置时,该组件订阅后会收到通知,并且它可以拒绝其不支持的配置。有关更多信息,请参阅 与组件配置交互

重置更新

重置更新定义了核心设备上要将哪些配置值重置为默认值。如果配置值没有默认值,则重置更新会将该值从组件的配置中删除。这可以帮助您修复因配置无效而中断的组件。

使用 JSON 指针列表来定义要重置哪些配置值。JSON 指针以正斜杠/开头。要标识嵌套组件配置中的值,请使用正斜杠 (/) 分隔配置中每个级别的键。有关更多信息,请参阅 JSON 指针规范

注意

您只能将整个列表重置为其默认值。您不能使用重置更新来重置列表中的单个元素。

要将组件的整个配置重置为其默认值,请指定一个空字符串作为重置更新。

"reset": [""]

合并更新

合并更新定义要插入核心组件配置的配置值。合并更新是一个 JSON 对象, Amazon IoT Greengrass 核心软件会在重置您在重置更新中指定的路径中重置值后合并该对象。使用 Amazon CLI 或 Amazon 软件开发工具包时,必须将此 JSON 对象序列化为字符串。

您可以合并组件默认配置中不存在的键值对。您也可以合并类型与具有相同键的值不同的键值对。新值将替换旧值。这意味着您可以更改配置对象的结构。

您可以合并空值和空字符串、列表和对象。

注意

不能将合并更新用于在列表中插入元素或将元素追加到列表中。您可以替换整个列表,也可以定义一个对象,其中每个元素都有一个唯一的键。

Amazon IoT Greengrass 使用 JSON 作为配置值。JSON 指定了数字类型,但不区分整数和浮点数。因此,配置值可能会转换为浮点数。 Amazon IoT Greengrass为确保您的组件使用正确的数据类型,我们建议您将数字配置值定义为字符串。然后,让你的组件将它们解析为整数或浮点数。这样可以确保您的配置值在配置和核心设备上具有相同的类型。

在合并更新中使用配方变量

此功能适用于 Greengrass nucleus 组件的 2.6.0 及更高版本。

如果您将 Greengrass n interpolateComponentConfigurationucleus 的配置选项设置为,则可以在合并更新中使用配方变量true以外的配component_dependency_name:configuration:json_pointer方变量。例如,您可以在合并更新中使用{iot:thingName}配方变量,将核心设备 Amazon IoT 的事物名称包含在组件配置值(例如进程间通信 (IPC) 授权策略)中。

示例

以下示例演示了具有以下默认配置的仪表板组件的配置更新。此示例组件显示有关工业设备的信息。

{ "name": null, "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 80, "https": 443 }, }, "tags": [] }
JSON
{ "RecipeFormatVersion": "2020-01-25", "ComponentName": "com.example.IndustrialDashboard", "ComponentVersion": "1.0.0", "ComponentDescription": "Displays information about industrial equipment.", "ComponentPublisher": "Amazon", "ComponentConfiguration": { "DefaultConfiguration": { "name": null, "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 80, "https": 443 }, }, "tags": [] } }, "Manifests": [ { "Platform": { "os": "linux" }, "Lifecycle": { "run": "python3 -u {artifacts:path}/industrial_dashboard.py" } }, { "Platform": { "os": "windows" }, "Lifecycle": { "run": "py -3 -u {artifacts:path}/industrial_dashboard.py" } } ] }
YAML
--- RecipeFormatVersion: '2020-01-25' ComponentName: com.example.IndustrialDashboard ComponentVersion: '1.0.0' ComponentDescription: Displays information about industrial equipment. ComponentPublisher: Amazon ComponentConfiguration: DefaultConfiguration: name: null mode: REQUEST network: useHttps: true port: http: 80 https: 443 tags: [] Manifests: - Platform: os: linux Lifecycle: run: | python3 -u {artifacts:path}/industrial_dashboard.py - Platform: os: windows Lifecycle: run: | py -3 -u {artifacts:path}/industrial_dashboard.py
例 示例 1:合并更新

您可以创建应用以下配置更新的部署,该更新指定合并更新,但不指定重置更新。此配置更新告诉组件在 HTTP 端口 8080 上显示仪表板,其中包含来自两个锅炉的数据。

Console
要合并的配置
{ "name": "Factory 2A", "network": { "useHttps": false, "port": { "http": 8080 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] }
Amazon CLI

以下命令创建对核心设备的部署。

aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment.json

dashboard-deployment.json文件包含以下 JSON 文档。

{ "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore", "deploymentName": "Deployment for MyGreengrassCore", "components": { "com.example.IndustrialDashboard": { "componentVersion": "1.0.0", "configurationUpdate": { "merge": "{\"name\":\"Factory 2A\",\"network\":{\"useHttps\":false,\"port\":{\"http\":8080}},\"tags\":[\"/boiler/1/temperature\",\"/boiler/1/pressure\",\"/boiler/2/temperature\",\"/boiler/2/pressure\"]}" } } } }
Greengrass CLI

以下 Greengrass CLI 命令在核心设备上创建本地部署。

sudo greengrass-cli deployment create \ --recipeDir recipes \ --artifactDir artifacts \ --merge "com.example.IndustrialDashboard=1.0.0" \ --update-config dashboard-configuration.json

dashboard-configuration.json文件包含以下 JSON 文档。

{ "com.example.IndustrialDashboard": { "MERGE": { "name": "Factory 2A", "network": { "useHttps": false, "port": { "http": 8080 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] } } }

此更新后,仪表板组件具有以下配置。

{ "name": "Factory 2A", "mode": "REQUEST", "network": { "useHttps": false, "port": { "http": 8080, "https": 443 } }, "tags": [ "/boiler/1/temperature", "/boiler/1/pressure", "/boiler/2/temperature", "/boiler/2/pressure" ] }
例 示例 2:重置和合并更新

然后,您创建一个应用以下配置更新的部署,该更新指定了重置更新和合并更新。这些更新指定在默认的 HTTPS 端口上显示仪表板,其中包含来自不同锅炉的数据。这些更新修改了上一个示例中配置更新所产生的配置。

Console
重置路径
[ "/network/useHttps", "/tags" ]
要合并的配置
{ "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure" ] }
Amazon CLI

以下命令创建对核心设备的部署。

aws greengrassv2 create-deployment --cli-input-json file://dashboard-deployment2.json

dashboard-deployment2.json文件包含以下 JSON 文档。

{ "targetArn": "arn:aws:iot:us-west-2:123456789012:thing/MyGreengrassCore", "deploymentName": "Deployment for MyGreengrassCore", "components": { "com.example.IndustrialDashboard": { "componentVersion": "1.0.0", "configurationUpdate": { "reset": [ "/network/useHttps", "/tags" ], "merge": "{\"tags\":[\"/boiler/3/temperature\",\"/boiler/3/pressure\",\"/boiler/4/temperature\",\"/boiler/4/pressure\"]}" } } } }
Greengrass CLI

以下 Greengrass CLI 命令在核心设备上创建本地部署。

sudo greengrass-cli deployment create \ --recipeDir recipes \ --artifactDir artifacts \ --merge "com.example.IndustrialDashboard=1.0.0" \ --update-config dashboard-configuration2.json

dashboard-configuration2.json文件包含以下 JSON 文档。

{ "com.example.IndustrialDashboard": { "RESET": [ "/network/useHttps", "/tags" ], "MERGE": { "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure" ] } } }

此更新后,仪表板组件具有以下配置。

{ "name": "Factory 2A", "mode": "REQUEST", "network": { "useHttps": true, "port": { "http": 8080, "https": 443 } }, "tags": [ "/boiler/3/temperature", "/boiler/3/pressure", "/boiler/4/temperature", "/boiler/4/pressure", ] }