amazeeio/algm_drutiny_plugin

此包已被废弃,不再维护。未建议替代包。

ALGM 核心插件库,包含我们用于运行 Drutiny 审计的策略和配置文件

v1.1.7 2020-11-11 17:38 UTC

README

此插件提供了一组标准策略集合,可以被 Drutiny 使用。

设置

  1. git clone git@github.com:AmazeeLabs/algm_drutiny.git

  2. composer install -o

  3. 需要 Drush - https://docs.drush.org/en/9.x/install/

We also need Drush locally (preferably Drush 9+). There are also a couple of things you need to ensure your drush alias files have in order to get this working with Drutiny which we will cover below.

If you have drush but need to update to Drush 9 way of things, then this is your friend:
https://stackoverflow.com/questions/55587919/where-drush-9-aliases-file-should-be-located-in-drupal-8
  1. 测试 Drutiny 是否运行 - ./vendor/bin/drutiny

这是什么?

此存储库是 Drutiny 的插件,意味着它作为扩展添加到核心 Drutiny 上。然后我们构建、发布并作为单个 phar 文件运行。因此,此插件为我们提供了一种添加自己的策略、配置文件、格式化程序以及任何我们喜欢的东西以扩展现有 Drutiny 功能的方法。

文档解释得很好 - https://drutiny.readthedocs.io/en/2.x/README/

工作流程

要添加策略/配置文件或以任何方式扩展我们的 Drutiny 插件,我们需要做几件事

  1. 确保本地有最新版本(获取并拉取以确保)
  2. 添加所需内容,提交并推送至 PR 或 master(取决于是否需要测试)
  3. 测试可以通过此存储库中的 drupal-web docker 实例进行,或者通过使用远程 drush 别名并直接运行它们对这些站点进行。当然,如果您不确定预期的结果,请谨慎在生产站点上运行东西。
  4. 一旦推送到 master,并且对测试/工作情况满意后,我们需要创建一个新的标签。
  5. 从远程获取所有标签,并检查列表,使用 git tag v1.0.x 等. 添加一个带有 -a 标志的注释。然后使用 git push origin --tags 推送到我们的 origin。
  6. 这将触发我们拥有的 phar 构建器 github action。您可以在以下位置看到此构建 - https://github.com/AmazeeLabs/algm_drutiny/actions
  7. 将构建新版本 - 检查构建输出以查看您的策略是否存在。您还可以在本地下载 phar 并在本地站点上运行它以检查其是否按预期工作。
  8. 然后我们将使用最新的发布版在我们的 ansible/awx playbooks 中!

使用 Drutiny

Drutiny 有两个核心命令,我们运行:policy:audit 和 profile:run。

policy:audit

此命令对目标运行策略(通常是 drush 目标),将执行我们想要运行的检查。

基本上,我们需要提供一个策略(例如 algm:ModuleUpdates)和一个目标(例如 drush 别名 @site-prod.site-name)。我们还可以传递选项,如 --format,它定义了输出格式。参数/默认值也可以通过 -p 标志传递到策略中,例如 -p module=8.6.8

policy:audit [options] [--] <policy> <target>

最终命令可能如下所示:./vendor/bin/drutiny policy:audit algm:ModuleUpdates @site-prod.site-name --format=markdown

profile:run

此操作将对目标进行配置运行,将遍历并检查整个策略套件。

对于配置文件,我们需要提供配置文件名称(例如 algm_sla_site)和目标(例如 drush 别名 @site-prod.site-name)。我们还可以传递一些选项,例如 '--format',它定义了输出格式。

profile:run [选项] [--] <配置文件> <目标>

最终的命令可能看起来像这样:./vendor/bin/drutiny profile:run algm_sla_site @site-prod.site-name --format==markdown

添加新策略

要添加新策略,我们需要两个文件:一个新策略 yaml 文件添加在这里(https://github.com/AmazeeLabs/algm_drutiny/tree/master/Policies)以及一个新审计类文件添加在这里(https://github.com/AmazeeLabs/algm_drutiny/tree/master/src/Audit

策略详细信息

最重要的是,我们有 $sandbox 对象,它是执行我们的策略的运行时对象。

exec

exec 是一个方法,可以访问远程 shell 并执行给定的命令 - 例如 $output = $sandbox->exec('ls -la');

drush

可以使用 drush 方法运行 Drush。Drutiny 在这里支持驼峰命名 - 例如 $list = $sandbox->drush(['format' => 'json'])->pmList(); 如果提供 json 格式,Drutiny 将解析响应并以 PHP 的形式返回输出。

Drush 别名

一个 Drush 9 别名文件示例

prod:
  host: ssh.lagoon.amazeeio.cloud
  root: /app/web
  user: site-prod
  remote-host: ssh.lagoon.amazeeio.cloud
  remote-user: site-prod
  ssh-options: '-o LogLevel=ERROR -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -p 32222'

请注意,这只是一个示例,您应该仅使用这些值更新您的 drush 别名以使其与 Drutiny 一起工作,而不是完全替换它。

此存储库中还有一个 example.drush.alias.yml

更改或添加新站点别名时,不要忘记清除 Drush 缓存 - drush cc

有关更多配置选项 - 这很有用 https://github.com/drush-ops/drush/blob/master/examples/example.site.yml

有用的事情

检查我们可用的策略列表:./vendor/bin/drutiny policy:list

检查配置文件列表:./vendor/bin/drutiny profile:list

可能需要清除 Drutiny 缓存:./vendor/bin/drutiny cache:clear

开发和测试

在包内部有一个 Drupal 安装,您可以在这里测试您的策略。请遵循 drupal-web/README.md 文件