Google Cloud Functions (GCF) - 使用 apt-get 安装包

Google Cloud Functions (GCF) - installing packages With apt-get

我的 Google 云函数 (GCF) 函数需要 2 个包才能正常工作。我通常会在 Ubuntu:

上使用以下命令安装它们
apt-get -y install firefox xvfb

但是,我不知道如何打包我的函数,这会指示 GCF 在 运行 我的代码之前下载这些包。

我尝试使用 subprocess.call().

从我的 Python 函数中安装它们

这是一些代码:

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)

不幸的是,这不起作用。我收到以下错误:

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)

如何修复此错误以从我的 python 代码中下载软件包?是否有任何其他 (easier/cleaner) 方法来实现我正在尝试做的事情?

谢谢!

您无法安排在 Cloud Functions 实例上安装包。这是因为您的代码没有 运行 root 权限。如果您需要二进制文件可用于部署到 Cloud Functions 的代码,您必须自己为 Debian 构建它,并将二进制文件包含在您的函数目录中,以便它与您的其余代码一起部署。

即使您能够做到这一点,也不能保证它一定会起作用,因为 Cloud Fucntions 图像可能不包含可执行文件起作用所需的所有共享库。

您无法安装软件包,但可以使用 puppeteer 而不是 xvfb。 https://cloud.google.com/blog/products/gcp/introducing-headless-chrome-support-in-cloud-functions-and-app-engine

不是您问题的直接答案,但您似乎正尝试在 GCF 中使用无头浏览器。可能值得一提的是,Cloud Functions 现在支持 headless Chrome,如果您能够从 Firefox 过渡到 Chrome:

,这可能会更容易使用

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

您可以使用 public issue tracker 请求将新包添加到运行时。请注意,问题跟踪器说 "App Engine" 但只是提到这是针对 Cloud Functions 的。