-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpic
executable file
·325 lines (322 loc) · 10.3 KB
/
pic
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#! /bin/bash
isRoot=$(whoami)
if [ "$isRoot" != "root" ]; then
echo "Please run as root"
exit 1
fi
args=("$@")
arg1=${args[0]}
PIC_DIR="$HOME/.pic"
LOGS_DIR="$PIC_DIR/logs/"
SCRIPTS_DIR="$PIC_DIR/scripts"
mkdir -p $LOGS_DIR
mkdir -p $SCRIPTS_DIR
if [ "$arg1" == "update" ]; then
echo "Updating PiCloud"
curl "https://raw.githubusercontent.com/qtvhao/picloud/main/pic?$(date +%s)" -o pic && chmod +x pic
exit 0
fi
if [ "$arg1" == "recommended" ]; then
set -e
./pic exec-local-or-remote "scripts" \
"recommended" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "install" ]; then
echo "Installing Docker"
curl -fsSL https://get.docker.com -o install-docker.sh
chmod +x install-docker.sh
docker version || ./install-docker.sh
echo "Installed Docker"
rm install-docker.sh
./pic expose
./pic reinstall-k3s
exit 0
fi
if [ "$arg1" == "expose" ]; then
set -e
./pic exec-local-or-remote "scripts/cloudflared" \
"expose" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "remove" ]; then
echo "Removing PiCloud"
docker rm -f picloud-k3s-server
apt remove docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-ce-rootless-extras docker-buildx-plugin -y >/dev/null
exit 0
fi
if [ "$arg1" == "start" ]; then
echo "Starting PiCloud"
exit 0
fi
if [ "$arg1" == "stop" ]; then
echo "Stopping PiCloud"
exit 0
fi
if [ "$arg1" == "restart" ]; then
echo "Restarting PiCloud"
exit 0
fi
if [ "$arg1" == "status" ]; then
echo "Status of PiCloud"
echo "PiCloud is running"
exit 0
fi
if [ "$arg1" == "reinstall-k3s" ]; then
set -e
./pic exec-local-or-remote "scripts/k3s" \
"reinstall-k3s" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "uninstall-k3s" ]; then
set -e
./pic exec-local-or-remote "scripts/k3s" \
"uninstall" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "wait" ]; then
set -e
RESOURCE_TYPE=$2
DEPLOYMENT=$3
NAMESPACE=$4
CONDITION=$5
while true; do #
kubectl get ns kube-system && break;
done
while true; do
kubectl wait --for=condition=$CONDITION $RESOURCE_TYPE/$DEPLOYMENT -n $NAMESPACE --timeout 10s && break || sleep 5
done
exit 0
fi
if [ "$arg1" == "self-update" ]; then
set -xeo pipefail
REPO="qtvhao/picloud"
REPO_EVENTS=$(curl "https://api.github.com/repos/$REPO/events")
EVENTS_URL="https://api.github.com/repos/$REPO/events"
which curl
which jq
echo "EVENTS_URL: $EVENTS_URL"
EVENTS_TYPE_PUSH_EVENT=$(echo $REPO_EVENTS | jq -r '[.[] | select(.type == "PushEvent")]') || true
if [ -z "$EVENTS_TYPE_PUSH_EVENT" ]; then
echo "No PushEvent found. Failed to update."
exit 0
fi
EVENTS_TYPE_PUSH_EVENT_FIRST=$(echo $EVENTS_TYPE_PUSH_EVENT | jq -r '.[0]')
PAYLOAD=$(echo $EVENTS_TYPE_PUSH_EVENT_FIRST | jq -r '.payload')
# echo "PAYLOAD: $PAYLOAD"
HEAD=$(echo $PAYLOAD | jq -r '.head')
echo "HEAD: $HEAD"
echo "$HEAD" > "$PIC_DIR/pic-version"
URL="https://raw.githubusercontent.com/qtvhao/picloud/$HEAD/pic"
curl "$URL" -o pic && chmod +x pic
exit 0
fi
if [ "$arg1" == "redis" ]; then
set -e
./pic exec-local-or-remote "scripts/database" \
"install-redis" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "exec-local-or-remote" ]; then
set -e
scriptFile="$HOME/.pic/scripts/$3"
if [ ! -f "$PIC_DIR/pic-version" ]; then
./pic self-update
fi
PIC_VERSION=$(cat "$PIC_DIR/pic-version")
remoteUrl="https://raw.githubusercontent.com/qtvhao/picloud/$PIC_VERSION/$2/$3?$(date +%s)"
scriptFileExists=$(test -f "$2/$3" && echo "true" || echo "false")
if [ "$scriptFileExists" == "true" ]; then
# cp "$2/$3" "$scriptFile" || true
"$2/$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
else
LOCAL_SCRIPTS_DIR="$HOME/.pic/scripts/$PIC_VERSION/$2-$3"
if [ ! -d "$LOCAL_SCRIPTS_DIR" ]; then
mkdir -p "$LOCAL_SCRIPTS_DIR"
fi
if [ ! -f "$LOCAL_SCRIPTS_DIR/$3" ]; then
wget -nc -P "$LOCAL_SCRIPTS_DIR" -O "$LOCAL_SCRIPTS_DIR/$3" -o /dev/null "$remoteUrl"
chmod +x "$LOCAL_SCRIPTS_DIR/$3"
fi
# wget -nc -P "$LOCAL_SCRIPTS_DIR" "$remoteUrl"
# chmod +x "$LOCAL_SCRIPTS_DIR/$3"
"$LOCAL_SCRIPTS_DIR/$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
# curl -s $remoteUrl -o - | bash -s -- "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
fi
exit 0
fi
if [ "$arg1" == "pxe-boot" ]; then
set -e
./pic exec-local-or-remote "scripts/infrastructure" \
"pxe-boot" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
# for example ./pic pxe-boot 192.168.1.110 8c:dc:d4:34:0c:f0 sda eno1
exit 0
fi
if [ "$arg1" == "cilium" ]; then
echo "Installing Cilium"
helm repo add cilium https://helm.cilium.io/
helm install cilium cilium/cilium --namespace=kube-system
exit 0
fi
if [ "$arg1" == "registry-as-a-pull-through-cache" ]; then
echo "Installing registry-as-a-pull-through-cache"
fi
SCRIPTS_SUBFOLDER=""
case "$arg1" in
"recommended1"|"recommended2"|"recommended-is-already-installed"|"get-recommended-if-not-installed")
SCRIPTS_SUBFOLDER="scripts"
;;
"get-personal-access-token"|"applications-settings"|"mirror-org"|"succeed-pipeline"|"create-pipeline"|"get-pipelines"|"add-ssh-key-gitlab"|"declare-project-variables"|"git-folk"|"create-remote-mirrors"|"force-connect-gitlab-agent"|"refresh-token-gitlab"|"get-refresh-token-gitlab"|"install-gitlab"|"install-harbor")
SCRIPTS_SUBFOLDER="scripts/continuous-integration-deployment"
;;
"sync-across-nodes"|"install-longhorn"|"install-minio"|"install-minio-csi-driver")
SCRIPTS_SUBFOLDER="scripts/storage"
;;
"Registry-as-a-pull-through-cache")
SCRIPTS_SUBFOLDER="scripts/storage"
;;
"tailscale"|"hostname")
SCRIPTS_SUBFOLDER="scripts/infrastructure"
;;
"setup-static-IP"|"discover-nodes"|"dump-images"|"auto-join"|"logs"|"pull-image"|"pull-images")
SCRIPTS_SUBFOLDER="scripts/k3s"
;;
"start-pxe-server"|"auto-pxe"|"get-mac-address"|"list-authenticated-addresses"|"list-ssh-addresses"|"list-ip-addresses"|"unpack"|"print-available-node")
SCRIPTS_SUBFOLDER="scripts/fai"
;;
"stop-vms"|"delete-vms"|"create-vm")
SCRIPTS_SUBFOLDER="scripts/virtualbox"
;;
esac
if [ -n "$SCRIPTS_SUBFOLDER" ]; then
set -e
./pic exec-local-or-remote "$SCRIPTS_SUBFOLDER" \
$arg1 "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "grafana-promtail-loki" ]; then
set -e
./pic exec-local-or-remote "scripts/logging-monitoring" \
"grafana-promtail-loki" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "join-k3s" ]; then
set -e
./pic exec-local-or-remote "scripts/k3s" \
"join-k3s" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "init-terraform" ]; then
set -e
./pic exec-local-or-remote "scripts/infrastructure" \
"init-terraform" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "fai" ]; then
set -e
time ./pic exec-local-or-remote "scripts/fai" \
"install" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "dd" ]; then
set -e
./pic exec-local-or-remote "scripts/fai" \
"dd" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
# status=progress
fi
if [ "$arg1" == "pxe" ]; then
set -e
./pic exec-local-or-remote "scripts/fai" \
"pxe" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
# status=progress
fi
if [ "$arg1" == "wait-node-to-shutdown" ]; then
set -e
./pic exec-local-or-remote "scripts/fai" \
"wait-node-to-shutdown" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "list-mass-storage" ]; then
set -e
./pic exec-local-or-remote "scripts/fai" \
"list-mass-storage" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "$arg1" == "list-nodename" ]; then
set -e
./pic exec-local-or-remote "scripts/fai" \
"list-nodename" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "${10}"
exit 0
fi
if [ "alias-kubectl" == "$arg1" ]; then
set -e
cat ~/.bashrc | grep "alias kubectl=\"k3s kubectl\"" || echo "alias kubectl=\"k3s kubectl\"" >> ~/.bashrc
exit 0
fi
if [ "$arg1" == "wait-nodename" ]; then
set -e
CHECK_AT=$(date +%s)
echo "Waiting for new node to be ready"
echo "CHECK_AT: $CHECK_AT"
while true; do
nodes=$(./pic list-nodename)
# loop through lines in $nodes, echo
while IFS= read -r line; do
if [ -z "$line" ]; then
continue
fi
echo "line: $line"
IP_ADDRESS=$(echo $line | awk '{print $1}')
# echo "IP_ADDRESS: $IP_ADDRESS"
LAST_BOOT=$(echo $line | awk '{print $2}')
if [ -z "$LAST_BOOT" ]; then
continue
fi
if [ -z "$IP_ADDRESS" ]; then
continue
fi
if [ "$LAST_BOOT" -gt "$CHECK_AT" ]; then
echo "IP_ADDRESS: $IP_ADDRESS"
echo "LAST_BOOT: $LAST_BOOT"
echo "CHECK_AT: $CHECK_AT"
echo
echo " Node $IP_ADDRESS is ready"
exit 0
fi
done <<< "$nodes"
sleep 5
done
fi
if [ "$arg1" == "help" ]; then
./pic exec-local-or-remote "scripts" \
"help"
exit 0
fi
if [ "$arg1" == "local-registry" ]; then
set -e
docker rm -f docker-registry || true
docker run -d --name docker-registry -p 5000:5000 \
-v ./k8s/.docker-registry-data:/var/lib/registry \
-e REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io \
--restart always \
registry:2
REGISTRY_IP_ADDRESS=$(hostname -I | head -n1 | awk '{print $1}')
cat <<EOF > /etc/docker/daemon.json
{
"insecure-registries":["$REGISTRY_IP_ADDRESS:5000"],
"registry-mirrors": ["http://$REGISTRY_IP_ADDRESS:5000"],
"runtimes": {
"nvidia": {
"path": "nvidia-container-runtime",
"runtimeArgs": []
}
}
}
EOF
service docker restart
docker pull alpine
exit 0
fi
./pic help