首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Packer EC2_HOME:没有设置EC2_AMITOOL_HOME或EC2_HOME环境变量

Packer EC2_HOME:没有设置EC2_AMITOOL_HOME或EC2_HOME环境变量
EN

Stack Overflow用户
提问于 2014-03-05 00:56:39
回答 1查看 1.3K关注 0票数 3

我使用packer构建Amazon,我遇到了这样的问题:

  1. base (ami-a4bdd194)没有ec2-ami-tools安装,所以我编写了一个小脚本来使用ec2-ami-tools属性来安装它。
  2. 它已成功安装到/usr/local/bin上,但在构建AMI时仍会出现以下错误。

任何帮助都是非常感谢的!谢谢!

//控制台错误日志

代码语言:javascript
复制
    amazon-instance: /usr/local/bin/ec2-bundle-vol: line 3: EC2_HOME: Neither of EC2_AMITOOL_HOME or EC2_HOME environment variables are set
    amazon-instance:
    amazon-instance:
    amazon-instance:
    amazon-instance:
    amazon-instance:
==> amazon-instance: Volume bundling failed. Please see the output above for more
==> amazon-instance: details on what went wrong.

// packer.json

代码语言:javascript
复制
{
  "variables": {
    "access_key": "",
    "secret_key": "",
    "x509_cert_path": "",
    "x509_key_path": ""
  },
  "builders": [
    {
      "access_key": "{{user `access_key`}}",
      "secret_key": "{{user `secret_key`}}",
      "x509_cert_path": "{{user `x509_cert_path`}}",
      "x509_key_path": "{{user `x509_key_path`}}",
      "account_id": "123456789012",
      "ami_name": "my-packer-example {{timestamp}}",
      "instance_type": "i2.xlarge",
      "region": "us-west-2",
      "s3_bucket": "my_bucket/my_folder",
      "source_ami": "ami-a4bdd194",
      "ssh_username": "ubuntu",
      "type": "amazon-instance"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "scripts": [
        "../tools/install-ec2-ami-tools.bash"
      ]
    }
  ]
}

// install-ec2-ami-tools.bash

代码语言:javascript
复制
#!/bin/bash

function trimString()
{
    echo "${1}" | sed -e 's/^ *//g' -e 's/ *$//g'
}

function isEmptyString()
{
    if [[ "$(trimString ${1})" = '' ]]
    then
        echo 'true'
    else
        echo 'false'
    fi
}

function installEC2AMITools()
{
    if [[ "$(isEmptyString ${EC2_HOME})" = 'true' || "$(which 'ec2-bundle-vol')" = '' ]]
    then
        sleep 10 &&
        sudo apt-get update &&
        sudo apt-get install -y 'unzip' 'wget' &&
        rm -rf 'ec2-ami-tools' &&
        rm -f 'ec2-ami-tools.zip' &&
        wget -q 'http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.zip' &&
        unzip -q 'ec2-ami-tools.zip' &&
        mv ec2-ami-tools-* 'ec2-ami-tools' &&
        sudo rsync -a --no-o --no-g 'ec2-ami-tools/' '/usr/local' &&
        rm -rf 'ec2-ami-tools' &&
        rm -f 'ec2-ami-tools.zip'
    else
        echo -e "\033[1;32mec2-ami-tools has already been installed!\033[0m"
    fi
}

function main()
{
    installEC2AMITools
}

main
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-05 17:16:18

下面是一个可以工作的配置文件的示例。诀窍是重写实际命令,以包括所需的路径:

代码语言:javascript
复制
{
  "variables": {
    "x509_cert_path": "/etc/aws/aws.cert",
    "x509_key_path": "/etc/aws/aws.key",
    "environment": "staging"
  },

  "builders": [
    {
      "type": "amazon-instance",
      "name": "chef-server-{{user `environment`}}",
      "region": "eu-west-1",
      "availability_zone": "eu-west-1a",
      "source_ami": "ami-46ba5331",
      "instance_type": "m1.medium",
      "ssh_username": "ubuntu",
      "ami_name": "chef-server-{{user `environment`}} {{isotime | clean_ami_name}}",
      "s3_bucket": "provisioning-images",
      "ami_description": "Chef Server instance",
      "account_id": "xxxx-yyyy-zzzz",
      "x509_cert_path": "{{user `x509_cert_path`}}",
      "x509_key_path": "{{user `x509_key_path`}}",
      "bundle_vol_command": "sudo -n EC2_HOME=/usr/local ec2-bundle-vol --no-filter -k {{.KeyPath}} -u {{.AccountId}} -c {{.CertPath}} -r {{.Architecture}} -e {{.PrivatePath}}/* -d {{.Destination}} -p {{.Prefix}} --batch",
      "bundle_upload_command": "sudo -n EC2_HOME=/usr/local ec2-upload-bundle -b {{.BucketName}} -m {{.ManifestPath}} -a {{.AccessKey}} -s {{.SecretKey}} -d {{.BundleDirectory}} --batch --location EU --retry"
    }
  ],

  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 3 && sudo apt-get install zip -y > /dev/null",
        "wget --quiet http://s3.amazonaws.com/ec2-downloads/ec2-ami-tools.zip",
        "unzip -qq ec2-ami-tools.zip",
        "sudo -E rsync -a --no-o --no-g ec2-ami-tools-*/ /usr/local/",
        "sudo apt-get install ruby -y >/dev/null"
      ]
    },

    {
      "type": "chef-solo",
      "execute_command": "{{if .Sudo}}sudo {{end}}chef-solo -E {{user `environment`}} --no-color -c {{.ConfigPath}} -j {{.JsonPath}}",
      "cookbook_paths": ["{{pwd}}/lib/cookbooks"],
      "data_bags_path": "{{pwd}}/../../data_bags",
      "encrypted_data_bag_secret_path": "/etc/chef/databags.pem",
      "environments_path": "{{pwd}}/environments",
      "run_list": ["recipe[chef::server]"],
      "json": {
        "openssh": {
          "server": {
            "allow_users": "deployer ubuntu",
            "use_p_a_m": "yes"
          }
        },
        "chef_client": {
          "config": {
            "environment": "{{user `environment`}}"
          }
        }
      }
    }
  ]
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22186390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档