Example #1
0
def delete(context, vpn_id):
    """ Delete vpn network """
    vpn = VPN.get(context.user_id, vpn_id)

    task = Task()
    task.type = 'vpn'
    task.action = 'delete'
    task.append_to([vpn], broadcast=True)
Example #2
0
def test_to_dict_cache():
    from corecluster.cache.task import Task
    t = Task()
    t.set_prop('x', 1)
    t.set_prop('y', 2)
    d = t.to_dict
    assert 'data' in d
    assert 'type' in d
    assert 'action' in d
Example #3
0
def stop(context, network_id):
    """
    Stop DHCP servers in network
    """
    network = Subnet.get(context.user_id, network_id)
    if network.network_pool.mode != 'isolated':
        raise CoreException('network_not_isolated')

    task = Task()
    task.type = 'dhcp'
    task.action = 'stop_dhcp'
    task.append_to([network], broadcast=True)
Example #4
0
def test_get_task():
    global test_taskA
    global test_taskB

    from corecluster.cache.task import Task

    tasks = Task.get_task('queue_b', ['test_action'])

    assert len(tasks) == 0

    tasks = Task.get_task('queue_a', ['test_action'])
    assert len(tasks) > 0
    tasks[0].set_state('not active')
    tasks[0].save()
Example #5
0
def test_empty_queue():
    from corecluster.cache.task import Task

    while True:
        tasks = Task.get_task('test_task', ['test_action'])
        if len(tasks) > 0:
            tasks[0].delete()
        else:
            break
Example #6
0
def test_empty_queue():
    from corecluster.cache.task import Task
    global imageA
    global imageB
    imageA = None
    imageB = None

    while True:
        tasks = Task.get_task('queue_a', ['test_action'])
        if len(tasks) > 0:
            tasks[0].delete()
        else:
            break

    while True:
        tasks = Task.get_task('queue_b', ['test_action'])
        if len(tasks) > 0:
            tasks[0].delete()
        else:
            break
Example #7
0
def test_get_task_c():
    # Here, get_task should return only task C (A and B are removed)
    global test_taskC

    from corecluster.cache.task import Task

    tasks = Task.get_task('test_task', ['test_action'])
    assert len(tasks) > 0
    assert tasks[0].cache_key() == test_taskC.cache_key()

    tasks[0].delete()
Example #8
0
def test_get_task():
    # We have only test_taskA in queue
    global test_taskA

    from corecluster.cache.task import Task

    tasks = Task.get_task('test_task', ['test_action'])

    assert len(tasks) > 0
    assert tasks[0].cache_key() == test_taskA.cache_key()

    tasks[0].delete()
Example #9
0
def test_get_task_with_another_in_queue():
    # taskA is done, get_task should return taskB
    global test_taskB

    from corecluster.cache.task import Task

    tasks = Task.get_task('test_task', ['test_action'])

    assert len(tasks) > 0
    assert tasks[0].cache_key() == test_taskB.cache_key()

    tasks[0].delete()
Example #10
0
def test_set_prop_cache():
    from corecluster.cache.task import Task
    t = Task()
    t.set_prop('x', 1)
    t.set_prop('y', 2)
    assert t.get_prop('x') == 1
    assert t.get_prop('y') == 2

    t.set_all_props({'a': 1, 'b': 2})
    props = t.get_all_props()
    assert 'a' in props
    assert 'b' in props
    assert 'x' not in props
    assert 'y' not in props
def test_get_task_with_another_in_queue():
    # taskA is still not active and first in queue
    global test_taskA

    from corecluster.cache.task import Task

    tasks = Task.get_task('test_task', ['test_action'])

    assert len(tasks) > 0
    assert tasks[0].cache_key() == test_taskA.cache_key()

    tasks[0].set_state('not active')
    tasks[0].save()
Example #12
0
def test_add_task():
    global imageA
    global imageB
    global test_taskA
    from corecluster.cache.task import Task

    test_taskA = Task()
    test_taskA.type = 'test_task'
    test_taskA.state = 'not active'
    test_taskA.action = 'test_action'
    test_taskA.append_to([imageA, imageB])
