-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added files from pracs and reformatted out of app/
- Loading branch information
Showing
23 changed files
with
338 additions
and
11 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
FROM --platform=linux/amd64 ubuntu:22.04 | ||
|
||
ENV SQLALCHEMY_DATABASE_URI=sqlite:///:memory: | ||
|
||
# Installing dependencies and cleaning up | ||
RUN apt-get update && \ | ||
apt-get install -y python3 python3-pip postgresql-client libpq-dev libcurl4-openssl-dev libssl-dev && \ | ||
apt-get clean && \ | ||
rm -rf /var/lib/apt/lists/* | ||
# Install pipenv | ||
RUN pip3 install poetry | ||
# Setting the working directory | ||
WORKDIR /app | ||
# Install pipenv dependencies | ||
COPY pyproject.toml . | ||
RUN poetry install --no-root | ||
# Copying our application into the container | ||
COPY bin bin | ||
COPY spamoverflow spamoverflow | ||
|
||
# Running our application | ||
ENTRYPOINT ["/app/bin/docker-entrypoint"] | ||
CMD ["serve"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
resource "aws_appautoscaling_target" "spamoverflow" { | ||
max_capacity = 4 | ||
min_capacity = 1 | ||
resource_id = "service/spamoverflow/spamoverflow" | ||
scalable_dimension = "ecs:service:DesiredCount" | ||
service_namespace = "ecs" | ||
|
||
depends_on = [ aws_ecs_service.taskoverflow ] | ||
} | ||
|
||
|
||
resource "aws_appautoscaling_policy" "spamoverflow-cpu" { | ||
name = "spamoverflow-cpu" | ||
policy_type = "TargetTrackingScaling" | ||
resource_id = aws_appautoscaling_target.todo.resource_id | ||
scalable_dimension = aws_appautoscaling_target.todo.scalable_dimension | ||
service_namespace = aws_appautoscaling_target.todo.service_namespace | ||
|
||
target_tracking_scaling_policy_configuration { | ||
predefined_metric_specification { | ||
predefined_metric_type = "ECSServiceAverageCPUUtilization" | ||
} | ||
|
||
target_value = 20 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
# If there is more than one argument then return the help | ||
if [ $# -gt 1 ]; then | ||
echo "Usage: docker-entrypoint [COMMAND]" | ||
echo " COMMAND: The command to run. If not specified, the default command is used." | ||
exit 1 | ||
fi | ||
|
||
# If the first argument is serve then run the default command | ||
if [ $# -eq 0 ] || [ "$1" = "serve" ]; then | ||
poetry run python3 bin/wait_for_db.py | ||
|
||
exec poetry run gunicorn --bind 0.0.0.0:6400 'spamoverflow:create_app()' | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import time | ||
import os | ||
import sqlalchemy | ||
|
||
def wait_for_db(db_url, retries=6, timeout=15): | ||
"""Wait for the database to be available. | ||
:param db_url: the database URL | ||
:param timeout: the maximum number of seconds to wait | ||
""" | ||
engine = sqlalchemy.create_engine(db_url, connect_args={'connect_timeout': 30}) | ||
for i in range(retries): | ||
try: | ||
engine.connect() | ||
return | ||
except sqlalchemy.exc.OperationalError: | ||
print(f"Waiting for the database to be available ({i+1}/{retries})") | ||
time.sleep(timeout) | ||
raise RuntimeError("Timeout waiting for the database") | ||
|
||
if __name__ == "__main__": | ||
# Get SQLIALCHEMY_DATABASE_URI from the environment | ||
db_url = os.environ["SQLALCHEMY_DATABASE_URI"] | ||
if "sqlite" in db_url: | ||
exit() | ||
wait_for_db(db_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
resource "aws_db_instance" "database" { | ||
allocated_storage = 20 | ||
max_allocated_storage = 1000 | ||
engine = "postgres" | ||
engine_version = "14" | ||
instance_class = "db.t4g.micro" | ||
db_name = "todo" | ||
username = local.database_username | ||
password = local.database_password | ||
parameter_group_name = "default.postgres14" | ||
skip_final_snapshot = true | ||
vpc_security_group_ids = [aws_security_group.database.id] | ||
publicly_accessible = true | ||
} | ||
|
||
resource "aws_security_group" "database" { | ||
name = "spamoverflow-database" | ||
description = "Allow inbound Postgres traffic" | ||
|
||
ingress { | ||
from_port = 5432 | ||
to_port = 5432 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
ipv6_cidr_blocks = ["::/0"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
resource "aws_ecs_cluster" "spamoverflow" { | ||
name = "taskoverflow" | ||
} | ||
|
||
resource "aws_ecs_task_definition" "spamoverflow" { | ||
family = "spamoverflow" | ||
network_mode = "awsvpc" | ||
requires_compatibilities = ["FARGATE"] | ||
cpu = 1024 | ||
memory = 2048 | ||
execution_role_arn = data.aws_iam_role.lab.arn | ||
|
||
container_definitions = jsonencode([ | ||
{ | ||
image = "${aws_ecr_repository.spamoverflow.repository_url}:latest" | ||
cpu = 1024 | ||
memory = 2048 | ||
name = "spamoverflow-app" | ||
networkMode = "awsvpc" | ||
portMappings = [ | ||
{ | ||
containerPort = 6400 | ||
hostPort = 6400 | ||
} | ||
] | ||
environment = [ | ||
{ | ||
name = "SQLALCHEMY_DATABASE_URI" | ||
value = "postgresql://${local.database_username}:${local.database_password}@${aws_db_instance.spamoverflow_database.address}:${aws_db_instance.spamoverflow_database.port}/${aws_db_instance.spamoverflow_database.db_name}" | ||
} | ||
] | ||
logConfiguration = { | ||
logDriver = "awslogs" | ||
options = { | ||
"awslogs-group" = "/spamoverflow/todo" | ||
"awslogs-region" = "us-east-1" | ||
"awslogs-stream-prefix" = "ecs" | ||
"awslogs-create-group" = "true" | ||
} | ||
} | ||
} | ||
]) | ||
} | ||
|
||
resource "aws_ecs_service" "spamoverflow" { | ||
name = "spamoverflow-ecs" | ||
cluster = aws_ecs_cluster.spamoverflow.id | ||
task_definition = aws_ecs_task_definition.todo.arn | ||
desired_count = 1 | ||
launch_type = "FARGATE" | ||
|
||
network_configuration { | ||
subnets = data.aws_subnets.private.ids | ||
security_groups = [aws_security_group.todo.id] | ||
assign_public_ip = true | ||
} | ||
|
||
load_balancer { | ||
target_group_arn = aws_lb_target_group.todo.arn | ||
container_name = "spamoverflow" | ||
container_port = 6400 | ||
} | ||
|
||
} | ||
|
||
resource "aws_security_group" "spamoverflow" { | ||
name = "spamoverflow" | ||
description = "SpamoverFloww Security Group" | ||
|
||
ingress { | ||
from_port = 6400 | ||
to_port = 6400 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
ingress { | ||
from_port = 22 | ||
to_port = 22 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
resource "aws_lb_target_group" "spamoverflow" { | ||
name = "spamoverflow" | ||
port = 6400 | ||
protocol = "HTTP" | ||
vpc_id = aws_security_group.spamoverflow.vpc_id | ||
target_type = "ip" | ||
|
||
health_check { | ||
path = "/api/v1/health" | ||
port = "6400" | ||
protocol = "HTTP" | ||
healthy_threshold = 2 | ||
unhealthy_threshold = 2 | ||
timeout = 5 | ||
interval = 10 | ||
} | ||
} | ||
|
||
|
||
resource "aws_lb" "spamoverflow" { | ||
name = "spamoverflow" | ||
internal = false | ||
load_balancer_type = "application" | ||
subnets = data.aws_subnets.private.ids | ||
security_groups = [aws_security_group.spamoverflow.id] | ||
} | ||
|
||
resource "aws_security_group" "spamoverflow" { | ||
name = "spamoverflow" | ||
description = "spamoverflow Security Group" | ||
|
||
ingress { | ||
from_port = 80 | ||
to_port = 80 | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
|
||
egress { | ||
from_port = 0 | ||
to_port = 0 | ||
protocol = "-1" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
//Listener aka entrypoint to the load balancer | ||
resource "aws_lb_listener" "spamoverflow" { | ||
load_balancer_arn = aws_lb.spamoverflow.arn | ||
port = "80" | ||
protocol = "HTTP" | ||
|
||
default_action { | ||
type = "forward" | ||
target_group_arn = aws_lb_target_group.spamoverflow.arn | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
#!/bin/bash | ||
|
||
#Change to the app folder | ||
cd app | ||
|
||
#Buildkit to make sure we know what env | ||
export DOCKER_BUILDKIT=1 | ||
|
||
|
Oops, something went wrong.