Exemplo n.º 1
0
 async def send_notification_and_close(
         self, status_notification: StatusNotification):
     msg = json.dumps(dictify(status_notification, StatusNotification))
     print(msg)
     await self._websocket.send(msg)
     if self._stdout_listener is not None:
         self._stdout_listener.close()
     self._websocket.close()
Exemplo n.º 2
0
async def _wait_for_task_finish(vine_task: VineTask) -> Tuple[VineTaskStatus, int]:
    task_id = vine_task.task_id

    task_status, timestamp = _vine_task_status(task_id)

    while task_status.complete_date is None:
        await asyncio.sleep(1)
        task_status, timestamp = _vine_task_status(task_id)

    if task_status.failed:
        msg = ('ERROR! message: ' + task_status.message + ', errors: ' + str(task_status.errors))
        print(msg)
        print(json.dumps(dictify(task_status, VineTaskStatus), indent=4, sort_keys=True))
        raise Exception(msg)

    return task_status, timestamp
Exemplo n.º 3
0
async def _construct_immortals_testbed(
        listener: StatusHandler, testbed_name: str, testbed_desc: str,
        include_android: bool) -> StatusNotification:
    with _lock:
        status = ActivityStatus.TESTBED_INITIALIZING
        try:
            latest_images = vine._get_latest_matching_images(
                [DAS_PREDEPLOY_IMAGE_REGEX, ANDROID_BASE_IMAGE_REGEX])

            await listener.send(status, testbed_name,
                                'Creating empty testbed...')
            testbed = vine.create_testbed(testbed_name=testbed_name,
                                          testbed_desc=testbed_desc)
            await asyncio.sleep(10)
            await listener.send(status, testbed_name, 'Empty testbed created.')
            # testbed = vine.get_testbed(testbed_name)

            await listener.send(status, testbed_name, 'Starting DAS...')
            das_vm = await testbed.add_vm(
                vm_name=testbed_name + 'DAS',
                image_id=latest_images[DAS_PREDEPLOY_IMAGE_REGEX].image_id,
                flavor_id=3)
            await asyncio.sleep(10)
            await listener.send(status, testbed_name, 'DAS started.')

            if include_android:
                await listener.send(status, testbed_name,
                                    'Starting Android Emulator 00...')
                emu0_vm = await testbed.add_vm(
                    vm_name=testbed_name + 'Emulator00',
                    image_id=latest_images[ANDROID_BASE_IMAGE_REGEX].image_id,
                    flavor_id=16)
                await asyncio.sleep(10)
                await listener.send(status, testbed_name,
                                    'Android Emulator 00 started.')

                await listener.send(status, testbed_name,
                                    'Starting Android Emulator 01...')
                emu1_vm = await testbed.add_vm(
                    vm_name=testbed_name + 'Emulator01',
                    image_id=latest_images[ANDROID_BASE_IMAGE_REGEX].image_id,
                    flavor_id=16)
                await listener.send(status, testbed_name,
                                    'Android Emulator 01 started.')

            wait_time = 30
            await listener.send(
                status, testbed_name, "Waiting " + str(wait_time) +
                " seconds for things to settle...")
            await asyncio.sleep(wait_time)

            immortals_override_dict = {
                "deploymentEnvironment": {
                    "martiAddress": das_vm.vm_details.public_ip,
                }
            }

            if include_android:
                immortals_override_dict["deploymentEnvironment"][
                    "androidEnvironments"] = [{
                        "adbIdentifier":
                        emu0_vm.vm_details.public_ip + ":5432",
                        "adbPort":
                        5432,
                        "adbUrl":
                        emu0_vm.vm_details.public_ip,
                        "environmentDetails": {
                            "androidVersion":
                            21,
                            "externallyAccessibleUrls":
                            ["dropbox.com:443", "dropbox.com:80"],
                            "uploadBandwidthLimitKilobitsPerSecond":
                            800
                        }
                    }, {
                        "adbIdentifier":
                        emu1_vm.vm_details.public_ip + ":5432",
                        "adbPort":
                        5432,
                        "adbUrl":
                        emu1_vm.vm_details.public_ip,
                        "environmentDetails": {
                            "androidVersion": 21
                        }
                    }]

            tmp_filepath = str(uuid4()) + '-tmp_immortals_override.json'

            open(tmp_filepath, 'w').write(
                json.dumps(immortals_override_dict, indent=4, sort_keys=True))
            await das_vm.copy_file(tmp_filepath,
                                   '~/immortals_override_file.json')
            await das_vm.exec_command([
                '\'echo "export IMMORTALS_OVERRIDE_FILE=~/immortals_override_file.json" >> ${HOME}/.bashrc\''
            ])

            await das_vm.exec_command([
                '\'echo "export IMMORTALS_ENV_PROFILE=vine" >> ${HOME}/.bashrc\''
            ])

            os.remove(tmp_filepath)

            testbed._update_vms_and_gateways()

            dtb = MutableDasTestbed(ActivityStatus.TESTBED_READY,
                                    testbed=testbed)

            return StatusNotification(status=dtb.status,
                                      testbed_name=testbed_name,
                                      message="Testbed ready for use.",
                                      data=dictify(dtb, MutableDasTestbed))

        except Exception as e:
            traceback.print_exc()
            msg = str(e) + "\n" + traceback.format_exc()
            raise BuildServiceException(ActivityStatus.SERVER_ERROR,
                                        testbed_name, msg)