Example #13
0
def test_add_second_task():
    global imageA
    global test_taskB
    from corecluster.cache.task import Task

    test_taskB = Task()
    test_taskB.type = 'test_task'
    test_taskB.state = 'not active'
    test_taskB.action = 'test_action'

    # New task is assigned only to imageA
    test_taskB.append_to([imageA])
Example #14
0
def start(context, network_id, gateway_ip):
    """
    Start DHCP server(s) in isolated network.
    :param network_id: Isolated network, which should be connected with new VPN server
    :param gateway_ip: Gateway ip for isolated network.
    """

    network = Subnet.get(context.user_id, network_id)
    if network.network_pool.mode != 'isolated':
        raise CoreException('network_not_isolated')

    network.set_prop('gateway', gateway_ip)
    network.save()

    if network.get_prop('dhcp_running', False):
        task = Task()
        task.type = 'dhcp'
        task.action = 'stop_dhcp'
        task.append_to([network], broadcast=True)

    task = Task()
    task.type = 'dhcp'
    task.action = 'start_dhcp'
    task.append_to([network])
Example #15
0
def create(context, name, network_id):
    """
    Create new vpn server attached to isolated network. There is no dhcp
    :param name: Network name
    :param network_id: Isolated network, which should be connected with new VPN server
    """

    network = Subnet.get(context.user_id, network_id)
    if network.network_pool.mode != 'isolated':
        raise CoreException('network_not_isolated')

    vpn = VPN()
    vpn.state = 'init'
    vpn.name = name
    vpn.network = Subnet.get(context.user_id, network_id)
    vpn.user_id = context.user_id
    vpn.save()

    task = Task()
    task.type = 'vpn'
    task.action = 'create'
    task.append_to([vpn, network])

    return vpn.to_dict
def test_add_task():
    global imageA
    global test_taskA
    from corecluster.cache.task import Task

    assert imageA.last_task == None

    test_taskA = Task()
    test_taskA.type = 'queue_a'
    test_taskA.state = 'not active'
    test_taskA.action = 'test_action'
    test_taskA.append_to([imageA])

    assert test_taskA.blockers == []
Example #17
0
def stop(context, network_id):
    """
    Stop DHCP servers in network
    """
    network = Subnet.get(context.user_id, network_id)
    if network.network_pool.mode != 'isolated':
        raise CoreException('network_not_isolated')

    task = Task()
    task.type = 'dhcp'
    task.action = 'stop_dhcp'
    task.append_to([network], broadcast=True)
def test_saved_task():
    global imageA
    global test_taskA
    from corecluster.cache.task import Task

    task_a = Task(cache_key=test_taskA.cache_key())
    assert hasattr(task_a, 'Image_module')
    assert hasattr(task_a, 'Image_id')
    assert task_a.blockers == []
    assert task_a.Image_module != None
    assert task_a.Image_id != None

    image = test_taskA.get_obj('Image')

    assert image != None
    assert image.id != None
    img_a = Image.objects.get(pk=imageA.id)
    assert img_a.last_task == test_taskB.cache_key()
Example #19
0
def test_append_third_task():
    # Append next task to empty queue
    global imageA
    global imageB
    global test_taskC

    from corecluster.cache.task import Task
    from corecluster.models.core import Image

    test_taskC = Task()
    test_taskC.type = 'test_task'
    test_taskC.state = 'not active'
    test_taskC.action = 'test_action'

    # New task is assigned only to imageA
    test_taskC.append_to([imageA])

    img_a = Image.objects.get(pk=imageA.id)

    assert img_a.last_task == test_taskC.cache_key()
