interplane-install
This project has not described itself yet.
Brought by drakestapleton
interplane-install / bin / interplane
| 1 | #!/bin/bash |
| 2 | # interplane: your own Interplane store, on your own machine. |
| 3 | # |
| 4 | # interplane list the projects in your store |
| 5 | # interplane new NAME [--public] a new empty project (private unless --public) |
| 6 | # interplane import SOURCE [NAME] [--public] [--branch BRANCH] |
| 7 | # bring in a Git repository you choose (a URL or a folder) |
| 8 | # interplane get NAME [FOLDER] a working copy of a project to change |
| 9 | # interplane land NAME [FOLDER] [--receipt FILE] [--verdict FILE] |
| 10 | # record the working copy's current branch head in your store |
| 11 | # interplane verify [NAME] check every signed record (all projects when no NAME) |
| 12 | # interplane share [--listen ADDR] [--port PORT] |
| 13 | # serve your public projects read-only over git:// (Ctrl-C stops). |
| 14 | # Only this machine can reach it unless you give --listen |
| 15 | # (0.0.0.0 for every network this machine is on) |
| 16 | # interplane where where your store, key and tools are |
| 17 | # |
| 18 | # Private projects are never served. Visibility is chosen when a project is made and is part of its |
| 19 | # signed first record; changing it later is not built yet. |
| 20 | # git:// has no encryption and no sign-in: anyone who can reach the address can read what you share, |
| 21 | # and what they receive is the Git history only, not your signed records. |
| 22 | # Every change is signed with your owner key and checked by Interplane's store tool (ipl, built from |
| 23 | # Interplane's source by the installer with the Omega compiler). This command only puts the steps in |
| 24 | # order; the store tool decides what is accepted. It is shell glue, like ipl itself, and moves to |
| 25 | # Omega with ipl's own decisions. |
| 26 | # Exit: 0 done; 1 usage; 2 refused (says why); 3 your store or a project does not verify. |
| 27 | set -u |
| 28 | UMASK0=$(umask); umask 077 |
| 29 | CONF=${XDG_CONFIG_HOME:-$HOME/.config}/interplane |
| 30 | die() { local c=$1; shift; printf 'interplane: %s\n' "$*" >&2; exit "$c"; } |
| 31 | usage() { awk 'NR > 1 && /^#/ { sub(/^# ?/, ""); print; next } NR > 1 { exit }' "$(readlink -f "$0")"; } |
| 32 | [ -f "$CONF/config" ] || die 2 "not installed for this user ($CONF/config is missing); run the installer" |
| 33 | conf() { sed -n "s/^$1=//p" "$CONF/config" | head -n 1; } |
| 34 | STORE=$(conf store); TOOLS=$(conf tools); KEY=$(conf key) |
| 35 | IPL=$TOOLS/ipl/ipl |
| 36 | [ -d "$STORE/projects" ] && [ -x "$TOOLS/ipl/bin/landrec" ] && [ -f "$KEY" ] || die 2 "the installation is incomplete; run the installer again" |
| 37 | DATA=$(dirname "$TOOLS"); RECS=$DATA/records; mkdir -p "$RECS" |
| 38 | ipl() { bash "$IPL" "$@"; } |
| 39 | utc() { date -u +%Y-%m-%dT%H:%M:%SZ; } |
| 40 | reqid() { printf '%s-%s-%s' "$1" "$(date -u +%Y%m%dT%H%M%SZ)" "$(od -An -N4 -tx1 /dev/urandom | tr -d ' \n')"; } |
| 41 | vis_of() { awk -F'\t' -v n="$1" '$1 == n {print $5}' "$STORE/catalog.tsv" 2>/dev/null; } |
| 42 | # the name ipl would accept: letters, digits, . _ -; starting with a letter or digit, not ending in . |
| 43 | # put FILE: what comes in on stdin becomes FILE whole or not at all (a kill cannot leave half of it) |
| 44 | put() { cat > "$1.new" && mv -f "$1.new" "$1"; } |
| 45 | # part_way RC WHAT: say that a command stopped part-way and how to finish it; exit with its code |
| 46 | part_way() { echo "interplane: the $2 stopped part-way; run the same command again to finish it" >&2; exit "$1"; } |
| 47 | chain_lines() { if [ -f "$1" ]; then wc -l < "$1"; else echo 0; fi; } |
| 48 | name_ok() { [[ $1 =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] && [[ $1 != *. ]]; } |
| 49 | |
| 50 | cmd=${1:-help}; [ $# -gt 0 ] && shift |
| 51 | case $cmd in |
| 52 | help | -h | --help) usage ;; |
| 53 | |
| 54 | list) |
| 55 | [ -s "$STORE/catalog.tsv" ] || { echo "Your store is empty."; exit 0; } |
| 56 | printf '%-28s %-8s %-10s %s\n' PROJECT SHARED BRANCH HEAD |
| 57 | awk -F'\t' '{printf "%-28s %-8s %-10s %s\n", $1, ($5 == "public" ? "yes" : "no"), $3, substr($4, 1, 12)}' "$STORE/catalog.tsv" ;; |
| 58 | |
| 59 | new) |
| 60 | [ $# -ge 1 ] || die 1 "usage: interplane new NAME [--public]" |
| 61 | name=$1; shift; vis=private |
| 62 | while [ $# -gt 0 ]; do case $1 in --public) vis=public ;; *) die 1 "unknown option $1" ;; esac; shift; done |
| 63 | name_ok "$name" || die 2 "a name is letters, digits, . _ and -, starting with a letter or digit" |
| 64 | p=$STORE/projects/$name |
| 65 | [ -z "$(vis_of "$name")" ] || die 2 "$name already exists" |
| 66 | # a folder with no chain is what a killed command left (or an empty folder); one with a chain is an |
| 67 | # init that stopped part-way, which ipl finishes when the terms are the same |
| 68 | [ ! -e "$p" ] || [ -e "$p/chain.tsv" ] || rm -rf "$p" |
| 69 | mkdir -p "$p" || die 2 "cannot make $p" |
| 70 | rm -rf "$p/repo.git.new" |
| 71 | [ -d "$p/repo.git" ] || { git init -q --bare -b main "$p/repo.git.new" && mv "$p/repo.git.new" "$p/repo.git"; } || die 2 "cannot make $p" |
| 72 | ipl init "$STORE" "$name" main "$vis" - "$KEY" >/dev/null || { rc=$?; [ "$rc" -ge 1 ] && [ "$rc" -le 4 ] && exit "$rc"; part_way "$rc" "new project"; } |
| 73 | echo "Made $name ($vis). Next: interplane get $name" ;; |
| 74 | |
| 75 | import) |
| 76 | [ $# -ge 1 ] || die 1 "usage: interplane import SOURCE [NAME] [--public] [--branch BRANCH]" |
| 77 | src=$1; shift; name= vis=private br= |
| 78 | while [ $# -gt 0 ]; do |
| 79 | case $1 in |
| 80 | --public) vis=public ;; |
| 81 | --branch) [ $# -ge 2 ] || die 1 "--branch needs a name"; br=$2; shift ;; |
| 82 | -*) die 1 "unknown option $1" ;; |
| 83 | *) [ -z "$name" ] || die 1 "one NAME only"; name=$1 ;; |
| 84 | esac; shift |
| 85 | done |
| 86 | [ -d "$src" ] && src=$(cd "$src" && pwd) |
| 87 | if [ -z "$name" ]; then name=$(basename "${src%/}"); name=${name%.git}; [ "$name" != repo ] || name=$(basename "$(dirname "${src%/}")"); fi |
| 88 | name_ok "$name" || die 2 "'$name' cannot be a project name; give one: interplane import SOURCE NAME" |
| 89 | p=$STORE/projects/$name |
| 90 | ev=$RECS/$name.import-evidence; nt=$RECS/$name.import-note; rq=$RECS/$name.import-request |
| 91 | # what is recorded never holds a password or token from the address |
| 92 | shown=$(printf '%s' "$src" | sed 's#^\([a-z+]*://\)[^/]*@#\1#') |
| 93 | # a folder with no chain and no request is what a killed command left: it is not a project |
| 94 | [ ! -e "$p" ] || [ -e "$p/chain.tsv" ] || [ -f "$rq" ] || rm -rf "$p" |
| 95 | # the same command again (same source, visibility and branch) goes on from the step that is missing; |
| 96 | # anything else is refused |
| 97 | row=$(awk -F'\t' -v n="$name" '$1 == n {print $4}' "$STORE/catalog.tsv" 2>/dev/null) |
| 98 | resume=0 |
| 99 | if [ -f "$rq" ] && [ "$(sed -n 's/^source //p' "$rq")" = "$shown" ] && [ "$(sed -n 's/^visibility //p' "$rq")" = "$vis" ] && |
| 100 | [ "$(sed -n 's/^branch //p' "$rq")" = "${br:--}" ] && { [ -z "$row" ] || [ "$row" = none ]; }; then resume=1; fi |
| 101 | [ "$resume" = 1 ] || [ ! -e "$p" ] || die 2 "$name already exists in your store; give another name" |
| 102 | if [ "$resume" = 1 ]; then req=$(sed -n 's/^request //p' "$rq") |
| 103 | else |
| 104 | req=$(reqid import) |
| 105 | printf 'request %s\nsource %s\nvisibility %s\nbranch %s\n' "$req" "$shown" "$vis" "${br:--}" | put "$rq" || die 2 "cannot write $rq" |
| 106 | fi |
| 107 | mkdir -p "$p" || die 2 "cannot make $p" |
| 108 | if [ ! -d "$p/repo.git" ]; then |
| 109 | rm -rf "$p/repo.git.new" |
| 110 | if [ -n "$br" ]; then git clone -q --bare --single-branch --branch "$br" -- "$src" "$p/repo.git.new" |
| 111 | else git clone -q --bare --single-branch -- "$src" "$p/repo.git.new"; fi && |
| 112 | git --git-dir="$p/repo.git.new" remote remove origin 2>/dev/null |
| 113 | if [ ! -d "$p/repo.git.new" ]; then rm -rf "$p"; rm -f "$rq"; die 2 "could not clone $shown"; fi |
| 114 | mv "$p/repo.git.new" "$p/repo.git" || { rm -rf "$p"; rm -f "$rq"; die 2 "could not put $name in your store"; } |
| 115 | fi |
| 116 | if [ -f "$ev" ] && [ "$resume" = 1 ]; then br=$(sed -n 's/^branch //p' "$ev") |
| 117 | else |
| 118 | [ -n "$br" ] || br=$(git --git-dir="$p/repo.git" symbolic-ref --short HEAD 2>/dev/null) |
| 119 | head=$(git --git-dir="$p/repo.git" rev-parse -q --verify "refs/heads/$br") || { rm -rf "$p"; rm -f "$rq"; die 2 "$shown has no branch to import"; } |
| 120 | printf 'interplane import\nsource %s\nbranch %s\nhead %s\nutc %s\n' "$shown" "$br" "$head" "$(utc)" | put "$ev" |
| 121 | printf 'Imported by the owner as it was cloned: not gated or reviewed here.\n' | put "$nt" |
| 122 | fi |
| 123 | [ -f "$nt" ] || printf 'Imported by the owner as it was cloned: not gated or reviewed here.\n' | put "$nt" |
| 124 | head=$(git --git-dir="$p/repo.git" rev-parse -q --verify "refs/heads/$br") || die 2 "$name in your store has no branch $br" |
| 125 | if [ "$(chain_lines "$p/chain.tsv")" -lt 2 ]; then |
| 126 | ipl init "$STORE" "$name" "$br" "$vis" - "$KEY" >/dev/null || { rc=$?; if [ "$rc" -ge 1 ] && [ "$rc" -le 4 ]; then rm -rf "$p"; rm -f "$rq"; exit "$rc"; fi; part_way "$rc" import; } |
| 127 | fi |
| 128 | ipl import "$STORE" "$name" "$br" "$ev" "$nt" "$req" "$KEY" >/dev/null || part_way $? import |
| 129 | if [ "$resume" = 1 ]; then echo "Imported $name ($(vis_of "$name")), $br (finished an import that had stopped)." |
| 130 | else echo "Imported $name ($vis), $br at ${head:0:12}."; fi ;; |
| 131 | |
| 132 | get) |
| 133 | [ $# -ge 1 ] || die 1 "usage: interplane get NAME [FOLDER]" |
| 134 | name=$1; dir=${2:-$1} |
| 135 | [ -n "$(vis_of "$name")" ] || die 2 "no project $name in your store (interplane list)" |
| 136 | (umask "$UMASK0" && git clone -q "$STORE/projects/$name/repo.git" "$dir") || die 2 "could not make $dir" |
| 137 | echo "Working copy of $name in $dir. Commit there, then: interplane land $name $dir" |
| 138 | # the copy follows your own umask; a private project's history is then readable by other accounts when it is open |
| 139 | if [ "$(vis_of "$name")" != public ]; then |
| 140 | m=$(stat -c %a "$dir") |
| 141 | case $m in *00) ;; *) echo "note: $name is a private project, and $dir is accessible to other accounts on this machine (mode $m, from your umask $UMASK0): they can read its whole history. To close it: chmod -R go-rwx $dir" ;; esac |
| 142 | fi ;; |
| 143 | |
| 144 | land) |
| 145 | [ $# -ge 1 ] || die 1 "usage: interplane land NAME [FOLDER] [--receipt FILE] [--verdict FILE]" |
| 146 | name=$1; shift; dir=. rf= vf= |
| 147 | while [ $# -gt 0 ]; do |
| 148 | case $1 in |
| 149 | --receipt) [ $# -ge 2 ] || die 1 "--receipt needs a file"; rf=$2; shift ;; |
| 150 | --verdict) [ $# -ge 2 ] || die 1 "--verdict needs a file"; vf=$2; shift ;; |
| 151 | -*) die 1 "unknown option $1" ;; |
| 152 | *) dir=$1 ;; |
| 153 | esac; shift |
| 154 | done |
| 155 | [ -n "$(vis_of "$name")" ] || die 2 "no project $name in your store (interplane list)" |
| 156 | dir=$(cd "$dir" 2>/dev/null && git rev-parse --show-toplevel 2>/dev/null) || die 2 "$dir is not a Git working copy" |
| 157 | [ -z "$(git -C "$dir" status --porcelain)" ] || die 2 "$dir has changes that are not committed; commit them first" |
| 158 | br=$(git -C "$dir" symbolic-ref --short HEAD 2>/dev/null) || die 2 "$dir is not on a branch" |
| 159 | new=$(git -C "$dir" rev-parse HEAD) || die 2 "$dir has no commit" |
| 160 | # A landing that stopped part-way left its request in $pend. The same head again finishes it with the |
| 161 | # same request, receipt and verdict; another head first finishes (or finds nothing of) the old one. |
| 162 | pend=$RECS/$name.land-pending |
| 163 | req= |
| 164 | if [ -f "$pend" ]; then |
| 165 | if [ "$(sed -n 's/^head //p' "$pend")" = "$new" ] && [ "$(sed -n 's/^branch //p' "$pend")" = "$br" ]; then |
| 166 | req=$(sed -n 's/^request //p' "$pend"); rf=$(sed -n 's/^receipt //p' "$pend"); vf=$(sed -n 's/^verdict //p' "$pend") |
| 167 | else |
| 168 | ipl land "$STORE" "$name" "$(sed -n 's/^branch //p' "$pend")" "$(sed -n 's/^head //p' "$pend")" - \ |
| 169 | "$(sed -n 's/^receipt //p' "$pend")" "$(sed -n 's/^verdict //p' "$pend")" "$(sed -n 's/^request //p' "$pend")" "$KEY" >/dev/null |
| 170 | rc=$? |
| 171 | if [ "$rc" = 0 ] || { [ "$rc" -ge 1 ] && [ "$rc" -le 4 ]; }; then rm -f "$pend"; else part_way "$rc" landing; fi |
| 172 | fi |
| 173 | fi |
| 174 | if [ -z "$req" ]; then |
| 175 | req=$(reqid land) |
| 176 | if [ -z "$rf" ]; then rf=$RECS/$name.$req.receipt; printf 'interplane land by the owner\nfrom %s\nbranch %s\nhead %s\nutc %s\nchecks none were run by this command\n' "$dir" "$br" "$new" "$(utc)" | put "$rf"; fi |
| 177 | if [ -z "$vf" ]; then vf=$RECS/$name.$req.verdict; printf 'no review: landed by the owner on their own machine\n' | put "$vf"; fi |
| 178 | printf 'request %s\nbranch %s\nhead %s\nreceipt %s\nverdict %s\n' "$req" "$br" "$new" "$rf" "$vf" | put "$pend" || die 2 "cannot write $pend" |
| 179 | fi |
| 180 | ipl land "$STORE" "$name" "$br" "$new" "$dir" "$rf" "$vf" "$req" "$KEY" >/dev/null; rc=$? |
| 181 | if [ "$rc" = 0 ] || { [ "$rc" -ge 1 ] && [ "$rc" -le 4 ]; }; then rm -f "$pend"; else part_way "$rc" landing; fi |
| 182 | [ "$rc" = 0 ] || exit "$rc" |
| 183 | echo "Landed $name $br at ${new:0:12}." ;; |
| 184 | |
| 185 | verify) |
| 186 | if [ $# -ge 1 ]; then out=$(ipl verify "$STORE" "$1") || exit $?; echo "$out"; exit 0; fi |
| 187 | rc=0 |
| 188 | for d in "$STORE"/projects/*/; do |
| 189 | [ -f "$d/chain.tsv" ] || continue |
| 190 | n=$(basename "$d") |
| 191 | if out=$(ipl verify "$STORE" "$n" 2>&1); then echo "$out"; else echo "FAIL $n: $out"; rc=3; fi |
| 192 | done |
| 193 | exit $rc ;; |
| 194 | |
| 195 | share) |
| 196 | listen=127.0.0.1 port=9418 |
| 197 | while [ $# -gt 0 ]; do |
| 198 | case $1 in |
| 199 | --listen) [ $# -ge 2 ] || die 1 "--listen needs an address"; listen=$2; shift ;; |
| 200 | --port) [ $# -ge 2 ] || die 1 "--port needs a number"; port=$2; shift ;; |
| 201 | *) die 1 "unknown option $1" ;; |
| 202 | esac; shift |
| 203 | done |
| 204 | [[ $port =~ ^[0-9]+$ ]] || die 1 "--port needs a number" |
| 205 | # Only a project that verifies and whose signed first record says public is exported; every other |
| 206 | # repository loses its export mark first, so a private project is never served. |
| 207 | n=0 |
| 208 | for d in "$STORE"/projects/*/; do |
| 209 | b=$(basename "$d"); rm -f "$d/repo.git/git-daemon-export-ok" |
| 210 | if [ "$(vis_of "$b")" = public ] && ipl verify "$STORE" "$b" >/dev/null 2>&1; then |
| 211 | : > "$d/repo.git/git-daemon-export-ok"; n=$((n + 1)); echo "sharing git://$listen:$port/$b/repo.git" |
| 212 | fi |
| 213 | done |
| 214 | [ "$n" -gt 0 ] || die 2 "you have no public projects to share (make one with --public)" |
| 215 | case $listen in |
| 216 | 127.*|localhost|::1) echo "Read-only. Only this machine can reach it. To let others clone, stop and run: interplane share --listen 0.0.0.0" ;; |
| 217 | *) echo "Read-only, no encryption and no sign-in: anyone who can reach $listen:$port can clone the projects above." ;; |
| 218 | esac |
| 219 | echo "Clone with: git clone git://ADDRESS:$port/NAME/repo.git (Ctrl-C stops)" |
| 220 | exec git daemon --reuseaddr --listen="$listen" --port="$port" --base-path="$STORE/projects" "$STORE/projects" ;; |
| 221 | |
| 222 | where) |
| 223 | echo "store $STORE" |
| 224 | echo "key $KEY $(ssh-keygen -lf "$KEY.pub" | awk '{print $2}')" |
| 225 | echo "tools $TOOLS (Interplane $(git -C "$TOOLS" rev-parse --short=12 HEAD))" |
| 226 | command -v omc >/dev/null && echo "omc $(command -v omc) sha256 $(sha256sum < "$(command -v omc)" | cut -c1-12)" ;; |
| 227 | |
| 228 | *) die 1 "unknown command $cmd (try: interplane help)" ;; |
| 229 | esac |