예제 #1
0
 def __init__(self, netinfo, connection=None):
     self.netinfo = netinfo
     if connection is None:
         self.connection = libvirtutil.LibvirtUtil()
     else:
         self.connection = connection
     self.locks = []
예제 #2
0
def action(networkid, publicip, publicgwip, publiccidr, password, vlan, privatenetwork):
    from CloudscalerLibcloud.utils import libvirtutil
    from CloudscalerLibcloud.utils.network import Network, NetworkTool
    import pexpect
    import netaddr
    import jinja2
    import time
    import os

    hrd = j.atyourservice.get(name='vfwnode', instance='main').hrd
    netrange = hrd.get("instance.vfw.netrange.internal")
    defaultpasswd = hrd.get("instance.vfw.admin.passwd")
    username = hrd.get("instance.vfw.admin.login")
    newpassword = hrd.get("instance.vfw.admin.newpasswd")
    destinationfile = None

    data = {'nid': j.application.whoAmI.nid,
            'gid': j.application.whoAmI.gid,
            'username': username,
            'password': newpassword
            }


    networkidHex = '%04x' % int(networkid)
    internalip = str(netaddr.IPAddress(netaddr.IPNetwork(netrange).first + int(networkid)))
    privatenet = netaddr.IPNetwork(privatenetwork)
    name = 'routeros_%s' % networkidHex

    j.clients.redisworker.execFunction(cleanup, _queue='hypervisor', name=name,
                                       networkid=networkid)
    print 'Testing network'
    if not j.system.net.tcpPortConnectionTest(internalip, 22, 1):
        print "OK no other router found."
    else:
        raise RuntimeError("IP conflict there is router with %s" % internalip)

    connection = libvirtutil.LibvirtUtil()
    network = Network(connection)
    netinfo = [{'type': 'vlan', 'id': vlan}, {'type': 'vxlan', 'id': networkid}]
    try:
        templatepath = '/var/lib/libvirt/images/routeros/template/routeros.qcow2'
        destination = '/var/lib/libvirt/images/routeros/%s/' % networkidHex
        destinationfile = os.path.join(destination, 'routeros.qcow2')
        print 'Creating image snapshot %s -> %s' % (templatepath, destination)
        if j.system.fs.exists(destinationfile):
            raise RuntimeError("Path %s already exists" % destination)
        j.system.fs.createDir(destination)
        j.system.fs.copyFile(templatepath, destinationfile)

        imagedir = j.system.fs.joinPaths(j.dirs.baseDir, 'apps/routeros/template/')
        xmltemplate = jinja2.Template(j.system.fs.fileGetContents(j.system.fs.joinPaths(imagedir, 'routeros-template.xml')))

        with NetworkTool(netinfo, connection):
            # setup network vxlan
            print('Creating network')
            bridgename = j.system.ovsnetconfig.getVlanBridge(vlan)
            xmlsource = xmltemplate.render(networkid=networkidHex, destinationfile=destinationfile, publicbridge=bridgename)

            print 'Starting VM'
            try:
                domuuid = j.clients.redisworker.execFunction(createVM, _queue='hypervisor', xml=xmlsource, _timeout=180)
            except Exception, e:
                raise RuntimeError("Could not create VFW vm from template, network id:%s:%s\n%s" % (networkid, networkidHex, e))
        print 'Protect network'
        domain = connection.get_domain_obj(domuuid)
        network.protect_external(domain, publicip)
        network.protect_gwmgmt(domain, internalip)

        data['internalip'] = internalip
        run = pexpect.spawn("virsh console %s" % name, timeout=300)
        try:
            print "Waiting to attach to console"
            run.expect("Connected to domain", timeout=10)
            run.sendline()  # first enter to clear welcome message of kvm console
            print 'Waiting for Login'
            run.expect("Login:"******"Password:"******"\] >", timeout=120)  # wait for primpt
            run.send("/ip addr add address=%s/22 interface=ether3\r\n" % internalip)
            print 'waiting for end of command'
            run.expect("\] >", timeout=10)  # wait for primpt
            run.send("/quit\r\n")
            run.expect("Login:"******"Could not set internal ip on VFW, network id:%s:%s\n%s" % (networkid, networkidHex, e))
예제 #3
0
 def __init__(self, libvirtcon=None):
     if libvirtcon:
         self.libvirtutil = libvirtcon
     else:
         self.libvirtutil = libvirtutil.LibvirtUtil()