本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
组件
使用component
中的命令Amazon IoT Greengrass用于创建、构建和发布自定义 Greengrass 组件的开发工具包命令行界面 (GDK CLI)。
init
从组件模板或社区组件初始化 Greengrass 组件文件夹。
GDK CLI 从Greengrass 软件目录和组件模板来自Amazon IoT GreengrassGitHub 上的组件模板仓库
如果使用 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 项目名称必须与组件文件夹的名称匹配。如果在 Windows 设备上使用 GDK CLI 1.0.0 版,组件文件夹和 zip 文件名必须仅包含小写字母。
如果将使用 zip 构建系统的模板或社区组件初始化到名称与模板或组件不同的文件夹,则必须在组件配方中更改 zip 项目名称。更新Artifacts
和Lifecycle
定义,使 zip 文件名称与组件文件夹的名称匹配。下面的示例突出显示了中的 zip 文件名称。Artifacts
和Lifecycle
定义。
- 摘要
-
$
gdk component init [--language] [--template] [--repository] [--name] - 参数(从组件模板初始化)
-
-
-l
、--language
— 用于指定的模板的编程语言。您必须指定
--repository
要么--language
和--template
. -
-t
、--template
— 用于本地组件项目的组件模板。要查看可用的模板,请使用列表命令。您必须指定
--repository
要么--language
和--template
. -
-n
、--name
—(可选)GDK CLI 初始化组件的本地文件夹的名称。指定不存在的文件夹。GDK CLI 会为您创建此文件夹。此功能适用于 GDK CLI v1.1.0 及更高版本。
-
- 参数(从社区组件初始化)
-
-
-r
、--repository
— 要签出到本地文件夹的社区组件。要查看可用的社区组件,请使用列表命令。您必须指定
--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/
folder。componentName
/componentVersion
如果您使用 GDK CLI v1.1.0 或更高版本,组件配方可以指定 S3 存储桶中但不存在于本地组件构建文件夹中的工件。在开发具有大型伪影的组件(例如机器学习模型)时,可以使用此功能来减少带宽使用。
构建组件后,您可以执行以下操作之一,在 Greengrass 核心设备上测试组件:
-
如果你在不同于运行的设备上开发Amazon IoT Greengrass核心软件,您必须发布组件才能将其部署到 Greengrass 核心设备。将组件发布到Amazon IoT Greengrass并将其部署到 Greengrass 核心设备。有关更多信息,请参阅 。发布命令和创建部署.
-
如果你在运行Amazon IoT Greengrass核心软件,你可以将组件发布到Amazon IoT Greengrass服务进行部署,也可以创建本地部署来安装和运行组件。要创建本地部署,请使用 Greengrass CLI。有关更多信息,请参阅 Greengrass Command Line Interface 和 测试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'.
publish
将此组件发布到Amazon IoT Greengrass服务。此命令将构建工件上传到 S3 存储桶,更新配方中的工件 URI,并根据配方创建新版本的组件。GDK CLI 使用 S3 存储桶并Amazon您在中指定的区域GDK CLI 配置文件、gdk-config.json
. 您必须在其中的同一文件夹中运行此命令。gdk-config.json
文件存在。
如果您使用 GDK CLI v1.1.0 或更高版本,则可以指定--bucket
参数,以指定 GDK CLI 在其中上传组件的工件的 S3 存储桶。如果您不指定此参数,GDK CLI 会上传到名称为的 S3 存储桶。
,其中,bucketPrefix
-region
-accountId
BucketPrefix
和领域
是你在中指定的值gdk-config.json
, 和accountId
是您的Amazon Web Services 账户ID。如果存储桶不存在,GDK CLI 会创建存储桶。
运行此命令时,GDK CLI 将使用您在配方中指定的版本发布组件。如果你指定NEXT_PATCH
,GDK CLI 使用尚未存在的下一个修补程序版本。语义版本使用重大的.未成年人.补丁编号系统。有关更多信息,请参阅 。语义版本规范
如果您使用 GDK CLI v1.1.0 或更高版本,则在运行此命令时,GDK CLI 会检查是否已构建组件。如果没有构建组件,那么 GDK CLI构建组件在发布组件之前。
- 摘要
-
$
gdk component publish [--bucket] - Arguments (参数)
-
-
-b
、--bucket
—(可选)指定 GDK CLI 在其中发布组件项目的 S3 存储桶的名称。如果您不指定此参数,GDK CLI 会上传到名称为的 S3 存储桶。
,其中,bucketPrefix
-region
-accountId
BucketPrefix
和领域
是你在中指定的值gdk-config.json
, 和accountId
是您的Amazon Web Services 账户ID。如果存储桶不存在,GDK CLI 会创建存储桶。
此功能适用于 GDK CLI v1.1.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 GreengrassGitHub 上的组件模板仓库
你可以将此命令的输出传递给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