def _determine_status(statuses, repo_id, is_demo): """ Determine the general status given the actual two task statuses of the cloud and Explora tasks. For example, as long as one task is deploying/shutting down/shut down, both are considered deploying/shutting down/shut down. Args: statuses (list): The two task statuses of the cloud and Explora tasks. repo_id (str): The repo ID of the repo associated with this task. is_demo (bool): Boolean for whether this repo is a demo repo or not. Returns: str: The general status describing the state of both the tasks. """ if any([status == ERROR_STATUS for status in statuses]): return "ERROR" elif any([status in SHUTTING_DOWN_STATUSES for status in statuses]): return "SHUTTING DOWN" elif any([status in DEPLOYING_STATUSES for status in statuses]): return "DEPLOYING" elif all([status == RUNNING_STATUS for status in statuses]): demo_domain = _get_demo_cloud_domain() cloud_node_url = DEMO_CLOUD_DOMAIN.format(demo_domain) \ if is_demo else CLOUD_SUBDOMAIN.format(repo_id) # TODO: Add HTTPS to cloud node cloud_response = requests.get("http://{0}/status/{1}".format( cloud_node_url, repo_id)) status_data = cloud_response.json() return "ACTIVE" if status_data["Busy"] else "AVAILABLE" else: statuses = str(statuses) raise Exception("Found an unexpected set of statuses: {statuses}")
def delete_demo_node(): """ Deletes the demo cloud node. """ demo_domain = _get_demo_cloud_domain() full_domain = DEMO_CLOUD_DOMAIN.format(demo_domain) with open("demo_cloud_details.json", "r") as f: details = json.load(f) ip_address = details["CloudIpAddress"] task_arn = details["CloudTaskArn"] _stop_task(task_arn, full_domain, ip_address)
def reset_cloud_node(repo_id, is_demo): """ Reset the cloud node with the given repo ID. Args: repo_id (str): The repo ID of the repo associated with this task. is_demo (bool): Boolean for whether this repo is a demo repo or not. """ demo_domain = _get_demo_cloud_domain() cloud_node_url = DEMO_CLOUD_DOMAIN.format(demo_domain) \ if is_demo else CLOUD_SUBDOMAIN.format(repo_id) # TODO: Add HTTPS to cloud node cloud_response = requests.get("http://{0}/status/{1}".format( cloud_node_url, repo_id))
def create_demo_node(): """ Create the demo cloud node. """ name = "cloud" details = TASK_DETAILS[name] task_definition, container_name, subdomain = details demo_domain = _get_demo_cloud_domain() full_domain = DEMO_CLOUD_DOMAIN.format(demo_domain) demo_api_key = _get_demo_api_key() task_arn, ip_address = _run_new_task(task_definition, container_name, \ demo_domain, demo_api_key, "", CLOUD_SUBDOMAIN) results = {"CloudIpAddress": ip_address, "CloudTaskArn": task_arn} with open("demo_cloud_details.json", "w") as f: json.dump(results, f) return results
def _run_new_task(task_definition, container_name, repo_id, api_key, \ token, subdomain): """ Run new task in the provided cluster with the provided task definition. Args: task_definition (str): Task definition of the task to be run. container_name (str): Name of the Docker container to run in the task. repo_id (str): The repo ID of the repo associated with this task. api_key (str): The corresponding API key of the repo. Used for auth in the tasks. subdomain (str): The name of the subdomain to create the domain at. Returns: (str, str): The task ARN and public IP address of the newly created task. """ new_task_response = ecs_client.run_task( cluster=CLUSTER_NAME, taskDefinition=task_definition, launchType="FARGATE", networkConfiguration={ "awsvpcConfiguration": { "subnets": ["subnet-066927cc2aa7d231f"], "assignPublicIp": "ENABLED", } }, overrides={ "containerOverrides": [{ "name": container_name, "environment": [{ "name": "REPO_ID", "value": repo_id }, { "name": "DEMO_REPO_ID", "value": _get_demo_cloud_domain(), }, { "name": "API_KEY", "value": api_key, }, { "name": "DEMO_API_KEY", "value": _get_demo_api_key(), }, { "name": "TOKEN", "value": token, }] }] }) if new_task_response["failures"]: raise Exception(str(new_task_response["failures"])) task_arn = new_task_response["tasks"][0]["taskArn"] network_interface_id = _get_network_interface_id(task_arn) ip_address = _get_public_ip(network_interface_id) full_domain = subdomain.format(repo_id) _modify_domain("CREATE", full_domain, ip_address) return task_arn, ip_address