libcloud Python example – kick.py

libcloud Python example – kick.py

So here is an example python script that you can use to build new CloudServers and then deploy software on them. This particular example is pretty basic and only installs either a LAMP stack or just a simple installation of MySQL. Don’t let the limitations of this particular script fool you into thinking that is all libcloud or python can do because that is not the case. You can do something pretty basic like this one, extend the script to utilize threading which will allow you to deploy multiple instances at a time, or simply have more complex software installations.

Before you run the script you will need to have the libcloud python module installed. It was written by the cloudkick team over at: https://www.cloudkick.com/

You can obtain the libcloud modules via their website at http://incubator.apache.org/libcloud/

The syntax and usage for the script is pretty simple, however instructions are as follows:

$ wget http://www.timgalyean.com/kick.py
$ chmod +x kick.py
$ ./kick.py
Usage:  ./kick.py  <username> <apikey> <OS 3-CentOS5.4 | 13-RHEL5.4 | 9-Ubuntu 10.04> <serversize> <servername> <service LAMP | MYSQL>

Please keep in mind the instructions listed above assume you are using Linux as your operating system, and that you have python 2.6 installed. Instructions may vary, however you can still download the kick.py script here: http://www.timgalyean.com/kick.py

Examples:
LAMP Stack on CentOS 5.4 1GB Server

./kick.py USERNAME APIKEY 3 2 SERVERNAME LAMP

MySQL Database on RHEL 5.4 512MB Server

./kick.py USERNAME APIKEY 13 1 SERVERNAME MYSQL

LAMP Stack on Ubuntu 10.04 LTS 1GB Server

./kick.py USERNAME APIKEY 9 2 SERVERNAME LAMP

MySQL Database on Ubuntu 10.04 1GB Server

./kick.py USERNAME APIKEY 9 2 SERVERNAME MYSQL
#!/usr/bin/python
# 
# File:         kick.py
# Date:         8-24-2010
# Written by:   Tim Galyean
#
##################################################
 
import sys
from libcloud.types import Provider 
from libcloud.providers import get_driver 
from libcloud.deployment import MultiStepDeployment, ScriptDeployment
 
 
if(len(sys.argv[1:])<5):
    print "Usage: ", sys.argv[0], " <username> <apikey> <OS 3-CentOS5.4 | 13-RHEL5.4 | 9-Ubuntu 10.04> <serversize> <servername> <service LAMP | MYSQL>"
    sys.exit()
else:
    RACKSPACE_USER = sys.argv[1]
    RACKSPACE_KEY = sys.argv[2]
    os = int(sys.argv[3])
    ram = int(sys.argv[4])
    servername = str(sys.argv[5])
    service = sys.argv[6]
 
Driver = get_driver(Provider.RACKSPACE) 
conn = Driver(RACKSPACE_USER, RACKSPACE_KEY) 
images = conn.list_images() 
sizes = conn.list_sizes() 
 
if '3' in str(sys.argv[3]) or '13' in str(sys.argv[3]):
    if "LAMP" in str(sys.argv[6]):
        LAMP = ScriptDeployment("yum -y install httpd php php-mysql mysql mysql-server")
        msd = MultiStepDeployment([LAMP]) 
        node = conn.deploy_node(name=servername, image=images[os], size=sizes[ram], deploy=msd)
    else:
        if "MYSQL" in str(sys.argv[6]):
            MYSQL = ScriptDeployment("yum -y install mysql mysql-server")
            msd = MultiStepDeployment([MYSQL]) 
            node = conn.deploy_node(name=servername, image=images[os], size=sizes[ram], deploy=msd)
else:
    if '9' in str(sys.argv[3]):
        if "LAMP" in str(sys.argv[6]):
            LAMP = ScriptDeployment("apt-get -y install apache2 php5 php5-mysql mysql-server")
            msd = MultiStepDeployment([LAMP])
            node = conn.deploy_node(name=servername, image=images[os], size=sizes[ram], deploy=msd)
        else:
            if "MYSQL" in str(sys.argv[6]):
                MYSQL = ScriptDeployment("apt-get -y install mysql mysql-server")
                msd = MultiStepDeployment([MYSQL])
                node = conn.deploy_node(name=servername, image=images[os], size=sizes[ram], deploy=msd)