forked from tuna/tunasync-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxc-images.sh
executable file
·69 lines (52 loc) · 2.5 KB
/
lxc-images.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
#!/bin/bash
# requires: lftp wget jq
set -e
set -o pipefail
BASE_URL="${TUNASYNC_UPSTREAM_URL:-"http://images.linuxcontainers.org"}"
function sync_lxc_images() {
repo_url="$1"
repo_dir="$2"
[[ ! -d "$repo_dir" ]] && mkdir -p "$repo_dir"
cd "$repo_dir"
lftp "${repo_url}" -e "mirror --verbose -P 5 ; bye"
echo "lftp returns $?"
}
echo "=== Downloading /meta/1.0 ==="
mkdir -p "${TUNASYNC_WORKING_DIR}/meta/1.0"
for i in index-system index-system.asc index-user index-user.asc; do
wget -O "${TUNASYNC_WORKING_DIR}/meta/1.0/$i.work-in-progress" "${BASE_URL}/meta/1.0/$i"
done
echo "=== Downloading /streams/v1 ==="
mkdir -p "${TUNASYNC_WORKING_DIR}/streams/v1"
wget -O "${TUNASYNC_WORKING_DIR}/streams/v1/index.json.work-in-progress" "${BASE_URL}/streams/v1/index.json"
jq -r '.index[].path' "${TUNASYNC_WORKING_DIR}/streams/v1/index.json.work-in-progress" | while read line; do
[[ ! -d "${TUNASYNC_WORKING_DIR}/$(dirname $line)" ]] && mkdir -p "${TUNASYNC_WORKING_DIR}/$(dirname $line)"
wget -O "${TUNASYNC_WORKING_DIR}/${line}.work-in-progress" "${BASE_URL}/${line}"
done
echo "=== Downloading images ==="
sync_lxc_images "${BASE_URL}/images" "${TUNASYNC_WORKING_DIR}/images"
images_json="${TUNASYNC_WORKING_DIR}/streams/v1/images.json.work-in-progress"
[[ -f "$images_json" ]] || exit 1
jq -r '.products[].versions[].items[].path' "$images_json" > /tmp/filelist.txt
cat /tmp/filelist.txt | while read line; do
# $line looks like 'images/ubuntu/xenial/armhf/default/20200219_07:42/rootfs.tar.xz'
if [[ ! -f "${TUNASYNC_WORKING_DIR}/${line}" ]]; then
echo "Error: ${TUNASYNC_WORKING_DIR}/${line} vanished"
exit 1
fi
done
echo "=== Replacing /meta/1.0 ==="
for i in index-system index-system.asc index-user index-user.asc; do
mv -f "${TUNASYNC_WORKING_DIR}/meta/1.0/$i.work-in-progress" "${TUNASYNC_WORKING_DIR}/meta/1.0/$i"
done
echo "=== Replacing /streams/v1 ==="
jq -r '.index[].path' "${TUNASYNC_WORKING_DIR}/streams/v1/index.json.work-in-progress" | while read line; do
mv -f "${TUNASYNC_WORKING_DIR}/${line}.work-in-progress" "${TUNASYNC_WORKING_DIR}/${line}"
done
mv -f "${TUNASYNC_WORKING_DIR}/streams/v1/index.json.work-in-progress" "${TUNASYNC_WORKING_DIR}/streams/v1/index.json"
echo "=== Removing old images ==="
cd "${TUNASYNC_WORKING_DIR}"
find images/ -maxdepth 5 -mindepth 5 -mtime +3 | while read line; do
# $line looks like 'images/ubuntu/xenial/armhf/default/20200217_07:42'
grep --quiet "$line" /tmp/filelist.txt || ( echo "Removing $line"; rm -rf "$line" )
done