-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_sccache.sh
executable file
·224 lines (186 loc) · 6.01 KB
/
deploy_sccache.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
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
# Sccache Deployment Script
# 作者:Groveer
# 创建日期:2024-11-22
# 描述:在 Arch Linux 或 Debian 系统上自动部署 Sccache 分布式编译系统
# 脚本权限要求:user, 可能需要 sudo
# The socket address the scheduler will listen on. It's strongly recommended
# to listen on localhost and put a HTTPS server in front of it.
# ==============================================================================
# public_addr = "127.0.0.1:10600"
# [client_auth]
# type = "token"
# token = "123456"
#
# [server_auth]
# type = "jwt_hs256"
# secret_key = "eBCniERli1CvqakwBm5qQVT0nxuXhpVYlQkxKq1uiM0"
# ==============================================================================
# 检查 sccache 和 bubblewrap 是否已安装
check_installed() {
local package=$1
if pacman -Qi "$package" &>/dev/null; then
return 0 # 已安装
else
return 1 # 未安装
fi
}
# 安装 sccache 和 bubblewrap
install_sccache_and_bubblewrap() {
if check_installed "sccache"; then
info "sccache 已安装,跳过安装步骤"
else
info "正在安装 sccache..."
sudo pacman -Sy --noconfirm sccache || error "安装 sccache 失败"
fi
if check_installed "bubblewrap"; then
info "bubblewrap 已安装,跳过安装步骤"
else
info "正在安装 bubblewrap..."
sudo pacman -Sy --noconfirm bubblewrap || error "安装 bubblewrap 失败"
fi
}
# 输出信息
info() {
echo "信息:$1"
}
tip() {
echo "提示:$1"
}
# 输出错误信息并退出
error() {
echo "错误:$1" >&2
exit 1
}
# 获取 tailscale0 网卡的 IP 地址
get_tailscale_ip() {
local interface="tailscale0"
local ip_address=$(ip -4 addr show "$interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [ -z "$ip_address" ]; then
error "无法获取 $interface 网卡的 IP 地址"
fi
echo "$ip_address"
}
# 生成 token
generate_token() {
local ip_address=$(get_tailscale_ip)
local secret_key="eBCniERli1CvqakwBm5qQVT0nxuXhpVYlQkxKq1uiM0"
local server="$ip_address:10501"
local server_token=$(sccache-dist auth generate-jwt-hs256-server-token --secret-key "$secret_key" --server "$server" 2>/dev/null)
if [ -z "$server_token" ]; then
error "生成 token 失败"
fi
echo "$server_token"
}
# 创建 sccache 配置文件
create_sccache_config() {
local config_file="/etc/sccache/server.conf"
local ip_address=$1
local server_token=$2
info "正在创建 sccache 配置文件..."
sudo mkdir -p "$(dirname "$config_file")"
sudo tee "$config_file" >/dev/null <<EOL
# This is where client toolchains will be stored.
cache_dir = "/tmp/toolchains"
# The maximum size of the toolchain cache, in bytes.
# If unspecified the default is 10GB.
# toolchain_cache_size = 10737418240
# A public IP address and port that clients will use to connect to this builder.
public_addr = "$ip_address:10501"
# The URL used to connect to the scheduler (should use https, given an ideal
# setup of a HTTPS server in front of the scheduler)
scheduler_url = "https://100.126.10.38"
[builder]
type = "overlay"
# The directory under which a sandboxed filesystem will be created for builds.
build_dir = "/tmp/build"
# The path to the bubblewrap version 0.3.0+ \`bwrap\` binary.
bwrap_path = "/usr/bin/bwrap"
[scheduler_auth]
type = "jwt_token"
# This will be generated by the \`generate-jwt-hs256-server-token\` command or
# provided by an administrator of the sccache cluster.
token = "$server_token"
EOL
if [ $? -eq 0 ]; then
info "sccache 配置文件创建成功"
else
error "sccache 配置文件创建失败"
fi
}
# 创建 systemd 配置文件
create_systemd_service() {
local service_file="/etc/systemd/system/sccache-server.service"
info "正在创建 systemd 配置文件..."
sudo tee "$service_file" >/dev/null <<EOL
[Unit]
Description=sccache-dist server
Wants=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/bin/sccache-dist server --config /etc/sccache/server.conf
[Install]
WantedBy=multi-user.target
EOL
if [ $? -eq 0 ]; then
info "systemd 配置文件创建成功"
else
error "systemd 配置文件创建失败"
fi
# 重新加载 systemd 配置并启用服务
sudo systemctl daemon-reload
sudo systemctl enable sccache-server.service
sudo systemctl start sccache-server.service
if [ $? -eq 0 ]; then
info "sccache-server 服务已启动并设置为开机自启"
else
error "sccache-server 服务启动失败"
fi
}
# 创建客户端配置文件
create_client_config() {
local config_file="$HOME/.config/sccache/config"
info "正在创建客户端配置文件..."
mkdir -p "$(dirname "$config_file")"
tee "$config_file" >/dev/null <<EOL
[dist]
# The URL used to connect to the scheduler (should use https, given an ideal
# setup of a HTTPS server in front of the scheduler)
scheduler_url = "https://100.126.10.38"
# Used for mapping local toolchains to remote cross-compile toolchains. Empty in
# this example where the client and build server are both Linux.
toolchains = []
# Size of the local toolchain cache, in bytes (5GB here, 10GB if unspecified).
toolchain_cache_size = 5368709120
[dist.auth]
type = "token"
# This should match the \`client_auth\` section of the scheduler config.
token = "123456"
EOL
if [ $? -eq 0 ]; then
info "客户端配置文件创建成功"
else
error "客户端配置文件创建失败"
fi
}
info_end() {
info "sccache 部署成功"
tip "rust 编译示例:"
tip "export RUSTC_WRAPPER=/usr/bin/sccache"
tip "cargo build"
tip "CMake 编译示例:"
tip "-DCMAKE_C_COMPILER_LAUNCHER=sccache"
tip "-DCMAKE_CXX_COMPILER_LAUNCHER=sccache"
}
# 主函数
main() {
install_sccache_and_bubblewrap
local ip_address=$(get_tailscale_ip)
local token=$(generate_token)
create_sccache_config "$ip_address" "$token"
create_systemd_service
create_client_config
info_end
}
# 执行主函数
main "$@"