5

I need 2 packages for my Google Cloud Functions (GCF) functions to work properly. I'd typically install them with the following command on Ubuntu:

apt-get -y install firefox xvfb

However, I don't know anyway to package my functions which would direct GCF to download these packages before running my code.

I tried installing them from within my Python function, using subprocess.call().

Here is some code:

try:
    print(subprocess.check_output("apt-get -y install firefox", shell=True, stderr=subprocess.STDOUT))
except subprocess.CalledProcessError as e:
    print("Ping stdout output:\n", e.output)

try:
    print(subprocess.check_output("apt-get -y install xvfb", shell=True, stderr=subprocess.STDOUT))
except subprocess.CalledProcessError as e:
    print("Ping stdout output:\n", e.output)

Unfortunately, this does not work. I get the following errors:

Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
 libcanberra0 libdbusmenu-gtk3-4 libstartup-notification0 libtdb1
 libxcb-util1 sound-theme-freedesktop xul-ext-ubufox
Suggested packages:\n fonts-lyx libcanberra-gtk0 libcanberra-pulse
The following NEW packages will be installed: firefox
 libcanberra0 libdbusmenu-gtk3-4 libstartup-notification0 libtdb1
 libxcb-util1 sound-theme-freedesktop xul-ext-ubufox
0 upgraded, 8 newly installed, 0 to remove and 5 not upgraded.
Need to get 44.5 MB of archives.
After this operation, 170 MB of additional disk space will be used.
W: Not using locking for read only lock file /var/lib/dpkg/lock
W: chown to _apt:root of directory /var/cache/apt/archives/partial failed - SetupAPTPartialDirectory (30: Read-only file system)
W: chmod 0700 of directory /var/cache/apt/archives/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
W: chown to _apt:root of directory /var/lib/apt/lists/auxfiles failed - SetupAPTPartialDirectory (30: Read-only file system)
W: chmod 0700 of directory /var/lib/apt/lists/auxfiles failed - SetupAPTPartialDirectory (1: Operation not permitted)
W: Not using locking for read only lock file /var/cache/apt/archives/lock
E: Couldn't determine free space in /var/cache/apt/archives/ - statvfs (38: Function not implemented)

How can I fix this error to download the packages from within my python code? Is there any other (easier/cleaner) way to achieve what I'm trying to do?

Thanks!

Astar
  • 258
  • 4
  • 14

4 Answers4

6

You can't arrange for packages to be installed on Cloud Functions instances. This is because your code doesn't run with root privileges. If you need binaries to be available to your code deployed to Cloud Functions, you will have to build it yourself for Debian, and include the binaries in your functions directory so it gets deployed along with the rest of your code.

Even if you're able to do that, there's no guarantee it will work, because the Cloud Fucntions images may not include all the shared libraries required for the executables to work.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • 1
    Is there any documentation detailing which OS(es) that GCF uses, and whether they're subject to change? In another SO question, I've asked about pre-building some Python libs and I know I can build them for Debian ("Linux-4.4.0-x86_64-with-debian-buster-sid" is what my last function ran on) - but I don't know if the platform is subject to change underneath me – SJoshi Jul 26 '20 at 20:44
2

You can request that new packages be added to the runtime using the public issue tracker. Note that the issue tracker says "App Engine" but just mention that this is for Cloud Functions.

stewblr
  • 79
  • 5
0

You can't install packages but can use puppeteer instead of xvfb. https://cloud.google.com/blog/products/gcp/introducing-headless-chrome-support-in-cloud-functions-and-app-engine

Ismail Baskin
  • 374
  • 6
  • 9
0

Not a direct answer to your question, but it seems like you're trying to use a headless browser inside GCF. It's probably worth mentioning that headless Chrome is now supported in Cloud Functions, which might be easier to use if you're able to transition from Firefox to Chrome:

https://cloud.google.com/blog/products/gcp/introducing-headless-chrome-support-in-cloud-functions-and-app-engine

Dustin Ingram
  • 20,502
  • 7
  • 59
  • 82