Exemplo n.º 1
0
def createRouter():
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    net = nova_client.networks.find(label="ext-net")
    try:
        credentials = get_credentials()
        neutron = client.Client(**credentials)
        neutron.format = 'json'
        request = {
            'router': {
                'name': 'External Router',
                'admin_state_up': True,
                'external_gateway_info': {
                    'network_id': net.id
                }
            }
        }
        router = neutron.create_router(request)
        router_id = router['router']['id']

        router = neutron.show_router(router_id)
        print(router)

        addInterface(neutron, router_id, 'FirstSubnet')
        addInterface(neutron, router_id, 'SecondSubnet')
        addInterface(neutron, router_id, 'ThirdSubnet')
        addInterface(neutron, router_id, 'FourthSubnet')
    finally:
        print("created router")
Exemplo n.º 2
0
def createInstance(instance_name, key_name, image_name, first_network,
                   second_network):
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    try:
        image = nova_client.images.find(name=image_name)
        flavor = nova_client.flavors.find(name="m1.tiny")

        if not second_network:
            net = nova_client.networks.find(label=first_network)
            nics = [{'net-id': net.id}]
        else:
            fnet = nova_client.networks.find(label=first_network)
            snet = nova_client.networks.find(label=second_network)
            nics = [{'net-id': fnet.id}, {'net-id': snet.id}]

        instance = nova_client.servers.create(name=instance_name,
                                              image=image,
                                              flavor=flavor,
                                              key_name=key_name,
                                              nics=nics)
        print("Sleeping for 5s after create command")
        time.sleep(10)
        print("List of VMs")
        print(nova_client.servers.list())
    finally:
        print("Execution Completed")
Exemplo n.º 3
0
def addInterface(neutron, router_id, network_name):
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    subnets = neutron.list_subnets(name=network_name)
    subnet_id = subnets['subnets'][0]['id']
    print(subnet_id)
    router_interface = {'subnet_id': subnet_id}
    neutron.add_interface_router(router_id, router_interface)
Exemplo n.º 4
0
def assignFloatingIP(instance_name):
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    nova_client.floating_ip_pools.list()
    floating_ip = nova_client.floating_ips.create(
        nova_client.floating_ip_pools.list()[0].name)
    print("Floating IP is ", floating_ip)
    instance = nova_client.servers.find(name=instance_name)
    instance.add_floating_ip(floating_ip)
def connectToInstance(instance_name, network_name, network2_name):
    credentials = get_credentials()
    neutron = client.Client(**credentials)
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    instance = nova_client.servers.find(name=instance_name)
    server = nova_client.servers.get(instance)
    if len(server.networks.get(network_name)) == 1:
        public_ip = server.networks.get(network2_name)[1]
    else:
        public_ip = server.networks.get(network_name)[1]
    print(public_ip)
    #Providing private key to keystone to authenticate.
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    key = paramiko.RSAKey.from_private_key_file("./id_rsa")
    ssh.connect(public_ip, username='******', pkey=key)
    return ssh
#!/usr/bin/env python
"""
Description: This script is to allocate & associate the floating IP.
Developer: [email protected]
"""

import pdb
from neutronclient.v2_0 import client
import novaclient.v1_1.client as nvclient
from credentials import get_credentials, get_nova_credentials, \
    get_tenant_nova_credentials
from config import FLOATING_IP_POOL

neutron_credentials = get_credentials()
credentials = get_nova_credentials()
neutron = client.Client(**neutron_credentials)
nova = nvclient.Client(**credentials)

#if not nova.keypairs.findall(name="admin"):
    #with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as fpubkey:
        #nova.keypairs.create(name="admin", public_key=fpubkey.read())


def add_floating_ip_for_vm(tenant_name, instance):
    """
    This method is used to allocate & associate floating IP to the given VM\
    based on the availability from the defined pool.
    """
    tenant_credentials = get_tenant_nova_credentials(tenant_name)
    pdb.set_trace()
    nova = nvclient.Client(**tenant_credentials)
Exemplo n.º 7
0
#!/usr/bin/env python

#Lists the ports created by the create-ports.py

#!/usr/bin/env python
from neutronclient.v2_0 import client
import novaclient.v2.client as nvclient
from credentials import get_credentials
from credentials import get_nova_credentials
from utils import print_values_server

credentials = get_nova_credentials()
nova_client = nvclient.Client(credentials)

