-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholidays.sh
130 lines (108 loc) · 2.65 KB
/
holidays.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
#!/bin/bash
set -e
DIR=/root/holidays
SERVICE_FILE="/etc/systemd/system/holidays.service"
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
GREEN='\e[32m'
RED='\e[31m'
RESET='\e[0m'
function get_arch() {
local arch=""
case $ARCH in
x86_64)
arch=amd64
;;
i386)
arch=386
;;
*)
arch=$ARCH
;;
esac
echo "$arch"
}
function add_systemd() {
cat <<EOL | sudo tee "$SERVICE_FILE" >/dev/null
[Unit]
Description=holidays api Service
After=network.target
[Service]
Environment=HOLIDAY_PORT=$1
ExecStart=$DIR/holidays
WorkingDirectory=$DIR
Type=simple
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOL
systemctl daemon-reload
systemctl start holidays.service
systemctl enable holidays.service
}
function install() {
mkdir -p "$DIR"
cd "$DIR" || exit 1
arch=$(get_arch)
UNAME="${OS}_${arch}"
URL=$(curl -s https://api.github.com/repos/xiaoxuan6/chinese-holidays-api/releases/latest | grep "browser_download_url" | grep "tar.gz" | cut -d '"' -f 4 | grep "$UNAME")
if [ -z "$URL" ]; then
echo -e "${RED}Unsupported platform: $(uname -s) $(uname -m)${RESET}"
exit 1
fi
curl -L -O "$URL"
FILENAME=$(echo "$URL" | cut -d '/' -f 9)
if [ ! -f "$FILENAME" ]; then
echo "url: $URL"
echo -e "${RED}filename $FILENAME dose not exist${RESET}"
exit 1
fi
tar xf "$FILENAME"
rm "$FILENAME" "LICENSE" "README.md"
mv chinese-holidays-api holidays
if [ -z "$1" ]; then
# shellcheck disable=SC2162
read -p "请输入 holidays 服务有效端口号:" port
else
port=$1
fi
if [[ "$port" =~ ^[0-9]+$ ]]; then
add_systemd "$port"
else
echo "无效的端口号: $port, 端口必须是数字"
exit 1
fi
echo -e "${GREEN}holidays service install done.${RESET}"
}
function uninstall() {
systemctl stop holidays.service
systemctl disable holidays.service
if [ -f "$SERVICE_FILE" ]; then
rm "$SERVICE_FILE"
echo "服务文件 $SERVICE_FILE 已删除"
else
echo "服务文件 $SERVICE_FILE 不存在!"
fi
systemctl daemon-reload
if [ -d "$DIR" ]; then
rm -rf "$DIR"
echo "文件夹 $DIR 已删除"
else
echo "文件夹 $DIR 不存在!"
fi
echo -e "${GREEN}holidays service uninstall successful!${RESET}"
}
case $1 in
install) install "$2" ;;
uninstall) uninstall ;;
reinstall)
remove
install
;;
*)
echo "Not found $1 option"
echo "Usage: $0 {install|uninstall|reinstall}"
echo ""
exit 1
;;
esac