Why can't I run "source" command from within a packer build?

时间:2015-06-25 18:39:36

标签: python packer

I'm attempting to do a packer build.

My source AMI is an Ubuntu box. I would like to install and run a python virtual environment.

My provisioner is shown below. The apt-get works fine and it creates a virtualenv. However, when I do source on activate, it fails. I get the following error: /tmp/script.sh: 53: /tmp/script.sh

It seems I cannot run source in my packer build. How do I get around this issue? I need to run the virtualenv and do pip install from inside it.

  "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'",
      ]
    }
  ]

Here is the console output:

  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

2 个答案:

答案 0 :(得分:3)

You can only source within a shell, which you don't have. And there is a simpler solution anyway, directly use the binaries provided for you within the virtualenv. $ virtualenv foobarbaz $ foobarbaz/bin/python -c 'import sys; print sys.prefix;' illustrates the concept & will work in the same way with pip.

答案 1 :(得分:2)

请在“问题排查”部分here中注意有关shell脚本的评论。默认情况下,打包器使用/ bin / sh,它在Ubuntu上是破折号外壳。破折号外壳不支持"来源"命令。相反,你可以:

  • 使用"。"而不是源
  • 在您的打包文件中指定inline_shebang
  • 使用"脚本"而不是"内联"