-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
212 lines (169 loc) · 5.47 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/bash
AGENT_X64_URL=https://pcdn.titannet.io/test4/bin/agent-linux.zip
AGENT_ARM_URL=https://pcdn.titannet.io/test4/bin/agent-arm.zip
install_agent() {
echo "Installing agent"
case "$TARGETARCH" in \
amd64) AGENT_URL=$AGENT_X64_URL ;; \
arm64) AGENT_URL=$AGENT_ARM_URL ;; \
*) echo "Unsupported architecture: $TARGETARCH"; exit 1 ;; \
esac
mkdir -p $AGENT_PATH
cd $AGENT_PATH
echo "Downloading agent from $AGENT_URL"
curl -L $AGENT_URL -o agent.zip
unzip -o agent.zip
chmod +x agent
rm agent.zip
echo "Agent installed"
}
run_agent() {
echo "Start agent"
cd $AGENT_PATH
mkdir -p $WORKSPACE
$AGENT_PATH/agent --working-dir=$WORKSPACE --server-url=$SERVER_URL --key=$KEY &
agentId=$!
echo "Agent started with pid $agentId"
}
start_multipass() {
cd $MULTIPASS_PATH
sleep 5
./multipassd &
multipassId=$!
sleep 15
echo "Waiting for multipass socket to be ready..."
for i in {1..60}; do
if [[ -S $MULTIPASS_SOCKET_PATH ]]; then
echo "Multipass socket is ready."
break
fi
sleep 1
done
if [[ ! -S $MULTIPASS_SOCKET_PATH ]]; then
echo "Multipass socket is still not available after 60 seconds."
kill $multipassId
return 1
fi
}
run_multipass() {
echo "Starting multipass..."
cd $MULTIPASS_PATH
mkdir -p $DATA_DIR/multipass
# 如果 ~/.config 軟連結 存在,則刪除並建立連結
if [[ -L ~/.config ]]; then
rm ~/.config
fi
mkdir -p $DATA_DIR/.config
ln -s $DATA_DIR/.config ~/.config
start_multipass
# check return code
if [ $? -ne 0 ]; then
return 1
fi
# 如果密碼未設定(~/.config/multipassd/multipassd.conf 包含 local.passphrase=),則設定密碼
if ! grep -q "local.passphrase=" ~/.config/multipassd/multipassd.conf; then
/app/set_passphrase.sh
sleep 10
if ! kill -0 $multipassId 2>/dev/null; then
start_multipass
fi
else
echo "Multipass passphrase is already set."
fi
multipass authenticate $MULTIPASS_PASSPHRASE
# check ~/.config/multipassd/multipassd.conf 是否有 local.driver=qemu
if ! grep -q "local.driver=qemu" ~/.config/multipassd/multipassd.conf; then
echo "Setting multipass driver to qemu."
multipass set local.driver=qemu
sleep 15
if ! kill -0 $multipassId 2>/dev/null; then
kill $multipassId
start_multipass
multipass authenticate $MULTIPASS_PASSPHRASE
fi
else
echo "Multipass driver is already set to qemu."
fi
if multipass list; then
echo "Multipass client connected successfully."
else
echo "Multipass client failed to connect."
kill $multipassId
start_multipass
multipass authenticate $MULTIPASS_PASSPHRASE
if multipass list; then
echo "Multipass client connected successfully."
else
echo "Multipass client failed to connect."
kill $multipassId
return 1
fi
fi
echo "Multipass started with pid $multipassId"
}
main() {
if [ -z "$KEY" ]; then
echo "Error: --key is required"
exit 1
fi
# 禁用 IPv6
echo "Disabling IPv6..."
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
install_agent
run_multipass
# check return code
if [ $? -ne 0 ]; then
echo "Failed to start multipass."
exit 1
fi
run_agent
while true; do
sleep 30
# check if agent is still running
if ! kill -0 $agentId 2>/dev/null; then
echo "Agent stopped restarting......."
run_agent
fi
# check if multipass is still running
if ! kill -0 $multipassId 2>/dev/null; then
echo "Multipass stopped restarting......."
kill $multipassId
start_multipass
multipass authenticate $MULTIPASS_PASSPHRASE
fi
if ! multipass list > /dev/null 2>&1; then
echo "Multipass client failed to connect."
if [ -n "$multipassId" ]; then
kill $multipassId
fi
sleep 15
start_multipass
sleep 6
if multipass authenticate $MULTIPASS_PASSPHRASE; then
if multipass list > /dev/null 2>&1; then
echo "Multipass client connected successfully."
echo "Multipass started with pid $multipassId"
else
echo "Multipass client failed to connect."
if [ -n "$multipassId" ]; then
kill $multipassId
fi
fi
else
echo "Multipass authentication failed."
fi
fi
# 偵測 multipass list 是否包含 ubuntu-niulink 並且狀態不為 Running ,則啟動
# if multipass list | grep -q "ubuntu-niulink"; then
# if ! multipass list | grep -q "ubuntu-niulink.*Running"; then
# sleep 10
# if ! multipass list | grep -q "ubuntu-niulink.*Running"; then
# echo "Starting ubuntu-niulink..."
# multipass start ubuntu-niulink
# fi
# fi
# fi
done
}
main "$@"