# Upgrade Platforma

Install a new version of the Backend, and return to the old version if the upgrade causes problems.

An upgrade replaces the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) with a new version, while your projects and results stay where they are. Jobs (One run of a tool on data. The Backend starts jobs on the server where it runs, or in a Kubernetes cluster.) that run in the cluster continue, and the Desktop App (The program that each user installs on their own computer to work with Platforma. It is a client of the Backend.) connects again by itself after a short break.

The new version can change the format of the database when it starts for the first time. An older version cannot start on that database again. So a fresh backup and the name of the old version are your way back: you get both in the steps before the upgrade.

Upgrade to the most recent release: you can go to it directly from any older version, and you do not need to install the versions between them. Only the most recent release gets fixes. See [Supported versions](/reference-versions/#supported-versions).

## Before the upgrade

1. **Read what changes.** Read the [release notes](/reference-release-notes/) of the new version, and of each version that you skip: a major version can change the configuration. Check whether it needs a newer Desktop App: if it does, plan to upgrade the Desktop App on the computers of the users at the same time. The Desktop App and the Backend have their own versions.

2. **Write down the version that runs now.** You need it to go back.

   #### Variant: AWS installer

   ```sh
   kubectl get deployment platforma -n platforma \
     -o jsonpath='{.spec.template.spec.containers[?(@.name=="platforma")].image}{"\n"}{.metadata.labels.helm\.sh/chart}{"\n"}'
   ```

   Result: the image, for example `quay.io/milaboratories/platforma:4.7.0`, and the chart, for example `platforma-4.7.0`: the chart version is `4.7.0`.

   Also save the template of the stack. You need it to go back:

   ```sh
   aws cloudformation get-template --stack-name <stack-name> \
     --query TemplateBody --output text > template-before-upgrade.yaml
   ```

   #### Variant: Google Cloud installer

   ```sh
   kubectl get deployment platforma -n platforma \
     -o jsonpath='{.spec.template.spec.containers[?(@.name=="platforma")].image}{"\n"}{.metadata.labels.helm\.sh/chart}{"\n"}'
   ```

   Result: the image and the chart. Also write down the release tag of `platforma-helm` that you installed from, `git -C platforma-helm describe --tags`, and check that you have the file of the answers that you saved at the installation.

   #### Variant: Terraform modules

   ```sh
   kubectl get deployment platforma -n platforma \
     -o jsonpath='{.spec.template.spec.containers[?(@.name=="platforma")].image}{"\n"}{.metadata.labels.helm\.sh/chart}{"\n"}'
   ```

   Result: the image and the chart. In the root `services`, `terraform output release` also shows the chart version and the app version, and `terraform output versions` the contract version and the chart version. In the root `cluster`, `terraform output versions` shows the versions of Kueue (A Kubernetes add-on that holds jobs in a queue until the cluster has free CPU, memory and GPU for them.) and AppWrapper (A Kubernetes add-on that wraps the pods of one job into a single unit, so that Kueue admits and starts them together.).

   Also write down the release tag of platforma-install that you applied, for example with `git -C <platforma-install> describe --tags`, and keep this clone until the new version works well.

   #### Variant: Helm chart

   ```sh
   kubectl get deployment platforma -n platforma \
     -o jsonpath='{.spec.template.spec.containers[?(@.name=="platforma")].image}{"\n"}{.metadata.labels.helm\.sh/chart}{"\n"}'
   helm get values platforma -n platforma -o yaml > values-before-upgrade.yaml
   ```

   Result: the image, the chart version, and your current values in `values-before-upgrade.yaml`.

   #### Variant: Single Server

   ```sh
   /opt/platforma/binaries/platforma --version
   ```

   You keep the folder of the old version during the upgrade, until the new version works well.

3. **Check that the installation is healthy.** Do not start an upgrade on an installation that already has problems: after the upgrade, you cannot tell which problems the upgrade caused. Connect with the Desktop App, open a project, and check that its results are there. Then check the Backend:

   #### Variant: AWS installer

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   kubectl get pods -n kueue-system
   kubectl get pods -n appwrapper-system
   ```

   Result: all pods are `Running`, with no recent restarts.

   #### Variant: Google Cloud installer

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   kubectl get pods -n kueue-system
   kubectl get pods -n appwrapper-system
   ```

   Result: all pods are `Running`, with no recent restarts.

   #### Variant: Terraform modules

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   kubectl get pods -n kueue-system
   kubectl get pods -n appwrapper-system
   ```

   Result: all pods are `Running`, with no recent restarts.

   #### Variant: Helm chart

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   kubectl get pods -n kueue-system
   kubectl get pods -n appwrapper-system
   ```

   Result: all pods are `Running`, with no recent restarts.

   #### Variant: Single Server

   ```sh
   sudo systemctl status platforma
   ```

   Result: `active (running)`, and the service did not restart recently.

4. **Tell the users.** The Backend restarts during the upgrade, and the Desktop App loses its connection for a short time. Choose a time when few people work.

5. **Make a fresh backup.** Follow [Make a backup now](/maintain-backup-and-restore/#make-a-backup-now), and copy the backup out of the installation. Keep it until you are sure that the new version works well.

## Upgrade

#### Variant: AWS installer

Update the stack with a change set from the template of the new release: see [Upgrade to a new version](/maintain-updating-cloud-installer/#upgrade-to-a-new-version) on the page “Update an installer installation”.

#### Variant: Google Cloud installer

Run `install.sh` of the new release tag with the answers that you saved: see [Upgrade to a new version](/maintain-updating-cloud-installer/#upgrade-to-a-new-version) on the page “Update an installer installation”.

#### Variant: Terraform modules

A release of the modules pins the versions of the charts: `platforma-tf-services` pins the chart `platforma-backend`, whose app version is the version of the Backend, and `platforma-tf-services-cluster` pins Kueue and AppWrapper. An input cannot change these versions. To upgrade, you apply a new release of the modules, root by root, in the order of the installation. Each module checks its inputs at plan time and stops the plan at a wrong value.

1. **Get the new release** of platforma-install: its new release tag. See [Where the modules are](/install-cloud-terraform/#where-the-modules-are).

   #### Variant: AWS or Google Cloud

   Get the new release into the same clone:

   ```sh
   git -C <platforma-install> fetch --tags
   git -C <platforma-install> checkout <new release tag>
   ```

   The state files, the files `terraform.tfvars` and the hand-off files stay in place: git ignores them.

   #### Variant: Existing cluster

   Get the new release into the clone that `chart_repository` points to, with the commands of the tab **AWS or Google Cloud**. In `cluster/main.tf` and `services/main.tf`, change the tag in `ref=` of `source` to the new release tag. The plan stops when the folder of the charts does not hold the version that the module pins.

   *services/main.tf*

   ```hcl
   module "platforma" {
     source = "git::https://github.com/milaboratory/platforma-install.git//terraform/cloud-agnostic/platforma-tf-services?ref=<new release tag>"


     namespace      = "platforma"
     infrastructure = jsondecode(file("../handoff/infrastructure.json"))
     backend_service = {
       chart_repository = "<platforma-install>/helm/charts"
       # the other values stay the same
     }
   }
   ```

   When the cluster gets the charts and the images from mirrors, copy the new versions into the mirrors first: the README of each module lists the versions that it pins. See [Registries mirroring](/registries-mirroring/).

   If you set `backend_service.backend.image.tag`, the Backend stays at that version. Change the tag too, or remove it: then the image has the app version of the new chart.

2. **Apply the root `cloud`.** Only for AWS and Google Cloud. Read the plan before you apply it. When the plan changes the nodes of a job pool, apply it when no job runs: see [Upgrade the nodes](#upgrade-the-nodes).

   ```sh
   cd <platforma-install>/terraform/<cloud>/examples/cloud   # <cloud> is aws or gcp
   terraform init -upgrade
   terraform plan -out=upgrade.tfplan
   terraform apply upgrade.tfplan
   terraform output -json platforma_infrastructure > ../handoff/platforma-infrastructure.json
   terraform output -json cluster_connection > ../handoff/cluster-connection.json
   ```

3. **Apply the root `cluster`.** When the new release pins new versions of Kueue and AppWrapper, this apply upgrades them. The jobs that run continue, and the jobs in the queues wait and start when the new controllers are ready. While the controllers restart, the cluster can refuse new jobs for a short time: apply this root when few jobs start. Helm waits up to 10 minutes until each controller is ready, and goes back to the old version if it is not.

   #### Variant: AWS or Google Cloud

   ```sh
   cd ../cluster
   ```

   #### Variant: Existing cluster

   ```sh
   cd cluster
   ```

   ```sh
   terraform init -upgrade
   terraform plan -out=upgrade.tfplan
   terraform apply upgrade.tfplan
   terraform output -json infrastructure > ../handoff/infrastructure.json
   terraform output versions
   ```

   Result: `versions` shows the new versions of Kueue and AppWrapper, and the pods in the namespaces `kueue-system` and `appwrapper-system` are `Running`.

4. **Apply the root `services`.** The new chart changes the image of the Backend, so the Backend restarts: see [Restart of the Backend](/configuration-storage/#restart-of-the-backend). The apply waits until the Backend is ready.

   #### Variant: AWS or Google Cloud

   ```sh
   cd <platforma-install>/terraform/cloud-agnostic/examples/services
   terraform init -upgrade
   terraform plan -out=upgrade.tfplan \
     -var infrastructure_file=../../../<cloud>/examples/handoff/infrastructure.json \
     -var cluster_connection_file=../../../<cloud>/examples/handoff/cluster-connection.json
   terraform apply upgrade.tfplan
   ```

   #### Variant: Existing cluster

   ```sh
   cd ../services
   terraform init -upgrade
   terraform plan -out=upgrade.tfplan
   terraform apply upgrade.tfplan
   ```

   Result: `terraform output release` shows the new chart version and app version.

> **A long database conversion makes the apply fail:**
>
> The Backend converts the database when it starts, before its health check answers. The module waits 15 minutes for the Backend. On a large database, the conversion can take longer: then the apply fails, and Helm deploys the old version again, which does not start on the converted database. Do not go back: apply the new release again, and the conversion continues from the last step that it finished.

Each release of a module accepts a fixed list of [contract versions](/infrastructure-contract-modules/#the-contract-version): the README of the new release lists them. When the new `platforma-tf-services` accepts the contract version of the old release, the root `services` can go first, when the infrastructure is not ready for the new version. If it does not accept the old one, apply all roots. To change the inputs of a root after the upgrade, see [Install in cloud with Terraform](/install-cloud-terraform/) or [Install in custom infrastructure](/install-custom-infrastructure/).

#### Variant: Helm chart

```sh
helm upgrade platforma oci://ghcr.io/milaboratory/platforma-install/platforma-backend \
  --version <new-chart-version> -n platforma -f values-before-upgrade.yaml
```

The chart is also in the folder `helm/charts/platforma-backend` of [platforma-install](https://github.com/milaboratory/platforma-install), at each release tag. To install from a clone of the new release tag, use the path of this folder in place of the OCI address and remove `--version`.

#### Variant: Single Server

1. **Stop the Backend:** `sudo systemctl stop platforma`.

2. **Check that the Backend stopped.**

   Check that the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) stopped:

   ```sh
   systemctl is-active platforma
   pgrep -fa /opt/platforma/binaries/platforma
   ```

   Result: the first command prints `inactive`, and the second command prints nothing. If a process is still there, wait and run the commands again. Do not continue while the process exists: it can still write to the database.

3. **Copy `/data/platforma` to a safe place,** if you did not do it in [Before the upgrade](#before-the-upgrade).

4. **Replace the program.** Move the old program aside, then get the new release as in the installation:

   ```sh
   sudo mv /opt/platforma /opt/platforma-old
   sudo mkdir /opt/platforma
   curl -fL -o /tmp/platforma.tgz https://dl.platforma.bio/software/platforma-backend/pl-linux-amd64.tgz
   sudo tar -xzf /tmp/platforma.tgz -C /opt/platforma
   /opt/platforma/binaries/platforma --version
   ```

   Result: the command shows the new version. Keep `/opt/platforma-old` until the new version works well.

5. **Start the Backend:** `sudo systemctl start platforma`.

The data in `/data/platforma` stays in place.

## After the upgrade

1. **The Backend runs.** The first start after an upgrade can take longer, because the Backend converts the database to the new format. Do not restart the Backend during this time.

   The same rule applies on all paths when the conversion stops, for example when an apply or a stack update fails, or the server restarts. Do not deploy the old version: it does not start on a converted database. Start the new version again, and the conversion continues from the last step that it finished.

   #### Variant: AWS installer

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   ```

   Result: the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) pod is `Running` and `Ready`.

   If the pod does not become `Ready`, read its recent output. This output shows only the failures at start. The full log is in `/var/log/platforma` in the Backend container.

   ```sh
   kubectl logs deployment/platforma -n platforma -c platforma --tail=50
   ```

   #### Variant: Google Cloud installer

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   ```

   Result: the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) pod is `Running` and `Ready`.

   If the pod does not become `Ready`, read its recent output. This output shows only the failures at start. The full log is in `/var/log/platforma` in the Backend container.

   ```sh
   kubectl logs deployment/platforma -n platforma -c platforma --tail=50
   ```

   #### Variant: Terraform modules

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   ```

   Result: the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) pod is `Running` and `Ready`.

   If the pod does not become `Ready`, read its recent output. This output shows only the failures at start. The full log is in `/var/log/platforma` in the Backend container.

   ```sh
   kubectl logs deployment/platforma -n platforma -c platforma --tail=50
   ```

   #### Variant: Helm chart

   ```sh
   kubectl get pods -n platforma -l app.kubernetes.io/instance=platforma
   ```

   Result: the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.) pod is `Running` and `Ready`.

   If the pod does not become `Ready`, read its recent output. This output shows only the failures at start. The full log is in `/var/log/platforma` in the Backend container.

   ```sh
   kubectl logs deployment/platforma -n platforma -c platforma --tail=50
   ```

   #### Variant: Single Server

   ```sh
   sudo systemctl status platforma
   ```

   Result: `active (running)`. If the Backend does not start, read its recent output:

   ```sh
   sudo journalctl -u platforma -n 50
   ```

2. **The Backend has the new version.** Run the command from [Before the upgrade](#before-the-upgrade), step 2, again.

3. **Your projects are there.** In the Desktop App, open an existing project and check its results.

4. **Jobs run.** Run a small Block (A small application for one step of an analysis, for example the import of reads or clustering. It can combine several tools.) and check that its job starts and finishes.

## Go back to the old version

Contact MiLaboratories support first. Go back to the old version only when support cannot fix the problem quickly, because the database returns to the state of the backup:

- All changes that users made after the backup are lost. Results made after the backup stay in the storage, but no project refers to them.
- Some results that existed at the time of the backup can be gone, because the new version can delete them. Run their Blocks again, or get the files back with the versioning or soft delete of the bucket.

> **Caution:**
>
> It is not sufficient to deploy the old version. The old Backend does not start on a database that the new version converted: it stops with an error, for example `attempt to run service on top of DB generated by newer Platforma Backend version`. Restore the backup first, then deploy the old version.

1. **Restore the backup.**

   #### Variant: AWS installer

   Follow [Restore](/maintain-backup-and-restore/#restore) up to the step “Start the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.)”. In the restore pod, use the old image that you wrote down before the upgrade.

   #### Variant: Google Cloud installer

   Follow [Restore](/maintain-backup-and-restore/#restore) up to the step “Start the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.)”. In the restore pod, use the old image that you wrote down before the upgrade.

   #### Variant: Terraform modules

   Follow [Restore](/maintain-backup-and-restore/#restore) up to the step “Start the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.)”. In the restore pod, use the old image that you wrote down before the upgrade.

   #### Variant: Helm chart

   Follow [Restore](/maintain-backup-and-restore/#restore) up to the step “Start the Backend (The server program of Platforma. It keeps the projects, runs the Blocks and starts jobs.)”. In the restore pod, use the old image that you wrote down before the upgrade.

   #### Variant: Single Server

   Follow [Restore](/maintain-backup-and-restore/#restore). Before you start the Backend, put the old program back in place: `sudo rm -rf /opt/platforma && sudo mv /opt/platforma-old /opt/platforma`.

2. **Deploy the old version.** This also starts the Backend.

   #### Variant: AWS installer

   Update the stack with the template that you saved before the upgrade, `template-before-upgrade.yaml`. In the console, select **Update**, then **Replace existing template**, then **Upload a template file**. Check that the parameter `PlatformaVersion` is the chart version that you wrote down.

   #### Variant: Google Cloud installer

   In your `platforma-helm` folder, check out the release that you wrote down (`git checkout <tag>`). Then run `bash install.sh` again with the same answers. For a later uninstall, use the `teardown.sh` of this release: see [Uninstall an installer installation](/maintain-uninstalling-cloud-installer/).

   #### Variant: Terraform modules

   Put every root back to the old release that you wrote down, and apply each root in the order of the installation, with the commands of [Upgrade](#upgrade), steps 2 to 4. To apply only the root `services` with the old release is not supported. In place of going back, you can also apply a new release with a fix.

   #### Variant: AWS or Google Cloud

   Get the old release into the clone, for example with `git -C <platforma-install> checkout <old release tag>`.

   #### Variant: Existing cluster

   Get the old release into the clone, for example with `git -C <platforma-install> checkout <old release tag>`, and change the tag in `ref=` back to the old release tag in `cluster/main.tf` and `services/main.tf`.

   #### Variant: Helm chart

   ```sh
   helm upgrade platforma oci://ghcr.io/milaboratory/platforma-install/platforma-backend \
     --version <old-chart-version> -n platforma -f values-before-upgrade.yaml
   ```

   #### Variant: Single Server

   You did this in step 1.

3. **Check the old version** with the steps of [After the upgrade](#after-the-upgrade).

## Upgrade the nodes

A job fails when its node stops. Upgrade the nodes of the Platforma pools when no job runs. In a cluster without an autoscaler, after a change of the nodes of a job pool, apply `platforma-tf-services-cluster` again, so that the quotas match the new nodes.

## Next

- [Verify the installation](/install-verify/)
- [Update an installer installation](/maintain-updating-cloud-installer/)
- [Backup and restore](/maintain-backup-and-restore/)

Source: https://docs.platforma.bio/maintain-upgrading/
