-1

I have written a bunch of code that's deployed as a Firebase function. Some of it could be replaced with a simple call to an executable CLI. i.e., cat x | xargs ... But a lot of that function's code would still need to exist to process the result. Is there a way to make a Firebase function execute something on the CLI, like this:

const { exec } = require("child_process");

exec("ls -la", (error, stdout, stderr) => {
    if (error) {
        console.log(`error: ${error.message}`);
        return;
    }
    if (stderr) {
        console.log(`stderr: ${stderr}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
});

My function code is in nodejs.

Gourav B
  • 864
  • 5
  • 17
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • The phrasing here is confusing: you mention that, in your view, "*[The firebase function] could be replaced with a simple call*", but then follow that claim immediately with "*a lot of that function's code would still need to exist to process the result*", which reads to me as a direct contradiction to one another. Which is it? Why is any of this information relevant to the core question, anyway? – esqew Aug 18 '21 at 20:22
  • 1
    You can exec commands in Cloud Functions as you would normally expect for a node process. There is nothing special going on. The command just has to exist. If your question is actually asking why your code doesn't work, there's not enough information here to tell. We need a full repro. – Doug Stevenson Aug 18 '21 at 20:25
  • @esqew thank you! I knew I worded this poorly, but I couldn't figure out where. – Daniel Kaplan Aug 18 '21 at 21:01
  • @DougStevenson well the thing is the command isn't natively installed (it's a node app with a CLI). How would I install it? Keep in mind, I'm talking about Firebase Functions here. – Daniel Kaplan Aug 18 '21 at 21:03
  • I think the other pertinent question here is, *why* would you want to do this? In most all cases there are more efficient, compatible, robust options, like bindings native to your language of choice, than running shell commands and writing custom parsers to parse and act on `stdout`, which is largely meant for human consumption. – esqew Aug 18 '21 at 21:06
  • It sounds like that's a second question entirely: How do I install a CLI program to execute in my Cloud Functions code that targets nodejs? I suggest doing research on that and ask a new question if you're having problems. Your question should be specific about what you're trying to achieve (like, where do you expect to get this program? Did you build it yourself? Or download a binary somewhere? What have you tried so far that didn't work?) – Doug Stevenson Aug 18 '21 at 21:18
  • @DougStevenson ok thanks. I didn't know what I didn't know until this. – Daniel Kaplan Aug 18 '21 at 21:44
  • 2
    You can call existing binaries with `spawn`, which for example we do in this example of converting images: https://github.com/firebase/functions-samples/blob/main/convert-images/functions/index.js#L63. You can include custom binaries into your Cloud Functions deploy. See some examples here: https://serverfault.com/q/993790/ and here: https://stackoverflow.com/a/60256010 and here: https://stackoverflow.com/a/42777596. – Frank van Puffelen Aug 18 '21 at 22:45

1 Answers1

1
  • You can exec commands in Cloud Functions as you would normally expect for a node process.
  • Although there is no restriction to install any third-party library for a relevant programming language with a package manager (npm, pip), sometimes even that is not enough. Cloud Functions are built on top of the Ubuntu 18.04 image and provide some useful system packages,a feature that is not often mentioned and this is the topic I want to focus on.

Note : Cloud Functions runtimes have pre-installed system packages because Cloud Functions runtimes are based on Ubuntu 18.04 image, it's like having an Ubuntu sandbox below your function. All runtimes contain the same system packages. However, you can request packages to be added to the runtime.Before requesting a package, check for existing requests of the package in the issue tracker. If there are no existing requests, explain your use case in a new issue. That will be added as a feature request.

A little suggestion here : You should really consider using Cloud Run for this use case. Cloud Run gives you the flexibility of creating your own container image that can include the binaries and libraries you want. You can find a tutorial that bundles custom binaries on Cloud Run here

Priyashree Bhadra
  • 3,182
  • 6
  • 23
  • Thank you for the answer. "All runtimes contain the same system packages. However, you can request packages to be added to the runtime." How do you do that? – Daniel Kaplan Aug 26 '21 at 00:11
  • "You should really consider using Cloud Run for this use case." Why is this a better use case for that? Just you know, I am downloading and converting videos, similar to that third bullet point. – Daniel Kaplan Aug 26 '21 at 00:13
  • 1
    Google Cloud offers Cloud Run which is a serverless container platform and allows to install any package/library/program within Docker container and it's more suitable when you want to run your own custom software, but Cloud Functions feel more lightweight. I hope [this](https://medium.com/google-cloud/cloud-run-and-cloud-function-what-i-use-and-why-12bb5d3798e1) answers why Cloud Run is a better choice and [this](https://medium.com/google-cloud/yet-another-image-file-converter-using-gcp-serverless-components-c97d6857ee6a) explains how Cloud Run can be used as a image file converter. – Priyashree Bhadra Aug 26 '21 at 09:29