def launch_emulator(request): user_id = request.user.id platform = request.POST['platform'] oauth = request.POST['token'] tz_offset = request.POST['tz_offset'] versions = { 'aplite': '2.9', 'basalt': '3.0', } version = versions[platform] redis_key = 'qemu-user-%s-%s' % (user_id, platform) qemu_instance = redis_client.get(redis_key) if qemu_instance is not None: qemu_instance = json.loads(qemu_instance) try: response = requests.post(qemu_instance['ping_url'], timeout=2, verify=settings.COMPLETION_CERTS) response.raise_for_status() response = response.json() except (requests.RequestException, ValueError) as e: print "couldn't fetch old instance: %s" % e pass else: if response.get('alive', False): return json_response(qemu_instance) else: print "old instance is dead." token = _generate_token() servers = set(settings.QEMU_URLS) while len(servers) > 0: server = random.choice(list(servers)) servers.remove(server) try: result = requests.post(server + 'qemu/launch', data={'token': token, 'platform': platform, 'version': version, 'oauth': oauth, 'tz_offset': tz_offset}, headers={'Authorization': settings.QEMU_LAUNCH_AUTH_HEADER}, timeout=15, verify=settings.COMPLETION_CERTS) result.raise_for_status() response = result.json() url = urlparse.urlsplit(server) response['host'] = url.hostname response['secure'] = (url.scheme == 'https') response['api_port'] = url.port or (443 if url.scheme == 'https' else 80) response['ping_url'] = '%sqemu/%s/ping' % (server, response['uuid']) response['kill_url'] = '%sqemu/%s/kill' % (server, response['uuid']) response['token'] = token redis_client.set(redis_key, json.dumps(response)) return json_response(response) except requests.HTTPError as e: print e.response.text except (requests.RequestException, ValueError) as e: print e pass return json_failure("Unable to create emulator instance.")
def launch_emulator(request): user_id = request.user.id platform = request.POST['platform'] oauth = request.POST['token'] tz_offset = request.POST['tz_offset'] versions = { 'aplite': '3.0', 'basalt': '3.0', 'chalk': '3.0', 'diorite': '3.0', 'emery': '3.0', } version = versions[platform] redis_key = 'qemu-user-%s-%s' % (user_id, platform) qemu_instance = redis_client.get(redis_key) if qemu_instance is not None: qemu_instance = json.loads(qemu_instance) try: response = requests.post(qemu_instance['ping_url'], timeout=2) response.raise_for_status() response = response.json() except (requests.RequestException, ValueError) as e: logger.info("couldn't fetch old instance: %s", e) else: if response.get('alive', False): return qemu_instance else: logger.info("old instance is dead.") token = _generate_token() servers = set(settings.QEMU_URLS) while len(servers) > 0: server = random.choice(list(servers)) servers.remove(server) try: result = requests.post(server + 'qemu/launch', data={'token': token, 'platform': platform, 'version': version, 'oauth': oauth, 'tz_offset': tz_offset}, headers={'Authorization': settings.QEMU_LAUNCH_AUTH_HEADER}, timeout=settings.QEMU_LAUNCH_TIMEOUT) result.raise_for_status() response = result.json() url = urlparse.urlsplit(server) response['host'] = url.hostname response['secure'] = (url.scheme == 'https') response['api_port'] = url.port or (443 if url.scheme == 'https' else 80) response['ping_url'] = '%sqemu/%s/ping' % (server, response['uuid']) response['kill_url'] = '%sqemu/%s/kill' % (server, response['uuid']) response['token'] = token redis_client.set(redis_key, json.dumps(response)) return response except requests.HTTPError as e: logger.warning("Got HTTP error from QEMU launch. Content:\n%s", e.response.text) except (requests.RequestException, ValueError) as e: logger.error("Error launching qemu: %s", e) raise InternalServerError(_("Unable to create emulator instance."))
def handle_phone_token(request, token): redis_key = 'qemu-phone-token-%s' % token qemu = redis_client.get(redis_key) if qemu is not None: qemu = json.loads(qemu) return render(request, "ide/qemu-sensors.html", { 'url': qemu['url'], 'token': qemu['token'], }) else: return render(request, "ide/qemu-enter-token.html", {'failed': True})
def handle_phone_token(request, token): redis_key = 'qemu-phone-token-%s' % token qemu = redis_client.get(redis_key) if qemu is not None: qemu = json.loads(qemu) return render(request, "ide/qemu-sensors.html", { 'url': qemu['url'], 'token': qemu['token'], }) else: return render(request, "ide/qemu-enter-token.html", { 'failed': True })
def launch_emulator(request): user_id = request.user.id platform = request.POST['platform'] oauth = request.POST['token'] tz_offset = request.POST['tz_offset'] versions = { 'aplite': '3.0', 'basalt': '3.0', 'chalk': '3.0', } version = versions[platform] redis_key = 'qemu-user-%s-%s' % (user_id, platform) qemu_instance = redis_client.get(redis_key) if qemu_instance is not None: qemu_instance = json.loads(qemu_instance) try: response = requests.post(qemu_instance['ping_url'], timeout=2, verify=settings.COMPLETION_CERTS) response.raise_for_status() response = response.json() except (requests.RequestException, ValueError) as e: logger.info("couldn't fetch old instance: %s", e) else: if response.get('alive', False): return qemu_instance else: logger.info("old instance is dead.") token = _generate_token() servers = set(settings.QEMU_URLS) while len(servers) > 0: server = random.choice(list(servers)) servers.remove(server) try: result = requests.post( server + 'qemu/launch', data={ 'token': token, 'platform': platform, 'version': version, 'oauth': oauth, 'tz_offset': tz_offset }, headers={'Authorization': settings.QEMU_LAUNCH_AUTH_HEADER}, timeout=settings.QEMU_LAUNCH_TIMEOUT, verify=settings.COMPLETION_CERTS) result.raise_for_status() response = result.json() url = urlparse.urlsplit(server) response['host'] = url.hostname response['secure'] = (url.scheme == 'https') response['api_port'] = url.port or (443 if url.scheme == 'https' else 80) response['ping_url'] = '%sqemu/%s/ping' % (server, response['uuid']) response['kill_url'] = '%sqemu/%s/kill' % (server, response['uuid']) response['token'] = token redis_client.set(redis_key, json.dumps(response)) return response except requests.HTTPError as e: logger.warning("Got HTTP error from QEMU launch. Content:\n%s", e.response.text) except (requests.RequestException, ValueError) as e: logger.error("Error launching qemu: %s", e) raise InternalServerError(_("Unable to create emulator instance."))
def check_phone(request, request_id): ip = redis_client.get('phone-ip-{0}'.format(request_id)) if ip is None: return {'pending': True} else: return {'pending': False, 'response': json.loads(ip)}
def check_phone(request, request_id): ip = redis_client.get("phone-ip-{0}".format(request_id)) if ip is None: return json_response({"pending": True}) else: return json_response({"pending": False, "response": json.loads(ip)})
def check_phone(request, request_id): ip = redis_client.get('phone-ip-{0}'.format(request_id)) if ip is None: return json_response({'pending': True}) else: return json_response({'pending': False, 'response': json.loads(ip)})