Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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
like image 850
Saqib Ali Avatar asked Feb 10 '23 17:02

Saqib Ali


2 Answers

Please note the comment about shell scripts in the Troubleshooting section here. By default packer uses /bin/sh which on Ubuntu is the dash shell. The dash shell does not support the "source" command. Instead you could:

  • Use "." instead of source
  • Specify inline_shebang in your packer file
  • Use a "script" instead of "inline"
like image 113
mark Avatar answered Feb 12 '23 09:02

mark


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.

like image 45
deets Avatar answered Feb 12 '23 08:02

deets