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

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

组件

使用 Amazon IoT Greengrass 开发套件component命令行界面 (GDK CLI) 中的命令创建、构建和发布自定义 Greengrass 组件。

init

从组件模板或社区组件初始化 Greengrass 组件文件夹。

GDK CLI 从 Greengrass 软件目录中检索社区组件,并从组件模板存储库中检索组件模板。Amazon IoT Greengrass GitHub

注意

如果您使用的是 GDK CLI v1.0.0,则必须在空文件夹中运行此命令。GDK CLI 会将模板或社区组件下载到当前文件夹。

如果您使用的是 GDK CLI v1.1.0 或更高版本,则可以指定 --name 参数来指定供 GDK CLI 下载模板或社区组件的文件夹。如果使用此参数,请指定一个不存在的文件夹。GDK CLI 会为您创建文件夹。如果您不指定此参数,GDK CLI 将使用当前文件夹,此文件夹必须为空。

如果组件使用 zip 构建系统,则 GDK CLI 会将组件文件夹中的某些文件压缩到与组件文件夹同名的 zip 文件中。例如,如果组件文件夹的名称为 HelloWorld,则 GDK CLI 会创建名为 HelloWorld.zip 的 zip 文件。在组件配方中,zip 构件名称必须与组件文件夹的名称相匹配。如果在 Windows 设备上使用 GDK CLI 版本 1.0.0,则组件文件夹名称和 ZIP 文件名称必须仅包含小写字母。

如果将使用 zip 构建系统的模板或社区组件初始化为与模板或组件名称不同的文件夹,则必须更改组件配方中的 zip 构件名称。更新 ArtifactsLifecycle 定义,使 zip 文件名与组件文件夹的名称相匹配。以下示例突出显示了 ArtifactsLifecycle 定义中的 zip 文件名。

JSON
{ ... "Manifests": [ { "Platform": { "os": "all" }, "Artifacts": [ { "URI": "s3://BUCKET_NAME/COMPONENT_NAME/COMPONENT_VERSION/HelloWorld.zip", "Unarchive": "ZIP" } ], "Lifecycle": { "Run": "python3 -u {artifacts:decompressedPath}/HelloWorld/main.py {configuration:/Message}" } } ] }
YAML
--- ... Manifests: - Platform: os: all Artifacts: - URI: "s3://BUCKET_NAME/COMPONENT_NAME/COMPONENT_VERSION/HelloWorld.zip" Unarchive: ZIP Lifecycle: Run: "python3 -u {artifacts:decompressedPath}/HelloWorld/main.py {configuration:/Message}"
摘要
$ gdk component init [--language] [--template] [--repository] [--name]
参数(从组件模板初始化)
  • -l--language – 用于您指定的模板的编程语言。

    您必须指定 --repository--language--template

  • -t--template – 用于本地组件项目的组件模板。要查看可用模板,请使用 list 命令。

    您必须指定 --repository--language--template

  • -n--name –(可选)供 GDK CLI 初始化组件的本地文件夹的名称。指定不存在的文件夹。GDK CLI 会为您创建文件夹。

    此功能适用于 GDK CLI v1.1.0 及更高版本。

参数(从社区组件初始化)
  • -r--repository – 要签出到本地文件夹中的社区组件。要查看可用的社区组件,请使用 list 命令。

    您必须指定 --repository--language--template

  • -n--name –(可选)供 GDK CLI 初始化组件的本地文件夹的名称。指定不存在的文件夹。GDK CLI 会为您创建文件夹。

    此功能适用于 GDK CLI v1.1.0 及更高版本。

输出

以下示例显示了运行此命令从 Python Hello World 模板初始化组件文件夹时生成的输出。

$ gdk component init -l python -t HelloWorld [2021-11-29 12:51:40] INFO - Initializing the project directory with a python component template - 'HelloWorld'. [2021-11-29 12:51:40] INFO - Fetching the component template 'HelloWorld-python' from Greengrass Software Catalog.

以下示例显示了运行此命令从社区组件初始化组件文件夹时生成的输出。