Example #20
0
def attach_configdrive(context, userdata_id, vm_id):
    userdata = UserData.get(context.user_id, userdata_id)
    vm = VM.get(context.user_id, vm_id)

    system.call(['mkdir', '-p', '/tmp/configdrive_' + userdata.id + '/openstack/latest/'])
    ud = open('/tmp/configdrive_' + userdata.id + '/openstack/latest/user_data', 'w')
    ud.write(userdata.data)
    ud.close()

    # Detailed instruction: https://coreos.com/os/docs/latest/config-drive.html
    #TODO: Convert to: http://serverfault.com/questions/43634/how-to-mount-external-vfat-drive-as-user
    system.call(['genisoimage', '-R', '-V', 'config-2', '-o', '/tmp/configdrive_' + userdata.id + '.img', '/tmp/configdrive_' + userdata.id])
    system.call(['rm', '-rf', '/tmp/configdrive_' + userdata.id])
    system.call(['qemu-img', 'convert', '-f', 'raw', '-O', 'qcow2', '/tmp/configdrive_' + userdata.id + '.img', '/tmp/configdrive_' + userdata.id + '.qcow2'])
    system.call(['rm', '-rf', '/tmp/configdrive_' + userdata.id + '.img'])

    image = Image.create(name='CoreTalk ConfigDrive',
                         description='UserData: %s, VM: %s' % (userdata.id, vm.id),
                         access='private',
                         format='qcow2',
                         size=os.stat('/tmp/configdrive_' + userdata.id + '.qcow2').st_size,
                         type='permanent',
                         disk_controller='virtio',
                         user=vm.user)
    image.save()

    create_img = Task()
    create_img.user_id = context.user_id
    create_img.type = 'image'
    create_img.action = 'create'
    create_img.append_to([vm, image, image.storage])

    chunk = DataChunk()
    contents = open('/tmp/configdrive_' + userdata.id + '.qcow2').read(image.size)
    chunk.data = base64.b64encode(contents)
    chunk.offset = 0
    chunk.image_id = image.id
    chunk.type = 'upload'
    chunk.save()

    upload_data = Task()
    upload_data.user_id = context.user_id
    upload_data.type = 'image'
    upload_data.action = 'upload_data'
    upload_data.set_all_props({'offset': 0,
                               'size': image.size,
                               'chunk_id': chunk.cache_key()})
    upload_data.append_to([image, vm])

    attach_img = Task()
    attach_img.user_id = context.user_id
    attach_img.type = 'image'
    attach_img.action = 'attach'
    attach_img.append_to([vm, image])
Example #21
0
def test_add_task():
    global imageA
    global imageB
    global test_taskA
    global test_taskB
    from corecluster.cache.task import Task

    assert imageA.last_task == None
    assert imageB.last_task == None

    test_taskA = Task()
    test_taskA.type = 'queue_a'
    test_taskA.state = 'not active'
    test_taskA.action = 'test_action'
    test_taskA.append_to([imageA, imageB])

    assert test_taskA.blockers == []

    test_taskB = Task()
    test_taskB.type = 'queue_b'
    test_taskB.state = 'not active'
    test_taskB.action = 'test_action'
    test_taskB.append_to([imageA, imageB])

    assert test_taskB.blockers == [
        test_taskA.cache_key(), test_taskA.cache_key()
    ]
Example #22
0
def start(context, network_id, gateway_ip):
    """
    Start DHCP server(s) in isolated network.
    :param network_id: Isolated network, which should be connected with new VPN server
    :param gateway_ip: Gateway ip for isolated network.
    """

    network = Subnet.get(context.user_id, network_id)

    if network.network_pool.mode != 'isolated':
        raise CoreException('network_not_isolated')

    network.set_prop('gateway', gateway_ip)
    network.save()

    if network.get_prop('dhcp_running', False):
        task = Task()
        task.type = 'dhcp'
        task.action = 'stop_dhcp'
        task.append_to([network], broadcast=True)

    task = Task()
    task.type = 'dhcp'
    task.action = 'start_dhcp'
    task.append_to([network])
Example #23
0
def test_get_task_empty():
    # There is no more tasks in queue
    from corecluster.cache.task import Task

    tasks = Task.get_task('test_task', ['test_action'])
    assert len(tasks) == 0