Skip to content

Commit 77af9b1

Browse files
feat(upload-assets): add script for uploading assets to S3 with validation and error handling
1 parent 9dfa17b commit 77af9b1

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

upload-assets.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/bin/bash
2+
3+
# Verifying required utilities
4+
echo "Verifying utilities are installed"
5+
apps=(jq aws)
6+
for program in "${apps[@]}"; do
7+
if ! command -v "$program" >/dev/null 2>&1; then
8+
echo "$program not found. This Script needs jq and aws cli. Please install the application/s and restart deployment, Exiting."
9+
exit
10+
else
11+
echo "$program found"
12+
fi
13+
done
14+
15+
# Check required parameters
16+
if [ $# -lt 2 ]; then
17+
echo "Usage: $0 <amiid> <bucketname>"
18+
echo ' Param 1: The AMI from which the EC2 for Research Gateway should be created'
19+
echo ' Param 2: The S3 bucket to create for holding the CFT templates'
20+
exit 1
21+
fi
22+
23+
amiid=$1
24+
bucketname=$2
25+
region=$(aws configure get region)
26+
[ -z "$region" ] && echo "AWS region not configured. Run 'aws configure'" && exit 1
27+
28+
# Validate AMI ID
29+
echo "Validating AMI ID: $amiid"
30+
if ! aws ec2 describe-images --image-id "$amiid" >/dev/null 2>&1; then
31+
echo "Error: AMI ID $amiid does not exist."
32+
exit 1
33+
fi
34+
35+
# Validate bucket name
36+
echo "Validating bucket name: $bucketname"
37+
if ! echo "$bucketname" | grep -q -P '(?=^.{3,63}$)(?!^xn--)(?!.*s3alias$)(?!^(\d+\.)+\d+$)(^(([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$)'; then
38+
echo "Error: Invalid S3 bucket name."
39+
exit 1
40+
fi
41+
42+
localhome=$(pwd)
43+
bucketstackname="RG-PortalStack-Bucket-$(date +%s)"
44+
45+
# Create or check bucket
46+
if aws s3api head-bucket --bucket "$bucketname" 2>/dev/null; then
47+
echo "Bucket $bucketname exists. Proceeding to upload files..."
48+
else
49+
echo "Bucket $bucketname does not exist. Creating it using CloudFormation..."
50+
aws cloudformation deploy --template-file rg_deploy_bucket.yml --stack-name "$bucketstackname" \
51+
--parameter-overrides S3NewBucketName="$bucketname"
52+
aws cloudformation wait stack-create-complete --stack-name "$bucketstackname"
53+
if [ $? -ne 0 ]; then
54+
echo "Error: Failed to create S3 bucket via CloudFormation."
55+
exit 1
56+
fi
57+
fi
58+
59+
echo "Uploading required files to s3://$bucketname..."
60+
61+
# Upload individual config and script files
62+
aws s3 cp "$localhome"/docker-compose.yml s3://"$bucketname"
63+
aws s3 cp "$localhome"/nginx.conf s3://"$bucketname"
64+
aws s3 cp "$localhome"/updatescripts.sh s3://"$bucketname"
65+
aws s3 cp "$localhome"/makeconfigs.sh s3://"$bucketname"
66+
aws s3 cp "$localhome"/mainonly.sh s3://"$bucketname"
67+
aws s3 cp "$localhome"/makestudies.sh s3://"$bucketname"
68+
69+
# Sync CFT templates
70+
aws s3 sync "$localhome/cft-templates/" s3://"$bucketname/"
71+
72+
# Upload config files
73+
tar -czf config.tar.gz config/*
74+
tar -tf config.tar.gz
75+
aws s3 cp config.tar.gz s3://"$bucketname"
76+
rm -f config.tar.gz
77+
78+
# Upload scripts
79+
tar -czf scripts.tar.gz scripts/*
80+
tar -tf scripts.tar.gz
81+
aws s3 cp scripts.tar.gz s3://"$bucketname"
82+
rm -f scripts.tar.gz
83+
84+
# Upload bootstrap scripts
85+
aws s3 cp "$localhome"/scripts/bootstrap-scripts/ s3://"$bucketname/bootstrap-scripts" --recursive
86+
87+
# Upload Lambda zip files
88+
cd "$localhome"/lambdas || exit
89+
zip -j pre_verification_custom_message.zip pre_verification_custom_message/index.js
90+
zip -j post_verification_send_message.zip post_verification_send_message/index.js
91+
aws s3 cp pre_verification_custom_message.zip s3://"$bucketname"
92+
aws s3 cp post_verification_send_message.zip s3://"$bucketname"
93+
rm -f pre_verification_custom_message.zip post_verification_send_message.zip
94+
95+
# Upload Image Builder products
96+
cd "$localhome"/products || exit
97+
98+
tar -czf nicedcv.tar.gz Nicedcv/*
99+
aws s3 cp nicedcv.tar.gz s3://"$bucketname/"
100+
rm -f nicedcv.tar.gz
101+
102+
103+
zip -r ec2-winsecure-image.zip ec2-secure-windows/*
104+
aws s3 cp ec2-winsecure-image.zip s3://"$bucketname/"
105+
rm -f ec2-winsecure-image.zip
106+
107+
# Upload dump data
108+
cd "$localhome" || exit
109+
zip dump.zip dump/*
110+
unzip -l dump.zip
111+
aws s3 cp dump.zip s3://"$bucketname/"
112+
rm -f dump.zip
113+
echo "✅ All files uploaded successfully to s3://$bucketname"
114+
exit 0
115+
116+
117+
# ./upload-assets.sh ami-0f78b782bj5ef10a6 single-cft-test-s3

0 commit comments

Comments
 (0)