$ gdk component init -r aws-greengrass-labs-database-influxdb [2022-01-24 15:44:33] INFO - Initializing the project directory with a component from repository catalog - 'aws-greengrass-labs-database-influxdb'. [2022-01-24 15:44:33] INFO - Fetching the component repository 'aws-greengrass-labs-database-influxdb' from Greengrass Software Catalog.

build

将组件的源代码构建成可以发布到 Amazon IoT Greengrass 服务的配方和构件。GDK CLI 运行您在 GDK CLI 配置文件 gdk-config.json 中指定的构建系统。您必须在 gdk-config.json 文件所在的同一文件夹中运行此命令。

运行此命令时,GDK CLI 会在组件文件夹的 greengrass-build 文件夹中创建配方和构件。GDK CLI 将配方保存在 greengrass-build/recipes 文件夹中,并将构件保存在 greengrass-build/artifacts/componentName/componentVersion 文件夹中。

如果您使用的是 GDK CLI v1.1.0 或更高版本,则组件配方可以指定存在于 S3 存储桶中但不存在于本地组件构建文件夹中的构件。在开发具有大型构件的组件(例如机器学习模型)时,可以使用此功能来减少带宽使用量。

构建组件后,您可以执行以下操作之一,在 Greengrass 核心设备上对组件进行测试:

  • 如果您在与运行 C Amazon IoT Greengrass ore 软件的设备不同的设备上进行开发,则必须发布该组件才能将其部署到 Greengrass 核心设备上。将组件发布到 Amazon IoT Greengrass 服务,然后将其部署到 Greengrass 核心设备。有关更多信息,请参阅 publish 命令和创建部署

  • 如果您在运行 Amazon IoT Greengrass Core 软件的同一台设备上进行开发,则可以将组件发布到 Amazon IoT Greengrass 服务进行部署,也可以创建本地部署来安装和运行该组件。要创建本地部署,请使用 Greengrass CLI。有关更多信息,请参阅Greengrass 命令行界面使用本地部署测试 Amazon IoT Greengrass 组件。创建本地部署时,请将 greengrass-build/recipes 指定为配方文件夹,将 greengrass-build/artifacts 指定为构件文件夹。

摘要
$ gdk component build
Arguments (参数)

输出

下面的示例显示运行此命令时产生的输出。

$ gdk component build [2021-11-29 13:18:49] INFO - Getting project configuration from gdk-config.json [2021-11-29 13:18:49] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2021-11-29 13:18:49] INFO - Building the component 'com.example.PythonHelloWorld' with the given project configuration. [2021-11-29 13:18:49] INFO - Using 'zip' build system to build the component. [2021-11-29 13:18:49] WARNING - This component is identified as using 'zip' build system. If this is incorrect, please exit and specify custom build command in the 'gdk-config.json'. [2021-11-29 13:18:49] INFO - Zipping source code files of the component. [2021-11-29 13:18:49] INFO - Copying over the build artifacts to the greengrass component artifacts build folder. [2021-11-29 13:18:49] INFO - Updating artifact URIs in the recipe. [2021-11-29 13:18:49] INFO - Creating component recipe in 'C:\Users\MyUser\Documents\greengrass-components\python\HelloWorld\greengrass-build\recipes'.

发布

将此组件发布到 Amazon IoT Greengrass 服务。此命令将构建构件上传到 S3 存储桶,更新配方中的构件 URI,并根据配方创建新版本的组件。GDK CLI 使用您在 GDK CLI 配置文件中指定的 S3 存储桶和 Amazon 区域。gdk-config.json您必须在 gdk-config.json 文件所在的同一文件夹中运行此命令。

如果您使用的是 GDK CLI v1.1.0 或更高版本,则可以指定 --bucket 参数来指定供 GDK CLI 上传组件构件的 S3 存储桶。如果您不指定此参数,GDK CLI 会上传到名称为bucket-region-accountId、其中bucket和是您在中指定的值gdk-config.json,并且region是您的 ID accountId 的 S3 存储桶。 Amazon Web Services 账户 如果存储桶不存在,GDK CLI 将创建该存储桶。

如果您使用 GDK CLI v1.2.0 或更高版本,则可以使用参数覆盖 GDK CLI 配置文件中 Amazon Web Services 区域 指定的内容。--region您还可以使用 --options 参数指定其他选项。有关可用选项列表,请参阅 Greengrass 开发工具包 CLI 配置文件

