-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·62 lines (48 loc) · 1.88 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
#!/bin/bash
# Check if the config file exists
CONFIG_FILE="projects.config"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file '$CONFIG_FILE' not found. Please create it with a list of project IDs."
exit 1
fi
# Read projects from the config file
PROJECTS=($(cat $CONFIG_FILE))
# Display available projects
echo "Available Projects:"
for i in "${!PROJECTS[@]}"; do
echo "$((i+1)). ${PROJECTS[$i]}"
done
# Prompt for project selection
echo -n "Select the project number: "
read PROJECT_SELECTION
if [[ $PROJECT_SELECTION -lt 1 || $PROJECT_SELECTION -gt ${#PROJECTS[@]} ]]; then
echo "Invalid selection. Exiting."
exit 1
fi
PROJECT_ID=${PROJECTS[$((PROJECT_SELECTION-1))]}
# Set the selected project
echo "Setting project to $PROJECT_ID..."
gcloud config set project $PROJECT_ID
# List active instances with numbering
echo "Fetching list of active instances..."
gcloud compute instances list --filter="status=RUNNING" --format="table[no-heading](name, zone, external_ip)" | nl
# Check if any instances are listed
INSTANCE_COUNT=$(gcloud compute instances list --filter="status=RUNNING" --format="value(name)" | wc -l)
if [ "$INSTANCE_COUNT" -eq 0 ]; then
echo "No active instances found. Exiting."
exit 1
fi
# Prompt for instance selection
echo -n "Select the instance number to connect to: "
read INSTANCE_NUMBER
# Get the selected instance details
INSTANCE_NAME=$(gcloud compute instances list --filter="status=RUNNING" --format="table[no-heading](name)" | sed -n "${INSTANCE_NUMBER}p")
INSTANCE_ZONE=$(gcloud compute instances list --filter="status=RUNNING" --format="table[no-heading](zone)" | sed -n "${INSTANCE_NUMBER}p")
# Check if the instance name is valid
if [ -z "$INSTANCE_NAME" ]; then
echo "Invalid instance selection. Exiting."
exit 1
fi
# Connect to the instance
echo "Connecting to $INSTANCE_NAME in zone $INSTANCE_ZONE..."
gcloud compute ssh $INSTANCE_NAME --zone $INSTANCE_ZONE