Exemplo n.º 4
0
async def handler(websocket: WebSocketServerProtocol, path: str):
    # TODO: Add testbed name verification server-side
    # noinspection PyBroadException
    try:
        print("PATH: " + path)

        req = parse.urlparse(path)

        if req.path == '/stdoutListener':
            websocket_identifier = str(uuid.uuid4())
            _stdout_sockets[websocket_identifier] = websocket
            await websocket.send(websocket_identifier)
            await websocket.wait_for_connection_lost()

        params = parse.parse_qsl(req.query)

        method_params = dict()

        for p in params:
            if p[0] == 'include_android':
                method_params[p[0]] = p[1] == 'True'
            else:
                method_params[p[0]] = p[1]

        if 'stdout_identifier' in method_params:
            stdout_socket = _stdout_sockets[method_params['stdout_identifier']]
            listener = WebsocketStatusHandler(websocket,
                                              stdout_listener=stdout_socket)
            method_params.pop('stdout_identifier')
        else:
            listener = WebsocketStatusHandler(websocket)

        method_params['listener'] = listener

        # noinspection PyBroadException
        try:
            if req.path == '/claimBuildpoolPlainTestbed':
                notification = await testbedmanager.claim_buildsystem_plain_testbed(
                    **method_params)
                await listener.send_notification_and_close(notification)

            if req.path == '/claimBuildpoolAndroidTestbed':
                notification = await testbedmanager.claim_buildsystem_android_testbed(
                    **method_params)
                await listener.send_notification_and_close(notification)

            if req.path == '/createTestbed':
                notification = await testbedmanager.construct_unmanaged_testbed(
                    **method_params)
                await listener.send_notification_and_close(notification)

            elif req.path == '/deleteTestbed':
                notification = await testbedmanager.delete_testbed(
                    **method_params)
                await listener.send_notification_and_close(notification)

            elif req.path == '/addPlainTestbedToBuildPool':
                notification = await testbedmanager.add_buildsystem_testbed_to_pool(
                    listener, False)
                await listener.send_notification_and_close(notification)

            elif req.path == '/addAndroidTestbedToBuildPool':
                notification = await testbedmanager.add_buildsystem_testbed_to_pool(
                    listener, True)
                await listener.send_notification_and_close(notification)

            elif req.path == '/replaceTestbedInBuildPool':
                method_params['validate'] = True
                notification = await testbedmanager.destroy_and_trigger_testbed_replacement(
                    **method_params)
                await listener.send_notification_and_close(notification)

            elif req.path == '/replaceTestbedInBuildPoolNoWait':
                method_params['validate'] = False
                notification = await testbedmanager.destroy_and_trigger_testbed_replacement(
                    **method_params)
                await listener.send_notification_and_close(notification)

            elif req.path == '/updateRepo':
                testbed = testbedmanager.get_testbed(
                    method_params['testbed_name'])
                branch = None if 'branch' not in method_params else method_params[
                    'branch']
                notification = await testbed.das_repo_update(listener, branch)
                await listener.send_notification_and_close(notification)

            elif req.path == '/dasDeploy':
                testbed = testbedmanager.get_testbed(
                    method_params['testbed_name'])
                branch = None if 'branch' not in method_params else method_params[
                    'branch']
                cp_profile = None if 'cp_profile' not in method_params else method_params[
                    'cp_profile']
                notification = await testbed.das_deploy(
                    listener, cp_profile, branch)
                await listener.send_notification_and_close(notification)

            elif req.path == '/dasExecuteTest':
                testbed = testbedmanager.get_testbed(
                    method_params['testbed_name'])
                branch = None if 'branch' not in method_params else method_params[
                    'branch']
                cp_profile = None if 'cp_profile' not in method_params else method_params[
                    'cp_profile']
                await testbed.das_execute_test(
                    listener, method_params['test_identifier'], cp_profile,
                    branch)

            elif req.path == '/getExistingTestbed':
                testbed = testbedmanager.get_testbed(
                    method_params['testbed_name'])
                testbed_d = dictify(testbed, DasTestbed)
                await listener.send_and_close(testbed.status,
                                              method_params['testbed_name'],
                                              "Fetched testbed",
                                              data=testbed_d)
            #
            # elif req.path == '/rebuildPredeployImage':
            #     notification = await vh.rebuild_predeploy_das_image(**method_params)
            #     await listener.send_notification_and_close(notification)

        except BuildServiceException as be:
            await listener.send_notification_and_close(be)
        except Exception:
            traceback.print_exc()
            await listener.send_and_close(
                ActivityStatus.SERVER_ERROR, "null",
                "An unexpected exception has occurred trying to handle " +
                " the request. Please see the server logs for details.")

    except Exception:
        traceback.print_exc()
        websocket.send(
            json.dumps({
                "status":
                "SERVER_ERROR",
                "testbed_name":
                "null",
                "message":
                "An error occured trying to route the request. Please see the server logs for details!"
            }))
        websocket.close(
            reason=
            "An error occured trying to route the request. Please see the server logs for details!"
        )
Exemplo n.º 5
0
 async def send_notification(self, status_notification: StatusNotification):
     msg = json.dumps(dictify(status_notification, StatusNotification))
     print(msg)
     await self._websocket.send(msg)