{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# S5 lab notebook \u2014 reading the NAS evidence\n",
    "\n",
    "This notebook reads the evidence a completed run leaves under `data/run/` and turns it into one checked artifact, `nas_results.json`. It does not connect to the cloud: every number comes from a file your commands produced, so the notebook works offline and the assertions are about your run, not about a vendor's status page.\n",
    "\n",
    "The recorded run shipped in `data/run/` is the verified reference. To analyse your own run, copy your `results/` into `data/run/` and rerun the cells.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e42\u0e19\u0e49\u0e15\u0e1a\u0e38\u0e4a\u0e01\u0e19\u0e35\u0e49\u0e2d\u0e48\u0e32\u0e19\u0e2b\u0e25\u0e31\u0e01\u0e10\u0e32\u0e19\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23\u0e23\u0e31\u0e19\u0e17\u0e35\u0e48\u0e40\u0e01\u0e47\u0e1a\u0e44\u0e27\u0e49\u0e43\u0e19 data/run/ \u0e41\u0e25\u0e49\u0e27\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e44\u0e1f\u0e25\u0e4c\u0e2a\u0e23\u0e38\u0e1b\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e44\u0e1f\u0e25\u0e4c\u0e0a\u0e37\u0e48\u0e2d nas_results.json \u0e44\u0e21\u0e48\u0e21\u0e35\u0e01\u0e32\u0e23\u0e40\u0e0a\u0e37\u0e48\u0e2d\u0e21\u0e15\u0e48\u0e2d\u0e04\u0e25\u0e32\u0e27\u0e14\u0e4c\u0e23\u0e30\u0e2b\u0e27\u0e48\u0e32\u0e07\u0e17\u0e33\u0e07\u0e32\u0e19 \u0e17\u0e38\u0e01\u0e15\u0e31\u0e27\u0e40\u0e25\u0e02\u0e21\u0e32\u0e08\u0e32\u0e01\u0e44\u0e1f\u0e25\u0e4c\u0e17\u0e35\u0e48\u0e04\u0e33\u0e2a\u0e31\u0e48\u0e07\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e02\u0e36\u0e49\u0e19 \u0e08\u0e36\u0e07\u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a\u0e44\u0e14\u0e49\u0e41\u0e21\u0e49\u0e44\u0e21\u0e48\u0e21\u0e35\u0e2d\u0e34\u0e19\u0e40\u0e17\u0e2d\u0e23\u0e4c\u0e40\u0e19\u0e47\u0e15 \u0e2b\u0e32\u0e01\u0e15\u0e49\u0e2d\u0e07\u0e01\u0e32\u0e23\u0e27\u0e34\u0e40\u0e04\u0e23\u0e32\u0e30\u0e2b\u0e4c\u0e1c\u0e25\u0e01\u0e32\u0e23\u0e23\u0e31\u0e19\u0e02\u0e2d\u0e07\u0e04\u0e38\u0e13\u0e40\u0e2d\u0e07 \u0e43\u0e2b\u0e49\u0e04\u0e31\u0e14\u0e25\u0e2d\u0e01\u0e42\u0e1f\u0e25\u0e40\u0e14\u0e2d\u0e23\u0e4c results/ \u0e44\u0e1b\u0e44\u0e27\u0e49\u0e17\u0e35\u0e48 data/run/ \u0e41\u0e25\u0e49\u0e27\u0e23\u0e31\u0e19\u0e40\u0e0b\u0e25\u0e25\u0e4c\u0e43\u0e2b\u0e21\u0e48\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u672c notebook \u8bfb\u53d6\u4fdd\u5b58\u5728 data/run/ \u4e2d\u7684\u8fd0\u884c\u8bc1\u636e\uff0c\u5e76\u751f\u6210\u4e00\u4e2a\u540d\u4e3a nas_results.json \u7684\u6c47\u603b\u6587\u4ef6\u3002\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u4e0d\u8fde\u63a5\u4e91\u7aef\uff0c\u6bcf\u4e2a\u6570\u5b57\u90fd\u6765\u81ea\u4f60\u7684\u547d\u4ee4\u751f\u6210\u7684\u6587\u4ef6\uff0c\u56e0\u6b64\u5373\u4f7f\u6ca1\u6709\u4e92\u8054\u7f51\u4e5f\u80fd\u9a8c\u8bc1\u3002\u5982\u679c\u4f60\u60f3\u5206\u6790\u81ea\u5df1\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u8bf7\u628a results/ \u6587\u4ef6\u5939\u590d\u5236\u5230 data/run/\uff0c\u7136\u540e\u91cd\u65b0\u8fd0\u884c\u5355\u5143\u683c\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "import json\n",
    "import os\n",
    "import random\n",
    "import re\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "SEED = 42\n",
    "random.seed(SEED)\n",
    "print(\"seed\", SEED)\n",
    "print(\"Python\", sys.version.split()[0])\n",
    "assert sys.version_info >= (3, 11)\n",
    "DEVICE = \"cpu\"\n",
    "print(\"device\", DEVICE, \"(this lab has no accelerator path)\")\n",
    "\n",
    "RUN_MODE = os.environ.get(\"S5_RUN_MODE\", \"recorded\")\n",
    "print(\"run mode\", RUN_MODE)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Find the evidence\n",
    "\n",
    "The resolver tries the notebook's `data/run/`, then `data/`, then a Kaggle input path, then `COURSE_DATA`. It never downloads anything.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e15\u0e31\u0e27\u0e0a\u0e48\u0e27\u0e22\u0e04\u0e49\u0e19\u0e2b\u0e32\u0e42\u0e1f\u0e25\u0e40\u0e14\u0e2d\u0e23\u0e4c\u0e08\u0e30\u0e25\u0e2d\u0e07 data/run/ \u0e01\u0e48\u0e2d\u0e19 \u0e41\u0e25\u0e49\u0e27\u0e08\u0e36\u0e07 data/ \u0e08\u0e32\u0e01\u0e19\u0e31\u0e49\u0e19\u0e1e\u0e32\u0e18\u0e02\u0e2d\u0e07 Kaggle \u0e41\u0e25\u0e30\u0e15\u0e31\u0e27\u0e41\u0e1b\u0e23 COURSE_DATA \u0e42\u0e14\u0e22\u0e44\u0e21\u0e48\u0e14\u0e32\u0e27\u0e19\u0e4c\u0e42\u0e2b\u0e25\u0e14\u0e2d\u0e30\u0e44\u0e23\u0e40\u0e25\u0e22\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u6587\u4ef6\u5939\u89e3\u6790\u5668\u4f1a\u5148\u5c1d\u8bd5 data/run/\uff0c\u7136\u540e\u5c1d\u8bd5 data/\uff0c\u63a5\u7740\u662f Kaggle \u8def\u5f84\u548c COURSE_DATA \u53d8\u91cf\uff0c\u5b8c\u5168\u4e0d\u4e0b\u8f7d\u4efb\u4f55\u5185\u5bb9\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def data_dir():\n",
    "    candidates = [Path(\"data\") / \"run\", Path(\"data\"),\n",
    "                  Path(\"/kaggle/input/s5-nextcloud-nas/run\")]\n",
    "    env = os.environ.get(\"COURSE_DATA\")\n",
    "    if env:\n",
    "        candidates.insert(0, Path(env) / \"run\")\n",
    "        candidates.insert(1, Path(env))\n",
    "    for path in candidates:\n",
    "        if path and (path / \"mdstat.txt\").exists():\n",
    "            return path\n",
    "    raise FileNotFoundError(\"mdstat.txt not found; put the run evidence in data/run/\")\n",
    "\n",
    "RUN = data_dir()\n",
    "print(\"evidence directory\", RUN)\n",
    "\n",
    "REQUIRED = [\n",
    "    \"mdstat.txt\", \"mdadm-detail.txt\", \"findmnt.txt\", \"df.txt\",\n",
    "    \"health.txt\", \"compose-ps.txt\", \"running-services.txt\",\n",
    "    \"checksum-before.txt\", \"checksum-after.txt\",\n",
    "    \"backup-ls.txt\", \"dump-size.txt\",\n",
    "    \"restore-original-hash.txt\", \"restore-hash.txt\",\n",
    "    \"instance-original.txt\", \"instance-empty.txt\", \"instance-recovered.txt\",\n",
    "    \"recovery-checksum.txt\", \"recovery-status.json\", \"status-first-boot.json\",\n",
    "]\n",
    "missing = [name for name in REQUIRED if not (RUN / name).exists()]\n",
    "assert not missing, f\"missing evidence files: {missing}\"\n",
    "print(\"evidence files present:\", len(REQUIRED))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. The mirror exists and is healthy\n",
    "\n",
    "`/proc/mdstat` reports the array state as two numbers: the number of working devices out of the total and the per-device state letters. `[2/2]` and `[UU]` mean both members are present and in sync.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "/proc/mdstat \u0e23\u0e32\u0e22\u0e07\u0e32\u0e19\u0e2a\u0e16\u0e32\u0e19\u0e30\u0e2d\u0e32\u0e23\u0e4c\u0e40\u0e23\u0e22\u0e4c\u0e40\u0e1b\u0e47\u0e19\u0e08\u0e33\u0e19\u0e27\u0e19\u0e2d\u0e38\u0e1b\u0e01\u0e23\u0e13\u0e4c\u0e17\u0e35\u0e48\u0e17\u0e33\u0e07\u0e32\u0e19\u0e44\u0e14\u0e49\u0e40\u0e17\u0e35\u0e22\u0e1a\u0e01\u0e31\u0e1a\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14 \u0e41\u0e25\u0e30\u0e15\u0e31\u0e27\u0e2d\u0e31\u0e01\u0e29\u0e23\u0e2a\u0e16\u0e32\u0e19\u0e30\u0e02\u0e2d\u0e07\u0e41\u0e15\u0e48\u0e25\u0e30\u0e2d\u0e38\u0e1b\u0e01\u0e23\u0e13\u0e4c [2/2] \u0e01\u0e31\u0e1a [UU] \u0e2b\u0e21\u0e32\u0e22\u0e04\u0e27\u0e32\u0e21\u0e27\u0e48\u0e32\u0e2a\u0e21\u0e32\u0e0a\u0e34\u0e01\u0e17\u0e31\u0e49\u0e07\u0e2a\u0e2d\u0e07\u0e25\u0e39\u0e01\u0e1e\u0e23\u0e49\u0e2d\u0e21\u0e41\u0e25\u0e30\u0e0b\u0e34\u0e07\u0e01\u0e4c\u0e15\u0e23\u0e07\u0e01\u0e31\u0e19\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "/proc/mdstat \u628a\u9635\u5217\u72b6\u6001\u62a5\u544a\u4e3a\u53ef\u7528\u8bbe\u5907\u6570\u4e0e\u603b\u8bbe\u5907\u6570\u4e4b\u6bd4\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u8bbe\u5907\u7684\u72b6\u6001\u5b57\u6bcd\u3002[2/2] \u548c [UU] \u8868\u793a\u4e24\u4e2a\u6210\u5458\u90fd\u5728\u4f4d\u5e76\u5df2\u540c\u6b65\u4e00\u81f4\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdstat = (RUN / \"mdstat.txt\").read_text()\n",
    "mdadm = (RUN / \"mdadm-detail.txt\").read_text()\n",
    "findmnt = (RUN / \"findmnt.txt\").read_text()\n",
    "df = (RUN / \"df.txt\").read_text()\n",
    "\n",
    "assert \"[2/2]\" in mdstat, mdstat\n",
    "assert \"[UU]\" in mdstat, mdstat\n",
    "assert \"Raid Level : raid1\" in mdadm, mdadm\n",
    "assert \"State : clean\" in mdadm, mdadm\n",
    "assert \"Active Devices : 2\" in mdadm, mdadm\n",
    "assert \"/dev/md1\" in findmnt and \"/srv/nas\" in findmnt and \"xfs\" in findmnt, findmnt\n",
    "\n",
    "size_match = re.search(r\"([0-9.]+)G\\s+\\S+\\s+\\S+\\s+\\S+\\s+/srv/nas\", df)\n",
    "assert size_match, df\n",
    "usable_gib = float(size_match.group(1))\n",
    "assert 8.0 <= usable_gib <= 10.5, usable_gib\n",
    "print(\"mirror\", \"raid1\", \"clean\", \"usable GiB\", usable_gib)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Every service is up and the ones with checks are healthy\n",
    "\n",
    "`docker compose ps` lists four services; `running-services.txt` is the filtered list of running services. The health file records the Docker health status for Nextcloud, MariaDB and Redis. Caddy has no health check, so its evidence is the successful HTTPS response, not a status string.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "docker compose ps \u0e41\u0e2a\u0e14\u0e07\u0e1a\u0e23\u0e34\u0e01\u0e32\u0e23\u0e17\u0e31\u0e49\u0e07\u0e2a\u0e35\u0e48 \u0e41\u0e25\u0e30 running-services.txt \u0e04\u0e37\u0e2d\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e1a\u0e23\u0e34\u0e01\u0e32\u0e23\u0e17\u0e35\u0e48\u0e01\u0e33\u0e25\u0e31\u0e07\u0e17\u0e33\u0e07\u0e32\u0e19 \u0e2a\u0e48\u0e27\u0e19\u0e44\u0e1f\u0e25\u0e4c health \u0e1a\u0e31\u0e19\u0e17\u0e36\u0e01\u0e2a\u0e16\u0e32\u0e19\u0e30\u0e2a\u0e38\u0e02\u0e20\u0e32\u0e1e\u0e02\u0e2d\u0e07 Nextcloud, MariaDB \u0e41\u0e25\u0e30 Redis \u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a Caddy \u0e44\u0e21\u0e48\u0e21\u0e35\u0e01\u0e32\u0e23\u0e15\u0e23\u0e27\u0e08\u0e2a\u0e38\u0e02\u0e20\u0e32\u0e1e \u0e2b\u0e25\u0e31\u0e01\u0e10\u0e32\u0e19\u0e08\u0e36\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e04\u0e33\u0e15\u0e2d\u0e1a HTTPS \u0e17\u0e35\u0e48\u0e2a\u0e33\u0e40\u0e23\u0e47\u0e08 \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e02\u0e49\u0e2d\u0e04\u0e27\u0e32\u0e21\u0e2a\u0e16\u0e32\u0e19\u0e30\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "docker compose ps \u5217\u51fa\u56db\u4e2a\u670d\u52a1\uff0crunning-services.txt \u662f\u6b63\u5728\u8fd0\u884c\u7684\u670d\u52a1\u5217\u8868\u3002health \u6587\u4ef6\u8bb0\u5f55 Nextcloud\u3001MariaDB \u548c Redis \u7684\u5065\u5eb7\u72b6\u6001\u3002Caddy \u6ca1\u6709\u5065\u5eb7\u68c0\u67e5\uff0c\u6240\u4ee5\u5b83\u7684\u8bc1\u636e\u662f\u6210\u529f\u7684 HTTPS \u54cd\u5e94\uff0c\u800c\u4e0d\u662f\u72b6\u6001\u5b57\u7b26\u4e32\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "health = (RUN / \"health.txt\").read_text()\n",
    "running = [line.strip() for line in\n",
    "           (RUN / \"running-services.txt\").read_text().splitlines() if line.strip()]\n",
    "compose_ps = (RUN / \"compose-ps.txt\").read_text()\n",
    "\n",
    "healthy = health.count('\"healthy\"')\n",
    "assert healthy == 3, health\n",
    "assert set(running) == {\"caddy\", \"db\", \"nextcloud\", \"redis\"}, running\n",
    "assert \"nextcloud:35-apache\" in compose_ps or \"nextcloud\" in compose_ps, compose_ps\n",
    "assert \"mariadb\" in compose_ps, compose_ps\n",
    "print(\"services\", sorted(running), \"healthy\", healthy)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. The container was replaced and the data did not move\n",
    "\n",
    "The checksum is taken inside the container before and after `docker compose up -d --force-recreate nextcloud`. The container is a different container; the file is the same file because it lives on the bind mount.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e41\u0e2e\u0e0a\u0e16\u0e39\u0e01\u0e04\u0e33\u0e19\u0e27\u0e13\u0e20\u0e32\u0e22\u0e43\u0e19\u0e04\u0e2d\u0e19\u0e40\u0e17\u0e19\u0e40\u0e19\u0e2d\u0e23\u0e4c\u0e01\u0e48\u0e2d\u0e19\u0e41\u0e25\u0e30\u0e2b\u0e25\u0e31\u0e07\u0e01\u0e32\u0e23\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e04\u0e2d\u0e19\u0e40\u0e17\u0e19\u0e40\u0e19\u0e2d\u0e23\u0e4c\u0e43\u0e2b\u0e21\u0e48 \u0e04\u0e2d\u0e19\u0e40\u0e17\u0e19\u0e40\u0e19\u0e2d\u0e23\u0e4c\u0e40\u0e1b\u0e47\u0e19\u0e04\u0e19\u0e25\u0e30\u0e15\u0e31\u0e27 \u0e41\u0e15\u0e48\u0e44\u0e1f\u0e25\u0e4c\u0e40\u0e1b\u0e47\u0e19\u0e44\u0e1f\u0e25\u0e4c\u0e40\u0e14\u0e35\u0e22\u0e27\u0e01\u0e31\u0e19 \u0e40\u0e1e\u0e23\u0e32\u0e30\u0e40\u0e01\u0e47\u0e1a\u0e2d\u0e22\u0e39\u0e48\u0e1a\u0e19 bind mount\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u5728\u91cd\u65b0\u521b\u5efa\u5bb9\u5668\u4e4b\u524d\u548c\u4e4b\u540e\uff0c\u90fd\u4f1a\u5728\u5bb9\u5668\u5185\u90e8\u8ba1\u7b97\u54c8\u5e0c\u3002\u5bb9\u5668\u5df2\u7ecf\u662f\u53e6\u4e00\u4e2a\u5bb9\u5668\uff0c\u4f46\u6587\u4ef6\u4ecd\u662f\u540c\u4e00\u4e2a\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u4f4d\u4e8e bind mount \u4e0a\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def first_hash(path):\n",
    "    return path.read_text().split()[0]\n",
    "\n",
    "before = first_hash(RUN / \"checksum-before.txt\")\n",
    "after = first_hash(RUN / \"checksum-after.txt\")\n",
    "assert len(before) == 64 and len(after) == 64, (before, after)\n",
    "assert before == after, (before, after)\n",
    "print(\"catalogue hash survives recreate:\", before[:16], \"...\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. The backup exists off the machine\n",
    "\n",
    "`rclone ls` lists objects with their sizes. The data tree, the configuration tree and the database dump must all be present; a listing with only one of them is a partial backup.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "rclone ls \u0e41\u0e2a\u0e14\u0e07\u0e23\u0e32\u0e22\u0e01\u0e32\u0e23\u0e2d\u0e2d\u0e1a\u0e40\u0e08\u0e01\u0e15\u0e4c\u0e1e\u0e23\u0e49\u0e2d\u0e21\u0e02\u0e19\u0e32\u0e14 \u0e15\u0e49\u0e2d\u0e07\u0e21\u0e35\u0e17\u0e31\u0e49\u0e07\u0e42\u0e04\u0e23\u0e07\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25 \u0e42\u0e04\u0e23\u0e07\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e01\u0e32\u0e23\u0e15\u0e31\u0e49\u0e07\u0e04\u0e48\u0e32 \u0e41\u0e25\u0e30\u0e44\u0e1f\u0e25\u0e4c\u0e14\u0e31\u0e21\u0e1b\u0e4c\u0e10\u0e32\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25 \u0e2b\u0e32\u0e01\u0e21\u0e35\u0e40\u0e1e\u0e35\u0e22\u0e07\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e43\u0e14\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e41\u0e2a\u0e14\u0e07\u0e27\u0e48\u0e32\u0e01\u0e32\u0e23\u0e2a\u0e33\u0e23\u0e2d\u0e07\u0e44\u0e21\u0e48\u0e04\u0e23\u0e1a\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "rclone ls \u5217\u51fa\u5bf9\u8c61\u53ca\u5176\u5927\u5c0f\u3002\u6570\u636e\u6811\u3001\u914d\u7f6e\u6811\u548c\u6570\u636e\u5e93\u8f6c\u50a8\u6587\u4ef6\u90fd\u5fc5\u987b\u5b58\u5728\uff1b\u5982\u679c\u53ea\u6709\u5176\u4e2d\u4e00\u9879\uff0c\u5c31\u8bf4\u660e\u5907\u4efd\u4e0d\u5b8c\u6574\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "backup = [line for line in\n",
    "          (RUN / \"backup-ls.txt\").read_text().splitlines() if line.strip()]\n",
    "paths = [line.split(maxsplit=1)[1] for line in backup if len(line.split(maxsplit=1)) == 2]\n",
    "assert any(\"nextcloud/\" in p for p in paths), paths[:5]\n",
    "assert any(\"config/\" in p for p in paths), paths[:5]\n",
    "assert any(\"s5-db.sql\" in p for p in paths), paths[:5]\n",
    "dump_size = (RUN / \"dump-size.txt\").read_text().strip()\n",
    "print(\"backup objects\", len(backup), \"dump\", dump_size)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. The restore is proven by a hash, not by a listing\n",
    "\n",
    "The original photo was hashed, deleted, fetched from R2 and uploaded again. Equal hashes mean the object came back intact.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e23\u0e39\u0e1b\u0e15\u0e49\u0e19\u0e09\u0e1a\u0e31\u0e1a\u0e16\u0e39\u0e01\u0e04\u0e33\u0e19\u0e27\u0e13\u0e41\u0e2e\u0e0a \u0e25\u0e1a\u0e2d\u0e2d\u0e01 \u0e14\u0e36\u0e07\u0e01\u0e25\u0e31\u0e1a\u0e08\u0e32\u0e01 R2 \u0e41\u0e25\u0e30\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\u0e43\u0e2b\u0e21\u0e48 \u0e41\u0e2e\u0e0a\u0e17\u0e35\u0e48\u0e40\u0e17\u0e48\u0e32\u0e01\u0e31\u0e19\u0e2b\u0e21\u0e32\u0e22\u0e04\u0e27\u0e32\u0e21\u0e27\u0e48\u0e32\u0e44\u0e1f\u0e25\u0e4c\u0e01\u0e25\u0e31\u0e1a\u0e21\u0e32\u0e04\u0e23\u0e1a\u0e16\u0e49\u0e27\u0e19\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u539f\u59cb\u7167\u7247\u5148\u88ab\u8ba1\u7b97\u54c8\u5e0c\u3001\u5220\u9664\u3001\u4ece R2 \u53d6\u56de\uff0c\u7136\u540e\u91cd\u65b0\u4e0a\u4f20\u3002\u54c8\u5e0c\u76f8\u540c\u610f\u5473\u7740\u8be5\u6587\u4ef6\u5b8c\u6574\u5730\u56de\u6765\u4e86\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "original = first_hash(RUN / \"restore-original-hash.txt\")\n",
    "restored = first_hash(RUN / \"restore-hash.txt\")\n",
    "assert original == restored, (original, restored)\n",
    "print(\"restore hash matches:\", restored[:16], \"...\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. The failure produced a different instance, and recovery restored the original\n",
    "\n",
    "Starting the stack with `/srv/nas` unmounted created a new Nextcloud installation on the root disk. Its instance id differs; after remounting and recreating, the id and the catalogue hash return to their original values.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e01\u0e32\u0e23\u0e40\u0e23\u0e34\u0e48\u0e21\u0e2a\u0e41\u0e15\u0e01\u0e02\u0e13\u0e30\u0e17\u0e35\u0e48 /srv/nas \u0e44\u0e21\u0e48\u0e44\u0e14\u0e49\u0e40\u0e21\u0e32\u0e19\u0e15\u0e4c\u0e17\u0e33\u0e43\u0e2b\u0e49\u0e40\u0e01\u0e34\u0e14\u0e01\u0e32\u0e23\u0e15\u0e34\u0e14\u0e15\u0e31\u0e49\u0e07 Nextcloud \u0e43\u0e2b\u0e21\u0e48\u0e1a\u0e19\u0e14\u0e34\u0e2a\u0e01\u0e4c\u0e23\u0e30\u0e1a\u0e1a \u0e2b\u0e21\u0e32\u0e22\u0e40\u0e25\u0e02\u0e2d\u0e34\u0e19\u0e2a\u0e41\u0e15\u0e19\u0e0b\u0e4c\u0e08\u0e36\u0e07\u0e15\u0e48\u0e32\u0e07\u0e2d\u0e2d\u0e01\u0e44\u0e1b \u0e2b\u0e25\u0e31\u0e07\u0e08\u0e32\u0e01\u0e40\u0e21\u0e32\u0e19\u0e15\u0e4c\u0e01\u0e25\u0e31\u0e1a\u0e41\u0e25\u0e30\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e04\u0e2d\u0e19\u0e40\u0e17\u0e19\u0e40\u0e19\u0e2d\u0e23\u0e4c\u0e43\u0e2b\u0e21\u0e48 \u0e2b\u0e21\u0e32\u0e22\u0e40\u0e25\u0e02\u0e2d\u0e34\u0e19\u0e2a\u0e41\u0e15\u0e19\u0e0b\u0e4c\u0e41\u0e25\u0e30\u0e41\u0e2e\u0e0a\u0e41\u0e04\u0e15\u0e15\u0e32\u0e25\u0e47\u0e2d\u0e01\u0e01\u0e25\u0e31\u0e1a\u0e21\u0e32\u0e40\u0e1b\u0e47\u0e19\u0e04\u0e48\u0e32\u0e40\u0e14\u0e34\u0e21\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u5728\u672a\u6302\u8f7d /srv/nas \u7684\u60c5\u51b5\u4e0b\u542f\u52a8\u8fd9\u5957\u6808\uff0c\u4f1a\u5728\u7cfb\u7edf\u76d8\u4e0a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Nextcloud \u5b89\u88c5\u3002\u5b83\u7684\u5b9e\u4f8b ID \u56e0\u6b64\u4e0d\u540c\uff1b\u91cd\u65b0\u6302\u8f7d\u5e76\u91cd\u65b0\u521b\u5efa\u5bb9\u5668\u4e4b\u540e\uff0c\u8be5 ID \u548c\u76ee\u5f55\u54c8\u5e0c\u90fd\u56de\u5230\u539f\u6765\u7684\u503c\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "instance_original = (RUN / \"instance-original.txt\").read_text().strip()\n",
    "instance_empty = (RUN / \"instance-empty.txt\").read_text().strip()\n",
    "instance_recovered = (RUN / \"instance-recovered.txt\").read_text().strip()\n",
    "assert instance_original and instance_empty, (instance_original, instance_empty)\n",
    "assert instance_original != instance_empty, \"the empty instance reused the real id\"\n",
    "assert instance_recovered == instance_original, (instance_recovered, instance_original)\n",
    "\n",
    "recovered_hash = first_hash(RUN / \"recovery-checksum.txt\")\n",
    "assert recovered_hash == before, (recovered_hash, before)\n",
    "status = json.loads((RUN / \"recovery-status.json\").read_text())\n",
    "assert status.get(\"installed\") is True, status\n",
    "print(\"instance\", instance_original[:8], \"empty\", instance_empty[:8], \"recovered\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Write the artifact\n",
    "\n",
    "The dictionary is the submitted evidence for the notebook half of the lab. It records **recorded** or **live** mode so a reader knows whether the numbers came from the shipped run or the student's own.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e1e\u0e08\u0e19\u0e32\u0e19\u0e38\u0e01\u0e23\u0e21\u0e19\u0e35\u0e49\u0e04\u0e37\u0e2d\u0e2b\u0e25\u0e31\u0e01\u0e10\u0e32\u0e19\u0e17\u0e35\u0e48\u0e2a\u0e48\u0e07\u0e2a\u0e33\u0e2b\u0e23\u0e31\u0e1a\u0e2a\u0e48\u0e27\u0e19\u0e42\u0e19\u0e49\u0e15\u0e1a\u0e38\u0e4a\u0e01\u0e02\u0e2d\u0e07\u0e41\u0e25\u0e47\u0e1a \u0e41\u0e25\u0e30\u0e1a\u0e31\u0e19\u0e17\u0e36\u0e01\u0e42\u0e2b\u0e21\u0e14 recorded \u0e2b\u0e23\u0e37\u0e2d live \u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e43\u0e2b\u0e49\u0e1c\u0e39\u0e49\u0e2d\u0e48\u0e32\u0e19\u0e23\u0e39\u0e49\u0e27\u0e48\u0e32\u0e15\u0e31\u0e27\u0e40\u0e25\u0e02\u0e21\u0e32\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23\u0e23\u0e31\u0e19\u0e17\u0e35\u0e48\u0e41\u0e19\u0e1a\u0e21\u0e32\u0e2b\u0e23\u0e37\u0e2d\u0e08\u0e32\u0e01\u0e01\u0e32\u0e23\u0e23\u0e31\u0e19\u0e02\u0e2d\u0e07\u0e19\u0e31\u0e01\u0e28\u0e36\u0e01\u0e29\u0e32\u0e40\u0e2d\u0e07\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u8fd9\u4e2a\u5b57\u5178\u5c31\u662f\u5b9e\u9a8c notebook \u90e8\u5206\u6240\u63d0\u4ea4\u7684\u8bc1\u636e\u3002\u5b83\u8bb0\u5f55 recorded \u6216 live \u6a21\u5f0f\uff0c\u8ba9\u8bfb\u8005\u77e5\u9053\u8fd9\u4e9b\u6570\u5b57\u6765\u81ea\u968f\u9644\u7684\u8fd0\u884c\u7ed3\u679c\u8fd8\u662f\u6765\u81ea\u5b66\u751f\u81ea\u5df1\u7684\u8fd0\u884c\u3002\n",
    "\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = {\n",
    "    \"session\": \"05\",\n",
    "    \"artifact\": \"nas_results.json\",\n",
    "    \"run\": {\"mode\": RUN_MODE, \"region\": \"sgp1\", \"size\": \"s-2vcpu-4gb\",\n",
    "            \"image\": \"rockylinux-10-x64\"},\n",
    "    \"nas\": {\"device\": \"/dev/md1\", \"level\": \"raid1\", \"state\": \"clean\",\n",
    "            \"filesystem\": \"xfs\", \"mountpoint\": \"/srv/nas\",\n",
    "            \"raw_gib\": 20, \"usable_gib\": usable_gib},\n",
    "    \"services\": {\"count\": len(running), \"healthy\": healthy, \"names\": sorted(running)},\n",
    "    \"checksums\": {\"original\": before, \"after_recreate\": after,\n",
    "                  \"recovered\": recovered_hash, \"restored\": restored},\n",
    "    \"backup\": {\"objects\": len(backup), \"has_database_dump\": True},\n",
    "    \"failure\": {\"instance_before\": instance_original, \"instance_empty\": instance_empty,\n",
    "                \"instance_recovered\": instance_recovered, \"empty_instance_created\": True},\n",
    "    \"verified\": {\"mirror_clean\": True, \"container_disposable\": True,\n",
    "                 \"backup_present\": True, \"restore_hash_matches\": True,\n",
    "                 \"failure_reproduced\": True, \"recovery_restores_original\": True},\n",
    "}\n",
    "out = Path(\"nas_results.json\")\n",
    "out.write_text(json.dumps(results, indent=2) + \"\\n\")\n",
    "print(out.resolve())\n",
    "print(json.dumps(results[\"verified\"], indent=2))\n",
    "assert results[\"verified\"] == {k: True for k in results[\"verified\"]}\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14"
  },
  "course": {
   "session": "05",
   "artifact": "nas_results.json",
   "verified": "Mac verified 2026-11-13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
