# Deploying OOS to the dev server

One command updates all three repos on the dev server, rebuilds them and restarts the services:

```bash
ssh dev-server 'sudo -u devgroup /var/www/go-projects/oos-service/deploy/dev/deploy.sh'
```

Then open `https://uat-<site>.admedia.com/` (for example https://uat-abenuva.admedia.com/).

## What you need

- SSH access to `dev-server` (192.168.30.106) and permission to run `sudo -u devgroup`. Ask System Administration if either is missing.
- Your changes merged into the branch you deploy. The server pulls from Bitbucket, not from your laptop.

## Commands

| Command | What it does |
|---|---|
| `deploy.sh` | Deploys `main` |
| `deploy.sh <branch>` | Deploys a branch. A repo that does not have that branch stays on `main`. |
| `deploy.sh --restart` | Restarts the services only, with no pull and no build. Use it after editing a file on the server. |
| `deploy.sh --status` | Shows the deployed commits, listening ports and the health check |

All commands are run as `ssh dev-server 'sudo -u devgroup /var/www/go-projects/oos-service/deploy/dev/deploy.sh <option>'`.

A deploy takes about 1 to 2 minutes. It:

1. Fetches each repo and fast-forwards it to the branch. It stops if a repo has local changes or local commits.
2. Installs dependencies (`npm ci`) and builds `oos-tesseract` (which compiles `oos-singlecube` into it).
3. Builds `oos-service` (`go build`).
4. Writes `oos-service/config.json` from `environments/dev/config.json` with the dev server's port and bundle path.
5. Stops and starts both services, then checks that they answer.

It prints `Deployed` and the commit of each repo when it succeeds. On a failure it prints the tail of the log and stops.

## Where things are

| Item | Value |
|---|---|
| Server | `dev-server` (dev-ab-ad2-30-106), 192.168.30.106 |
| User and folder | `devgroup`, `/var/www/go-projects/{oos-service,oos-tesseract,oos-singlecube}` |
| oos-service | port **8082** |
| oos-tesseract bundle | port **8888** (`vite preview` of `dist/`) |
| Logs | `/var/www/go-projects/logs/` (`oos-service.log`, `tesseract.log`, `build-*.log`) |

How a request flows:

```
https://uat-<site>.admedia.com
  -> nginx (openresty), wildcard certificate
       /oos/*  -> 127.0.0.1:8888   (tesseract.js, tesseract.css)
       /*      -> 127.0.0.1:8082   (oos-service, Host header kept)
  -> oos-service picks the site from the Host header
     (client_configs/site_to_client.json -> client_configs/<site>.json)
```

`tesseract_cdn_url` is `/oos`, so the browser loads the bundle from the same domain over HTTPS. The nginx rules are owned by System Administration (ticket SA-1470).

## Adding or changing a site

1. Add or edit `client_configs/<site>.json` in `oos-service` and map the hostnames in `client_configs/site_to_client.json`: the domain, `www.<domain>`, and `uat-<site>.admedia.com` (the UAT address). Copy the entries of an existing site.
2. Merge to `main` (or the branch you deploy) and run `deploy.sh`. The configs are read at startup, so the restart is what applies them.
3. Open `https://uat-<site>.admedia.com/`.

No nginx change is needed for a new `uat-<site>.admedia.com` name; nginx accepts every `uat-` name on the wildcard certificate.

Frontend changes (`oos-singlecube`, `oos-tesseract`) need the same deploy, because the bundle is rebuilt from source.

## Troubleshooting

| Symptom | Cause and fix |
|---|---|
| `{"error":"Project \"uat-x\" not found"}` | nginx sent the request to the shared project router instead of port 8082. Tell System Administration; the `uat-` rule must win over `*.admedia.com`. |
| Page loads but is unstyled or blank | The bundle is not reachable at `https://<domain>/oos/tesseract.js`. Check that port 8888 is up (`--status`) and that nginx has the `/oos/` rule. |
| Wrong site, or the default site, is shown | The Host header is not reaching `oos-service`, or the hostname is missing from `site_to_client.json`. |
| `has local changes` or `cannot be fast-forwarded` | Someone edited files or committed on the server. Look with `git status` in that repo and clean it up. Do not work on the server; change the repos on your machine and push. |
| `npm ci` fails with `ENOTFOUND npm.imds.lan` | The lockfiles point at an internal npm registry. The script already uses `--replace-registry-host=always`; if you run npm by hand, add it. |
| `git fetch` fails with `Permission denied (publickey)` | The server's read-only deploy key (`~devgroup/.ssh/bit-oos-deploy`) was removed from Bitbucket (Repository settings, Access keys, in all three repos). |
| `ports are still in use after 30 seconds` | `oos-service` takes about 11 seconds to stop. Check `ss -ltnp` for what holds 8082 and 8888. |
| Browser reaches nothing | The firewall only opens some ports. Use the `uat-` domain, not `192.168.30.106:8082` directly. |

## Good to know

- The services run as plain background processes under `devgroup`. They do **not** restart after a server reboot. After a reboot, run `deploy.sh --restart`. A systemd unit would fix this and needs System Administration.
- `oos-service/config.json` holds API keys and is rewritten on every deploy. The script discards local changes to it before pulling. Never paste its contents into tickets or chat.
- A category added in the CMS returns 404 until `oos-service` next refreshes its category list, which takes up to about 12 hours. Its sitemap and listings wait for the same refresh. To pick it up now, run `deploy.sh --restart`.
- To change the ports or the bundle path, set `OOS_PORT`, `CDN_PORT` or `CDN_PATH` when running the script, and update the nginx rules to match.
- Production is deployed differently (see the Makefile, `init/oos-service.service` and the ticket for the production release). This script is for the dev server only.