#server_id and network_id are got from the environment details

server_id = '9a52795a-a70d-49a8-a5d0-5b38d78bd12d'
network_id = 'ce5d204a-93f5-43ef-bd89-3ab99ad09a9a'
server_detail = nova_client.servers.get(server_id)
print(server_detail.id)

if server_detail is not None:
    credentials = get_credentials()
    neutron = client.Client(**credentials)
    ports = neutron.list_ports()

    print_values_server(ports, server_id, 'ports')
    body_value = {
        'port': {
            'admin_state_up': True,
            'device_id': server_id,
def configureOspf(ssh, instance_name, cost):
    stdin, stdout, stderr = ssh.exec_command('sudo rm /etc/quagga/ospfd.conf')
    #configuring ospf
    #stdin, stdout, stderr = ssh.exec_command('echo '+ line + ' | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo password zebra | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo log file /var/log/quagga/quagga.log | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo log stdout | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo interface eth0 | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo ' + costLine + ' | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo interface eth1 | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo ' + costLine + ' | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo interface lo | sudo tee -a /etc/quagga/ospfd.conf')
    #stdin, stdout, stderr = ssh.exec_command('echo router ospf | sudo tee -a /etc/quagga/ospfd.conf')

    stdin, stdout, stderr = ssh.exec_command(
        unicode(
            'echo hostname ' + instance_name +
            ' | sudo tee --append /etc/quagga/ospfd.conf; echo password zebra | sudo tee --append /etc/quagga/ospfd.conf; echo log file /var/log/quagga/quagga.log | sudo tee --append /etc/quagga/ospfd.conf; echo log stdout | sudo tee --append /etc/quagga/ospfd.conf; echo interface eth0 | sudo tee --append /etc/quagga/ospfd.conf; echo ip ospf cost '
            + str(cost) +
            ' | sudo tee --append /etc/quagga/ospfd.conf; echo interface eth1 | sudo tee --append /etc/quagga/ospfd.conf;  echo ip ospf cost '
            + str(cost) +
            ' | sudo tee --append /etc/quagga/ospfd.conf; echo interface lo | sudo tee --append /etc/quagga/ospfd.conf; echo router ospf | sudo tee --append /etc/quagga/ospfd.conf'
        ))
    for line in stdout:
        print line.strip('\n')
    for line in stderr:
        print("Exceptions: ")
        print line.strip('\n')

    subnets = []
    credentials = get_nova_credentials()
    nova_client = nvclient.Client(**credentials)
    instance = nova_client.servers.find(name=instance_name)
    for network in instance.networks:
        ipaddress = instance.networks[network][0]
        index = ipaddress.rfind('.')
        subnets.append(unicode(ipaddress[0:index] + '.0/24'))

    #ospfd.conf
    for subnet in subnets:
        stdin, stdout, stderr = ssh.exec_command(
            'echo network ' + subnet +
            ' area 0.0.0.0  | sudo tee --append /etc/quagga/ospfd.conf')
        for line in stdout:
            print line.strip('\n')
        for line in stderr:
            print("Exceptions: ")
            print line.strip('\n')
        stdin, stdout, stderr = ssh.exec_command(
            'echo line vty | sudo tee --append /etc/quagga/ospfd.conf')

    #zeba.conf
    stdin, stdout, stderr = ssh.exec_command('sudo rm /etc/quagga/zebra.conf')
    stdin, stdout, stderr = ssh.exec_command(
        'echo hostname ' + instance_name +
        ' | sudo tee --append /etc/quagga/zebra.conf; echo password zebra | sudo tee --append /etc/quagga/zebra.conf; echo enable password zebra | sudo tee --append /etc/quagga/zebra.conf'
    )
    for line in stdout:
        print line.strip('\n')
    for line in stderr:
        print line.strip('\n')

    #services start
    stdin, stdout, stderr = ssh.exec_command(
        'sudo chown quagga.quaggavty /etc/quagga/*.conf; sudo chmod 640 /etc/quagga/*.conf; sudo /etc/init.d/quagga stop; sudo /etc/init.d/quagga start'
    )
    for line in stdout:
        print line.strip('\n')
    for line in stderr:
        print line.strip('\n')
Exemplo n.º 9
0
 def get_nova_intance(self):
     creds = get_nova_credentials()
     nvclient = Client(**creds)
     return nvclient