-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·108 lines (90 loc) · 2.91 KB
/
start.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#!/bin/bash
#!/bin/bash
set -e
# Load environment variables
source .env
# Function to check instance state
check_instance_state() {
aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].State.Name' --output text
}
# Function to get instance IP
get_instance_ip() {
aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[0].Instances[0].PublicIpAddress' --output text
}
# Wait for instance to reach 'stopped' state if it's stopping
while true; do
state=$(check_instance_state)
if [ "$state" = "stopped" ]; then
break
elif [ "$state" = "running" ]; then
break
else
echo "Waiting for instance to reach a stable state. Current state: $state"
sleep 10
fi
done
# Start the instance if it's not already running
if [ "$state" != "running" ]; then
echo "Starting instance..."
aws ec2 start-instances --instance-ids $INSTANCE_ID
aws ec2 wait instance-running --instance-ids $INSTANCE_ID
fi
# Get the IP address with retry
echo "Waiting for IP address assignment..."
for i in {1..30}; do
AWS_IP=$(get_instance_ip)
if [ -n "$AWS_IP" ] && [ "$AWS_IP" != "None" ]; then
echo "Instance IP: $AWS_IP"
break
fi
if [ $i -eq 30 ]; then
echo "Failed to get instance IP after 30 attempts. Exiting."
exit 1
fi
sleep 10
done
echo "export AWS_IP=$AWS_IP" > ~/aws_ip.sh
# Update SSH config
echo "Updating SSH config..."
sed -i.bak '/Host aws-transcribe/,/UserKnownHostsFile \/dev\/null/d' ~/.ssh/config
cat >> ~/.ssh/config <<EOL
Host aws-transcribe
HostName ${AWS_IP}
User ubuntu
IdentityFile ${AWS_KEY_PATH}
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
EOL
# Ensure the mount directory exists
mkdir -p "${MOUNT_DIR}"
# Wait for SSH to become available
echo "Waiting for SSH to become available..."
for i in {1..30}; do
if ssh -o ConnectTimeout=5 aws-transcribe exit 2>/dev/null; then
echo "SSH is now available."
break
fi
if [ $i -eq 30 ]; then
echo "SSH did not become available after 30 attempts. Exiting."
exit 1
fi
sleep 10
done
# Mount remote directory
echo "Mounting remote directory..."
sshfs aws-transcribe:${REMOTE_DIR} ${MOUNT_DIR}
if [ $? -eq 0 ]; then
echo "Remote directory successfully mounted at ${MOUNT_DIR}"
else
echo "Failed to mount remote directory. Please check your connection and try again."
exit 1
fi
# Start Docker containers
echo "Starting Docker containers..."
ssh aws-transcribe "cd ${REMOTE_DIR} && docker compose up --build -d"
echo "Environment setup complete. Docker containers are starting."
echo "You can now edit files in the remote volume locally at ${MOUNT_DIR}"
echo "To test the server, use: curl http://aws-transcribe:8000"
echo "To SSH into the instance, use: ssh aws-transcribe"
echo "To use the IP address in your current shell, run: source ~/aws_ip.sh"