|
| 1 | +#!/bin/bash |
| 2 | +# Daily sync helper for cell2state: laptop <-> Crick git pull/push + reinstall. |
| 3 | +# |
| 4 | +# Run from the laptop. Bundles the safe order: |
| 5 | +# 1. Refuse if laptop tree is dirty (or stash with --allow-dirty). |
| 6 | +# 2. git pull --ff-only on laptop. |
| 7 | +# 3. git push on laptop. |
| 8 | +# 4. ssh login "cd <CRICK_PROJ> && git pull --ff-only". |
| 9 | +# 5. cluster reinstall via ssh login "... pip install . --no-deps -q". |
| 10 | +# 6. local reinstall via run_python_cmd.sh -m pip install . --no-deps -q. |
| 11 | +# |
| 12 | +# CLI: |
| 13 | +# bash pull_reinstall.sh [--allow-dirty] [--no-remote] [--no-local] |
| 14 | +# |
| 15 | +# --allow-dirty Stash uncommitted changes (push -u) and restore on EXIT |
| 16 | +# instead of refusing. |
| 17 | +# --no-remote Skip the cluster pull + cluster reinstall. |
| 18 | +# --no-local Skip the laptop reinstall. |
| 19 | + |
| 20 | +set -euo pipefail |
| 21 | + |
| 22 | +# --- Constants --------------------------------------------------------------- |
| 23 | +LAPTOP_PROJ=/Users/kleshcv/Desktop/my_packages/regularizedvi |
| 24 | +CRICK_PROJ=/nemo/lab/briscoej/home/users/kleshcv/my_packages/regularizedvi |
| 25 | +CRICK_SSH=login # login node for git ops; crick-default-shared is compute-only |
| 26 | + |
| 27 | +# --- SSH connection multiplexing -------------------------------------------- |
| 28 | +# This script makes 3-5 ssh calls in succession (status, pull, reinstall); |
| 29 | +# multiplex them onto a single connection so the login node doesn't see |
| 30 | +# repeated handshakes. |
| 31 | +SSH_CONTROL_PATH="${TMPDIR:-/tmp}/pull_reinstall_ssh_$$.sock" |
| 32 | +SSH_OPTS=(-o "ControlMaster=auto" -o "ControlPath=$SSH_CONTROL_PATH" -o "ControlPersist=300") |
| 33 | + |
| 34 | +cleanup_ssh_socket() { |
| 35 | + if [[ -S "$SSH_CONTROL_PATH" ]]; then |
| 36 | + ssh "${SSH_OPTS[@]}" -O exit "$CRICK_SSH" 2>/dev/null || true |
| 37 | + fi |
| 38 | + rm -f "$SSH_CONTROL_PATH" |
| 39 | +} |
| 40 | + |
| 41 | +# --- Flags ------------------------------------------------------------------- |
| 42 | +ALLOW_DIRTY=0 |
| 43 | +DO_REMOTE=1 |
| 44 | +DO_LOCAL=1 |
| 45 | + |
| 46 | +while [[ $# -gt 0 ]]; do |
| 47 | + case "${1:-}" in |
| 48 | + --allow-dirty) ALLOW_DIRTY=1; shift ;; |
| 49 | + --no-remote) DO_REMOTE=0; shift ;; |
| 50 | + --no-local) DO_LOCAL=0; shift ;; |
| 51 | + -h|--help) |
| 52 | + grep -E '^#( |$)' "$0" | sed 's/^# \{0,1\}//' |
| 53 | + exit 0 ;; |
| 54 | + *) |
| 55 | + echo "ERROR: unknown flag '$1'" >&2 |
| 56 | + echo "Usage: bash $0 [--allow-dirty] [--no-remote] [--no-local]" >&2 |
| 57 | + exit 2 ;; |
| 58 | + esac |
| 59 | +done |
| 60 | + |
| 61 | +# --- State for trap ---------------------------------------------------------- |
| 62 | +STEP="init" |
| 63 | +STASHED=0 |
| 64 | +LAPTOP_SHA_BEFORE="" |
| 65 | +LAPTOP_SHA_AFTER="" |
| 66 | +CRICK_SHA_AFTER="" |
| 67 | +LOCAL_REINSTALL_OK="skipped" |
| 68 | +REMOTE_REINSTALL_OK="skipped" |
| 69 | + |
| 70 | +cleanup() { |
| 71 | + local rc=$? |
| 72 | + if [[ "$STASHED" == "1" ]]; then |
| 73 | + echo "[trap] restoring stashed changes via 'git stash pop'" >&2 |
| 74 | + if ! git -C "$LAPTOP_PROJ" stash pop 2>&1; then |
| 75 | + echo "WARNING: 'git stash pop' did not apply cleanly. Your changes are still" >&2 |
| 76 | + echo " in the stash. Run 'git -C $LAPTOP_PROJ stash list' to inspect," >&2 |
| 77 | + echo " then 'git stash pop' / 'git stash apply' / 'git stash drop'" >&2 |
| 78 | + echo " once you have resolved any conflicts." >&2 |
| 79 | + fi |
| 80 | + fi |
| 81 | + if [[ "$rc" -ne 0 ]]; then |
| 82 | + echo "[FAIL] pull_reinstall.sh failed at step: $STEP (exit $rc)" >&2 |
| 83 | + fi |
| 84 | + exit "$rc" |
| 85 | +} |
| 86 | +trap 'cleanup; cleanup_ssh_socket' EXIT |
| 87 | + |
| 88 | +# --- Step 1: laptop git repo + branch --------------------------------------- |
| 89 | +STEP="verify-laptop-repo" |
| 90 | +cd "$LAPTOP_PROJ" |
| 91 | +if ! git rev-parse --git-dir >/dev/null 2>&1; then |
| 92 | + echo "ERROR: $LAPTOP_PROJ is not a git repository." >&2 |
| 93 | + exit 10 |
| 94 | +fi |
| 95 | +BRANCH="$(git rev-parse --abbrev-ref HEAD)" |
| 96 | +if [[ -z "$BRANCH" || "$BRANCH" == "HEAD" ]]; then |
| 97 | + echo "ERROR: laptop repo is not on a named branch (detached HEAD?)." >&2 |
| 98 | + exit 11 |
| 99 | +fi |
| 100 | +LAPTOP_SHA_BEFORE="$(git rev-parse HEAD)" |
| 101 | +echo "[1/7] laptop branch=$BRANCH HEAD=$LAPTOP_SHA_BEFORE" |
| 102 | + |
| 103 | +# --- Step 2: dirty-tree check ------------------------------------------------ |
| 104 | +STEP="check-dirty-tree" |
| 105 | +DIRTY="$(git status --porcelain)" |
| 106 | +if [[ -n "$DIRTY" ]]; then |
| 107 | + if [[ "$ALLOW_DIRTY" == "1" ]]; then |
| 108 | + echo "[2/7] tree is dirty; --allow-dirty given -> stashing" |
| 109 | + echo "$DIRTY" | sed 's/^/ /' |
| 110 | + git stash push -u -m "pull_reinstall.sh auto-stash" |
| 111 | + STASHED=1 |
| 112 | + else |
| 113 | + echo "ERROR: laptop repo has uncommitted changes. Commit or stash first," >&2 |
| 114 | + echo " or rerun with --allow-dirty to auto-stash + restore." >&2 |
| 115 | + echo " Dirty files:" >&2 |
| 116 | + echo "$DIRTY" | sed 's/^/ /' >&2 |
| 117 | + exit 12 |
| 118 | + fi |
| 119 | +else |
| 120 | + echo "[2/7] tree is clean" |
| 121 | +fi |
| 122 | + |
| 123 | +# --- Step 3: laptop git pull --ff-only -------------------------------------- |
| 124 | +STEP="laptop-git-pull" |
| 125 | +echo "[3/7] git pull --ff-only origin $BRANCH (laptop)" |
| 126 | +if ! git pull --ff-only origin "$BRANCH"; then |
| 127 | + echo "ERROR: laptop 'git pull --ff-only origin $BRANCH' failed." >&2 |
| 128 | + echo " Likely the branch has diverged. Resolve manually with:" >&2 |
| 129 | + echo " cd $LAPTOP_PROJ && git fetch origin && git status" >&2 |
| 130 | + echo " and either rebase or merge before rerunning." >&2 |
| 131 | + exit 13 |
| 132 | +fi |
| 133 | + |
| 134 | +# --- Step 4: laptop git push ------------------------------------------------ |
| 135 | +STEP="laptop-git-push" |
| 136 | +echo "[4/7] git push origin $BRANCH (laptop)" |
| 137 | +if ! git push origin "$BRANCH"; then |
| 138 | + echo "ERROR: laptop 'git push origin $BRANCH' failed." >&2 |
| 139 | + exit 14 |
| 140 | +fi |
| 141 | +LAPTOP_SHA_AFTER="$(git rev-parse HEAD)" |
| 142 | + |
| 143 | +# --- Step 5: cluster git pull ----------------------------------------------- |
| 144 | +if [[ "$DO_REMOTE" == "1" ]]; then |
| 145 | + STEP="cluster-clean-check" |
| 146 | + echo "[5/7] cluster: verifying clean tree at $CRICK_PROJ" |
| 147 | + REMOTE_DIRTY="$(ssh "${SSH_OPTS[@]}" "$CRICK_SSH" "cd '$CRICK_PROJ' && git status --porcelain" || true)" |
| 148 | + if [[ -n "$REMOTE_DIRTY" ]]; then |
| 149 | + echo "ERROR: cluster repo has divergent state — log in via 'ssh $CRICK_SSH' and resolve." >&2 |
| 150 | + echo " Dirty files on cluster:" >&2 |
| 151 | + echo "$REMOTE_DIRTY" | sed 's/^/ /' >&2 |
| 152 | + exit 15 |
| 153 | + fi |
| 154 | + |
| 155 | + STEP="cluster-git-pull" |
| 156 | + echo " cluster: git pull --ff-only" |
| 157 | + if ! ssh "${SSH_OPTS[@]}" "$CRICK_SSH" "cd '$CRICK_PROJ' && git pull --ff-only"; then |
| 158 | + echo "ERROR: cluster 'git pull --ff-only' failed." >&2 |
| 159 | + echo " Resolve manually: ssh $CRICK_SSH; cd $CRICK_PROJ; git status" >&2 |
| 160 | + exit 16 |
| 161 | + fi |
| 162 | + CRICK_SHA_AFTER="$(ssh "${SSH_OPTS[@]}" "$CRICK_SSH" "cd '$CRICK_PROJ' && git rev-parse HEAD" | tr -d '\r\n')" |
| 163 | + |
| 164 | + # --- Step 6: cluster reinstall ------------------------------------------ |
| 165 | + STEP="cluster-reinstall" |
| 166 | + echo "[6/7] cluster: pip install . --no-deps -q" |
| 167 | + # Direct ssh form: run_python_cmd.sh on the cluster handles env activation. |
| 168 | + # We pipe through bash on the login node; cd into project so '.' resolves |
| 169 | + # to the cluster repo. (--remote crick from the wrapper would attempt to |
| 170 | + # translate the script-arg '.', which is positional for pip, not a script |
| 171 | + # path — so we go direct.) |
| 172 | + if ! ssh "${SSH_OPTS[@]}" "$CRICK_SSH" "cd '$CRICK_PROJ' && bash scripts/helper_scripts/run_python_cmd.sh -m pip install . --no-deps -q"; then |
| 173 | + echo "ERROR: cluster reinstall failed." >&2 |
| 174 | + REMOTE_REINSTALL_OK="FAIL" |
| 175 | + exit 17 |
| 176 | + fi |
| 177 | + REMOTE_REINSTALL_OK="ok" |
| 178 | +else |
| 179 | + echo "[5/7] --no-remote: skipping cluster pull + reinstall" |
| 180 | + echo "[6/7] --no-remote: skipping cluster reinstall" |
| 181 | +fi |
| 182 | + |
| 183 | +# --- Step 7: laptop reinstall ----------------------------------------------- |
| 184 | +if [[ "$DO_LOCAL" == "1" ]]; then |
| 185 | + STEP="laptop-reinstall" |
| 186 | + echo "[7/7] laptop: pip install . --no-deps -q" |
| 187 | + if ! bash "$LAPTOP_PROJ/scripts/helper_scripts/run_python_cmd.sh" -m pip install . --no-deps -q; then |
| 188 | + echo "ERROR: laptop reinstall failed." >&2 |
| 189 | + LOCAL_REINSTALL_OK="FAIL" |
| 190 | + exit 18 |
| 191 | + fi |
| 192 | + LOCAL_REINSTALL_OK="ok" |
| 193 | +else |
| 194 | + echo "[7/7] --no-local: skipping laptop reinstall" |
| 195 | +fi |
| 196 | + |
| 197 | +# --- Summary ----------------------------------------------------------------- |
| 198 | +STEP="summary" |
| 199 | +echo |
| 200 | +echo "=== pull_reinstall.sh summary ===" |
| 201 | +echo " branch: $BRANCH" |
| 202 | +echo " laptop HEAD: $LAPTOP_SHA_BEFORE -> $LAPTOP_SHA_AFTER" |
| 203 | +if [[ "$DO_REMOTE" == "1" ]]; then |
| 204 | + echo " cluster HEAD: $CRICK_SHA_AFTER" |
| 205 | + if [[ "$LAPTOP_SHA_AFTER" == "$CRICK_SHA_AFTER" ]]; then |
| 206 | + echo " laptop == cluster: yes" |
| 207 | + else |
| 208 | + echo " WARNING: laptop and cluster HEADs differ after pull — investigate." >&2 |
| 209 | + fi |
| 210 | +else |
| 211 | + echo " cluster HEAD: (skipped)" |
| 212 | +fi |
| 213 | +echo " reinstall local: $LOCAL_REINSTALL_OK" |
| 214 | +echo " reinstall remote: $REMOTE_REINSTALL_OK" |
| 215 | +echo "=================================" |
0 commit comments