-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
Β·133 lines (124 loc) Β· 3.33 KB
/
run.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/bash
# Display usage instructions
usage() {
echo "Usage: $0 [--help] [up|down]"
echo " --help: Display usage instructions"
echo " up: Bring up devcontainer if not running"
echo " down: Delete the devcontainer if it exists"
}
# Enable SSH agent with GPG support
enable_ssh_agent_with_gpg_support() {
if ! command -v gpg-agent &> /dev/null; then
echo "Error: gpg is not installed. Please install gpg."
echo "Check https://gpgtools.org"
exit 1
fi
eval "$(gpg-agent --daemon --enable-ssh-support > /dev/null)"
}
# Update changes from remote repository
update_repository() {
if ! command -v git &> /dev/null; then
echo "Error: git is not installed. Please install git."
exit 1
fi
git pull origin main
}
# Connect to running devcontainer
connect_container() {
if command -v sshpass &> /dev/null; then
if ! sshpass -p codespace ssh codespace@localhost -p 8000; then
echo "Error: Failed to login into the devcontainer."
exit 1
fi
else
echo "Error: sshpass is not installed. Please install sshpass."
exit 1
fi
}
# Start devcontainer if it exists
start_container() {
if docker ps --format '{{.Image}}' | grep -q "devcontainer"; then
connect_container
elif docker ps -a --format '{{.Image}}' | grep -q "devcontainer"; then
container_id=$(docker ps -a --format '{{.ID}}' --filter "ancestor=devcontainer")
if ! docker start "$container_id" &> /dev/null; then
echo "Error: Failed to start the devcontainer."
exit 1
fi
# wait for the devcontainer to start before connecting
sleep 2 && connect_container
else
echo "Error: devcontainer does not exist, nothing to start."
exit 1
fi
}
# Bring up devcontainer if not running, otherwise, build the container
build_container() {
if docker ps -a --format '{{.Image}}' | grep -q "devcontainer"; then
echo "Error: devcontainer already exists, skipping build."
exit 1
fi
if [ -z "$HOME" ]; then
echo "Error: \$HOME is not set."
exit 1
fi
if ! docker build -t devcontainer .; then
echo "Error: Failed to build the devcontainer."
exit 1
fi
if ! docker run --privileged -d \
--platform linux/amd64 \
-v "$HOME"/data:/home/codespace/data \
-p 8000:2222 \
-p 8001:8001 \
devcontainer > /dev/null; then
echo "Error: Failed to run the devcontainer."
exit 1
fi
}
# Delete the devcontainer if it exists
delete_container() {
if docker ps -a --format '{{.Image}}' | grep -q "devcontainer"; then
container_id=$(docker ps -a --format '{{.ID}}' --filter "ancestor=devcontainer")
if ! docker rm -f "$container_id" > /dev/null; then
echo "Error: Failed to remove the devcontainer"
exit 1
fi
else
echo "Error: devcontainer does not exist, nothing to delete."
exit 1
fi
}
while [[ "$#" -gt 0 ]]; do
case "$1" in
--help)
usage
exit 0
;;
-*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
*)
break
;;
esac
done
if [[ "$#" -gt 0 ]]; then
if [[ "$1" == "up" ]]; then
enable_ssh_agent_with_gpg_support
update_repository
build_container
elif [[ "$1" == "down" ]]; then
delete_container
else
echo "Invalid argument. Argument must be 'up' or 'down'."
usage
exit 1
fi
else
enable_ssh_agent_with_gpg_support
update_repository
start_container
fi