1

我正在尝试使用 scepter !cmd钩子(在创建/更新之前)设置环境变量,然后可以通过sceptre_user_data!environment_variable解析器在同一个配置文件中进行解析。

该命令在从 shell 执行时运行良好,但由于某种原因,!cmd 挂钩在运行时不执行分配

不知道,如果我在这里遗漏了什么

hooks:
  before_create:
    - !cmd "export OBJECT_VERSION=$(aws s3api put-object --body file --bucket bucket-name --key path --query 'VersionId')"

sceptre_user_data:
  ObjectVersion: !environment_variable OBJECT_VERSION

4

1 回答 1

1

不幸的是,您目前需要一个自定义解析器

sceptre_plugins_cmd_extras.py

import os
import subprocess
from sceptre.resolvers import Resolver


class CmdResolver(Resolver):
    """
    Resolver for running shell commands and returning the value
    :param argument: Name of the environment variable to return.
    :type argument: str
    """

    def resolve(self):
        """
        Runs a command and returns the output as a string
        :returns: Value of the environment variable.
        :rtype: str
        """
        return subprocess.check_output(self.argument, shell=True)

安装程序.py

from setuptools import setup

setup(
    name='sceptre-plugins-cmd-extras',
    description="Extra Sceptre Plguins (Hook & Resolver) for subprocess",
    keywords="sceptre,cloudformation",
    version='0.0.1',
    author="Cloudreach",
    author_email="juan.canham@cloudreach.com",
    license='Apache2',
    url="https://github.com/cloudreach/sceptre",
    entry_points={
        'sceptre.resolvers': [
            'cmd = sceptre_plugins_cmd_extras:CmdResolver',
        ],
    },
    py_modules=['sceptre_plugins_cmd_extras'],
    install_requires=["sceptre"],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators"
        "Natural Language :: English",
        "Environment :: Plugins",
        "Programming Language :: Python :: 2.6",
        "Programming Language :: Python :: 2.7",
        "Programming Language :: Python :: 3.5",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7"
    ]
)

但是你需要使你的命令幂等(类似)

sceptre_user_data:
  ObjectVersion: !cmd aws s3api get-object --bucket bucket-name --key path --query 'VersionId' || aws s3api put-object --body file --bucket bucket-name --key path --query 'VersionId'
于 2019-06-03T10:55:53.427 回答