-
Notifications
You must be signed in to change notification settings - Fork 2
/
mirror.zsh
186 lines (149 loc) · 5.56 KB
/
mirror.zsh
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
function cache_mirror_data() {
pkgcache=$(jq -r '.cachedir' ${config})
rm -rf ${pkgcache}
sync=$(jq -r .sync.enabled ${repo})
if [ "${sync}" = "false" ]; then
return 0
fi
mkdir -p ${pkgcache}/mirrors
nmirrors=$(jq -r '.mirrors | length' ${config})
for ((i=0; i < ${nmirrors}; i++)); do
if [ $(jq -r ".mirrors[${i}].enabled" ${config}) = "false" ]; then
continue
fi
name=$(jq -r ".mirrors[${i}].name" ${config})
reponame=$(jq -r ".name" ${repo})
mkdir ${pkgcache}/mirrors/${name}
mirrorconf="${pkgcache}/mirrors/${name}/config"
jq -r ".mirrors[${i}]" ${config} > ${mirrorconf}
syncuser=$(jq -r .user ${mirrorconf})
synchost=$(jq -r .hostname ${mirrorconf})
basedir=$(jq -r .basedir ${mirrorconf})
symlink=$(ssh ${syncuser}@${synchost} readlink ${basedir}/${reponame})
if [ ${#symlink} -eq 0 ]; then
echo "[-] Could not connect to mirror ${name}. Disabling mirror."
rm -rf ${pkgcache}/mirrors/${name}
continue
fi
bump=$((${symlink##*sync} % 2 + 1))
newsymlink="${basedir}/${reponame}-sync${bump}"
ln -s ${newsymlink} ${pkgcache}/mirrors/${name}/syncdir
done
}
function clean_mirrors() {
cachedir=$(jq -r .cachedir ${config})
for mirror in $(find ${cachedir}/mirrors -type d); do
if [ ! -f ${mirror}/config ]; then
continue
fi
syncuser=$(jq -r .user ${mirror}/config)
synchost=$(jq -r .hostname ${mirror}/config)
syncdir=$(readlink ${mirror}/syncdir)
ssh ${syncuser}@${synchost} "rm -rf ${syncdir}/*"
res=${?}
if [ ${res} -gt 0 ]; then
return ${res}
fi
ssh ${syncuser}@${synchost} "mkdir ${syncdir}/All ${syncdir}/Latest"
res=${?}
if [ ${res} -gt 0 ]; then
return ${res}
fi
done
return 0
}
function sync_repo_metadata() {
cachedir=$(jq -r .cachedir ${config})
datadir=$(jq -r .datadir ${config})
reponame=$(jq -r .name ${repo})
datadir="${datadir}/${reponame}-local/"
for mirror in $(find ${cachedir}/mirrors -type d); do
if [ ! -f ${mirror}/config ]; then
continue
fi
syncdir=$(readlink ${mirror}/syncdir)
syncuser=$(jq -r .user ${mirror}/config)
synchost=$(jq -r .hostname ${mirror}/config)
for ((i=0; i < 5; i++)); do
scp ${datadir}/Latest/* ${syncuser}@${synchost}:${syncdir}/Latest/
res=${?}
if [ ${res} -gt 0 ]; then
echo "[METADATA:Latest]" >> ${mirror}/syncfailed
sleep 10
continue
fi
scp ${datadir}/*.txz ${syncuser}@${synchost}:${syncdir}/
res=${?}
if [ ${res} -gt 0 ]; then
echo "[METADATA:Metadata]" >> ${mirror}/syncfailed
sleep 10
continue
fi
break
done
if [ -f ${mirror}/syncfailed ]; then
echo "[-] Some metadata failed to sync. Please check ${mirror}/syncfailed" >&2
fi
done
return 0
}
function promote_mirrors() {
cachedir=$(jq -r .cachedir ${config})
datadir=$(jq -r .datadir ${config})
reponame=$(jq -r .name ${repo})
for mirror in $(find ${cachedir}/mirrors -type d); do
if [ ! -f ${mirror}/config ]; then
continue
fi
name=$(jq -r '.name' ${mirror}/config)
if [ -f ${mirror}/syncfailed ]; then
# 5 sync errors in a row means that artifact failed to sync entirely.
# Less than 5 errors means the artifact did succeed in syncing.
if sort ${mirror}/syncfailed | uniq -c | awk '{print $1;}' | grep -q 5; then
echo "[-] Mirror ${name} failed to sync. Not promoting mirror." >&2
continue
fi
fi
syncdir=$(readlink ${mirror}/syncdir)
basedir=$(jq -r .basedir ${mirror}/config)
syncuser=$(jq -r .user ${mirror}/config)
synchost=$(jq -r .hostname ${mirror}/config)
ssh ${syncuser}@${synchost} \
"rm ${basedir}/${reponame} && ln -s ${syncdir} ${basedir}/${reponame}"
done
}
# This function is called from pkgbuild.zsh, which gets called via a
# Poudriere hook.
function sync_package() {
pkgname="${1}"
cachedir=$(jq -r .cachedir /tmp/pkgconfig.conf)
datadir=$(jq -r .datadir /tmp/pkgconfig.conf)
reponame=$(jq -r .name /tmp/pkgrepo.conf)
datadir="${datadir}/${reponame}-local/.building/All"
localpkg="${datadir}/${pkgname}.txz"
localhash=$(sha256 -q ${localpkg} 2> /dev/null)
if [ -z "${localhash}" ]; then
echo "${pkgname}" >> ${mirror}/syncfailed
fi
for mirror in $(find ${cachedir}/mirrors -type d); do
if [ ! -f ${mirror}/config ]; then
continue
fi
syncdir=$(readlink ${mirror}/syncdir)
syncuser=$(jq -r .user ${mirror}/config)
synchost=$(jq -r .hostname ${mirror}/config)
for ((i=0; i < 5; i++)); do
scp ${localpkg} ${syncuser}@${synchost}:${syncdir}/All/
remotehash=$(ssh ${syncuser}@${synchost} sha256 -q ${syncdir}/All/${pkgname}.txz 2> /dev/null)
if [ "${localhash}" = "${remotehash}" ]; then
break
fi
echo "${pkgname}" >> ${mirror}/syncfailed
sleep 10
done
if [ -f ${mirror}/syncfailed ]; then
echo "[-] Some packages failed to sync. Please check ${mirror}/syncfailed" >&2
fi
done
return 0
}