]> CyberLeo.Net >> Repos - CDN/cdn-ports-overlay.git/blob - update
net/rsync: rename patch so that the ports system can auto-apply it
[CDN/cdn-ports-overlay.git] / update
1 #!/bin/sh
2
3 # Run as root
4 [ "$(id -u)" -eq 0 ] || exec sudo "${0}" "${@}"
5 cd "$(dirname "${0}")"
6
7 [ "${1}" = "-n" ] && NO_SYNC=true
8
9 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}"; }
10 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}"; }
11 wtf() { printf " \033[1;31m*\033[0m %s\n" "${*}"; kill $$; exit 1; }
12
13 sync="rsync://paka/freebsd-ports"
14
15 sync_opts="--archive --compress --delete --hard-links --sparse --stats --verbose"
16
17 ports=alba/ports
18 upstream=alba/srcs/freebsd/ports/upstream
19 prepare=alba/srcs/freebsd/ports/prepare
20
21 today="$(date +%Y-%m-%d)"
22 ports_origin="$(zfs get -H -o value origin "${ports}")"
23 ports_origin="${ports_origin%%@*}"
24 ports_fs="$(zfs get -H -o value mountpoint "${ports}")"
25 ports_fs="${ports_fs:-/usr/ports}"
26 upstream_fs="$(zfs get -H -o value mountpoint "${upstream}")"
27
28 # Make sure the ports tree is a descendent of the ports upstream
29 [ "${ports_origin}" = "${upstream}" ] || wtf "Target ${ports} is not a child of ${upstream}
30   (is ${ports_origin})"
31
32 if [ -z "${NO_SYNC}" ]
33 then
34   # Compute the next available snapshot
35   last_snapshot="$(zfs list -rHt snapshot -o name "${upstream}" | grep "@${today}_" | sort | tail -n 1 | sed -e 's/^.*_\([0-9]\{2\}\)$/\1/')"
36   if [ "${last_snapshot}" ]
37   then
38     snapshot="$(printf "${today}_%02u" "$(( ${last_snapshot} + 1 ))")"
39     echo "${today}_${last_snapshot} -> ${snapshot}"
40   else
41     snapshot="${today}_00"
42     echo "None -> ${snapshot}"
43   fi
44 else
45   # Don't bother creating a new snapshot if the tree will not be synchronized
46   last_snapshot="$(zfs list -rHt snapshot -o name "${upstream}" | sort | tail -n 1)"
47   snapshot="${last_snapshot##*@}"
48   echo "Using existing snapshot ${snapshot}"
49 fi
50
51 # Bail out if anything is /
52 [ -z "${ports_fs}"  -o -z "${upstream_fs}" ] && wtf "Writing to /? Are you nuts?"
53
54 if [ -z "${NO_SYNC}" ]
55 then
56   meh "Update"
57   rsync ${sync_opts} "${sync}/" "${upstream_fs}/" || wtf "update failed"
58
59   meh "Snapshot"
60   zfs snapshot "${upstream}@${snapshot}" || wtf "snapshot failed"
61 fi
62
63 meh "Clone"
64 zfs clone "${upstream}@${snapshot}" "${prepare}" || wtf "clone failed"
65 zfs set atime=off "${prepare}" || wtf "atime-off failed"
66
67 # Resolve filesystem mountpoints, now that everything should exist
68 prepare_fs="$(zfs get -H -o value mountpoint "${prepare}")"
69
70 meh "Overlay"
71 ( cd ports; find * | cpio -p "${prepare_fs}" ) || wtf "overlay failed"
72
73 meh "Patch"
74 for patch in patch/*.patch
75 do
76   meh "... ${patch}"
77   ( cd "${prepare_fs}"; patch -p0 ) < "${patch}" || wtf "patch failed"
78 done
79
80 meh "Install"
81 zfs set readonly=on "${prepare}" || wtf "readonly failed"
82 # Only move old ports tree out of the way if it exists
83 zfs rename "${ports}" "${ports}-bak" || wtf "backup rename failed"
84 zfs rename "${prepare}" "${ports}" || wtf "rename prepare -> ports failed"
85 zfs inherit mountpoint "${ports}-bak" || wtf "inherit failed"
86 zfs set mountpoint="${ports_fs}" "${ports}" || wtf "mountpoint failed"
87 zfs destroy "${ports}-bak" || wtf "destroy failed"
88
89 meh "All done"