How to create my web frontend infrastructure in few lines of code ?


Published on May 29th, 2009

Today we’ll introduce the first article of “call the expert” category.

This category’s goal is to explain by example how to use NiftyName capacities through its API.

You can download attached files at the end of the article and test at home :)

Today case study is

“how can I quick create my 5 servers cluster for my new website frontend ?”

You have zish and zigui of course, but sometimes it’s not pretty for repetitive tasks. Well, we need to make our own script. This script is written in Python. You write yours in any language, in future posts we’ll try to make some examples in PhP, Ruby, etc…

And… voilĂ  ! the main script code is inlined behind. In downloadable archive there is some extra code, mainly code already used for zinn XMLRPC functionnal tests (if you’re curious, you can look at it).

To make this code work for you, customize the MyXmlRPC class and that’s all.

import os, sys, basexmlrpc

#Personnal XML-RPC class
class MyXmlRPC(basexmlrpc.BaseXmlRPC):
    def __init__(self):
        super(MyXmlRPC, self).__init__()
        self.confdir    = '%s/.zinn/users' % os.getenv('HOME')
        self.publicname = 'I am a nifty user'
        self.name       = 'niftyuser'
        self.passphrase = 'jablonski'
        self.sitegroup  = 'marseille'
        self.client = 'niftycli'
        self.vsite = 'web-frontend-vsite'
        #self.vsite = 'myniftyvsite'
        self.proxy = None

#Initialize connection
try:
    client = MyXmlRPC()
    client.proxy = client.get_authentified_proxy()
except Exception, e:
    print e
    sys.exit(0)

#Common attributes
how_many_frontends = 5
vsite_attributes = {'siteGroup': client.sitegroup, 'client': client.client}
machine_attributes = {
        'vcpu'  : 2,
        'cpu'   : 'x86-64bits',
        'memory': '2G',
        'vnc'   : {'keymap': 'en-us', 'password': 'Mys3scr3tp4sS'}
     }
storage_attributes = {'size': '3G'}
interface_attributes = {'model': 'virtio'}
attach_stor_attributes = {
            'bus'       : 'virtio',
            'media'     : 'disk',
            'readOnly'  : False,
            'bootOrder' : 1,
            'bootable'  : False
        }

#Bulk creation
client.proxy.vsite.create(client.vsite, vsite_attributes)

for i in range(how_many_frontends):
    client.proxy.machine.create(client.vsite, 'front%d' %i, machine_attributes)
    client.proxy.storage.create(client.vsite, 'stor%d' %i, storage_attributes)
    client.proxy.interface.create(client.vsite, 'iface%d' %i, interface_attributes)

    ipaddress_attributes = {'reverse': 'front%d.%s' %(i, client.vsite), 'family': 'ipv4'}
    client.proxy.ipaddress.create(client.vsite, 'ip%d' %i, ipaddress_attributes)

    client.proxy.interface.ipaddress.attach(client.vsite, 'iface%d' %i, 'ip%d' %i)
    client.proxy.machine.interface.attach(client.vsite, 'front%d' %i, 'iface%d' %i)

    status = 'notrunning'
    while status != 'running':
        r = client.proxy.storage.list(client.vsite, 'stor%d' %i)
        status = r[0]['status']['current']

    client.proxy.machine.storage.attach(client.vsite, 'front%d' %i, 'stor%d' %i, attach_stor_attributes)

This first sample lacks a lot of advanced functionnalities, like having all the cluster in a private network. OS installation is not covered too. Stay tuned, there’s more to come.

Download the archive here


Comments Off

Comments

Comments are closed.