3

I've been playing with packer lately and found it an amazing tool. But I'm failing to use installed tools in the installation script.

Here's an example packer template I've been playing with (I've know there are better ways to use this with scripts and provisioners. but for simplicity sake I'm learning with this) :

{
  "variables":{
    "aws_access_key": "{{env `AWS_ACCESS_KEY`}}",
    "aws_secret_key": "{{env `AWS_SECRET_KEY`}}"
  },
  "builders":[{
    "type": "amazon-ebs",
    "access_key":"{{user `aws_access_key`}}",
    "secret_key":"{{user `aws_secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-de0d9eb7",
    "instance_type": "t1.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example2 {{timestamp}}"

  }],
  "provisioners":[{
    "type": "shell",
    "inline":[
      "sleep 30",
      "echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list",
      "echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list",
      "sudo apt-get-repository ppa:chris-lea/node.js",
      "wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc",
      "sudo apt-key add rabbitmq-signing-key-public.asc",
      "sudo apt-get update",
      "sudo apt-get install -y build-essential python-dev",
      "sudo apt-get build-dep -y python-imaging",
      "sudo apt-get install -y redis-server",
      "sudo apt-get install -y mongodb-org",
      "sudo apt-get install -y nodejs",
      "sudo apt-get install -y nginx",
      "sudo apt-get install -y rabbitmq-server",
      "sudo apt-get install -y git",
      "sudo apt-get -y install postgresql libpq-dev postgresql-contrib",
      "sudo apt-get -y install htop",
      "sudo apt-get -y install sysstat",
      "sudo apt-get install -y curl",
      "sudo apt-get install -y ntp",
      "sudo apt-get install -y wget",
      "npm install -g swamp bower grunt"
    ]
  }]
}

And I'm getting the following error message:

amazon-ebs: sudo: npm: command not found

I know npm installs with nodejs (at least from my experience) so I'm probably missing something. I guess this would also happen when I try to use similar platform install tools as:pip, gem etc...

4

1 回答 1

2

自从我提出这个问题以来已经有一段时间了,我重新审视了这个问题,现在我可以自己回答这个问题(现在我使用 packer 来构建我们的生产机器,仍然很棒),并提供了一些获得工作 Packer 解决方案的指针

  1. 阅读实际安装日志,它指向了问题!它可能使用不同的组件(就像这里的情况一样),但问题(未添加 apt-repo)可能早在错误出现之前就出现了。
  2. 实际的问题是sudo apt-add-repository对于 node.js 存储库,从来没有工作过,所以我真的没有安装 npm,并且得到了一个与 ubuntu 存储库捆绑在一起的旧 node.js 版本(没有 chris-lea 存储库)并且没有npm。
  3. 为了使 apt-add-repository 工作,必须先在脚本中安装一些先决条件:

sudo apt-get install -y software-properties-common sudo apt-get install -y python-software-properties

  1. 一个好主意是在尝试安装 npm 模块之前安装 git、curl、wget、unzip 等,因为某些节点模块/工具实际上依赖于它们。

  2. 根本不要使用 ubuntu node/npm bundle .. 安装nvm并使用它安装你想要的任何版本的 node.js 和 IO.js。在尝试使用新的 npm 之前不要忘记sourcenvm.sh。

希望这对遇到此问题的任何人有所帮助。

于 2015-03-12T19:56:55.797 回答