4

我正在尝试进行打包程序构建。

我的源 AMI 是一个 Ubuntu 盒子。我想安装并运行一个 python 虚拟环境。

我的供应商如下所示。apt-get 工作正常,它创建了一个 virtualenv。但是,当我激活源时,它失败了。我收到以下错误:/tmp/script.sh: 53: /tmp/script.sh

看来我无法source在我的打包程序版本中运行。我该如何解决这个问题?我需要运行 virtualenv 并pip install从里面做。

  "provisioners": [

    {
      "type": "shell",
      "inline": [
        "echo '70'",
        "sudo apt-get --assume-yes install python-virtualenv",
        "echo '80'",
        "sudo virtualenv /home/myuser/myVirtualEnv",
        "echo '90'",
        "ls -altr /home/myuser/myVirtualEnv/bin",
        "echo '95'",
        "source /home/myuser/myVirtualEnv/bin/activate",
        "echo '100'",
      ]
    }
  ]

这是控制台输出:

  myHostName: 70
  myHostName: Reading package lists... Done
  myHostName: Building dependency tree
  myHostName: Reading state information... Done
  myHostName: The following NEW packages will be installed:
  myHostName: python-virtualenv
  myHostName: 0 upgraded, 1 newly installed, 0 to remove and 125 not upgraded.
  myHostName: Need to get 1,485 kB of archives.
  myHostName: After this operation, 1,935 kB of additional disk space will be used.
  myHostName: Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ trusty/universe python-virtualenv all 1.11.4-1 [1,485 kB]
  myHostName: Fetched 1,485 kB in 0s (22.5 MB/s)
  myHostName: Selecting previously unselected package python-virtualenv.
  myHostName: (Reading database ... 56592 files and directories currently installed.)
  myHostName: Preparing to unpack .../python-virtualenv_1.11.4-1_all.deb ...
  myHostName: Unpacking python-virtualenv (1.11.4-1) ...
  myHostName: Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
  myHostName: Setting up python-virtualenv (1.11.4-1) ...
  myHostName: 80
  myHostName: New python executable in /home/myuser/myVirtualEnv/bin/python
  myHostName: Installing setuptools, pip...done.
  myHostName: 90
  myHostName: total 3312
  myHostName: lrwxrwxrwx 1 root root       6 Jun 25 17:21 python2.7 -> python
  myHostName: lrwxrwxrwx 1 root root       6 Jun 25 17:21 python2 -> python
  myHostName: -rwxr-xr-x 1 root root 3345416 Jun 25 17:21 python
  myHostName: -rwxr-xr-x 1 root root     254 Jun 25 17:21 easy_install-2.7
  myHostName: -rwxr-xr-x 1 root root     254 Jun 25 17:21 easy_install
  myHostName: -rwxr-xr-x 1 root root     226 Jun 25 17:21 pip2.7
  myHostName: -rwxr-xr-x 1 root root     226 Jun 25 17:21 pip2
  myHostName: -rwxr-xr-x 1 root root     226 Jun 25 17:21 pip
  myHostName: drwxr-xr-x 6 root root    4096 Jun 25 17:21 ..
  myHostName: -rw-r--r-- 1 root root    1129 Jun 25 17:21 activate_this.py
  myHostName: -rw-r--r-- 1 root root    2476 Jun 25 17:21 activate.fish
  myHostName: -rw-r--r-- 1 root root    1263 Jun 25 17:21 activate.csh
  myHostName: -rw-r--r-- 1 root root    2207 Jun 25 17:21 activate
  myHostName: drwxr-xr-x 2 root root    4096 Jun 25 17:21 .
  myHostName: 95
  myHostName: /tmp/script.sh: 53: /tmp/script.sh: source: not found
  myHostName: 100
4

3 回答 3

5

请注意此处的疑难解答部分中有关 shell 脚本的注释。默认情况下,packer 使用 /bin/sh,它在 Ubuntu 上是 dash shell。dash shell 不支持“source”命令。相反,您可以:

  • 采用 ”。” 而不是源
  • 在您的打包程序文件中指定 inline_shebang
  • 使用“脚本”而不是“内联”
于 2015-06-25T21:29:36.290 回答
3

您只能在没有的 shell 中获取资源。无论如何,有一个更简单的解决方案,直接使用 virtualenv 中为您提供的二进制文件。

$ virtualenv foobarbaz
$ foobarbaz/bin/python -c 'import sys; print sys.prefix;'

说明了这个概念 & 将以与 pip 相同的方式工作。

于 2015-06-25T18:42:15.737 回答
0

Packer 构建 HCL 文件:

build {
  provisioner "shell" {
    script = "../scripts/setup.sh"
  }
}

并在 setup.sh 中替换source.

#!/bin/bash

. env/bin/activate
于 2021-10-13T02:29:32.207 回答