diff --git a/docs/vm.md b/docs/vm.md index 36e88b3..0be4ebd 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,55 +271,116 @@ 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. 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`: ```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 + npm run db:seed:demo # the demo user in the README: creates it, or puts it back as it was + ``` + + Then start it with the `nohup` commands in [Start](#start), and run the checks there. Check + that the API knows the newest fields before you publish the frontend, for example: + + ```bash + curl -s http://127.0.0.1:3001/graphql -H 'content-type: application/json' -d '{"query":"{ wines(first: 1) { edges { node { myLists } } } }"}' + ``` + + `Cannot query field` means the old backend is still running, or the migration did not run. + +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: -## Load the Full dataset + ```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. Reviews and accounts created after the backup +are lost. + +```bash +sudo -u postgres pg_restore --clean --if-exists -d vinmonopolet ~/backups/.dump +``` -The VM uses the Full version of X-Wines (100 646 wines, 21 013 536 ratings), loaded on 17 November 2026. -The files are not in the repository. The X-Wines authors share them in a -[Google Drive folder](https://drive.google.com/drive/folders/1LqguJNV-aKh1PuWMVx5ELA61LPfGfuu_), +## Load the dataset + +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). -The database grows to about 2.4 GB. Check that `df -h /` shows at least 4 GB free before you start. - -1. Back up the database first. The VM has no backup, and student reviews and user accounts cannot be downloaded again: +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 - sudo -u postgres pg_dump -Fc vinmonopolet > ~/vinmonopolet-$(date +%F).dump + df -h / ``` -2. 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: +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 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 + wc -l XWines_Full_21M_ratings.csv # 21013537, including the header ``` - The ratings file has 21 013 537 lines, including the header. - -3. Import them with `nohup`, so the import continues if SSH disconnects. It takes about 8 minutes: +4. **Import them** with `nohup`, so the import is not stopped if the SSH connection drops: ```bash cd ~/ntnuhome/T18-Project-2/backend @@ -278,27 +389,26 @@ The database grows to about 2.4 GB. Check that `df -h /` shows at least 4 GB fre tail -f logs/seed-full.log ``` - The log reports progress every 500 000 ratings and ends with `Updated average ratings`. + 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. -4. Update the query statistics, then delete the files: +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. Existing wines are updated and existing ratings are skipped. -Student reviews and user accounts are not touched. The backend does not need a restart. +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. Student reviews and user accounts are not touched. +The backend does not need a restart. -To load the Slim version instead, use the same script with the Slim files from -[Kaggle](https://www.kaggle.com/datasets/rogerioxavier/x-wines-slim-version). Slim does not remove -Full wines that are already in the database. To go back to Slim, restore a backup taken before the -Full import. Reviews and accounts created after the backup are lost: - -```bash -sudo -u postgres pg_restore --clean --if-exists -d vinmonopolet ~/vinmonopolet-.dump -``` +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. 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