Ubidots CLI's functions command enables the user to set up a local development environment for UbiFunctions. As specified in the Installing section, Docker is required for this.
Before starting with local UbiFunctions development, remember to run the ubidots config command in order to properly set the environment's required configuration.
Create a local UbiFunction
Create a new local UbiFunction with the given runtime, method and other settings.
By editing this file, you can can manually change the UbiFunction's settings; however, it's recommended to use the designated options for each command to make changes.
Start the local development environment
Start the local development environment in order to enable the UbiFunction's execution.
Usage:ubidotsfunctionsstart [OPTIONS] Initializethefunctioncontainerenvironmentforexecution.╭─Options───────────────────────────────────────────────────────────────────────────────────────────────────────────────╮│--cors--no-corsFlagtoenableCross-OriginResourceSharing (CORS) for the function. ││ [default: no-cors]││--cronTEXTCronexpressiontoschedulethefunctionforperiodicexecution.││ [default: *****]││--methodsTEXTTheHTTPmethodsthefunctionwillrespondto. [default: GET]││--raw--no-rawFlagtodetermineiftheoutputshouldbeinrawformat. [default: no-raw]││--timeoutINTEGERMaximumtime (in seconds) the functionis allowed to run before being terminated. ││ [max: 300]││ [default: 10]││--tokenTEXTOptionalauthenticationtokentoinvokethefunction.││--verbose-vEnableverboseoutput.││--helpShowthismessageandexit.│╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
This command must be executed from the local UbiFunction's directory.
After executing, this command will output the following:
------------------StartingFunction:------------------Name:prueba-python-pushRuntime:python3.9:baseLocallabel:lambda_fn_prueba-python-push_Bk2L4H5bKp-------INPUTS:-------Raw:FalseMethods:GETToken:http://172.18.0.2:8042/lambda_fn_prueba-python-push_Bk2L4H5bKp> [DONE]: Function started successfully.
The displayed URL is where the local UbiFunction is exposed.
This command must be executed from the local UbiFunction's directory.
After executing this command and if the UbiFunction doesn't exists yet in the Ubidots cloud, it will prompt you for creating it:
Thisfunctionisnotcreated.Wouldyouliketocreateanewfunctionandpushit? [y/N]: y> [DONE]: Function uploaded successfully.
On the other hand, if the function already exists in Ubidots, it will promp you for overwriting:
Areyousureyouwanttooverwritetheremotefiles? [y/N]: y> [DONE]: Function uploaded successfully.
Pull an UbiFunction from Ubidots.
This command is helpful when you've pushed a local UbiFunction to Ubidots, made further changes there, and now want to pull those updates back to your local environment.
ubidotsfunctionsnew--namemy-first-function--runtimepython3.9:lite> [DONE]: Project 'my-first-function' created in '/home/inumaki/test-cli/my-first-function'.
Navigate to the UbiFunction's directory and open the main.py file with your preferred text editor. In the main function, locate the following lines:
if token isNoneor device isNone:print("[ERROR] Please send your Ubidots Token and device label to update in your args")return{"status":"error"}del args['token']del args['device']
Then add the following line just below del args['device']
del args['_auth_token']
The code should now look like this:
if token isNoneor device isNone:print("[ERROR] Please send your Ubidots Token and device label to update in your args")return{"status":"error"}del args['token']del args['device']del args['_auth_token']
Starting the UbiFunction
Now save the changes and go back to the terminal. Then run the following command to start the local UbiFunction:
ubidotsfunctionsstart--methodsPOST
The output of that command will look like:
------------------StartingFunction:------------------Name:my-first-functionRuntime:python3.9:baseLocallabel:lambda_fn_my-first-function_OWWTAWxPGi-------INPUTS:-------Raw:FalseMethods:POSTToken:URL:http://172.18.0.2:8042/lambda_fn_my-first-function_OWWTAWxPGi> [DONE]: Function started successfully.
The displayed URL is where the UbiFunction is exposed, thus you can perform HTTP request to it in order to trigger the UbiFunction's execution.
Running the UbiFunction
Perform an HTTP POST request to the given URL by running:
You can confirm proper execution by going to your Ubidots account and checking if there's a new device called first-device-cli containing a variable called temperature holding a value of 78.
Pushing the UbiFunction to Ubidots
After runing locally your UbiFunction and determining it's working as intended, you can deploy it to your production environment (Ubidots) by running:
ubidotsfunctionspushAreyousureyouwanttooverwritetheremotefiles? [y/N]: y> [DONE]: Function uploaded successfully.
This will prompt you for overwriting the changes in Ubidots. Upon confirmation, the local changes will be pushed to Ubidots.
Pulling the UbiFunction from Ubidots
If you need to make changes to the UbiFunction's production version and want to edit it from your local machine, you can pull the changes from Ubidots, make the necessary modifications locally, and then push the updates back to Ubidots. Start by pulling the changes by running:
ubidotsfunctionspullAreyousureyouwanttooverwritethelocalfiles? [y/N]: y> [DONE]: Function downloaded successfully.
After that, you can use the start, run and push commands as described before.