From b3c7b9da1e973c8cf9e80fa157ae973043b09cbb Mon Sep 17 00:00:00 2001 From: Isaac Hunter Date: Sat, 2 Mar 2024 15:23:48 -0500 Subject: [PATCH] configure prod docker-compose, db issues, created automation for env variables --- .vscode/settings.json | 7 +++ Dockerfile | 2 +- deploy/ec2.tf | 46 +++++++++++++++++-- .../{server-setup.sh => server-setup.sh.tpl} | 7 ++- deploy/variables.tf | 32 +++++++++++-- docker-compose.prod.yml | 22 +++++---- proxy/Dockerfile | 2 +- proxy/{default.conf => default.conf.tpl} | 2 +- src/routes/index.ts | 2 +- 9 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json rename deploy/templates/ec2/{server-setup.sh => server-setup.sh.tpl} (85%) rename proxy/{default.conf => default.conf.tpl} (93%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8b3445c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "files.associations": { + "*.sh.tpl": "shellscript", + "*.json.tpl": "JSON", + "*.conf.tpl": "NGINX Conf" + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 090ef1a..ef586b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,7 +29,7 @@ COPY ./src /app/src RUN npm run build -EXPOSE 80 +EXPOSE 8000 VOLUME ["/app/src"] CMD ["npm", "start"] diff --git a/deploy/ec2.tf b/deploy/ec2.tf index e201ff3..c856711 100644 --- a/deploy/ec2.tf +++ b/deploy/ec2.tf @@ -9,6 +9,21 @@ is the easiest resource to create in AWS, and will only need: ########################################## # EC2 INSTANCE - Host Server Application # ########################################## +# data "template_file" "server_init_script" { +# template = file("./templates/ec2/server-setup.sh") + +# vars = { +# NODE_ENV = var.server_env["NODE_ENV"] +# PORT = var.server_env["PORT"] +# HOST = var.server_env["HOST"] +# JWT_SECRET_KEY = var.server_env["JWT_SECRET_KEY"] +# TOKEN_HEADER_KEY = var.server_env["TOKEN_HEADER_KEY"] +# MONGO_URI = var.server_env["MONGO_URI"] +# SP_ID = var.server_env["SP_ID"] +# SP_SECRET = var.server_env["SP_SECRET"] +# } +# } + data "aws_ami" "amzn_linux_2" { most_recent = true owners = ["amazon"] @@ -26,13 +41,24 @@ data "aws_ami" "amzn_linux_2" { resource "aws_instance" "jukebox_server" { - ami = data.aws_ami.amzn_linux_2.id - instance_type = "t3.micro" - user_data = file("./templates/ec2/server-setup.sh") + ami = data.aws_ami.amzn_linux_2.id + instance_type = "t3.micro" + # user_data = file("./templates/ec2/server-setup.sh") + # user_data = data.template_file.server_init_script.rendered + user_data = templatefile("${path.module}/templates/ec2/server-setup.sh.tpl", { env = { + NODE_ENV = var.SERVER__NODE_ENV + PORT = var.SERVER__PORT + HOST = var.SERVER__HOST + JWT_SECRET_KEY = var.SERVER__JWT_SECRET_KEY + TOKEN_HEADER_KEY = var.SERVER__TOKEN_HEADER_KEY + MONGO_URI = var.SERVER__MONGO_URI + SP_ID = var.SERVER__SP_ID + SP_SECRET = var.SERVER__SP_SECRET + } }) key_name = var.ssh_key_name - subnet_id = aws_subnet.public_a.id user_data_replace_on_change = true + subnet_id = aws_subnet.public_a.id vpc_security_group_ids = [ aws_security_group.jukebox_server.id ] @@ -70,6 +96,18 @@ resource "aws_security_group" "jukebox_server" { to_port = 80 cidr_blocks = ["0.0.0.0/0"] } + egress { + protocol = "tcp" + from_port = 8080 + to_port = 8080 + cidr_blocks = ["0.0.0.0/0"] + } + egress { + protocol = "tcp" + from_port = 8000 + to_port = 8000 + cidr_blocks = ["0.0.0.0/0"] + } tags = local.common_tags } diff --git a/deploy/templates/ec2/server-setup.sh b/deploy/templates/ec2/server-setup.sh.tpl similarity index 85% rename from deploy/templates/ec2/server-setup.sh rename to deploy/templates/ec2/server-setup.sh.tpl index 6b3e56a..c009d43 100644 --- a/deploy/templates/ec2/server-setup.sh +++ b/deploy/templates/ec2/server-setup.sh.tpl @@ -17,10 +17,13 @@ sudo usermod -aG docker ec2-user # Add user to "docker" group for permissions cd ~ -# TODO: Create network docker-compose file, link to :80 - sudo yum install -y git git clone https://github.com/ufosc/Jukebox-Server.git /home/ec2-user/Jukebox-Server + +%{ for env_key, env_value in env } +echo "${env_key}=${env_value}" >> /home/ec2-user/Jukebox-Server/.env +%{ endfor ~} + sudo docker-compose -f /home/ec2-user/Jukebox-Server/docker-compose.prod.yml up -d --build diff --git a/deploy/variables.tf b/deploy/variables.tf index c17bc1d..02f9e19 100644 --- a/deploy/variables.tf +++ b/deploy/variables.tf @@ -12,12 +12,12 @@ variable "contact" { # variable "ssh_auth" { # type = map(string) - + # default = { # key_pair = { # description = "Key pair used to log into EC2 server." # } - + # public_key = { # description = "Public key used to log into EC2 server." # } @@ -30,4 +30,30 @@ variable "ssh_key_name" { # variable "ssh_public_key" { # description = "Public key used to log into EC2 server." -# } \ No newline at end of file +# } + +variable "SERVER__NODE_ENV" { + default = "production" +} +variable "SERVER__PORT" { + default = "8000" +} +variable "SERVER__HOST" { + default = "0.0.0.0" +} +variable "SERVER__JWT_SECRET_KEY" { + description = "Random string used to encrypt JWT token." +} +variable "SERVER__TOKEN_HEADER_KEY" { + default = "Authorization" +} +variable "SERVER__MONGO_URI" { + default = "mongodb://root:changeme@mongo-jukebox:27017" +} +variable "SERVER__SP_ID" { + description = "Spotify App ID" +} +variable "SERVER__SP_SECRET" { + description = "Spotify App Secret" +} + diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 591d216..33d1486 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -3,21 +3,22 @@ version: "3.9" services: api: restart: always + container_name: api build: context: . networks: - cluster environment: - - NODE_ENV=production - - PORT=8000 - - HOST=localhost - - JWT_SECRET_KEY=changeme - - TOKEN_HEADER_KEY=Authorization - - MONGO_URI=mongodb://root:changeme@mongodb:27017 + - NODE_ENV=${NODE_ENV} + - PORT=${PORT} + - HOST=${HOST} + - JWT_SECRET_KEY=${JWT_SECRET_KEY} + - TOKEN_HEADER_KEY=${TOKEN_HEADER_KEY} + - MONGO_URI=${MONGO_URI} - SP_ID=${SP_ID} - SP_SECRET=${SP_SECRET} ports: - - 80:8000 + - 8000:8000 depends_on: - mongodb volumes: @@ -27,7 +28,7 @@ services: build: context: ./proxy/ ports: - - 8080:8080 + - 80:80 networks: - cluster depends_on: @@ -37,6 +38,7 @@ services: mongodb: image: mongo:6.0.9 + container_name: mongodb restart: always ports: - 27017:27017 @@ -45,8 +47,8 @@ services: volumes: - mongo-data:/data/db environment: - - MONGO_INITDB_ROOT_USERNAME=root - - MONGO_INITDB_ROOT_PASSWORD=changeme + - MONGO_INITDB_ROOT_USERNAME=${MONGO_USER} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_PASSWORD} command: mongod --quiet --logpath /dev/null --bind_ip_all volumes: diff --git a/proxy/Dockerfile b/proxy/Dockerfile index d8ddd09..4802bea 100644 --- a/proxy/Dockerfile +++ b/proxy/Dockerfile @@ -15,7 +15,7 @@ RUN mkdir -p /vol/client && \ USER nginx -EXPOSE 8080 +EXPOSE 80 VOLUME /vol/client CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/proxy/default.conf b/proxy/default.conf.tpl similarity index 93% rename from proxy/default.conf rename to proxy/default.conf.tpl index 6099bdc..375a94a 100644 --- a/proxy/default.conf +++ b/proxy/default.conf.tpl @@ -3,7 +3,7 @@ upstream api { } server { - listen 8080; + listen 80; location /api { proxy_pass http://api; diff --git a/src/routes/index.ts b/src/routes/index.ts index 056c4fb..40504e2 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -13,7 +13,7 @@ import { spotifyRouter } from './spotifyRoutes' import { userRouter } from './userRoutes' const router = Router() -router.get('/', BaseController.healthCheck) +router.get('/api', BaseController.healthCheck) router.use('/api/spotify', spotifyRouter) router.use('/api/user', userRouter) router.use('/api/group', groupRoutes)