Skip to content

Commit

Permalink
configure prod docker-compose, db issues, created automation for env …
Browse files Browse the repository at this point in the history
…variables
  • Loading branch information
IkeHunter committed Mar 2, 2024
1 parent 69913d5 commit b3c7b9d
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files.associations": {
"*.sh.tpl": "shellscript",
"*.json.tpl": "JSON",
"*.conf.tpl": "NGINX Conf"
}
}
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ COPY ./src /app/src

RUN npm run build

EXPOSE 80
EXPOSE 8000
VOLUME ["/app/src"]

CMD ["npm", "start"]
46 changes: 42 additions & 4 deletions deploy/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
]
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

32 changes: 29 additions & 3 deletions deploy/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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."
# }
Expand All @@ -30,4 +30,30 @@ variable "ssh_key_name" {

# variable "ssh_public_key" {
# description = "Public key used to log into EC2 server."
# }
# }

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"
}

22 changes: 12 additions & 10 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -27,7 +28,7 @@ services:
build:
context: ./proxy/
ports:
- 8080:8080
- 80:80
networks:
- cluster
depends_on:
Expand All @@ -37,6 +38,7 @@ services:

mongodb:
image: mongo:6.0.9
container_name: mongodb
restart: always
ports:
- 27017:27017
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion proxy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN mkdir -p /vol/client && \

USER nginx

EXPOSE 8080
EXPOSE 80
VOLUME /vol/client

CMD ["/entrypoint.sh"]
2 changes: 1 addition & 1 deletion proxy/default.conf → proxy/default.conf.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ upstream api {
}

server {
listen 8080;
listen 80;
location /api {
proxy_pass http://api;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b3c7b9d

Please sign in to comment.