Skip to content

Commit 4113da1

Browse files
committed
made restart script more robust
1 parent 7884277 commit 4113da1

1 file changed

Lines changed: 79 additions & 22 deletions

File tree

scripts/reset.sh

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,112 @@
11
#!/bin/bash
2-
set -euo pipefail
2+
set -uo pipefail
33

44
###############################################
5-
# CLEAN UP OLD DEPLOYMENT
5+
# CONFIGURATION
66
###############################################
77

8-
echo "Removing objects from S3 bucket..."
9-
awslocal s3 rm s3://chatbot-conversations --recursive || true
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
10+
11+
cd "$PROJECT_DIR"
12+
13+
# Get bucket name from terraform or construct it
14+
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo "")
15+
BUCKET_NAME="chatbot-conversations-${ACCOUNT_ID}"
16+
LOG_GROUP="/aws/lambda/telegram-bot"
1017

11-
echo "Destroying existing Terraform infrastructure..."
12-
terraform destroy -auto-approve || true
18+
echo "==========================================="
19+
echo " Reset Script"
20+
echo "==========================================="
21+
echo "Account ID: ${ACCOUNT_ID:-unknown}"
22+
echo "Bucket: $BUCKET_NAME"
23+
echo ""
1324

25+
###############################################
26+
# CLEAN UP OLD DEPLOYMENT
27+
###############################################
28+
29+
# Delete CloudWatch Log Group (ignore if doesn't exist)
30+
echo "Removing CloudWatch Log Group..."
31+
aws logs delete-log-group --log-group-name "$LOG_GROUP" 2>/dev/null && echo " Deleted log group" || echo " Log group doesn't exist (skipping)"
32+
33+
# Delete S3 bucket with all versions
34+
if [ -n "$ACCOUNT_ID" ]; then
35+
echo "Checking S3 bucket..."
36+
if aws s3api head-bucket --bucket "$BUCKET_NAME" 2>/dev/null; then
37+
echo " Deleting all object versions..."
38+
39+
# Delete all versions
40+
aws s3api list-object-versions --bucket "$BUCKET_NAME" --query 'Versions[].{Key: Key, VersionId: VersionId}' --output json 2>/dev/null | \
41+
jq -c 'select(. != null) | {Objects: ., Quiet: true}' | \
42+
while read -r batch; do
43+
if [ "$batch" != '{"Objects":null,"Quiet":true}' ] && [ -n "$batch" ]; then
44+
aws s3api delete-objects --bucket "$BUCKET_NAME" --delete "$batch" 2>/dev/null || true
45+
fi
46+
done
47+
48+
# Delete all delete markers
49+
aws s3api list-object-versions --bucket "$BUCKET_NAME" --query 'DeleteMarkers[].{Key: Key, VersionId: VersionId}' --output json 2>/dev/null | \
50+
jq -c 'select(. != null) | {Objects: ., Quiet: true}' | \
51+
while read -r batch; do
52+
if [ "$batch" != '{"Objects":null,"Quiet":true}' ] && [ -n "$batch" ]; then
53+
aws s3api delete-objects --bucket "$BUCKET_NAME" --delete "$batch" 2>/dev/null || true
54+
fi
55+
done
56+
57+
echo " S3 bucket emptied"
58+
else
59+
echo " Bucket doesn't exist (skipping)"
60+
fi
61+
else
62+
echo " Skipping S3 cleanup (no AWS credentials)"
63+
fi
64+
65+
# Terraform destroy
66+
echo "Destroying Terraform infrastructure..."
67+
terraform destroy -auto-approve 2>/dev/null || echo " Terraform destroy completed (or nothing to destroy)"
68+
69+
# Clean build artifacts
1470
echo "Cleaning old build artifacts..."
1571
rm -rf package/ lambda_function.zip
1672

1773
###############################################
1874
# REBUILD LAMBDA PACKAGE
1975
###############################################
2076

77+
echo ""
2178
echo "Rebuilding Lambda deployment package..."
2279
mkdir -p package
2380

24-
echo "Installing Python dependencies into package/..."
25-
pip install -r requirements.txt -t ./package
81+
echo "Installing Python dependencies..."
82+
pip install -r requirements.txt -t ./package --quiet
2683

27-
echo "Copying handler.py into package..."
84+
echo "Copying handler.py..."
2885
cp handler.py package/
2986

3087
###############################################
3188
# TERRAFORM DEPLOY
3289
###############################################
3390

91+
echo ""
3492
echo "Initializing Terraform..."
35-
terraform init
93+
terraform init -input=false
94+
95+
echo "Planning Terraform..."
96+
terraform plan
3697

3798
echo "Applying Terraform..."
3899
terraform apply -auto-approve
39100

40101
###############################################
41-
# LAMBDA INVOKE LOOP
102+
# SETUP WEBHOOK
42103
###############################################
43104

44-
echo "Starting Lambda invoke loop. Press CTRL+C to exit."
45-
46-
while true; do
47-
awslocal lambda invoke \
48-
--function-name telegram-bot out.json > /dev/null
49-
50-
echo "Lambda output:"
51-
cat out.json | jq
52-
53-
sleep 1
54-
done
105+
echo ""
106+
echo "Setting up Telegram webhook..."
107+
"$SCRIPT_DIR/setup-webhook.sh"
55108

109+
echo ""
110+
echo "==========================================="
111+
echo " Reset Complete!"
112+
echo "==========================================="

0 commit comments

Comments
 (0)