From 69bb81ef6570096e7529314e236a4b0b72da3a8b Mon Sep 17 00:00:00 2001 From: Magnus Andreas Igesund Nerland Date: Tue, 17 Nov 2026 20:40:57 +0100 Subject: [PATCH 1/3] docs(vm): migrations, backup and backend/.env in the VM guide (#74) --- docs/vm.md | 116 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 11 deletions(-) diff --git a/docs/vm.md b/docs/vm.md index ce7b4c4..92cdabf 100644 --- a/docs/vm.md +++ b/docs/vm.md @@ -65,6 +65,47 @@ For an SSH clone URL, configure your VM account's SSH key for git.ntnu.no. Follow [setup](setup.md) to configure `backend/.env`. Use the Ubuntu steps in [database](database.md) to create the database and user once. +## `backend/.env` on the VM + +The backend reads `DATABASE_URL` from `backend/.env` in the checkout it runs from. Git does not +copy `.env`, so every checkout that starts the backend needs its own. The password is the one for +the database user `vinmonopolet_app` on the VM, not your VM password. + +Find the checkout the running backend uses, and so the `.env` that works. The file can be read with +`sudo`, also for the course staff: + +```bash +sudo readlink "/proc/$(pgrep -f 'src/server.ts' | head -1)/cwd" # e.g. /home//ntnuhome/T18-Project-2/backend +sudo ls -l /home/*/ntnuhome/T18-Project-2/backend/.env +``` + +To create your own, ask the group for the password, then: + +```bash +cd ~/ntnuhome/T18-Project-2/backend +cp .env.example .env +chmod 600 .env +nano .env +``` + +- Write `@`, `:`, `/`, `#` and `%` in the password as `%40`, `%3A`, `%2F`, `%23` and `%25`. +- Leave `COOKIE_SECURE` commented out. The VM serves plain HTTP, and secure cookies are only sent + over HTTPS, so login would stop working. + +Check the connection. The first line about GSSAPI or Kerberos is harmless; only +`password authentication failed` means the password is wrong: + +```bash +psql "$(grep ^DATABASE_URL .env | cut -d= -f2-)" -c "SELECT current_user;" +``` + +If nobody knows the password, a group member with `sudo` can set a new one. Every other `.env` on +the VM then stops working until it is updated, so tell the group first: + +```bash +sudo -u postgres psql -c "ALTER ROLE vinmonopolet_app WITH PASSWORD 'new_password';" +``` + ## Publish the frontend Run on the VM from the checkout's `frontend/` directory: @@ -107,7 +148,13 @@ ss -ltnp 'sport = :3001' ``` If a backend already uses the port, stop it before starting another instance. -Coordinate with the group if another user owns the process. +See who started it and from which checkout, and coordinate with the group if it is another user. +Never stop another user's process without asking: + +```bash +pgrep -af 'src/server.ts' +ps -o pid,user,args -p "$(pgrep -f 'src/server.ts' | head -1)" +``` Start the backend from its directory: @@ -130,10 +177,13 @@ Check that the backend started: ps -p "$(cat logs/backend.pid)" -o pid,user,args tail -n 50 logs/backend.log curl -i http://127.0.0.1:3001/ +curl -s http://127.0.0.1:3001/graphql -H 'content-type: application/json' -d '{"query":"{ me { id } wines(first: 1) { totalCount } }"}' ``` -For the database greeting backend, expect HTTP `200` and database information. -HTTP `503` means the database query failed. Check the log and database configuration. +The log should say `Backend listening`. The first `curl` shows HTTP `200` and database information; +`503` means the database query failed, so check the log and `backend/.env`. The second should answer +`{"data":{"me":null,"wines":{"totalCount":…}}}`. An error about a missing table or column means a +migration has not been run (see [Update and restart](#update-and-restart)). Follow the log with: @@ -153,7 +203,7 @@ backend_pid=$(cat logs/backend.pid) ps -p "$backend_pid" -o pid,user,args ``` -Check that the output identifies your Node process and this checkout's `server.js`. +Check that the output identifies your Node process and this checkout's `src/server.ts`. Then stop that process: ```bash @@ -221,25 +271,69 @@ so check that the backend process is still up. The file on the VM also has an older `ProxyPass "/api/hello"` from the first submission. It does nothing now and can be removed the next time the configuration is touched. See Apache's [reverse proxy documentation](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#examples). -ProxyPass "/project2/graphql" "http://127.0.0.1:3001/graphql" -ProxyPassReverse "/project2/graphql" "http://127.0.0.1:3001/graphql" ## Update and restart -1. Stop the backend using the instructions above. -2. Update the checkout and publish the frontend: +Do the backend first: new frontend code expects the new API, so publishing the frontend before the +backend runs the new code gives errors on the site until the backend is back. Tell the group before +you start, and check who runs the backend (see [Start](#start)). + +1. **Back up the database.** Migrations change tables, and the backup is the way back. + `sudo -u postgres` works without the database password: + + ```bash + mkdir -p ~/backups + sudo -u postgres pg_dump -Fc vinmonopolet > ~/backups/vinmonopolet-$(date +%F-%H%M).dump + ls -lh ~/backups + ``` + +2. **Stop the backend** using [Stop](#stop) above. +3. **Update the checkout** to `main`: ```bash cd ~/ntnuhome/T18-Project-2 + git status # clean, on main git pull --ff-only - cd frontend + ``` + +4. **Install, migrate and start the backend.** The migration is safe to run every time: it only + runs the migrations that are missing. Without it, the new code asks for tables and columns + the database does not have yet: + + ```bash + cd backend + node --version # 24 or newer; otherwise `source ~/.bash_profile` + npm ci + npm run db:migrate + ``` + + Then start it with the `nohup` commands in [Start](#start), and run the checks there. + +5. **Publish the frontend:** + + ```bash + cd ../frontend npm ci npm run buildvm curl -I http://localhost/project2/ ``` -3. Install the backend dependencies and start it using the `nohup` instructions above. -4. Check the backend log, API response, and application in the browser. +6. **Check the site** in a private browser window, so no old code is cached: the landing page and + search show wines, a wine shows its reviews, and you can log in, write a review, reload and + still be logged in. Delete test users afterwards: + + ```bash + sudo -u postgres psql -d vinmonopolet -c "DELETE FROM app_user WHERE username LIKE 'test\_%';" + ``` + +### Restore a backup + +If an update went wrong and the data must go back: stop the backend, restore, check out the +version that matches the backup, and start again. + +```bash +sudo -u postgres pg_restore --clean --if-exists -d vinmonopolet ~/backups/.dump +``` ## Load the Slim dataset From 97690b569051aceea2d7965849493923313e2d4e Mon Sep 17 00:00:00 2001 From: Magnus Andreas Igesund Nerland Date: Tue, 17 Nov 2026 20:51:07 +0100 Subject: [PATCH 2/3] docs(vm): the VM uses the Full dataset (#74) --- docs/vm.md | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/docs/vm.md b/docs/vm.md index 92cdabf..260999a 100644 --- a/docs/vm.md +++ b/docs/vm.md @@ -279,14 +279,20 @@ backend runs the new code gives errors on the site until the backend is back. Te you start, and check who runs the backend (see [Start](#start)). 1. **Back up the database.** Migrations change tables, and the backup is the way back. - `sudo -u postgres` works without the database password: + `sudo -u postgres` works without the database password. With the Full dataset the dump is + large, so check that the disk has room for it first: ```bash + df -h ~ # free space + sudo -u postgres psql -Atc "SELECT pg_size_pretty(pg_database_size('vinmonopolet'));" mkdir -p ~/backups sudo -u postgres pg_dump -Fc vinmonopolet > ~/backups/vinmonopolet-$(date +%F-%H%M).dump ls -lh ~/backups ``` + The compressed dump (`-Fc`) is smaller than the database, but plan for it to be a sizeable + part of it. Delete old dumps in `~/backups` when the update has gone well. + 2. **Stop the backend** using [Stop](#stop) above. 3. **Update the checkout** to `main`: @@ -335,33 +341,44 @@ version that matches the backup, and start again. sudo -u postgres pg_restore --clean --if-exists -d vinmonopolet ~/backups/.dump ``` -## Load the Slim dataset +## Load the dataset -The VM uses the Slim version of X-Wines (1 007 wines, 150 000 ratings). +The VM uses the Full version of X-Wines (100 646 wines, about 21 million ratings). The files are not in the repository. -1. Download the Slim version from - [Kaggle](https://www.kaggle.com/datasets/rogerioxavier/x-wines-slim-version) and unzip it. + + +1. Download the Full version from and unzip it. 2. Copy the two CSV files to the VM. Run this on your own computer: ```bash - ssh @it2810-18.idi.ntnu.no 'mkdir -p ~/xwines-slim' - scp XWines_Slim_1K_wines.csv XWines_Slim_150K_ratings.csv @it2810-18.idi.ntnu.no:~/xwines-slim/ + ssh @it2810-18.idi.ntnu.no 'mkdir -p ~/xwines-full' + scp .csv .csv @it2810-18.idi.ntnu.no:~/xwines-full/ ``` -3. Import them on the VM: +3. Check that the disk has room. The database takes about GB with Full: + + ```bash + df -h /var/lib/postgresql ~ + ``` + +4. Import them on the VM: ```bash cd ~/ntnuhome/T18-Project-2/backend - npx tsx src/scripts/seed-xwines.ts ~/xwines-slim/XWines_Slim_1K_wines.csv ~/xwines-slim/XWines_Slim_150K_ratings.csv + npx tsx src/scripts/seed-xwines.ts ~/xwines-full/.csv ~/xwines-full/.csv ``` -The same script loads the Full version (100 646 wines, 21 million ratings), but that needs about -2.4 GB of database and more disk than the VM has to spare, so we only use Full locally. + The import takes about minutes. Run it in `tmux` or with `nohup`, so it is not + stopped if the SSH connection drops. -The import is safe to run more than once. The Test wines are part of Slim, so they are updated, not duplicated. +The import is safe to run more than once: wines that are already there are updated, not duplicated. The backend does not need a restart. +The same script loads the Slim version (1 007 wines, 150 000 ratings) from +[Kaggle](https://www.kaggle.com/datasets/rogerioxavier/x-wines-slim-version), and the Test version +in `backend/data/xwines-test` (`npm run db:seed:test`), for a smaller local database. + ## Command reference - [GNU nohup manual](https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html) From d0e3968dbdac5ecc43a8e1ec05a20b81f2d5db24 Mon Sep 17 00:00:00 2001 From: William Mikal Ruud Schmidt Date: Tue, 17 Nov 2026 21:40:39 +0100 Subject: [PATCH 3/3] docs(vm): fill in the Full dataset details (#74) --- docs/vm.md | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/docs/vm.md b/docs/vm.md index 260999a..ba142c8 100644 --- a/docs/vm.md +++ b/docs/vm.md @@ -343,41 +343,62 @@ sudo -u postgres pg_restore --clean --if-exists -d vinmonopolet ~/backups/ ## Load the dataset -The VM uses the Full version of X-Wines (100 646 wines, about 21 million ratings). -The files are not in the repository. +The VM uses the Full version of X-Wines (100 646 wines, 21 013 536 ratings). It replaced Slim on +17 November 2026. The database is now about 2.4 GB, and the VM had 2.7 GB of disk left afterwards. - +The files are not in the repository, and they are not kept on the VM. The X-Wines authors share them +in a [Google Drive folder](https://drive.google.com/drive/folders/1LqguJNV-aKh1PuWMVx5ELA61LPfGfuu_), +linked from the [X-Wines repository](https://github.com/rogerioxavier/X-Wines). The two files are in +its `last` folder: `XWines_Full_100K_wines.csv` (36 MB) and `XWines_Full_21M_ratings.csv` (1.1 GB). -1. Download the Full version from and unzip it. -2. Copy the two CSV files to the VM. Run this on your own computer: +1. **Back up the database** as in step 1 of [Update and restart](#update-and-restart). The import does + not touch student reviews or user accounts, but the backup is the way back if it goes wrong. + The backup taken before the switch is `~/vinmonopolet-slim-2026-11-17.dump` in `williasc`'s home. +2. **Check that the disk has room.** The database grows to about 2.4 GB, and PostgreSQL needs up to + 1 GB more while it writes. Start only if `df -h /` shows at least 4 GB free: ```bash - ssh @it2810-18.idi.ntnu.no 'mkdir -p ~/xwines-full' - scp .csv .csv @it2810-18.idi.ntnu.no:~/xwines-full/ + df -h / ``` -3. Check that the disk has room. The database takes about GB with Full: +3. **Download the files on the VM** into `/tmp`. `/tmp` is kept in memory, so the files do not use + disk space, and it is emptied when the VM restarts: ```bash - df -h /var/lib/postgresql ~ + mkdir -p /tmp/xwines-full && cd /tmp/xwines-full + curl -fL -o XWines_Full_100K_wines.csv 'https://drive.usercontent.google.com/download?id=1uEEipmKNxdiKUAhjH-K14JOSQ2BLRFss&export=download&confirm=t' + curl -fL -o XWines_Full_21M_ratings.csv 'https://drive.usercontent.google.com/download?id=15mwSBmGv-FY3-gJzL0wd1grav6CpYw8j&export=download&confirm=t' + wc -l XWines_Full_21M_ratings.csv # 21013537, including the header ``` -4. Import them on the VM: +4. **Import them** with `nohup`, so the import is not stopped if the SSH connection drops: ```bash cd ~/ntnuhome/T18-Project-2/backend - npx tsx src/scripts/seed-xwines.ts ~/xwines-full/.csv ~/xwines-full/.csv + mkdir -p logs + nohup npx tsx src/scripts/seed-xwines.ts /tmp/xwines-full/XWines_Full_100K_wines.csv /tmp/xwines-full/XWines_Full_21M_ratings.csv >> logs/seed-full.log 2>&1 < /dev/null & + tail -f logs/seed-full.log ``` - The import takes about minutes. Run it in `tmux` or with `nohup`, so it is not - stopped if the SSH connection drops. + The import takes about 8 minutes on the VM: 14 s for the wines, 7.5 minutes for the ratings and + 13 s for the averages. The log reports every 500 000 ratings and ends with `Updated average ratings`. + Until then, the site shows old or missing averages. -The import is safe to run more than once: wines that are already there are updated, not duplicated. -The backend does not need a restart. +5. **Update the query statistics and delete the files:** + + ```bash + psql -h localhost -U vinmonopolet_app -d vinmonopolet -c 'VACUUM (ANALYZE) wine' -c 'VACUUM (ANALYZE) community_rating' + rm -rf /tmp/xwines-full + ``` + +The import is safe to run more than once: wines that are already there are updated, not duplicated, +and ratings that are already there are skipped. The backend does not need a restart. The same script loads the Slim version (1 007 wines, 150 000 ratings) from [Kaggle](https://www.kaggle.com/datasets/rogerioxavier/x-wines-slim-version), and the Test version -in `backend/data/xwines-test` (`npm run db:seed:test`), for a smaller local database. +in `backend/data/xwines-test` (`npm run db:seed:test`), for a smaller local database. Loading Slim +does not remove Full wines that are already there. To go back to Slim on the VM, restore the backup +from before the switch (see [Restore a backup](#restore-a-backup)). ## Command reference