-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtm.sh
115 lines (96 loc) · 3.91 KB
/
tm.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
#!/bin/bash
# 定义容器名
NAME='tm'
# 自定义字体彩色,read 函数,安装依赖函数
red(){ echo -e "\033[31m\033[01m$1$2\033[0m"; }
green(){ echo -e "\033[32m\033[01m$1$2\033[0m"; }
yellow(){ echo -e "\033[33m\033[01m$1$2\033[0m"; }
reading(){ read -rp "$(green "$1")" "$2"; }
# 必须以root运行脚本
check_root(){
[[ $(id -u) != 0 ]] && red " The script must be run as root, you can enter sudo -i and then download and run again." && exit 1
}
# 判断系统,并选择相应的指令集
check_operating_system(){
CMD=("$(grep -i pretty_name /etc/os-release 2>/dev/null | cut -d \" -f2)"
"$(hostnamectl 2>/dev/null | grep -i system | cut -d : -f2)"
"$(lsb_release -sd 2>/dev/null)" "$(grep -i description /etc/lsb-release 2>/dev/null | cut -d \" -f2)"
"$(grep . /etc/redhat-release 2>/dev/null)"
"$(grep . /etc/issue 2>/dev/null | cut -d \\ -f1 | sed '/^[ ]*$/d')"
)
for i in "${CMD[@]}"; do SYS="$i" && [[ -n $SYS ]] && break; done
REGEX=("debian" "ubuntu" "centos|red hat|kernel|oracle linux|amazon linux|alma|rocky")
RELEASE=("Debian" "Ubuntu" "CentOS")
PACKAGE_UPDATE=("apt -y update" "apt -y update" "yum -y update")
PACKAGE_INSTALL=("apt -y install" "apt -y install" "yum -y install")
PACKAGE_UNINSTALL=("apt -y autoremove" "apt -y autoremove" "yum -y autoremove")
for ((int = 0; int < ${#REGEX[@]}; int++)); do
[[ $(echo "$SYS" | tr '[:upper:]' '[:lower:]') =~ ${REGEX[int]} ]] && SYSTEM="${RELEASE[int]}" && break
done
[[ -z $SYSTEM ]] && red " ERROR: The script supports Debian, Ubuntu, CentOS or Alpine systems only.\n" && exit 1
}
# 判断宿主机的 IPv4 或双栈情况,没有拉取不了 docker
check_ipv4(){
! curl -s4m8 ip.sb | grep -q '\.' && red " ERROR:The host must have IPv4. " && exit 1
}
# 判断 CPU 架构
check_virt(){
ARCHITECTURE=$(uname -m)
case "$ARCHITECTURE" in
aarch64 ) ARCH=arm64v8;;
x64|x86_64|amd64 ) ARCH=latest;;
* ) red " ERROR: Unsupported architecture: $ARCHITECTURE\n" && exit 1;;
esac
}
# 输入 traffmonetizer 的个人 token
input_token(){
[ -z $TMTOKEN ] && reading " Enter your token, something end with =, if you do not find it, open https://traffmonetizer.com/?aff=247346: " TMTOKEN
}
container_build(){
# 宿主机安装 docker
green "\n Install docker.\n "
if ! systemctl is-active docker >/dev/null 2>&1; then
echo -e " \n Install docker \n "
if [ $SYSTEM = "CentOS" ]; then
${PACKAGE_INSTALL[int]} yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo &&
${PACKAGE_INSTALL[int]} docker-ce docker-ce-cli containerd.io
systemctl enable --now docker
else
${PACKAGE_INSTALL[int]} docker.io
fi
fi
# 删除旧容器(如有)
docker ps -a | awk '{print $NF}' | grep -qw "$NAME" && yellow " Remove the old traffmonetizer container.\n " && docker rm -f "$NAME" >/dev/null 2>&1
# 创建容器
yellow " Create the traffmonetizer container.\n "
docker run -d --name $NAME traffmonetizer/cli:$ARCH start accept --token "$TMTOKEN" >/dev/null 2>&1
# 创建 Towerwatch
[[ ! $(docker ps -a) =~ watchtower ]] && yellow " Create TowerWatch.\n " && docker run -d --name watchtower --restart always -p 2095:8080 -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --cleanup >/dev/null 2>&1
}
# 显示结果
result(){
docker ps -a | grep -q "$NAME" && docker ps -a | grep -q "watchtower" && green " Install success.\n" || red " install fail.\n"
}
# 卸载
uninstall(){
docker rm -f $(docker ps -a | grep -w "$NAME" | awk '{print $1}')
docker rmi -f $(docker images | grep traffmonetizer/cli | awk '{print $3}')
green "\n Uninstall containers and images complete.\n"
exit 0
}
# 传参
while getopts "UuT:t:" OPTNAME; do
case "$OPTNAME" in
'U'|'u' ) uninstall;;
'T'|'t' ) TMTOKEN=$OPTARG;;
esac
done
# 主程序
check_root
check_operating_system
check_ipv4
check_virt
input_token
container_build
result