Skip to content

Snipet to Power Off a VSI (Virtual Server) on IBM Cloud using the Functions.

Notifications You must be signed in to change notification settings

itirohidaka/PowerOff-Functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Power Off a VSI using IBM Cloud Functions

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.

The Easy Way

  1. 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
  1. 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"
  1. 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
  1. Open the IBM Cloud console
open https://cloud.ibm.com
  1. Login to the IBM Cloud Console using your Credentials (username and password)

  2. Click on the Three Line Menu (Hamburger Menu) and click on "Functions"

  3. 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.

  4. Click on "Trigger" item and click on the "Create" button.

  5. Click on "Create Trigger".

  6. Click on "Periodic"

  7. Type the name on "Trigger Name" field.

  8. Select the days and hours that the function will be executed. You can select a pre defined period on "Select pattern" field.

  9. In the JSON Payload, type:

{
  "username": "<softlayer_username>",
  "key": "<softlayer_api_key>",
  "vsiname":"<name_of_the_vsi>",
  "poweraction":"<power_action>"
}

Example:

{
  "username": "1234567_itiro@br.ibm.com",
  "key": "ahsdjklfhlajkshdjkfhaljksdhfkahsldhflasjdhfjkashdk",
  "vsiname":"virtualserver01",
  "poweraction":"on"
}

OBS: Change <name_of_the_vsi> with the name of the VSI that needs to Powered On/Off.

  1. Click on "Create" button.

  2. In the next screen, click on "Add" button to associate an action with the Trigger.

  3. Click on "Select Existing" Button.

  4. Click on "Select an Action" field and Select you action.

  5. Click on Add.

  6. 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.

The Hard Way

  1. 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
  1. 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"
  1. 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
  1. 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"
  1. 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
  1. Open the IBM Cloud console
open https://cloud.ibm.com
  1. Login to the IBM Cloud Console using your Credentials (username and password)

  2. Click on the Three Line Menu (Hamburger Menu) and click on "Functions"

  3. 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.

  4. Click on "Trigger" item and click on the "Create" button.

  5. Click on "Create Trigger".

  6. Click on "Periodic"

  7. Type the name on "Trigger Name" field.

  8. Select the days and hours that the function will be executed. You can select a pre defined period on "Select pattern" field.

  9. In the JSON Payload, type:

{
  "username": "<softlayer_username>",
  "key": "<softlayer_api_key>",
  "vsiname":"<name_of_the_vsi>",
  "poweraction":"<power_action>"
}

Example:

{
  "username": "1234567_itiro@br.ibm.com",
  "key": "ahsdjklfhlajkshdjkfhaljksdhfkahsldhflasjdhfjkashdk",
  "vsiname":"virtualserver01",
  "poweraction":"on"
}

OBS: Change <name_of_the_vsi> with the name of the VSI that needs to Powered On/Off.

  1. Click on "Create" button.

  2. In the next screen, click on "Add" button to associate an action with the Trigger.

  3. Click on "Select Existing" Button.

  4. Click on "Select an Action" field and Select you action.

  5. Click on Add.

  6. 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.

About

Snipet to Power Off a VSI (Virtual Server) on IBM Cloud using the Functions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages