如何部署包含脚本的 OpenStack Heat 模板

How to deploy an OpenStack heat template that includes a script

OpenStack 的编排引擎'Heat' 可以部署计算资源和配置软件,称为 HOT 模板。 github 这里有很多例子: https://github.com/openstack/heat-templates/tree/master/hot

heat 模板是用 YAML 编写的,我们可以使用这种语法部署模板

heat stack-create my_first_stack -f heat_1a.yaml

您也可以将模板文件直接上传到 OpenStack 仪表板。 然而,这是我的问题,许多模板还将包括 shell 脚本的 powershell 脚本,这些脚本在部署后 运行 - 我们如何将这些脚本上传到 OpenStack 以包含在堆栈?

例如,这里是 MicroSoft SQL 服务器模板的目录列表

C:\heat-templates\hot\Windows\MSSQLServer>ls
MSSQL.ps1  MSSQL.psm1  MSSQL.yaml  Tests  heat-powershell-utils.psm1

Heat 客户端只会将 YAML 文件作为参数,那么我们如何处理脚本?

谢谢, 抢

参考 heat 的模板指南: http://docs.openstack.org/developer/heat/template_guide/software_deployment.html

本质上,在 yaml 模板文件中定义的资源可以使用 "get_file" 指令从指定的文件名中读取字符串。因此,当您调用热客户端 MSSQL.yaml 时,您的热客户端将解析它,并且在它看到 "get_file" 的任何地方以文件名作为参数,然后从该文件中读取。

使用上述 "get_file" 的示例 link:

...
the_server:
  type: OS::Nova::Server
  properties:
    # flavor, image etc
    user_data:
      str_replace:
        template: {get_file: the_server_boot.sh}
        params:
          $FOO: {get_param: foo}

有时,我们需要根据HEAT模板中提供的参数创建脚本,并在创建Stack后执行。

对于这种需求,我们可以使用下面提到的模式。一旦 VM 启动并处于 cloud-init 阶段,这将创建并执行脚本。

services-cloud-init:
    type: OS::Heat::CloudConfig
    properties:
        cloud_config:
            timezone: {get_param: time_zone}
            write_files:
              - path: /tmp/change_password.sh
                owner: root:root
                permissions: '0777'
                content: |
                          #!/bin/bash
                          echo -e "pwd\npwd" | passwd cloud-user

              - path: /run/change_timezone.sh
                owner: root:root
                permissions: '0777'
                content: |
                          #!/bin/bash
                          ln -sf /usr/share/zoneinfo/timezone /etc/localtime

            runcmd:
              - echo "Executing change_timezone"
              - /tmp/change_timezone.sh
              - echo "Executing change_password"
              - /tmp/change_password.sh
              - reboot
            bootcmd:
              - echo "Boot Completed"