|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# POC for installing Trunk format binaries created with trunk.mk. Requires: |
| 4 | +# |
| 5 | +# * bash |
| 6 | +# * tar |
| 7 | +# * shasum |
| 8 | +# * jq |
| 9 | +# * uname |
| 10 | +# * pg_config |
| 11 | +# * rsync |
| 12 | + |
| 13 | +trap 'exit' ERR |
| 14 | +set -E |
| 15 | + |
| 16 | +install_trunk() { |
| 17 | + local trunk=$1 |
| 18 | + |
| 19 | + if [ -z "$trunk" ]; then |
| 20 | + printf "Usage:\n\n %s PACKAGE_PATH\n" "$0" |
| 21 | + exit 1 |
| 22 | + fi |
| 23 | + |
| 24 | + # Determine the platform. |
| 25 | + local my_os my_osv my_arch |
| 26 | + my_os=$(uname | tr '[:upper:]' '[:lower:]') |
| 27 | + my_osv=$(uname -r) |
| 28 | + my_arch="$(uname -m)" |
| 29 | + if [ "$my_arch" == "x86_64" ]; then my_arch=amd64; fi |
| 30 | + |
| 31 | + # Unpack. |
| 32 | + printf "Unpacking %s\n" "$trunk" |
| 33 | + tar zxf "$trunk" |
| 34 | + |
| 35 | + # Go into the directory |
| 36 | + local dir="${trunk%.*}" |
| 37 | + cd "$dir" || exit |
| 38 | + |
| 39 | + # Verify the checksums. |
| 40 | + printf "Verifying all checksums..." |
| 41 | + # XXX This does not fail if a file isn't present in digests. |
| 42 | + shasum --check -b --strict digests |
| 43 | + printf "Done!\n" |
| 44 | + |
| 45 | + # Verify the Trunk version |
| 46 | + printf "Verifying compatibility with Trunk package 0.1.0\n" |
| 47 | + local tv |
| 48 | + tv=$(jq -r .trunk trunk.json) |
| 49 | + if [ "$tv" != "0.1.0" ]; then |
| 50 | + printf "Unsupported Trunk format version %s\n" "$tv" |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + |
| 54 | + # Verify the Postgres version. |
| 55 | + local pkg_pg my_pg |
| 56 | + my_pg=$(pg_config --version | sed -E 's/^[^ ]+ ([^ ]+).*$/\1/') |
| 57 | + printf "Verifying compatibility with PostgreSQL %s\n" "$my_pg" |
| 58 | + pkg_pg=$(jq -r .postgres.version trunk.json) |
| 59 | + if [ "$pkg_pg" != "$my_pg" ]; then |
| 60 | + printf "Trunk package contains binaries for Postgres %s but this host runs Postgres %s\n" "$pkg_pg" "$my_pg" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + printf "Verifying compatibility with %s/%s:%s\n" "$my_os" "$my_arch" "$my_osv" |
| 65 | + local pkg_os |
| 66 | + pkg_os=$(jq -r .platform.os trunk.json) |
| 67 | + if [ "$pkg_os" != "any" ]; then |
| 68 | + # Verify the OS |
| 69 | + if [ "$pkg_os" != "$my_os" ]; then |
| 70 | + printf "Trunk package contains %s binaries but this host runs %s\n" "$pkg_os" "$my_os" |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + # Verify the architecture. |
| 75 | + local pkg_arch |
| 76 | + pkg_arch=$(jq -r .platform.arch trunk.json) |
| 77 | + if [ "$pkg_arch" != "$my_arch" ]; then |
| 78 | + printf "Trunk package contains %s binaries but this host runs %s\n" "$pkg_arch" "$my_arch" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + fi |
| 82 | + |
| 83 | + # Make sure we have pgsql directory. |
| 84 | + if [ ! -d 'pgsql' ]; then |
| 85 | + printf "Package contains no install files; exiting\n" |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | + |
| 89 | + cd 'pgsql' || exit |
| 90 | + for subdir in *; do |
| 91 | + [[ -d "$subdir" ]] || continue |
| 92 | + case $subdir in |
| 93 | + share) |
| 94 | + install_dir "$subdir" "$(pg_config --sharedir)" |
| 95 | + ;; |
| 96 | + pkglib) |
| 97 | + install_dir "$subdir" "$(pg_config --pkglibdir)" |
| 98 | + ;; |
| 99 | + pkginclude) |
| 100 | + install_dir "$subdir" "$(pg_config --pkgincludedir)" |
| 101 | + ;; |
| 102 | + lib) |
| 103 | + install_dir "$subdir" "$(pg_config --libdir)" |
| 104 | + ;; |
| 105 | + include) |
| 106 | + install_dir "$subdir" "$(pg_config --includedir)" |
| 107 | + ;; |
| 108 | + bin) |
| 109 | + install_dir "$subdir" "$(pg_config --bindir)" |
| 110 | + ;; |
| 111 | + doc) |
| 112 | + install_dir "$subdir" "$(pg_config --docdir)" |
| 113 | + ;; |
| 114 | + man) |
| 115 | + install_dir "$subdir" "$(pg_config --mandir)" |
| 116 | + ;; |
| 117 | + html) |
| 118 | + install_dir "$subdir" "$(pg_config --htmldir)" |
| 119 | + ;; |
| 120 | + locale) |
| 121 | + install_dir "$subdir" "$(pg_config --localedir)" |
| 122 | + ;; |
| 123 | + sysconf) |
| 124 | + install_dir "$subdir" "$(pg_config --sysconfdir)" |
| 125 | + ;; |
| 126 | + *) |
| 127 | + printf "Unknown install directory %s; skipping\n" "$subdir" |
| 128 | + ;; |
| 129 | + esac |
| 130 | + done |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +install_dir() { |
| 135 | + local src="$1" |
| 136 | + local dst="$2" |
| 137 | + printf "Installing %s into %s..." "$src" "$dst" |
| 138 | + cd "$src" || exit |
| 139 | + rsync -q -a -v . "$dst" || exit |
| 140 | + printf "Done\n" |
| 141 | + cd .. |
| 142 | +} |
| 143 | + |
| 144 | +install_trunk "$@" |
0 commit comments