Is it possible to run a script from Firebase Functions Shell? - firebase

I've been using the Firebase Functions Shell (firebase experimental:functions:shell) and am finding it excellent for testing the firebase database and http listeners.
However it is a little annoying that I have to copy-paste test code in the shell.
Is there any way I can just write some js code in a file and then run the complete file with the Firebase shell? Without copying and pasting?

Yes, send your script the the shell's stdin.
$ firebase experimental:functions:shell < tests.js
Taken from this blog.
You may still have to terminate the shell with ctl-C. Please file a bug report if you would like to see that change.

Related

How can I write a file from a DAG to my composer cloud storage bucket?

The accepted answer to this question states that
"...the gs://my-bucket/dags folder is available in the scheduler, web server, and workers at /home/airflow/gcs/dags."
(which is supported by the newer docs)
So I wrote a bash operator like this:
t1 = bash.BashOperator(
task_id='my_test',
bash_command="touch /home/airflow/gcs/data/test.txt",
)
I thought by prefacing my file creation with the path specified in the answer it would write to the data folder in my cloud composer environment's associated storage account. Simiarly, touch test.txt also ran successfully but didn't actually create a file anywhere I can see it (I assume it's written to the worker's temp storage which is then deleted when the worker is shut down following execution of the DAG). I can't seem to persist any data from simple commands run through a DAG? Is it even possible to simply write out some files from a bash script running in Cloud Composer? Thank you in advance.
Bizarrely, I needed to add a space at the end of the string containing the Bash command.
t1 = bash.BashOperator(
task_id='my_test',
bash_command="touch /home/airflow/gcs/data/test.txt ",
)
The frustrating thing was the error said the path didn't exist so I went down a rabbit-hole mapping the directories of the Airflow worker until I was absolutely certain it did - then I found a similar issue here. Although I didn't get the 'Jinja Template not Found Error' I should have got according to this note.

Firebase CLI - How to Manage Cloud Functions

Using the firebase-cli you can easily deploy functions and show the logs. But I have yet to find the option to download or retrieve cloud functions.
I've dug through quiet a bit of the -h commands and nothing yet.
Is it possible to download / retrieve a projects cloud functions through the firebase-cli? I tend to switch between computers from home to work and I need to go to the console to copy the source code and redeploy.
The Firebase CLI doesn't provide a way to get deployed code. It's expected that you use some form of source control for managing code between developers and machines.

How to Run Admin functions on Firebase

I'm trying to create a function to delete all users from the Database and write it on index.ts file and deploy it. Now, how can I run it?? I don't want my clients to run it from their mobile app, I need some admin tool to run the management functions. When I see cloud functions on Firebase console there's no option to run the functions, just to view their logs.
Cloud Functions isn't for running one-time scripts. You should do that on your local machine. Cloud Functions is for running code that responds to HTTP requests, or events that occur within other products in your project.
You may want to read this article: https://firebase.googleblog.com/2017/03/how-to-schedule-cron-jobs-with-cloud.html - it suggests using App Engine cron job for that purpose. Relevant project on github: https://github.com/FirebaseExtended/functions-cron
Also there's an example on similar topic in Firebase docs: https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron

Issue running cloud functions code locally using cloud functions shell

I am trying to test my functions locally using cloud functions shell. I was successful in making the shell thing work for my code. I see that this doesn't require my code to be deployed to the cloud. But whenever I run the function through shell it's working fine but it is using the deployed code, not the local code(I am checking this by using console statements as shown in sample code). I am not able to invoke local code unless I deploy.
Also, in my cloud functions, I am using the onCreate method for a real-time database and writing back to the same real-time database. When I test locally using the shell, I input data files for the function and write back to the real-time database. So I am actually trying to write code and run it locally to write to a real-time database on the cloud. Is this achievable using shell without deploying functions?
My sample function looks like this:
export const myCloudFunction = functions.database.instance(getDatabaseIdentifier()).ref(PATH).onCreate(async (snapshot, context) => {
console.log('local code invoked')
// or
console.log('deployed code invoked')
});
I figured it out since I am using typescript I need to transpile my code to javascript before I run the cloud functions shell.
The reason I thought it's invoking the deployed code is obvious as it is not actually invoking the deployed code but invoking the locally transpiled code which gets generated while I deploy to cloud. Now all I needed to do is transpile my code using the below command in my functions folder before I run the cloud functions shell.
// run this command in your functions folder
'npm run-script build'
This build generates transpiled javascript code in the 'lib' folder. Now we can run the below command to invoke the shell.
firebase functions:shell
Now we can emulate the local non deployed cloud functions and test them locally.
Check this medium post for detailed explanation:
https://medium.com/#moki298/test-your-firebase-cloud-functions-locally-using-cloud-functions-shell-32c821f8a5ce

Is it possible to retrieve a document from Cloud Storage from linux shell script?

I am currently working on an personal application where, I upload documents/pictures/videos from my phone to Cloud Storage. During this time my computer sitting at home is consistently running a shell script waiting for a new document to be uploaded to Cloud Storage, after it finds an uploaded file, it downloads it does some work to it, and then deletes it.
I can figure out how to upload and connect my application to Firebase, but I am not sure if its possible for a shell script to do the remaining work.
Should I look into some other service to do this, or another method?
thank you for your help!
You can use a command line program called gsutil to upload and download files from a Cloud Storage bucket. This should be easy to use from a shell script.

Resources