返回 CodeWhale
install-dogfood.sh
根目录 / scripts / release / install-dogfood.sh
1 #!/usr/bin/env bash
2 set -euo pipefail
3
4 # Atomically install the exact binaries built by this checkout and leave a
5 # durable identity receipt. Replacing the directory entry (rather than copying
6 # over a running vnode) keeps live sessions on their old image while new shells
7 # get the new build safely.
8
9 script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10 repo_root="$(cd "${script_dir}/../.." && pwd)"
11 src_dir="${1:-${repo_root}/target/release}"
12
13 if [[ ! -x "${src_dir}/codewhale" ]]; then
14 echo "ERROR: expected executable codewhale in ${src_dir}" >&2
15 # Since #5245 local builds are unstamped ("(dev)"); a dogfood build must be
16 # stamped explicitly or the identity check below will (correctly) refuse it.
17 echo "Build first: CODEWHALE_BUILD_SHA=\$(git rev-parse HEAD) cargo build --release -p codewhale-cli --locked" >&2
18 exit 1
19 fi
20
21 source_sha="$(git -C "${repo_root}" rev-parse HEAD)"
22 source_dirty="$(git -C "${repo_root}" status --porcelain --untracked-files=no)"
23 if [[ -n "${source_dirty}" ]]; then
24 if [[ "${CODEWHALE_ALLOW_DIRTY_DOGFOOD:-0}" != "1" ]]; then
25 echo "ERROR: refusing to install from a dirty source tree" >&2
26 echo "Commit/stash the source, or set CODEWHALE_ALLOW_DIRTY_DOGFOOD=1 explicitly." >&2
27 exit 1
28 fi
29 source_identity="${source_sha}-dirty"
30 else
31 source_identity="${source_sha}"
32 fi
33
34 cli_version="$("${src_dir}/codewhale" --version)"
35 shim_version="${cli_version}"
36 short_sha="${source_sha:0:12}"
37 if [[ "${cli_version}" != *"${short_sha}"* ]]; then
38 echo "ERROR: release binaries do not embed current HEAD ${short_sha}" >&2
39 echo " codewhale: ${cli_version}" >&2
40 echo "Rebuild this checkout before installing:" >&2
41 echo " CODEWHALE_BUILD_SHA=\$(git rev-parse HEAD) cargo build --release -p codewhale-cli --locked" >&2
42 exit 1
43 fi
44 cli_sha="$(shasum -a 256 "${src_dir}/codewhale" | awk '{print $1}')"
45 shim_sha="${cli_sha}"
46
47 default_install_dirs="${HOME}/.cargo/bin:${HOME}/.local/bin"
48 for command_name in codewhale codew; do
49 if command_path="$(command -v "${command_name}" 2>/dev/null)" \
50 && [[ "${command_path}" == "${HOME}/"* ]]; then
51 command_dir="$(dirname "${command_path}")"
52 if [[ ":${default_install_dirs}:" != *":${command_dir}:"* ]]; then
53 default_install_dirs="${default_install_dirs}:${command_dir}"
54 fi
55 fi
56 done
57 IFS=':' read -r -a dest_dirs <<< "${CODEWHALE_INSTALL_DIRS:-${default_install_dirs}}"
58
59 install_binary() {
60 local src="$1"
61 local dst="$2"
62 local tmp="${dst}.tmp.$$"
63 trap 'rm -f -- "${tmp}"' RETURN
64 cp "${src}" "${tmp}"
65 chmod 0755 "${tmp}"
66 # macOS AMFI kills ad-hoc linker-signed binaries after `cp` into a new
67 # path (SIGKILL on exec, no output). Re-sign in place with a proper
68 # ad-hoc signature so self-built dogfood installs run after install.
69 if [[ "$(uname -s)" == "Darwin" ]] && command -v codesign >/dev/null 2>&1; then
70 # One explicit identity keeps aliases byte-identical after signing.
71 # Otherwise codesign derives different identifiers from their filenames.
72 codesign --force --identifier codewhale --sign - "${tmp}" >/dev/null 2>&1 || {
73 echo "WARN: codesign failed for ${tmp}; binary may be killed by AMFI after install" >&2
74 }
75 fi
76 mv -f "${tmp}" "${dst}"
77 # Re-sign the final path as well — some macOS versions re-evaluate on rename.
78 if [[ "$(uname -s)" == "Darwin" ]] && command -v codesign >/dev/null 2>&1; then
79 codesign --force --identifier codewhale --sign - "${dst}" >/dev/null 2>&1 || true
80 fi
81 cmp -s "${src}" "${dst}" || {
82 # cmp can fail after codesign rewrote the code signature; verify exec instead.
83 if [[ ! -x "${dst}" ]]; then
84 echo "ERROR: installed binary not executable: ${dst}" >&2
85 return 1
86 fi
87 }
88 trap - RETURN
89 }
90
91 installed=()
92 for dest in "${dest_dirs[@]}"; do
93 mkdir -p "${dest}"
94 install_binary "${src_dir}/codewhale" "${dest}/codewhale"
95 install_binary "${src_dir}/codewhale" "${dest}/codew"
96 installed+=("${dest}/codewhale" "${dest}/codew")
97 done
98
99 verify_fresh_shell_binary() {
100 local command_name="$1"
101 local command_path
102 local command_version
103 local dest
104 local is_installed=0
105
106 command_path="$(zsh -lc "command -v ${command_name}" 2>/dev/null || true)"
107 if [[ -z "${command_path}" || ! -x "${command_path}" ]]; then
108 echo "ERROR: fresh login shell cannot resolve ${command_name}" >&2
109 return 1
110 fi
111 for dest in "${dest_dirs[@]}"; do
112 if [[ "${command_path}" == "${dest}/${command_name}" ]]; then
113 is_installed=1
114 break
115 fi
116 done
117 if [[ "${is_installed}" != "1" ]]; then
118 echo "ERROR: fresh-shell ${command_name} resolves outside the installed destinations: ${command_path}" >&2
119 return 1
120 fi
121 command_version="$(zsh -lc "${command_name} --version" 2>/dev/null || true)"
122 if [[ "${command_version}" != *"${short_sha}"* ]]; then
123 echo "ERROR: fresh-shell ${command_name} does not report current HEAD ${short_sha}" >&2
124 return 1
125 fi
126 printf '%s\n' "${command_path}"
127 }
128
129 path_cli="$(verify_fresh_shell_binary codewhale)"
130 path_shim="$(verify_fresh_shell_binary codew)"
131 installed_cli_sha="$(shasum -a 256 "${path_cli}" | awk '{print $1}')"
132 installed_shim_sha="$(shasum -a 256 "${path_shim}" | awk '{print $1}')"
133 if [[ "${installed_cli_sha}" != "${installed_shim_sha}" ]]; then
134 echo "ERROR: installed codewhale and codew differ; no successful identity receipt written" >&2
135 exit 1
136 fi
137
138 default_receipt_root="${HOME}/.codewhale/dogfood-receipts"
139 if [[ -d "/Volumes/VIXinSSD/CW/backups" ]]; then
140 default_receipt_root="/Volumes/VIXinSSD/CW/backups/dogfood-installs"
141 fi
142 receipt_root="${CODEWHALE_DOGFOOD_RECEIPT_DIR:-${default_receipt_root}}"
143 mkdir -p "${receipt_root}"
144 timestamp="$(date -u +%Y%m%dT%H%M%SZ)"
145 receipt="${receipt_root}/${timestamp}-${source_sha:0:12}.txt"
146 {
147 echo "installed_at_utc=${timestamp}"
148 echo "source_repo=${repo_root}"
149 echo "source_commit=${source_identity}"
150 echo "source_dir=${src_dir}"
151 echo "codewhale_version=${cli_version}"
152 echo "codewhale_sha256=${cli_sha}"
153 echo "installed_codewhale_sha256=${installed_cli_sha}"
154 echo "codew_version=${shim_version}"
155 echo "codew_sha256=${shim_sha}"
156 echo "installed_codew_sha256=${installed_shim_sha}"
157 echo "fresh_shell_codewhale=${path_cli}"
158 echo "fresh_shell_codew=${path_shim}"
159 printf 'installed_path=%s\n' "${installed[@]}"
160 } >"${receipt}"
161
162 echo "Installed ${source_identity}:"
163 printf ' %s\n' "${installed[@]}"
164 echo "Receipt: ${receipt}"
165 echo "Fresh-shell check: zsh -lc 'type -a codew codewhale; codew --version; codewhale --version'"
166
166 lines BASH