# Lease a workstation from a pool

> For the complete documentation index, see [llms.txt](https://docs.ringleader.dev/llms.txt).


> Create a workstation pool, lease one of its workstations for a build, connect to it, and hand it back.


Create a workstation pool, lease one of its workstations for a build, connect to it, and hand
it back. Use it when a CI job, a script or a person needs a workstation and shouldn't wait
the minutes a new one takes to build. You can also have the pool clean a workstation and keep
it for the next lease instead of deleting it, which takes an edit to the pool's declaration.

## Prerequisites

- [Install Ringleader](https://docs.ringleader.dev/install/), with the Ringleader application running.
- A cloud workstation to copy as the pool's template, in the namespace the pool will live in.
  [Cloud workstations (GCP, AWS, Azure)](https://docs.ringleader.dev/workstations/providers/cloud/)
  says how to build one.
- The right to lease from the pool. A pool's owner has it, and
  [Access for a lease holder](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#access-for-a-lease-holder)
  says who else can.
- The examples use a template workstation named `app-box` in the namespace `acme-dev`, a pool
  named `agents-linux` and a lease named `my-build`. Replace them with your own.

## Create a pool

Create a pool whose template is a copy of the workstation's configuration. This pool builds
up to eight members, which are the workstations it owns, and keeps two idle ones running:

```bash
rl workstationpool create agents-linux --from-workstation app-box --size-max 8 --warm-running 2 -n acme-dev
```

The command prints the pool it created. On stderr it prints a note that names what it left
out of the template. Every lease holder can read the template, as
[Pool template and owner](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#pool-template-and-owner)
explains.

## Lease a member

A lease is a request for one member. The pool binds a member to your lease as soon as one is
available, and the member is yours until you release the lease. On a new pool, the first lease
waits while a member builds, which takes minutes. Later leases take an idle member.

1. Request a member and wait for it:

   ```bash
   rl workstationpool lease agents-linux --name my-build --wait -n acme-dev
   ```

   When the lease is `Ready`, the command prints the lease and its member, such as
   `workstationpoollease/my-build Ready on agents-linux-e5f6a7b8`. The lease carries your
   device's SSH key.
2. Open a shell on the member, with the member's name from step 1:

   ```bash
   rl shell <member> -n acme-dev
   ```

3. Optional: If your build runs longer than the lease, renew it before it ends:

   ```bash
   rl workstationpoollease renew my-build --expires-in 4h -n acme-dev
   ```

4. Release the lease when you are done:

   ```bash
   rl workstationpoollease release my-build -n acme-dev
   ```

   The pool deletes the member.

If that's all you need, you are done. The next task has the pool clean a member and keep it,
and the last one deletes the pool.

## Recycle members between leases

By default the pool deletes every released member. With a `recycle` block, a member goes back
to the pool after a cleanup, and the next lease takes it. A recycled member keeps whatever its
last holder left, as
[Member recycling](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#member-recycling)
explains.

1. Open the pool in your editor:

   ```bash
   rl workstationpool edit agents-linux -n acme-dev
   ```

2. Add a `recycle` block under `spec`:

   ```yaml
   spec:
     recycle:
       maxReuses: 20   # after this many recycles the member is deleted instead
       timeout: 10m    # a recycle that takes longer deletes the member
       reboot: false   # true stops and starts the member after the scripts
   ```

3. Optional: Declare the cleanup you need as `onRecycle` scripts in the template, as
   [Setup scripts](https://docs.ringleader.dev/workstations/packages-tools-settings/scripts-files-services/#setup-scripts)
   describes.
4. Save the file and close the editor. Ringleader applies the change to the pool.
5. Lease a member again, because the last task released `my-build`:

   ```bash
   rl workstationpool lease agents-linux --name my-recycle --wait -n acme-dev
   ```

6. Release the lease with `--recycle`:

   ```bash
   rl workstationpoollease release my-recycle --recycle -n acme-dev
   ```

   The pool cleans the member and keeps it for the next lease. When it can't recycle the
   member, it deletes it instead.

## Optional: Delete the pool

A pool that keeps idle members running costs money until you delete it. Delete the pool when
you are done with it:

```bash
rl workstationpool delete agents-linux -n acme-dev
```

The command asks you to confirm, because it deletes each member's virtual machine and disk.
[Delete a pool](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#delete-a-pool) says what
happens to a member that is still leased.

## Troubleshoot

- If the lease stays `Pending`, run `rl workstationpoollease describe my-build -n acme-dev`.
  `status.message` says what the lease waits for, and
  [Pool leases](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#pool-leases) says how
  the pool picks a member.
- If the lease goes `Failed` with the reason `Expired`, nobody renewed it before its
  `expiresAt`, and the pool deleted the member. Lease another member, and renew before the
  lease ends.

## Next steps

- [Pools and leases](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/) says how a pool
  picks a member, keeps idle members, and takes a member out of service.
- [`rl workstationpool`](https://docs.ringleader.dev/reference/cli/commands/workstation-access/#rl-workstationpool) lists the pool
  and lease commands and their flags.
- [Lease spec fields](https://docs.ringleader.dev/reference/resources/workstationpool/#lease-spec-fields) lists the
  fields of a lease, for a program that writes its own leases.
- [SSH keys for outbound Git](https://docs.ringleader.dev/workstations/connectivity/git-ssh-keys/) lets a
  member clone private repositories with your key.
- [Scripts, files and services](https://docs.ringleader.dev/workstations/packages-tools-settings/scripts-files-services/)
  declares the `onRecycle` scripts that clean a member.

