Thursday 30 July 2015

DIFFERENCE BETWEEN @STATICMETHOD AND @CLASSMETHOD IN PYTHON

@classmethod
To pass class as argument instead of instance(self). We could have achieved this by creating function out of class and passing that class as argument but that could raise mentainance problem in future. So to overcome this Python2.2 came up with @classmethod.

1) Maintainance Problem Causing Senario
def get_no_of_instance(ins_obj):
    return ins_obj.__class__.no_inst

class Test(object):
    no_inst = 0

    def __init__(self):
        Test.no_inst = Test.no_inst + 1

inst1 = Test()
inst2 = Test()

print get_no_of_instance(inst1)  # will output 2

2) Implementation using @classmethod
class Test(object):
    no_inst = 0

    def __init__(self):
        Test.no_inst = Test.no_inst +1

    @classmethod
    def get_no_of_instance(test_class):
        return test_class.no_inst

inst1 = Test()
inst2 = Test()

print inst1.get_no_of_instance() # will output 2
print Test.get_no_of_instance()  # will output 2

So calling @classmethod either by instance or by Class it'll pass the same argument to @classmethod.

@staticmethod
Sometimes we don't need instance method but the normal method, to access those methods we need to decorate them with @staticmethod. It's implementation is same as @classmethod but we don't pass class as argument to method. Without using decorator also we could have achieved this by creating function outside of class but it would have raised maintainance issue in future as code was increased.

1) Maintainance Problem Causing Senario
def checkTOF():
    return true

class Test(object):
    def __init__(self,data):
        self.data = data

    def play1(self):
        if checkTOF():
            print 'play one data is ',self.data

test = Test(10)
test.play1()   

2) Implemenatation using @staticmethod
class Test(object):
    def __init__(self,data):
        self.data = data

    @staticmethod
    def checkTOF():
        return 'true'

    def play1(self):
        if self.checkTOF():
            print 'play one data is ',self.data

test = Test(10)
test.play1()

So this way can bring all related code at one class.

DIFFERENCE BETWEEN BEHAVIOUR AND FUNCTIONALITY...

BEHAVIOUR: How should one act in general. For example Pen behaviour is writting, eraser baviour is erasing and mobile phone behaviour is taking and making call.

FUNCTIONALITY: Rather it is same as behaviour but it marely depend upon state. For example, though PEN's behaviour is writting but its depend upon ink is there or not, I mean Refill is filled-up or not. Though phone"s behaviour is taking and making call but it's state may vary like silent mode, ringing mode, etc.

Tuesday 28 July 2015

THREE NODE ARCHITECTURE

Openstack three node architecture

1) Controller
2) Compute
3) Network

Following setting is applied to Oracle VM VirtualBox Manager.

Before creating 3 nodes we need to set-up.
- Management Network
 For openstack service communication
- Tunnel Network
 For openstack VM's communication      
- External Network
 Provides internet access to the virtual machines.

Now we'll set-up these networks.

Goto File-->Preferences-->Network-->Host-only Networks
Add three interfaces and edit it.
1st interface:
IPv4 Address: 10.10.10.1
IPv4 Network Mask: 255.255.255.0

2st interface:
IPv4 Address: 10.20.20.1
IPv4 Network Mask: 255.255.255.0

3st interface:
IPv4 Address: 192.168.100.1
IPv4 Network Mask: 255.255.255.0


Controller :

Create new VM
Name: Controller
Type: Linux
Version: Ubuntu(64 bit)
-->Next
Memory size: 2048MB or more
-->Next
Select Create a virtual hard drive now
-->Next
Select VDI(Virtual Disk Image)
-->Next
Select Dynamically allocated
-->Next
File Location: browse where it is available
Size: 20GB or more


Start the same VM and select ubuntu server image and complete the installation.
While installation Choose software to install: OpenSSH server
OR
Later also you can install this if it is skipped.
   
Once you finished the installation, try to ping google.com and if it works means VM is created successfully.

The first thing need to do is update and upgrade it.
   
$sudo su
#nano /etc/hosts
comment out 127.0.0.1
introduce the following host
10.10.10.10 controller
10.10.10.11 compute
10.10.10.12 network


On the same VM
Goto Setting-->Network

Under Adapter 1:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et0
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 2:
Check Enable Network Adapter
Attached to: NAT
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)


Now let's create two clone of this VM so we can have CONTROLLER, COMPUTE and NETWORK node.

Controller Node: -
# nano /etc/hostname
controller

# nano /etc/network/interfaces
auto eth0
iface eth0 inet static
address 10.10.10.10
netmask 255.255.255.0

auto eth1
iface eth1 inet dhcp

# restart

Compute :

Compute Node: -
Goto Setting-->Network

Under Adapter 1:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et0
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 2:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et1
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 3:
Check Enable Network Adapter
Attached to: NAT
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
   
$sudo su
#nano /etc/hostname
compute

#nano /etc/network/interfaces
auto eth0
iface eth0 inet static
address 10.10.10.11
netmask 255.255.255.0
   
auto eth1
iface eth1 inet static
address 10.20.20.11
netmask 255.255.255.0

auto eth2
iface eth2 inet dhcp

# ping google.com
# ping 10.10.10.10
# restart

Network :

Network Node: -
Goto Setting-->Network

Under Adapter 1:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et0
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 2:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et1
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 3:    
Check Enable Network Adapter
Attached to: Host-only Adapter
Name: et2
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
Promiscuous Mode: Allow All

Under Adapter 4:
Check Enable Network Adapter
Attached to: NAT
Under Advanced
Adapter Type: Paravirtualized Network(virtio-net)
   
$sudo su
#nano /etc/hostname
network

#nano /etc/network/interfaces
auto eth0
iface eth0 inet static
address 10.10.10.11
netmask 255.255.255.0
   
auto eth1
iface eth1 inet static
address 10.20.20.11
netmask 255.255.255.0

auto eth2
iface eth2 inet manual
up ip link set dev $IFACE up
down ip link set dev $IFACE down    

auto eth3
iface eth3 inet dhcp

#ping google.com
#ping 10.10.10.10
#ping 10.10.10.11
# restart

If nodes are set-up well then you should be able to ping among all node to each other.

At this level if all work well then openstack services are ready to install on these nodes.