Production automation capstone
This capstone integrates the course by creating a verified offline log summary archive. The lab accepts one fixed input path, input.txt, containing one severity token (INFO, WARN or ERROR) per line. Blank or malformed records fail with status 2; an empty file is valid and produces zero counts. The goal is to create report.tar containing only summary.txt, verify the extracted bytes, and on an identical repeat avoid replacement. The script refuses to overwrite a different existing archive. The directory must be fresh, private and learner-owned; all files are synthetic. Do not use this exercise in a shared or production directory.
Workflow and acceptance criteria
Validate arguments and regular-file input, restrict creation permissions, acquire an advisory lock, generate a candidate in a unique private directory, check each stage, restore into a separate directory and compare bytes. Publish a single archive only after verification. Retain the lock pathname (unlinking a lock file can break coordination); close the descriptor when the function subshell exits. For repeated runs compare the old archive's content and member list; this is a trusted locally produced archive, not untrusted archive validation. An input that produces a different summary is reported as a failure rather than replacing an old artifact: a separate authorized versioning policy would be necessary. Cleanup removes the owned staging directory. In production add schema/version identity, immutable inputs, output integrity policy, logging, signal handling, recovery and durability testing appropriate to the system. The first example is a smaller acceptance-test building block; the second is a complete lab workflow; the third runs an independent negative acceptance test with the same function. The code is staged for local manual practice, not an executable ByteMind assessment. The local capstone verifies cleanup before reporting the demonstration complete. On a repeated run it verifies both archive bytes extracted as summary and the exact expected member list; it refuses a different existing archive. A failed cleanup after publication can leave a published archive, so a caller must inspect the outcome before retrying.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq, GNU coreutils, GNU tar and flock are external dependencies for selected examples. Use only synthetic files in a private directory.
Verify archive contents in a small test
printf 'INFO=1\nWARN=0\nERROR=0\n' > expected.txt || exit 1cp expected.txt summary.txt || exit 1tar -cf report.tar summary.txt || exit 1tar -tf report.tar > members.txt || exit 1printf 'summary.txt\n' > expected-members.txt || exit 1cmp -s members.txt expected-members.txt || exit 1tar -xOf report.tar summary.txt > actual.txt || exit 1cmp -s actual.txt expected.txt || exit 1printf 'verified\n' Expected stdout:
verifiedCheck both member list and exact data; the archive was produced locally.
Run the verified report twice
set -o pipefailumask 077report() ( (( $# == 1 )) || exit 2 case $1 in input.txt) ;; *) exit 2 ;; esac [[ -f $1 && ! -L $1 ]] || exit 2 work=$(mktemp -d ./.report.XXXXXX) || exit 1 cleanup() { local rc=$? rm -rf -- "$work" || { (( rc != 0 )) || rc=1; } exit "$rc" } trap cleanup EXIT exec 9> report.lock || exit 1 flock -n 9 || exit 1 awk 'NF != 1 || $1 !~ /^(INFO|WARN|ERROR)$/ {bad=1; next} $1=="INFO" {i++} $1=="WARN" {w++} $1=="ERROR" {e++} END {if(bad) exit 2; printf "INFO=%d\nWARN=%d\nERROR=%d\n",i,w,e}' "$1" > "$work/summary.txt" || exit "$?" tar -cf "$work/report.tar" -C "$work" summary.txt || exit 1 mkdir "$work/restore" || exit 1 tar -xf "$work/report.tar" -C "$work/restore" || exit 1 cmp -s "$work/summary.txt" "$work/restore/summary.txt" || exit 1 if [[ -f report.tar && ! -L report.tar ]] && tar -xOf report.tar summary.txt > "$work/old.txt" 2>/dev/null && cmp -s "$work/summary.txt" "$work/old.txt"; then # No replacement: compare the full expected archive member list too. tar -tf report.tar > "$work/members.txt" || exit 1 printf 'summary.txt\n' > "$work/expected-members.txt" || exit 1 cmp -s "$work/members.txt" "$work/expected-members.txt" || exit 1 printf 'unchanged\n' else [[ ! -e report.tar && ! -L report.tar ]] || exit 1 mv -T -- "$work/report.tar" report.tar || exit 1 printf 'published\n' fi)printf '%s\n' INFO WARN INFO ERROR > input.txt || exit 1report input.txt || exit "$?"before=$(stat -c %i report.tar) || exit 1report input.txt || exit "$?"[[ $(stat -c %i report.tar) == "$before" ]] || exit 1tar -xOf report.tar summary.txtshopt -s nullglobleftovers=(.report.*)(( ${#leftovers[@]} == 0 )) || exit 1Expected stdout:
publishedunchangedINFO=2WARN=1ERROR=1The second run compares the summary and member list and skips replacement; the inode is unchanged. The lock remains but temporary directories are removed.
Reject a malformed input without publishing
set -o pipefailumask 077report() ( (( $# == 1 )) || exit 2 case $1 in input.txt) ;; *) exit 2 ;; esac [[ -f $1 && ! -L $1 ]] || exit 2 work=$(mktemp -d ./.report.XXXXXX) || exit 1 cleanup() { local rc=$? rm -rf -- "$work" || { (( rc != 0 )) || rc=1; } exit "$rc" } trap cleanup EXIT exec 9> report.lock || exit 1 flock -n 9 || exit 1 awk 'NF != 1 || $1 !~ /^(INFO|WARN|ERROR)$/ {bad=1; next} $1=="INFO" {i++} $1=="WARN" {w++} $1=="ERROR" {e++} END {if(bad) exit 2; printf "INFO=%d\nWARN=%d\nERROR=%d\n",i,w,e}' "$1" > "$work/summary.txt" || exit "$?" tar -cf "$work/report.tar" -C "$work" summary.txt || exit 1 mkdir "$work/restore" || exit 1 tar -xf "$work/report.tar" -C "$work/restore" || exit 1 cmp -s "$work/summary.txt" "$work/restore/summary.txt" || exit 1 if [[ -f report.tar && ! -L report.tar ]] && tar -xOf report.tar summary.txt > "$work/old.txt" 2>/dev/null && cmp -s "$work/summary.txt" "$work/old.txt"; then # No replacement: compare the full expected archive member list too. tar -tf report.tar > "$work/members.txt" || exit 1 printf 'summary.txt\n' > "$work/expected-members.txt" || exit 1 cmp -s "$work/members.txt" "$work/expected-members.txt" || exit 1 printf 'unchanged\n' else [[ ! -e report.tar && ! -L report.tar ]] || exit 1 mv -T -- "$work/report.tar" report.tar || exit 1 printf 'published\n' fi)printf '%s\n' INFO BROKEN > input.txt || exit 1if report input.txt; then exit 1; else rc=$?; fi[[ $rc == 2 ]] || exit 1printf 'rejected=%s\n' "$rc" || exit 1[[ ! -e report.tar && ! -L report.tar ]] || exit 1printf 'no archive\n' shopt -s nullglobleftovers=(.report.*)(( ${#leftovers[@]} == 0 )) || exit 1Expected stdout:
rejected=2no archiveThe validation stage fails before publication. The example handles the nonzero status to print a demonstration result; direct failure behavior is tested separately.
Common mistakes
Do not mistake an archive listing for restoration verification, or publish partial output on failure. Do not extract an untrusted archive using this exercise. Idempotence here means identical summary and member list, not identical input ordering or archive metadata.
Practice
Extend the lab with a fourth severity, update the schema and tests together, and add cases for empty input, changed input, malformed input, lock contention, and repeated execution. Verify the archive bytes and the absence of leftover staging directories. Explain a safe release-versioning policy without overwriting existing artifacts.
Next: Course review and further practice