运行此命令时,GDK CLI 会使用您在配方中指定的版本发布组件。如果指定 NEXT_PATCH,GDK CLI 将使用尚不存在的下一个补丁版本。语义版本使用 major.minor.patch 编号系统。有关更多信息,请参阅语义版本规范

注意

如果您使用的是 GDK CLI v1.1.0 或更高版本,则在运行此命令时,GDK CLI 会检查是否已构建组件。如果未构建组件,GDK CLI 将在发布组件之前构建组件

摘要
$ gdk component publish [--bucket] [--region] [--options]
Arguments (参数)
  • -b--bucket –(可选)指定 GDK CLI 在其中发布组件构件的 S3 存储桶的名称。

    如果您不指定此参数,GDK CLI 会上传到名称为bucket-region-accountId、其中bucket和是您在中指定的值gdk-config.json,并且region是您的 ID accountId 的 S3 存储桶。 Amazon Web Services 账户 如果存储桶不存在,GDK CLI 将创建该存储桶。

    如果存储桶不存在,GDK CLI 将创建该存储桶。

    此功能适用于 GDK CLI v1.1.0 及更高版本。

  • -r--region—(可选)在创建组件时 Amazon Web Services 区域 指定 to 的名称。此参数覆盖 GDK CLI 配置中的区域名称。

    此功能适用于 GDK CLI v1.2.0 及更高版本。

  • -o--options(可选)指定用于发布组件的选项列表。参数必须是有效的 JSON 字符串或包含发布选项的 JSON 文件的文件路径。此参数覆盖 GDK CLI 配置中的选项。

    此功能适用于 GDK CLI v1.2.0 及更高版本。

输出

下面的示例显示运行此命令时产生的输出。

$ gdk component publish [2021-11-29 13:45:29] INFO - Getting project configuration from gdk-config.json [2021-11-29 13:45:29] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2021-11-29 13:45:29] INFO - Found credentials in shared credentials file: ~/.aws/credentials [2021-11-29 13:45:30] INFO - Publishing the component 'com.example.PythonHelloWorld' with the given project configuration. [2021-11-29 13:45:30] INFO - No private version of the component 'com.example.PythonHelloWorld' exist in the account. Using '1.0.0' as the next version to create. [2021-11-29 13:45:30] INFO - Uploading the component built artifacts to s3 bucket. [2021-11-29 13:45:30] INFO - Uploading component artifacts to S3 bucket: {bucket}. If this is your first time using this bucket, add the 's3:GetObject' permission to each core device's token exchange role to allow it to download the component artifacts. For more information, see https://docs.aws.amazon.com/greengrass/v2/developerguide/device-service-role.html. [2021-11-29 13:45:30] INFO - Not creating an artifacts bucket as it already exists. [2021-11-29 13:45:30] INFO - Updating the component recipe com.example.PythonHelloWorld-1.0.0. [2021-11-29 13:45:30] INFO - Creating a new greengrass component com.example.PythonHelloWorld-1.0.0 [2021-11-29 13:45:30] INFO - Created private version '1.0.0' of the component in the account.'com.example.PythonHelloWorld'.

列表

检索可用组件模板和社区组件的列表。

GDK CLI 从 Greengrass 软件目录中检索社区组件,并从组件模板存储库中检索组件模板。Amazon IoT Greengrass GitHub

您可以将此命令的输出传递给 init 命令,以便从模板和社区组件初始化组件存储库。

摘要
$ gdk component list [--template] [--repository]
Arguments (参数)
  • -t--template –(可选)指定此参数以列出可用的组件模板。此命令以 name-language 格式输出每个模板的名称和语言。例如,在 HelloWorld-python 中,模板名称为 HelloWorld,语言为 python

  • -r--repository –(可选)指定此参数以列出可用的社区组件存储库。

输出

下面的示例显示运行此命令时产生的输出。

$ gdk component list --template [2021-11-29 12:29:04] INFO - Listing all the available component templates from Greengrass Software Catalog. [2021-11-29 12:29:04] INFO - Found '2' component templates to display. 1. HelloWorld-python 2. HelloWorld-java