mrfirdauss commited on
Commit
f92437f
·
1 Parent(s): a9ab385

fix deploy.sh

Browse files
Files changed (3) hide show
  1. deploy.sh +46 -21
  2. docker-compose.yaml +7 -4
  3. server.py +3 -2
deploy.sh CHANGED
@@ -1,45 +1,70 @@
1
  #!/bin/bash
2
- set -e
3
 
4
- cd "$(dirname "$0")"
 
 
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  if [[ -f "image-tag.txt" ]]; then
7
- IMAGE_TAG=$(cat image-tag.txt)
 
8
  else
9
  echo "ERROR: image-tag.txt not found"
10
  exit 1
11
  fi
12
 
13
-
14
  echo "=== Deployment Variables ==="
15
- echo "GOOGLE_AI_STUDIO_API_KEY=${GOOGLE_AI_STUDIO_API_KEY}"
16
- echo "IMAGE_TAG=${IMAGE_TAG}"
17
- echo "GOOGLE_AI_STUDIO_MODEL=${GOOGLE_AI_STUDIO_MODEL}"
18
- echo "DB_HOST=${DB_HOST}"
19
- echo "DB_NAME=${DB_NAME}"
20
- echo "DB_USERNAME=${DB_USERNAME}"
21
- echo "DB_PORT=${DB_PORT}"
22
- echo "DB_PASSWORD=${DB_PASSWORD}"
23
-
24
- echo "PORT=${PORT}"
25
- echo "PLAYWRIGHT_BROWSERS_PATH=${PLAYWRIGHT_BROWSERS_PATH}"
26
 
27
- export APP_ENV IMAGE_TAG API_PORT ES_PORT
28
-
29
- echo "=== Login AWS ==="
30
- aws --debug ecr get-login-password --region ap-southeast-3 | docker login --username AWS --password-stdin 825765383758.dkr.ecr.ap-southeast-3.amazonaws.com
31
-
32
- echo "=== Deploying containers for ${APP_ENV} ==="
33
 
 
34
  echo "=== Stopping containers ==="
35
  docker-compose -p "${APP_ENV}" down || true
36
 
37
  echo "=== Pulling images ==="
38
  docker-compose -p "${APP_ENV}" -f docker-compose.yml pull
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  echo "=== Starting containers ==="
 
41
  docker-compose -p "${APP_ENV}" -f docker-compose.yml up -d
42
 
 
43
  echo "=== Prune unused images ==="
44
  docker image prune -af
45
 
 
1
  #!/bin/bash
2
+ set -e # Exit immediately if a command fails
3
 
4
+ # --- Configuration ---
5
+ # ✅ DEFINE THE APP DIRECTORY (from your appspec.yml)
6
+ APP_DIR="/home/ubuntu/itinerary-generator"
7
+ cd ${APP_DIR}
8
 
9
+ # ✅ DEFINE YOUR CONTAINER/ECR REPO NAME
10
+ # This must match the $CONTAINER_NAME var in your CodeBuild project
11
+ export CONTAINER_NAME="itinerary-generator" # Or whatever your ECR repo is named
12
+
13
+ # 1. Get environment name from CodeDeploy
14
+ APP_ENV=${DEPLOYMENT_GROUP_NAME}
15
+ if [[ -z "$APP_ENV" ]]; then
16
+ echo "ERROR: DEPLOYMENT_GROUP_NAME is not set."
17
+ exit 1
18
+ fi
19
+
20
+ # 2. Read the image tag from the file
21
  if [[ -f "image-tag.txt" ]]; then
22
+ # ✅ Export this so docker-compose.yaml can read it
23
+ export IMAGE_TAG=$(cat image-tag.txt)
24
  else
25
  echo "ERROR: image-tag.txt not found"
26
  exit 1
27
  fi
28
 
 
29
  echo "=== Deployment Variables ==="
30
+ echo "Project Name (APP_ENV)=${APP_ENV}"
31
+ echo "Container Name=${CONTAINER_NAME}"
32
+ echo "Image Tag=${IMAGE_TAG}"
 
 
 
 
 
 
 
 
33
 
