Description: Snippet to Power Off a VSI (Virtual Server) on IBM Cloud using the IBM Cloud Functions.
Requirements:
- Docker CE installed on your Desktop/Laptop.
- Python 2.7 for some local tests.
- I'm using the MacOS X. But you can modify some commands to get it work on Windows.
The following steps will help you to create an Action on the IBM Cloud Functions. This Action is based on a Python 2 snippet.
- Clone this repo using "git clone" command and access the app folder. Example:
git clone https://github.com/itirohidaka/PowerOff-Functions.git
cd PowerOff-Functions
- You need to set the Org and Space on ibmcloud cli command. You can use the command bellow to see the orgs and spaces on you account Example:
ibmcloud login
ibmcloud account orgs
ibmcloud account spaces
ibmcloud target -o "WCP Individuals" -s "wcp_uss_itiro"
- Push the zip "package" to IBM Cloud Functions. You can upload my previouly created file (the easy way)
ibmcloud fn action create itiroaction01 hello.zip --kind python:2
- Open the IBM Cloud console
open https://cloud.ibm.com
-
Login to the IBM Cloud Console using your Credentials (username and password)
-
Click on the Three Line Menu (Hamburger Menu) and click on "Functions"
-
Click on "Actions" and check if you Action appear on the list. In my example, I'm using the "itiroaction01" name for the action name.
-
Click on "Trigger" item and click on the "Create" button.
-
Click on "Create Trigger".
-
Click on "Periodic"
-
Type the name on "Trigger Name" field.
-
Select the days and hours that the function will be executed. You can select a pre defined period on "Select pattern" field.
-
In the JSON Payload, type:
{
"username": "<softlayer_username>",
"key": "<softlayer_api_key>",
"vsiname":"<name_of_the_vsi>",
"poweraction":"<power_action>"
}
Example:
{
"username": "[email protected]",
"key": "ahsdjklfhlajkshdjkfhaljksdhfkahsldhflasjdhfjkashdk",
"vsiname":"virtualserver01",
"poweraction":"on"
}
OBS: Change <name_of_the_vsi> with the name of the VSI that needs to Powered On/Off.
-
Click on "Create" button.
-
In the next screen, click on "Add" button to associate an action with the Trigger.
-
Click on "Select Existing" Button.
-
Click on "Select an Action" field and Select you action.
-
Click on Add.
-
Your Function is now created with a Trigger and a Action!
You can use the monitor on Function main screen to check if your function is working properly.
- Clone this repo using "git clone" command and access the app folder. Example:
git clone https://github.com/itirohidaka/PowerOff-Functions.git
cd PowerOff-Functions
- Create the Python virtualenv using a docker command (no need to modify, just copy & paste).
docker run --rm -v "$PWD:/tmp" openwhisk/python2action bash -c "cd /tmp && virtualenv virtualenv && source virtualenv/bin/activate && pip install -r requirements.txt"
- Create the zip file with virtualenv folder and __main__.py file.
zip -r <zip_file> <virtualenv_folder> <main_file>
Example:
zip -r hello.zip virtualenv __main__.py
- You need to set the Org and Space on ibmcloud cli command. You can use the command bellow to see the orgs and spaces on you account Example:
ibmcloud login
ibmcloud account orgs
ibmcloud account spaces
ibmcloud target -o "WCP Individuals" -s "wcp_uss_itiro"
- Push the zip "package" to IBM Cloud Functions
ibmcloud fn action create <action_name> <zip_file> --kind <runtime>
Example:
ibmcloud fn action create itiroaction01 hello.zip --kind python:2
- Open the IBM Cloud console
open https://cloud.ibm.com
-
Login to the IBM Cloud Console using your Credentials (username and password)
-
Click on the Three Line Menu (Hamburger Menu) and click on "Functions"
-
Click on "Actions" and check if you Action appear on the list. In my example, I'm using the "itiroaction01" name for the action name.
-
Click on "Trigger" item and click on the "Create" button.
-
Click on "Create Trigger".
-
Click on "Periodic"
-
Type the name on "Trigger Name" field.
-
Select the days and hours that the function will be executed. You can select a pre defined period on "Select pattern" field.
-
In the JSON Payload, type:
{
"username": "<softlayer_username>",
"key": "<softlayer_api_key>",
"vsiname":"<name_of_the_vsi>",
"poweraction":"<power_action>"
}
Example:
{
"username": "[email protected]",
"key": "ahsdjklfhlajkshdjkfhaljksdhfkahsldhflasjdhfjkashdk",
"vsiname":"virtualserver01",
"poweraction":"on"
}
OBS: Change <name_of_the_vsi> with the name of the VSI that needs to Powered On/Off.
-
Click on "Create" button.
-
In the next screen, click on "Add" button to associate an action with the Trigger.
-
Click on "Select Existing" Button.
-
Click on "Select an Action" field and Select you action.
-
Click on Add.
-
Your Function is now created with a Trigger and a Action!
You can use the monitor on Function main screen to check if your function is working properly.
With this snippet you can have an action with several Triggers. Each Trigger with a diffrent VSI name.