1+ #! /bin/bash
2+ set -e
3+
4+ # GKE Deployment Script for AMP ERC20 Loader
5+ # This script automates the deployment of the ERC20 loader to Google Kubernetes Engine
6+
7+ # Colors for output
8+ RED=' \033[0;31m'
9+ GREEN=' \033[0;32m'
10+ YELLOW=' \033[1;33m'
11+ NC=' \033[0m' # No Color
12+
13+ # Configuration (override with environment variables)
14+ PROJECT_ID=" ${GCP_PROJECT_ID:- } "
15+ CLUSTER_NAME=" ${GKE_CLUSTER_NAME:- staging} "
16+ REGION=" ${GKE_REGION:- us-central1} "
17+ ZONE=" ${GKE_ZONE:- us-central1-a} "
18+ NODE_MACHINE_TYPE=" ${GKE_MACHINE_TYPE:- n1-highmem-4} "
19+ NUM_NODES=" ${GKE_NUM_NODES:- 1} "
20+
21+ # Function to print colored output
22+ print_info () {
23+ echo -e " ${GREEN} [INFO]${NC} $1 "
24+ }
25+
26+ print_warning () {
27+ echo -e " ${YELLOW} [WARN]${NC} $1 "
28+ }
29+
30+ print_error () {
31+ echo -e " ${RED} [ERROR]${NC} $1 "
32+ }
33+
34+ # Function to check if command exists
35+ command_exists () {
36+ command -v " $1 " > /dev/null 2>&1
37+ }
38+
39+ # Check prerequisites
40+ print_info " Checking prerequisites..."
41+
42+ if ! command_exists gcloud; then
43+ print_error " gcloud CLI is not installed. Please install it from https://cloud.google.com/sdk/docs/install"
44+ exit 1
45+ fi
46+
47+ if ! command_exists kubectl; then
48+ print_error " kubectl is not installed. Please install it: gcloud components install kubectl"
49+ exit 1
50+ fi
51+
52+ # Get or prompt for project ID
53+ if [ -z " $PROJECT_ID " ]; then
54+ print_warning " GCP_PROJECT_ID not set. Attempting to get from gcloud config..."
55+ PROJECT_ID=$( gcloud config get-value project 2> /dev/null)
56+
57+ if [ -z " $PROJECT_ID " ]; then
58+ print_error " Could not determine GCP project ID."
59+ echo " Please set it with: export GCP_PROJECT_ID=your-project-id"
60+ exit 1
61+ fi
62+ fi
63+
64+ print_info " Using GCP Project: $PROJECT_ID "
65+ print_info " Using Cluster: $CLUSTER_NAME in $REGION "
66+
67+ # SAFETY CHECK: Ensure project contains "staging"
68+ if [[ ! " $PROJECT_ID " =~ staging ]]; then
69+ print_error " SAFETY CHECK FAILED!"
70+ print_error " Project ID must contain 'staging' for deployment safety."
71+ print_error " Current project: $PROJECT_ID "
72+ echo " "
73+ print_error " This prevents accidental deployment to production."
74+ print_error " If you need to deploy to a different environment, update this script."
75+ exit 1
76+ fi
77+
78+ print_info " Safety check passed: Project contains 'staging'"
79+
80+ # Set the project
81+ gcloud config set project " $PROJECT_ID "
82+
83+ # Check if cluster exists
84+ print_info " Checking if GKE cluster exists..."
85+ if gcloud container clusters describe " $CLUSTER_NAME " --region=" $REGION " > /dev/null 2>&1 ; then
86+ print_info " Cluster '$CLUSTER_NAME ' already exists. Using existing cluster."
87+ else
88+ print_warning " Cluster '$CLUSTER_NAME ' does not exist."
89+ read -p " Do you want to create it? (y/n) " -n 1 -r
90+ echo
91+ if [[ $REPLY =~ ^[Yy]$ ]]; then
92+ print_info " Creating GKE cluster '$CLUSTER_NAME '..."
93+ gcloud container clusters create " $CLUSTER_NAME " \
94+ --region=" $REGION " \
95+ --machine-type=" $NODE_MACHINE_TYPE " \
96+ --num-nodes=" $NUM_NODES " \
97+ --enable-autoscaling \
98+ --min-nodes=1 \
99+ --max-nodes=3 \
100+ --enable-autorepair \
101+ --enable-autoupgrade \
102+ --disk-size=50 \
103+ --disk-type=pd-standard
104+
105+ print_info " Cluster created successfully!"
106+ else
107+ print_error " Cannot proceed without a cluster. Exiting."
108+ exit 1
109+ fi
110+ fi
111+
112+ # Get cluster credentials
113+ print_info " Getting cluster credentials..."
114+ gcloud container clusters get-credentials " $CLUSTER_NAME " --region=" $REGION "
115+
116+ # Create namespace if it doesn't exist
117+ # print_info "Ensuring namespace exists..."
118+ # kubectl create namespace amp-loader --dry-run=client -o yaml | kubectl apply -f -
119+
120+ # Set current context to namespace
121+ kubectl config set-context --current --namespace=nozzle
122+
123+ # Create secrets from .env file if it exists
124+ if [ -f " ../.test.env" ]; then
125+ print_info " Creating Kubernetes secrets from .env file..."
126+
127+ # Source the .env file
128+ export $( cat ../.env | grep -v ' ^#' | xargs)
129+
130+ # Create the amp-secrets secret
131+ kubectl create secret generic amp-secrets \
132+ --from-literal=amp-server-url=" $AMP_SERVER_GOOGLE_CLOUD_URL " \
133+ --from-literal=snowflake-account=" $SNOWFLAKE_ACCOUNT " \
134+ --from-literal=snowflake-user=" $SNOWFLAKE_USER " \
135+ --from-literal=snowflake-warehouse=" $SNOWFLAKE_WAREHOUSE " \
136+ --from-literal=snowflake-database=" $SNOWFLAKE_DATABASE " \
137+ --from-literal=snowflake-private-key=" $SNOWFLAKE_PRIVATE_KEY " \
138+ --dry-run=client -o yaml | kubectl apply -f -
139+
140+ print_info " Secrets created successfully!"
141+ else
142+ print_warning " .env file not found. You'll need to create secrets manually."
143+ print_info " You can use: kubectl apply -f k8s/secret.yaml"
144+ fi
145+
146+ # Create GitHub Container Registry secret if credentials are available
147+ # if [ -n "$GITHUB_USERNAME" ] && [ -n "$GITHUB_PAT" ]; then
148+ # print_info "Creating GitHub Container Registry secret..."
149+ # kubectl create secret docker-registry ghcr-secret \
150+ # --docker-server=ghcr.io \
151+ # --docker-username="$GITHUB_USERNAME" \
152+ # --docker-password="$GITHUB_PAT" \
153+ # --docker-email="${GITHUB_EMAIL:[email protected] }" \154+ # --dry-run=client -o yaml | kubectl apply -f -
155+ #
156+ # print_info "GHCR secret created successfully!"
157+ # else
158+ # print_warning "GitHub credentials not found (GITHUB_USERNAME, GITHUB_PAT)."
159+ # print_info "If using a private registry, create the secret manually:"
160+ # print_info " kubectl create secret docker-registry ghcr-secret \\"
161+ # print_info " --docker-server=ghcr.io \\"
162+ # print_info " --docker-username=YOUR_USERNAME \\"
163+ # print_info " --docker-password=YOUR_PAT"
164+ # fi
165+
166+ # Apply Kubernetes manifests
167+ print_info " Applying Kubernetes deployment..."
168+ kubectl apply -f deployment.yaml
169+
170+ # Wait for deployment to be ready
171+ print_info " Waiting for deployment to be ready..."
172+ kubectl wait --for=condition=available --timeout=300s deployment/amp-erc20-loader
173+
174+ # Get deployment status
175+ print_info " Deployment status:"
176+ kubectl get deployments
177+ kubectl get pods
178+
179+ # Show logs from the first pod
180+ print_info " Fetching logs from the loader..."
181+ POD_NAME=$( kubectl get pods -l app=amp-erc20-loader -o jsonpath=' {.items[0].metadata.name}' )
182+ echo " "
183+ print_info " Pod name: $POD_NAME "
184+ echo " "
185+ print_info " Recent logs (last 50 lines):"
186+ kubectl logs " $POD_NAME " --tail=50
187+
188+ echo " "
189+ print_info " Deployment completed successfully!"
190+ echo " "
191+ print_info " Useful commands:"
192+ echo " - View logs: kubectl logs -f deployment/amp-erc20-loader"
193+ echo " - Get pod status: kubectl get pods -l app=amp-erc20-loader"
194+ echo " - Describe pod: kubectl describe pod $POD_NAME "
195+ echo " - Delete deployment: kubectl delete -f k8s/deployment.yaml"
196+ echo " - Scale deployment: kubectl scale deployment/amp-erc20-loader --replicas=2"
197+ echo " "
0 commit comments