34
+ # 3. Login to AWS ECR
35
+ echo "=== Login AWS ECR ==="
36
+ aws ecr get-login-password --region ap-southeast-3 | docker login --username AWS --password-stdin 825765383758.dkr.ecr.ap-southeast-3.amazonaws.com
 
 
 
37
 
38
+ # 4. Stop and Pull
39
  echo "=== Stopping containers ==="
40
  docker-compose -p "${APP_ENV}" down || true
41
 
42
  echo "=== Pulling images ==="
43
  docker-compose -p "${APP_ENV}" -f docker-compose.yml pull
44
 
45
+ # 5. ✅ CREATE THE .env FILE
46
+ echo "=== Creating .env file ==="
47
+ # This is the file your server.py and docker-compose.yaml need.
48
+ # We will create it using the APP_ENV to build the correct SSM path.
49
+
50
+ # Define the SSM Parameter Store path based on the environment
51
+ # Example: /itinerary-generator/development or /itinerary-generator/production
52
+ PARAM_PATH="/${CONTAINER_NAME}/${APP_ENV}"
53
+
54
+ # Write the path into the .env file.
55
+ # The 'echo' command creates the file.
56
+ echo "AWS_PARAMETER_STORE_PATH=${PARAM_PATH}" > .env
57
+ # Add any other non-secret vars your app needs from the .env
58
+ echo "APP_ENV=${APP_ENV}" >> .env
59
+
60
+ echo "Created .env file with SSM Path: ${PARAM_PATH}"
61
+
62
+ # 6. Start Containers
63
  echo "=== Starting containers ==="
64
+ # Now this command will work because .env exists
65
  docker-compose -p "${APP_ENV}" -f docker-compose.yml up -d
66
 
67
+ # 7. Prune
68
  echo "=== Prune unused images ==="
69
  docker image prune -af
70
 
docker-compose.yaml CHANGED
@@ -2,11 +2,14 @@ version: '3.8'
2
 
3
  services:
4
  app:
5
- build: .
 
 
 
 
 
6
  ports:
7
  - "7860:7860"
8
  env_file:
9
  - .env
10
- volumes:
11
- - ./.env:/.env
12
- restart: always
 
2
 
3
  services:
4
  app:
5
+ # ⛔️ REMOVE this. You are not building on the EC2 instance.
6
+ # build: .
7
+
8
+ # ✅ ADD this. Use the image from ECR and the tag from image-tag.txt
9
+ image: 825765383758.dkr.ecr.ap-southeast-3.amazonaws.com/$CONTAINER_NAME:${IMAGE_TAG}
10
+
11
  ports:
12
  - "7860:7860"
13
  env_file:
14
  - .env
15
+ restart: always
 
 
server.py CHANGED
@@ -24,8 +24,6 @@ API = "https://api-dev.spun.global"
24
  def create_app() -> Flask:
25
  load_dotenv()
26
 
27
- WORKER_API_KEY = os.getenv("WORKER_API_KEY")
28
-
29
  app = Flask(__name__)
30
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
31
 
@@ -36,6 +34,9 @@ def create_app() -> Flask:
36
  logging.info("Secrets loaded from AWS SSM Parameter Store.")
37
  except Exception as e:
38
  logging.exception(f"Could not load secrets from SSM: {e}")
 
 
 
39
 
40
  @app.route('/scrape', methods=['POST'])
41
  async def scrape():
 
24
  def create_app() -> Flask:
25
  load_dotenv()
26
 
 
 
27
  app = Flask(__name__)
28
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
29
 
 
34
  logging.info("Secrets loaded from AWS SSM Parameter Store.")
35
  except Exception as e:
36
  logging.exception(f"Could not load secrets from SSM: {e}")
37
+
38
+ WORKER_API_KEY = os.getenv("WORKER_API_KEY")
39
+
40
 
41
  @app.route('/scrape', methods=['POST'])
42
  async def scrape():