Set up a Mac for a Federated Core, like CoreDesktop's configure
A macOS counterpart of CoreDesktop's configure, with the same drop-in items in configure.d/: - core: Mail, Calendar and Contacts through a configuration profile, Superpanel as an app, Nextcloud files through the Nextcloud app, and Element; with test-and-fix, password change and removal - tailscale: join the Core's network through headscale - backup: restic to Nextcloud through rclone, run by launchd through an app that can be given Full Disk Access - storage: SMB shares mounted the way Finder does, again at login and on network changes - agents, claude, opencode, hermes, sandbox: install the agents and run them in Anthropic's sandbox runtime (Seatbelt on macOS) - ai-servers: LLM servers on this Mac or the network, for the agents Apps and tools macOS lacks are offered for download, checked against their makers' Team IDs and notarization, or published SHA-256 sums. Everything runs on macOS' bash 3.2 with only built-in tools. smoketest/ runs scenarios in fresh macOS VMs with Tart. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Find the AI agents' real commands, outside the sandbox's own
|
||||
# (sandbox/bin), and the sandbox runtime. Sourced by configure.d
|
||||
# items and by sandbox/agent. Needs CONNECTOR.
|
||||
|
||||
SANDBOX_BIN="$CONNECTOR/sandbox/bin"
|
||||
FC_TOOLS="$HOME/.local/share/federated-connector"
|
||||
|
||||
# Where the agents' installers put them, in case your shell's PATH doesn't
|
||||
# have them yet (e.g. right after installing)
|
||||
AGENT_DIRS="$HOME/.local/bin:$HOME/.opencode/bin:$HOME/.hermes/bin:/opt/homebrew/bin:/usr/local/bin"
|
||||
|
||||
# The real command for an agent: the first on PATH, or in AGENT_DIRS, that
|
||||
# isn't in sandbox/bin
|
||||
agent_path() {
|
||||
local dir bin_dir IFS=:
|
||||
bin_dir="$(cd "$SANDBOX_BIN" 2> /dev/null && pwd -P)"
|
||||
for dir in $PATH:$AGENT_DIRS; do
|
||||
[ -n "$dir" ] && [ -d "$dir" ] || continue
|
||||
[ "$(cd "$dir" && pwd -P)" = "$bin_dir" ] && continue
|
||||
if [ -x "$dir/$1" ] && [ ! -d "$dir/$1" ]; then
|
||||
echo "$dir/$1"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
agent_installed() {
|
||||
agent_path "$1" > /dev/null
|
||||
}
|
||||
|
||||
# Run an agent's real command rather than its sandboxed one: setting an agent
|
||||
# up writes outside its sandbox
|
||||
unsandboxed() {
|
||||
local agent="$1" real
|
||||
shift
|
||||
real="$(agent_path "$agent")" || { echo "$agent isn't installed."; return 127; }
|
||||
"$real" "$@"
|
||||
}
|
||||
|
||||
# srt, and the Node.js it runs with: Federated Connector's own, or one
|
||||
# already installed
|
||||
srt_path() {
|
||||
PATH="$FC_TOOLS/srt/bin:$FC_TOOLS/node/bin:$PATH:/opt/homebrew/bin:/usr/local/bin" command -v srt
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Find the apps the Core's services need that macOS doesn't come
|
||||
# with, and offer to download and install them. Source this file
|
||||
# after lib/json.sh; it uses confirm_yes from configure.
|
||||
#
|
||||
# Each app is downloaded from its maker, and installed only if it's signed
|
||||
# with the maker's Developer ID (the Team ID below) and Gatekeeper accepts it
|
||||
# (it's notarized by Apple). The apps update themselves after that.
|
||||
#
|
||||
# To check an app's Team ID on a Mac where it's installed:
|
||||
# codesign -dv /Applications/<App>.app 2>&1 | grep TeamIdentifier
|
||||
|
||||
# app_info <name>: sets APP_TITLE, APP_BUNDLE, APP_TEAM, APP_KIND (dmg or
|
||||
# pkg) and APP_URL. Nextcloud's URL changes with each release, so app_url
|
||||
# looks it up.
|
||||
app_info() {
|
||||
case "$1" in
|
||||
element)
|
||||
APP_TITLE="Element" APP_BUNDLE="Element.app" APP_TEAM="7J4U792NQT" APP_KIND=dmg
|
||||
APP_URL="https://packages.element.io/desktop/install/macos/Element.dmg" ;;
|
||||
nextcloud)
|
||||
APP_TITLE="Nextcloud" APP_BUNDLE="Nextcloud.app" APP_TEAM="NKUJUXUJ3B" APP_KIND=pkg
|
||||
APP_URL="https://api.github.com/repos/nextcloud-releases/desktop/releases/latest" ;;
|
||||
tailscale)
|
||||
APP_TITLE="Tailscale" APP_BUNDLE="Tailscale.app" APP_TEAM="W5364U7YZB" APP_KIND=pkg
|
||||
APP_URL="https://pkgs.tailscale.com/stable/Tailscale-latest-macos.pkg" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Where the app is installed, if it is
|
||||
app_path() {
|
||||
app_info "$1" || return 1
|
||||
if [ -d "/Applications/$APP_BUNDLE" ]; then
|
||||
echo "/Applications/$APP_BUNDLE"
|
||||
elif [ -d "$HOME/Applications/$APP_BUNDLE" ]; then
|
||||
echo "$HOME/Applications/$APP_BUNDLE"
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
app_installed() {
|
||||
app_path "$1" > /dev/null
|
||||
}
|
||||
|
||||
# The download URL for app_info's app
|
||||
app_url() {
|
||||
case "$APP_URL" in
|
||||
https://api.github.com/*)
|
||||
curl -sf --max-time 30 "$APP_URL" \
|
||||
| json_get 'd.assets.map(function (x) { return x.browser_download_url; })
|
||||
.filter(function (u) { return /\.pkg$/.test(u); })[0]' ;;
|
||||
*) echo "$APP_URL" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# The Team ID a signed installer package or app is signed with
|
||||
pkg_team() {
|
||||
pkgutil --check-signature "$1" 2> /dev/null \
|
||||
| sed -nE 's/^ *1\. Developer ID Installer: .*\(([A-Z0-9]{10})\)$/\1/p'
|
||||
}
|
||||
|
||||
app_team() {
|
||||
codesign -dv "$1" 2>&1 | sed -n 's/^TeamIdentifier=//p'
|
||||
}
|
||||
|
||||
verify_team() {
|
||||
local what="$1" team="$2"
|
||||
if [ "$team" != "$APP_TEAM" ]; then
|
||||
echo "$RED$what isn't signed by $APP_TITLE's maker (Team ID ${team:-none}, not $APP_TEAM). Not installing it.$RESET"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Install a downloaded .pkg, once it's checked. The installer needs admin.
|
||||
install_pkg() {
|
||||
local pkg="$1"
|
||||
verify_team "The download" "$(pkg_team "$pkg")" || return 1
|
||||
if ! spctl --assess --type install "$pkg" 2> /dev/null; then
|
||||
echo "${RED}Gatekeeper doesn't accept the download (it isn't notarized). Not installing it.$RESET"
|
||||
return 1
|
||||
fi
|
||||
echo "Installing $APP_TITLE needs your Mac password."
|
||||
sudo installer -pkg "$pkg" -target / > /dev/null
|
||||
}
|
||||
|
||||
# Copy the app out of a downloaded .dmg into /Applications, once it's checked
|
||||
install_dmg() {
|
||||
local dmg="$1" mount="$2/mount" app status=0
|
||||
hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$mount" "$dmg" > /dev/null || return 1
|
||||
app="$mount/$APP_BUNDLE"
|
||||
if [ ! -d "$app" ]; then
|
||||
echo "The download doesn't have $APP_BUNDLE in it."
|
||||
status=1
|
||||
elif ! codesign --verify --deep --strict "$app" 2> /dev/null; then
|
||||
echo "$RED$APP_BUNDLE's signature doesn't check out. Not installing it.$RESET"
|
||||
status=1
|
||||
elif ! verify_team "$APP_BUNDLE" "$(app_team "$app")"; then
|
||||
status=1
|
||||
elif ! spctl --assess --type execute "$app" 2> /dev/null; then
|
||||
echo "${RED}Gatekeeper doesn't accept $APP_BUNDLE (it isn't notarized). Not installing it.$RESET"
|
||||
status=1
|
||||
elif ! ditto "$app" "/Applications/$APP_BUNDLE" 2> /dev/null; then
|
||||
echo "Copying $APP_TITLE to Applications needs your Mac password."
|
||||
sudo ditto "$app" "/Applications/$APP_BUNDLE" || status=1
|
||||
fi
|
||||
hdiutil detach -quiet "$mount" || hdiutil detach -quiet -force "$mount"
|
||||
return $status
|
||||
}
|
||||
|
||||
# Download and install an app
|
||||
app_install() {
|
||||
local name="$1" url tmp status
|
||||
app_info "$name" || return 1
|
||||
url="$(app_url)"
|
||||
[ -n "$url" ] || { echo "Couldn't find where to download $APP_TITLE."; return 1; }
|
||||
tmp="$(mktemp -d)" || return 1
|
||||
echo "Downloading $APP_TITLE from $url"
|
||||
if curl -fL --progress-bar --max-time 1800 -o "$tmp/download.$APP_KIND" "$url"; then
|
||||
"install_$APP_KIND" "$tmp/download.$APP_KIND" "$tmp"
|
||||
status=$?
|
||||
else
|
||||
echo "Couldn't download $APP_TITLE."
|
||||
status=1
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
[ $status -eq 0 ] && app_installed "$name" || return 1
|
||||
echo "Installed $APP_TITLE."
|
||||
}
|
||||
|
||||
# app_ensure <name> <why it's needed>: offer to install the app if it's
|
||||
# missing. Fails if it's still missing.
|
||||
app_ensure() {
|
||||
app_installed "$1" && return 0
|
||||
echo "$APP_TITLE isn't on this Mac. $2"
|
||||
confirm_yes "Download and install $APP_TITLE?" || return 1
|
||||
app_install "$1"
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Read and edit JSON for configure.d items. Source this file.
|
||||
#
|
||||
# macOS has no jq before macOS 15, and python3 needs the Command Line Tools,
|
||||
# so this uses JavaScript for Automation (osascript -l JavaScript), which
|
||||
# every macOS has. Expressions are JavaScript; the JSON is "d", and any
|
||||
# arguments after the expression are a[0], a[1], ... so values never have to
|
||||
# be pasted into the code.
|
||||
|
||||
# json_get <expression> [args...] < file.json
|
||||
# Prints the expression's value: strings as they are, anything else as JSON.
|
||||
# Fails if the input isn't JSON, or the value is undefined, null, false or "".
|
||||
# shellcheck disable=SC2016
|
||||
json_get() {
|
||||
osascript -l JavaScript -e '
|
||||
function run(argv) {
|
||||
ObjC.import("Foundation");
|
||||
var data = $.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile;
|
||||
var text = $.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding).js;
|
||||
var d = JSON.parse(text);
|
||||
var v;
|
||||
try {
|
||||
v = (new Function("d", "a", "return (" + argv[0] + ");"))(d, argv.slice(1));
|
||||
} catch (e) {
|
||||
v = undefined;
|
||||
}
|
||||
if (v === undefined || v === null || v === false || v === "") throw new Error("no value");
|
||||
return typeof v === "string" ? v : JSON.stringify(v);
|
||||
}' "$@" 2> /dev/null
|
||||
}
|
||||
|
||||
# json_edit <file> <statements> [args...]
|
||||
# Runs JavaScript statements that change "d", the file's JSON, and saves it.
|
||||
# A missing or empty file starts as {}. Leaves the file alone if it isn't
|
||||
# valid JSON.
|
||||
# shellcheck disable=SC2016
|
||||
json_edit() {
|
||||
local file="$1"
|
||||
mkdir -p "$(dirname "$file")" || return 1
|
||||
osascript -l JavaScript -e '
|
||||
function run(argv) {
|
||||
ObjC.import("Foundation");
|
||||
var path = argv[0];
|
||||
var s = $.NSString.stringWithContentsOfFileEncodingError(path, $.NSUTF8StringEncoding, null);
|
||||
var text = s.isNil() ? "" : s.js;
|
||||
var d = text.trim() === "" ? {} : JSON.parse(text);
|
||||
(new Function("d", "a", argv[1]))(d, argv.slice(2));
|
||||
var out = $(JSON.stringify(d, null, 2) + "\n");
|
||||
if (!out.writeToFileAtomicallyEncodingError(path, true, $.NSUTF8StringEncoding, null)) {
|
||||
throw new Error("could not write " + path);
|
||||
}
|
||||
}' "$@" > /dev/null 2>&1 || { echo "Couldn't change $file; is it valid JSON?"; return 1; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Keep secrets in your login keychain. Source this file; the
|
||||
# scripts in system/ do too.
|
||||
#
|
||||
# Items the security tool makes can be read back by it without asking, so
|
||||
# background jobs (backups) can use them while you're logged in.
|
||||
|
||||
# keychain_load <service> <account>
|
||||
keychain_load() {
|
||||
security find-generic-password -s "$1" -a "$2" -w 2> /dev/null
|
||||
}
|
||||
|
||||
# keychain_save <service> <account> <secret>
|
||||
# Saved through security's interactive mode, so the secret isn't in the
|
||||
# process list (printf is a shell builtin). That mode splits on spaces and
|
||||
# reads quotes, so only secrets without them are taken: Nextcloud's app
|
||||
# passwords and the passphrases this makes.
|
||||
keychain_save() {
|
||||
if [[ ! "$3" =~ ^[A-Za-z0-9+/=._-]+$ ]]; then
|
||||
echo "That secret has characters the keychain helper can't save." >&2
|
||||
return 1
|
||||
fi
|
||||
printf 'add-generic-password -U -s "%s" -a "%s" -l "%s" -w "%s"\n' "$1" "$2" "$1 ($2)" "$3" \
|
||||
| security -i > /dev/null 2>&1 \
|
||||
&& [ "$(keychain_load "$1" "$2")" = "$3" ]
|
||||
}
|
||||
|
||||
# keychain_delete <service> <account>
|
||||
keychain_delete() {
|
||||
security delete-generic-password -s "$1" -a "$2" > /dev/null 2>&1
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Find what's on the local network over mDNS (Bonjour), for the
|
||||
# configure.d items that set up servers and storage on it. Source
|
||||
# this file.
|
||||
#
|
||||
# macOS has dns-sd rather than avahi-browse. dns-sd keeps browsing until it's
|
||||
# stopped, and macOS has no timeout command, so run_for stops it.
|
||||
|
||||
# run_for <seconds> <command> [args]: run the command, and stop it after that
|
||||
# many seconds
|
||||
run_for() {
|
||||
local seconds="$1" pid
|
||||
shift
|
||||
"$@" &
|
||||
pid=$!
|
||||
sleep "$seconds"
|
||||
kill -INT "$pid" 2> /dev/null
|
||||
wait "$pid" 2> /dev/null
|
||||
return 0
|
||||
}
|
||||
|
||||
# The instance names dns-sd -B lists, which can have spaces in them: they're
|
||||
# the rest of each line from where the "Instance Name" heading starts
|
||||
dns_sd_names() {
|
||||
awk '
|
||||
/Instance Name/ { start = index($0, "Instance Name"); next }
|
||||
start && $2 == "Add" { print substr($0, start) }'
|
||||
}
|
||||
|
||||
# The service types announced on the network, e.g. _smb._tcp
|
||||
mdns_types() {
|
||||
run_for 3 dns-sd -B _services._dns-sd._udp local. 2> /dev/null \
|
||||
| awk '$2 == "Add" { split($6, proto, "."); print $7 "." proto[1] }' | sort -u
|
||||
}
|
||||
|
||||
# The instances of a service type, one name per line
|
||||
mdns_instances() {
|
||||
run_for 3 dns-sd -B "$1" local. 2> /dev/null | dns_sd_names | sort -u
|
||||
}
|
||||
|
||||
# Where an instance is: "host port", host without the trailing dot
|
||||
mdns_resolve() {
|
||||
run_for 2 dns-sd -L "$1" "$2" local. 2> /dev/null \
|
||||
| sed -nE 's/.* can be reached at ([^ ]+)\.:([0-9]+).*/\1 \2/p' | head -n 1
|
||||
}
|
||||
|
||||
# Every instance of the types given (or of all types): "name|type|host|port"
|
||||
mdns_services() {
|
||||
local dir types type name n=0
|
||||
dir="$(mktemp -d)" || return 1
|
||||
if [ $# -gt 0 ]; then types="$*"; else types="$(mdns_types)"; fi
|
||||
for type in $types; do
|
||||
mdns_instances "$type" > "$dir/browse.$type" &
|
||||
done
|
||||
wait
|
||||
for type in $types; do
|
||||
while IFS= read -r name; do
|
||||
[ -n "$name" ] || continue
|
||||
n=$((n + 1))
|
||||
# shellcheck disable=SC2046
|
||||
( set -- $(mdns_resolve "$name" "$type")
|
||||
[ -n "$1" ] && printf '%s|%s|%s|%s\n' "$name" "$type" "$1" "$2" ) > "$dir/resolve.$n" &
|
||||
done < "$dir/browse.$type"
|
||||
done
|
||||
wait
|
||||
cat "$dir"/resolve.* 2> /dev/null | sort -u
|
||||
rm -rf "$dir"
|
||||
}
|
||||
|
||||
# This Mac's own .local name, which mdns_hosts leaves out
|
||||
mdns_own_host() {
|
||||
echo "$(scutil --get LocalHostName 2> /dev/null).local"
|
||||
}
|
||||
|
||||
# The other machines on the network: one .local name per line
|
||||
mdns_hosts() {
|
||||
local own
|
||||
own="$(mdns_own_host)"
|
||||
mdns_services | cut -d'|' -f3 | grep -vixF "$own" | sort -fu
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Nextcloud app passwords for configure.d items, and keeping them in
|
||||
# your login keychain. Source this file after lib/json.sh
|
||||
# and lib/keychain.sh.
|
||||
|
||||
KEYCHAIN_SERVICE="Federated Connector"
|
||||
|
||||
# Basic auth header for curl, read from stdin (-H @-) so the password isn't
|
||||
# in the process list (printf is a shell builtin)
|
||||
auth_header() {
|
||||
printf 'Authorization: Basic %s\n' "$(printf '%s:%s' "$1" "$2" | base64 | tr -d '\n')"
|
||||
}
|
||||
|
||||
# Create a Nextcloud app password, so this Mac doesn't keep your Core
|
||||
# password for Calendar and Contacts. It shows in Nextcloud under Settings >
|
||||
# Security with the name given, where it can be revoked. Uses CORE_USER,
|
||||
# PASSWORD and NEXTCLOUD; sets APP_PASSWORD. Returns 2 if Nextcloud says the
|
||||
# password is wrong.
|
||||
nextcloud_app_password() {
|
||||
local name="$1" response code
|
||||
response="$(auth_header "$CORE_USER" "$PASSWORD" | curl -s --max-time 30 -w '\n%{http_code}' -H @- \
|
||||
-H 'OCS-APIRequest: true' -A "$name" \
|
||||
"$NEXTCLOUD/ocs/v2.php/core/getapppassword?format=json")"
|
||||
code="${response##*$'\n'}"
|
||||
case "$code" in
|
||||
200) APP_PASSWORD="$(json_get 'd.ocs.data.apppassword' <<< "${response%$'\n'*}")"
|
||||
[ -n "$APP_PASSWORD" ] ;;
|
||||
401) return 2 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Revoke an app password, signing in with it. Uses CORE_USER and NEXTCLOUD.
|
||||
nextcloud_revoke_app_password() {
|
||||
auth_header "$CORE_USER" "$1" | curl -sf --max-time 30 -o /dev/null -X DELETE -H @- \
|
||||
-H 'OCS-APIRequest: true' "$NEXTCLOUD/ocs/v2.php/core/apppassword"
|
||||
}
|
||||
|
||||
# Sign in to Nextcloud as CORE_USER with a password ($1), or PASSWORD if not
|
||||
# given. Signing in with your Core password (not an app password) also turns
|
||||
# back on the app passwords Nextcloud turned off when the Core password
|
||||
# changed. Uses NEXTCLOUD. Returns 2 if Nextcloud says the password is wrong.
|
||||
nextcloud_sign_in() {
|
||||
local code
|
||||
code="$(auth_header "$CORE_USER" "${1:-$PASSWORD}" | curl -s --max-time 30 -o /dev/null -w '%{http_code}' -H @- \
|
||||
-H 'OCS-APIRequest: true' "$NEXTCLOUD/ocs/v2.php/cloud/user?format=json")"
|
||||
case "$code" in
|
||||
200) return 0 ;;
|
||||
401) return 2 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# The app password this Mac gave Calendar and Contacts is kept in your login
|
||||
# keychain too, so it can be put in the profile again (e.g. when your Core
|
||||
# password changes) without making another, and revoked when you remove the
|
||||
# Core. Uses KEYCHAIN_ACCOUNT.
|
||||
|
||||
app_password_load() {
|
||||
keychain_load "$KEYCHAIN_SERVICE" "$KEYCHAIN_ACCOUNT"
|
||||
}
|
||||
|
||||
app_password_save() {
|
||||
keychain_save "$KEYCHAIN_SERVICE" "$KEYCHAIN_ACCOUNT" "$1"
|
||||
}
|
||||
|
||||
app_password_delete() {
|
||||
keychain_delete "$KEYCHAIN_SERVICE" "$KEYCHAIN_ACCOUNT"
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Configuration profiles (.mobileconfig) for configure.d items.
|
||||
# Source this file; it uses wait_until from configure.
|
||||
#
|
||||
# A configuration profile is how macOS itself sets up Mail, Calendar and
|
||||
# Contacts accounts: each account is a payload in the profile, and installing
|
||||
# the profile adds the accounts, with their passwords in the keychain.
|
||||
# Removing the profile removes them again.
|
||||
#
|
||||
# Since macOS 11 only you can install a profile (profiles install no longer
|
||||
# works without device management), so profile_install opens it, which puts
|
||||
# it under Downloaded in System Settings > Profiles, and waits until you've
|
||||
# installed it there. A profile with the same PayloadIdentifier as one that's
|
||||
# installed replaces it, and payloads keep their PayloadUUID between versions,
|
||||
# so the accounts are changed rather than added again.
|
||||
|
||||
PROFILES_PANE="x-apple.systempreferences:com.apple.Profiles-Settings.extension"
|
||||
|
||||
xml_escape() {
|
||||
local s="$1"
|
||||
s="${s//&/\&}"
|
||||
s="${s//</\<}"
|
||||
s="${s//>/\>}"
|
||||
s="${s//\"/\"}"
|
||||
printf '%s' "$s"
|
||||
}
|
||||
|
||||
# The same UUID each time for the same name, so a payload is the same account
|
||||
# in each version of the profile
|
||||
stable_uuid() {
|
||||
md5 -q -s "$1" | tr a-f A-F \
|
||||
| sed -E 's/^(.{8})(.{4})(.{4})(.{4})(.{12})$/\1-\2-\3-\4-\5/'
|
||||
}
|
||||
|
||||
# The installed configuration profiles, as XML. The form of the command
|
||||
# changed in macOS 10.13; the old one still works on some versions.
|
||||
profiles_xml() {
|
||||
profiles show -type configuration -output stdout-xml 2> /dev/null \
|
||||
|| profiles -P -o stdout-xml 2> /dev/null
|
||||
}
|
||||
|
||||
# profile_installed <identifier> [uuid]: with a UUID, only that version
|
||||
profile_installed() {
|
||||
local xml
|
||||
xml="$(profiles_xml)" || return 1
|
||||
grep -qF "<string>$1</string>" <<< "$xml" || return 1
|
||||
[ -z "$2" ] || grep -qF "<string>$2</string>" <<< "$xml"
|
||||
}
|
||||
|
||||
# profile_install <file> <identifier> <uuid> <name>
|
||||
# Opens the profile and waits for you to install it in System Settings. The
|
||||
# file has passwords in it, so the caller deletes it afterwards either way.
|
||||
profile_install() {
|
||||
local file="$1" id="$2" uuid="$3" name="$4"
|
||||
open "$file" || return 1
|
||||
sleep 1
|
||||
open "$PROFILES_PANE" 2> /dev/null
|
||||
echo
|
||||
echo "System Settings shows \"$name\" under Downloaded (in Privacy & Security >"
|
||||
echo "Profiles, or General > Device Management). Double-click it, choose Install,"
|
||||
echo "and enter your Mac password when asked."
|
||||
if wait_until 480 profile_installed "$id" "$uuid"; then
|
||||
echo "Installed \"$name\"."
|
||||
return 0
|
||||
fi
|
||||
echo "\"$name\" wasn't installed. macOS forgets a downloaded profile after a few"
|
||||
echo "minutes; to try again, run this again."
|
||||
return 1
|
||||
}
|
||||
|
||||
# Removing a profile you installed yourself works from the command line on
|
||||
# most versions of macOS; if it doesn't, you remove it in System Settings.
|
||||
profile_remove() {
|
||||
local id="$1" name="$2"
|
||||
profile_installed "$id" || return 0
|
||||
profiles remove -identifier "$id" > /dev/null 2>&1 \
|
||||
|| profiles -R -p "$id" > /dev/null 2>&1
|
||||
profile_installed "$id" || return 0
|
||||
open "$PROFILES_PANE" 2> /dev/null
|
||||
echo "In System Settings > Profiles, choose \"$name\", then the - button to remove it."
|
||||
wait_until 300 not_installed "$id"
|
||||
}
|
||||
|
||||
not_installed() {
|
||||
! profile_installed "$1"
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : SMB shares: which are mounted, and mounting one the way Finder
|
||||
# does. Sourced by configure.d/60-storage.sh and system/storage-mount.
|
||||
|
||||
# smb://user@host/share and //user@host/share (as mount shows it) the same
|
||||
# way: no user, lower case, host without .local or Bonjour's ._smb._tcp.local,
|
||||
# %20 as a space
|
||||
share_key() {
|
||||
local s="${1#smb:}" host share
|
||||
s="${s#//}"
|
||||
s="${s#*@}"
|
||||
host="${s%%/*}"
|
||||
share="${s#*/}"
|
||||
host="${host%._smb._tcp.local}"
|
||||
host="${host%.local}"
|
||||
host="${host%.}"
|
||||
echo "$host/${share//%20/ }" | tr '[:upper:]' '[:lower:]'
|
||||
}
|
||||
|
||||
# The SMB shares mounted now: "//user@host/share|mount point" per line
|
||||
smb_mounts() {
|
||||
mount -t smbfs | sed -nE 's#^(//[^ ]+) on (.+) \(smbfs.*#\1|\2#p'
|
||||
}
|
||||
|
||||
# The mount point of a share, if it's mounted
|
||||
mounted_at() {
|
||||
local key line
|
||||
key="$(share_key "$1")"
|
||||
while IFS= read -r line; do
|
||||
if [ "$(share_key "${line%%|*}")" = "$key" ]; then
|
||||
echo "${line#*|}"
|
||||
return 0
|
||||
fi
|
||||
done < <(smb_mounts)
|
||||
return 1
|
||||
}
|
||||
|
||||
host_of() {
|
||||
local s="${1#*://}"
|
||||
s="${s#*@}"
|
||||
echo "${s%%/*}"
|
||||
}
|
||||
|
||||
mount_share() {
|
||||
osascript -e 'on run argv' -e 'mount volume (item 1 of argv)' -e 'end run' "$1" > /dev/null 2>&1
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Purpose : Find the command-line tools some items need (restic and rclone
|
||||
# for backups; Node.js and Anthropic's sandbox runtime, srt, for the
|
||||
# agent sandbox), and offer to download them. Source this file after
|
||||
# lib/json.sh; it uses confirm_yes from configure.
|
||||
#
|
||||
# A tool already installed, e.g. with Homebrew, is used as it is. Otherwise
|
||||
# it's downloaded from its maker's releases into TOOLS, checked against the
|
||||
# SHA-256 sums the release publishes, and kept to itself there: nothing in
|
||||
# /usr/local or your shell changes. These are command-line programs, not
|
||||
# apps, so they aren't notarized; downloaded with curl, Gatekeeper doesn't
|
||||
# quarantine them.
|
||||
|
||||
TOOLS="$HOME/.local/share/federated-connector"
|
||||
TOOLS_BIN="$TOOLS/bin"
|
||||
|
||||
# Where to look for a tool, in order. launchd jobs start with a short PATH,
|
||||
# so Homebrew's directories are named too.
|
||||
TOOLS_PATH="$TOOLS_BIN:$TOOLS/node/bin:$TOOLS/srt/bin:/opt/homebrew/bin:/usr/local/bin"
|
||||
|
||||
# The full path of a tool, if it's installed
|
||||
tool_path() {
|
||||
PATH="$TOOLS_PATH:$PATH" command -v "$1"
|
||||
}
|
||||
|
||||
# restic and rclone name the architectures differently from Node
|
||||
machine_arch() {
|
||||
case "$(uname -m)" in
|
||||
arm64) echo arm64 ;;
|
||||
*) echo amd64 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# download_checked <url> <sums url> <file name in sums> <destination>
|
||||
download_checked() {
|
||||
local url="$1" sums_url="$2" name="$3" dest="$4" expected actual
|
||||
echo "Downloading $url"
|
||||
curl -fL --progress-bar --max-time 1800 -o "$dest" "$url" || return 1
|
||||
expected="$(curl -fsL --max-time 30 "$sums_url" | awk -v f="$name" '$2 == f || $2 == "*" f { print $1; exit }')"
|
||||
actual="$(shasum -a 256 "$dest" | cut -d' ' -f1)"
|
||||
if [ -z "$expected" ] || [ "$expected" != "$actual" ]; then
|
||||
echo "${RED}The download doesn't match the checksum its release publishes. Not installing it.$RESET"
|
||||
rm -f "$dest"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
install_restic() {
|
||||
local tmp version name status=1
|
||||
version="$(curl -fsL --max-time 30 https://api.github.com/repos/restic/restic/releases/latest \
|
||||
| json_get 'd.tag_name.replace(/^v/, "")')" || { echo "Couldn't find restic's latest release."; return 1; }
|
||||
name="restic_${version}_darwin_$(machine_arch).bz2"
|
||||
tmp="$(mktemp -d)" || return 1
|
||||
if download_checked "https://github.com/restic/restic/releases/download/v$version/$name" \
|
||||
"https://github.com/restic/restic/releases/download/v$version/SHA256SUMS" "$name" "$tmp/$name" \
|
||||
&& bunzip2 "$tmp/$name" && mkdir -p "$TOOLS_BIN" \
|
||||
&& install -m 755 "$tmp/${name%.bz2}" "$TOOLS_BIN/restic"; then
|
||||
status=0
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
return $status
|
||||
}
|
||||
|
||||
install_rclone() {
|
||||
local tmp version name status=1
|
||||
version="$(curl -fsL --max-time 30 https://downloads.rclone.org/version.txt | sed -n 's/^rclone v//p')"
|
||||
[ -n "$version" ] || { echo "Couldn't find rclone's latest release."; return 1; }
|
||||
name="rclone-v$version-osx-$(machine_arch).zip"
|
||||
tmp="$(mktemp -d)" || return 1
|
||||
if download_checked "https://downloads.rclone.org/v$version/$name" \
|
||||
"https://downloads.rclone.org/v$version/SHA256SUMS" "$name" "$tmp/$name" \
|
||||
&& unzip -q "$tmp/$name" -d "$tmp" && mkdir -p "$TOOLS_BIN" \
|
||||
&& install -m 755 "$tmp/${name%.zip}/rclone" "$TOOLS_BIN/rclone"; then
|
||||
status=0
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
return $status
|
||||
}
|
||||
|
||||
# srt needs Node.js 22.12 or later
|
||||
node_ok() {
|
||||
local node version
|
||||
node="$(tool_path node)" || return 1
|
||||
version="$("$node" -p 'process.versions.node' 2> /dev/null)" || return 1
|
||||
[ "$(printf '%s\n' 22.12.0 "$version" | sort -t. -k1,1n -k2,2n -k3,3n | head -n 1)" = 22.12.0 ]
|
||||
}
|
||||
|
||||
# The current long-term support release, kept in TOOLS/node
|
||||
install_node() {
|
||||
local tmp version arch name status=1
|
||||
version="$(curl -fsL --max-time 30 https://nodejs.org/dist/index.json \
|
||||
| json_get 'd.filter(function (x) { return x.lts; })[0].version')" \
|
||||
|| { echo "Couldn't find Node.js' current release."; return 1; }
|
||||
case "$(uname -m)" in arm64) arch=arm64 ;; *) arch=x64 ;; esac
|
||||
name="node-$version-darwin-$arch.tar.gz"
|
||||
tmp="$(mktemp -d)" || return 1
|
||||
if download_checked "https://nodejs.org/dist/$version/$name" \
|
||||
"https://nodejs.org/dist/$version/SHASUMS256.txt" "$name" "$tmp/$name" \
|
||||
&& tar -xzf "$tmp/$name" -C "$tmp" && mkdir -p "$TOOLS"; then
|
||||
rm -rf "$TOOLS/node" && mv "$tmp/${name%.tar.gz}" "$TOOLS/node" && status=0
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
return $status
|
||||
}
|
||||
|
||||
# srt from npm, into TOOLS/srt, with the Node.js found or installed. It has no
|
||||
# install scripts of its own; the ones npm would warn it blocked belong to a
|
||||
# Windows-only package it optionally depends on.
|
||||
install_srt() {
|
||||
local npm
|
||||
node_ok || install_node || return 1
|
||||
npm="$(dirname "$(tool_path node)")/npm"
|
||||
PATH="$(dirname "$npm"):$PATH" "$npm" install --global --prefix "$TOOLS/srt" \
|
||||
--no-fund --no-audit --ignore-scripts @anthropic-ai/sandbox-runtime > /dev/null
|
||||
}
|
||||
|
||||
tool_title() {
|
||||
case "$1" in
|
||||
restic) echo "restic (backups)" ;;
|
||||
rclone) echo "rclone (reaches your Nextcloud files for restic)" ;;
|
||||
node) echo "Node.js (runs the sandbox)" ;;
|
||||
srt) echo "the sandbox runtime (srt, from Anthropic)" ;;
|
||||
esac
|
||||
}
|
||||
|
||||
# tool_ensure <name>...: offer to install the tools that are missing. Fails if
|
||||
# any is still missing.
|
||||
tool_ensure() {
|
||||
local name missing=()
|
||||
for name in "$@"; do
|
||||
tool_path "$name" > /dev/null || missing+=("$name")
|
||||
done
|
||||
[ ${#missing[@]} -eq 0 ] && return 0
|
||||
echo "This needs:"
|
||||
for name in "${missing[@]}"; do
|
||||
echo " - $(tool_title "$name")"
|
||||
done
|
||||
confirm_yes "Download and install them (into $TOOLS)?" || return 1
|
||||
for name in "${missing[@]}"; do
|
||||
"install_$name" || { echo "$RED$(tool_title "$name") was not installed.$RESET"; return 1; }
|
||||
echo "Installed $(tool_title "$name")."
|
||||
done
|
||||
}
|
||||
Reference in New Issue
Block a user