def test_create_vpn():
    with mock.patch('fokia.provisioner.astakos'), \
            mock.patch('fokia.provisioner.KamakiConfig'), \
            mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner(None, "lambda")
        provisioner.network_client.create_network = test_ip
        provisioner.reserve_ip('6ff62e8e-0ce9-41f7-ad99-13a18ecada5f')
def test_check_all_resources():
    with mock.patch('fokia.provisioner.astakos'), \
            mock.patch('fokia.provisioner.KamakiConfig'), \
            mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner(None, "lambda")
        provisioner.astakos.get_projects.return_value = test_projects
        provisioner.astakos.get_quotas.return_value = test_quotas
        provisioner.check_all_resources(test_quotas,
                                        project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f',
                                        slaves=2,
                                        cluster_size=3,
                                        vcpus=12,
                                        ram=4096 * 3,
                                        disk=180,
                                        ip_allocation=all,
                                        network_request=1)
def create_cluster(auth_token=None, master_name='lambda-master',
                   slaves=1, vcpus_master=4, vcpus_slave=4,
                   ram_master=4096, ram_slave=4096, disk_master=40, disk_slave=40,
                   ip_allocation='master', network_request=1, project_name='lambda.grnet.gr'):
    provisioner = Provisioner(auth_token=auth_token)
    provisioner.create_lambda_cluster(vm_name=master_name,
                                      slaves=slaves,
                                      vcpus_master=vcpus_master,
                                      vcpus_slave=vcpus_slave,
                                      ram_master=ram_master,
                                      ram_slave=ram_slave,
                                      disk_master=disk_master,
                                      disk_slave=disk_slave,
                                      ip_allocation=ip_allocation,
                                      network_request=network_request,
                                      project_name=project_name)

    provisioner_response = provisioner.get_cluster_details()
    master_id = provisioner_response['nodes']['master']['id']
    master_ip = provisioner.get_server_private_ip(master_id)
    provisioner_response['nodes']['master']['internal_ip'] = master_ip
    # slave_ids = [slave['id'] for slave in provisioner_response['nodes']['slaves']]
    for i, slave in enumerate(provisioner_response['nodes']['slaves']):
        slave_ip = provisioner.get_server_private_ip(slave['id'])
        provisioner_response['nodes']['slaves'][i]['internal_ip'] = slave_ip
    provisioner_response['pk'] = provisioner.get_private_key()

    ansible_manager = Manager(provisioner_response)
    ansible_manager.create_inventory()

    return ansible_manager, provisioner_response
def test_find_flavor():
    with mock.patch('fokia.provisioner.astakos'), \
            mock.patch('fokia.provisioner.KamakiConfig'), \
            mock.patch('fokia.provisioner.cyclades'):
        provisioner = Provisioner(None, "lambda")
        provisioner.astakos.get_projects.return_value = test_projects
        provisioner.cyclades.list_images.return_value = test_images
        provisioner.cyclades.list_flavors.return_value = test_flavors

        provisioner.create_vm(vm_name="tost", project_name="lambda.grnet.gr",
                              project_mode="supahpower", image_name="archlinux", net_id="12345",
                              flavor={'id': 3})
        provisioner.cyclades.create_server.assert_called_with(flavor_id=3,
                                                              image_id=u'c6f5adce-21ad-4ce3-8591-acfe7eb73c02',
                                                              name='tost',
                                                              networks=[
                                                                  {u'uuid': '12345'}],
                                                              personality=[],
                                                              project_id=u'6ff62e8e-0ce9-41f7-ad99-13a18ecada5f')
def create_lambda_instance(auth_token=None, master_name='lambda-master',
                           slaves=1, vcpus_master=4, vcpus_slave=4,
                           ram_master=4096, ram_slave=4096, disk_master=40, disk_slave=40,
                           ip_allocation='master', network_request=1,
                           project_name='lambda.grnet.gr'):
    start_time = time.time()

    provisioner = Provisioner(auth_token=auth_token)
    provisioner.create_lambda_cluster(vm_name=master_name,
                                      slaves=slaves,
                                      vcpus_master=vcpus_master,
                                      vcpus_slave=vcpus_slave,
                                      ram_master=ram_master,
                                      ram_slave=ram_slave,
                                      disk_master=disk_master,
                                      disk_slave=disk_slave,
                                      ip_allocation=ip_allocation,
                                      network_request=network_request,
                                      project_name=project_name)

    provisioner_response = provisioner.get_cluster_details()
    master_id = provisioner_response['nodes']['master']['id']
    master_ip = provisioner.get_server_private_ip(master_id)
    provisioner_response['nodes']['master']['internal_ip'] = master_ip
    # slave_ids = [slave['id'] for slave in provisioner_response['nodes']['slaves']]
    for i, slave in enumerate(provisioner_response['nodes']['slaves']):
        slave_ip = provisioner.get_server_private_ip(slave['id'])
        provisioner_response['nodes']['slaves'][i]['internal_ip'] = slave_ip
    provisioner_response['pk'] = provisioner.get_private_key()

    print 'response =', provisioner_response
    provisioner_time = time.time()

    ansible_manager = Manager(provisioner_response)
    ansible_manager.create_inventory()

    ansible_result = ansible_manager.run_playbook(
        playbook_file=script_path + "/../../ansible/playbooks/cluster-install.yml")

    ansible_manager.cleanup()

    provisioner_duration = provisioner_time - start_time
    ansible_duration = time.time() - provisioner_time

    print 'VM provisioning took', round(provisioner_duration), 'seconds'
    print 'Ansible playbooks took', round(ansible_duration), 'seconds'
    print 'Ansible result', ansible_result

    return ansible_result
def destroy_cluster(cloud_name, cluster_id):
    provisioner = Provisioner(cloud_name=cloud_name)
    details = get_cluster_details(cluster_id=cluster_id)
    if details is not None:
        provisioner.delete_lambda_cluster(details)