|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Installs the passed in packages via macports. To make it fast enough |
| 4 | +# for CI, cache the installation as a .dmg file. To avoid |
| 5 | +# unnecessarily updating the cache, the cached image is only modified |
| 6 | +# when packages are installed or removed. Any package this script is |
| 7 | +# not instructed to install, will be removed again. |
| 8 | +# |
| 9 | +# This currently expects to be run in a macos cirrus-ci environment. |
| 10 | + |
| 11 | +set -e |
| 12 | +# set -x |
| 13 | + |
| 14 | +packages="$@" |
| 15 | + |
| 16 | +macports_url="https://github.com/macports/macports-base/releases/download/v2.8.1/MacPorts-2.8.1-13-Ventura.pkg" |
| 17 | +cache_dmg="macports.hfs.dmg" |
| 18 | + |
| 19 | +if [ "$CIRRUS_CI" != "true" ]; then |
| 20 | + echo "expect to be called within cirrus-ci" 1>2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +sudo mkdir -p /opt/local |
| 25 | +mkdir -p ${MACPORTS_CACHE}/ |
| 26 | + |
| 27 | +# If we are starting from clean cache, perform a fresh macports |
| 28 | +# install. Otherwise decompress the .dmg we created previously. |
| 29 | +# |
| 30 | +# After this we have a working macports installation, with an unknown set of |
| 31 | +# packages installed. |
| 32 | +new_install=0 |
| 33 | +update_cached_image=0 |
| 34 | +if [ -e ${MACPORTS_CACHE}/${cache_dmg}.zstd ]; then |
| 35 | + time zstd -T0 -d ${MACPORTS_CACHE}/${cache_dmg}.zstd -o ${cache_dmg} |
| 36 | + time sudo hdiutil attach -kernel ${cache_dmg} -owners on -shadow ${cache_dmg}.shadow -mountpoint /opt/local |
| 37 | +else |
| 38 | + new_install=1 |
| 39 | + curl -fsSL -o macports.pkg "$macports_url" |
| 40 | + time sudo installer -pkg macports.pkg -target / |
| 41 | + # this is a throwaway environment, and it'd be a few lines to gin |
| 42 | + # up a correct user / group when using the cache. |
| 43 | + echo macportsuser root | sudo tee -a /opt/local/etc/macports/macports.conf |
| 44 | +fi |
| 45 | +export PATH=/opt/local/sbin/:/opt/local/bin/:$PATH |
| 46 | + |
| 47 | +# mark all installed packages unrequested, that allows us to detect |
| 48 | +# packages that aren't needed anymore |
| 49 | +if [ -n "$(port -q installed installed)" ] ; then |
| 50 | + sudo port unsetrequested installed |
| 51 | +fi |
| 52 | + |
| 53 | +# if setting all the required packages as requested fails, we need |
| 54 | +# to install at least one of them |
| 55 | +if ! sudo port setrequested $packages > /dev/null 2>&1 ; then |
| 56 | + echo not all required packages installed, doing so now |
| 57 | + update_cached_image=1 |
| 58 | + # to keep the image small, we deleted the ports tree from the image... |
| 59 | + sudo port selfupdate |
| 60 | + # XXX likely we'll need some other way to force an upgrade at some |
| 61 | + # point... |
| 62 | + sudo port upgrade outdated |
| 63 | + sudo port install -N $packages |
| 64 | + sudo port setrequested $packages |
| 65 | +fi |
| 66 | + |
| 67 | +# check if any ports should be uninstalled |
| 68 | +if [ -n "$(port -q installed rleaves)" ] ; then |
| 69 | + echo superflous packages installed |
| 70 | + update_cached_image=1 |
| 71 | + sudo port uninstall --follow-dependencies rleaves |
| 72 | + |
| 73 | + # remove prior cache contents, don't want to increase size |
| 74 | + rm -f ${MACPORTS_CACHE}/* |
| 75 | +fi |
| 76 | + |
| 77 | +# Shrink installation if we created / modified it |
| 78 | +if [ "$new_install" -eq 1 -o "$update_cached_image" -eq 1 ]; then |
| 79 | + sudo /opt/local/bin/port clean --all installed |
| 80 | + sudo rm -rf /opt/local/var/macports/{software,sources}/* |
| 81 | +fi |
| 82 | + |
| 83 | +# If we're starting from a clean cache, start a new image. If we have |
| 84 | +# an image, but the contents changed, update the image in the cache |
| 85 | +# location. |
| 86 | +if [ "$new_install" -eq 1 ]; then |
| 87 | + # use a generous size, so additional software can be installed later |
| 88 | + time sudo hdiutil create -fs HFS+ -format UDRO -size 10g -layout NONE -srcfolder /opt/local/ ${cache_dmg} |
| 89 | + time zstd -T -10 -z ${cache_dmg} -o ${MACPORTS_CACHE}/${cache_dmg}.zstd |
| 90 | +elif [ "$update_cached_image" -eq 1 ]; then |
| 91 | + sudo hdiutil detach /opt/local/ |
| 92 | + time hdiutil convert -format UDRO ${cache_dmg} -shadow ${cache_dmg}.shadow -o updated.hfs.dmg |
| 93 | + rm ${cache_dmg}.shadow |
| 94 | + mv updated.hfs.dmg ${cache_dmg} |
| 95 | + time zstd --force -T -10 -z ${cache_dmg} -o ${MACPORTS_CACHE}/${cache_dmg}.zstd |
| 96 | + time sudo hdiutil attach -kernel ${cache_dmg} -owners on -shadow ${cache_dmg}.shadow -mountpoint /opt/local |
| 97 | +fi |
0 commit comments