Use services that run elsewhere
Some parts of a system do not run on your machine. A booking service can run on staging. A database can run on a test server. A payment API can be a sandbox of a third party. A template lists such a part as a remote or as an external part. localspace does not start, stop or remove it.
A template lists the remotes of a service or a resource. Each workspace selects if the service runs on your machine, or on one of its remotes. The services that use it get the address and the attributes of the choice, with the same references.
Remotes and external parts
Section titled “Remotes and external parts”A remote is a deployment of a service or a resource that runs off this machine. A URL gives it. The service or the resource is in the template, and it can also run on your machine. It lists its remotes in its remotes: field. Each workspace selects the local copy or one remote.
services: booking: port: 8080 remotes: staging: url: https://booking.staging.acme.comAn external part is a part that the services use and that is external to the workspace. It goes in the top-level external: field. It has no service and no resource in the template, so it has no local copy. A part with one URL has nothing to select. A part with a list of sources lets each workspace select one source.
A source can be a service of a different workspace. See connect workspaces of different templates.
external: stripe: url: https://api.stripe.com| Term | Where it goes in the template | Local copy | Selected for each workspace |
|---|---|---|---|
| Remote | In remotes: of a service or of a resource |
Yes | Yes |
| External part | In the top-level external:, next to services: |
No | Only with from |
References read the two with the same form. ${booking.internal_url} reads the remote when the workspace selects it. ${stripe.url} reads the external part. See template examples for complete templates.
List the remotes in the template
Section titled “List the remotes in the template”A service or a resource lists its remotes in remotes. Each remote has a url, and it can have a ready check and attrs.
services: booking: repo: booking-service port: 8080 attrs: api_key: dev-key remotes: staging: url: https://booking.staging.acme.com ready: /health attrs: api_key: ${secret:booking/staging_key} payment: repo: payment-service env: BOOKING_URL: ${booking.internal_url} BOOKING_API_KEY: ${booking.api_key}
resources: booking-db: kind: postgres remotes: staging: url: ${secret:booking-db/staging_url}
external: stripe: url: https://api.stripe.com attrs: key: ${secret:stripe/test_key}| Field | Meaning |
|---|---|
url |
Where the remote answers. It can have ${secret:...} and ${env:...}. |
ready |
A check that tells if the remote answers. See check a remote. |
attrs |
The attributes that references read from the remote, besides those that its URL gives. |
default |
On a service or a resource: the remote that a workspace uses when it selects nothing. |
external |
At the top level: the parts that the services use and that are external to the workspace, with no service or resource of the template. An external part is a remote, such as the sandbox of a third party. It can also reach a service of a workspace of a different template. |
A URL such as url: ${env:BOOKING_STAGING_URL} keeps the address out of the template. Each developer sets the variable on their machine.
The values that a remote gives
Section titled “The values that a remote gives”The URL of a remote gives the built-in attributes. For a service, internal_url, public_url and url are the URL. host and internal_host are its host. port and public_port are its port. A URL without a port gives the port of its scheme, such as 443 for https.
A remote of a resource gives url, host, port and name from its URL. name is the path of the URL, for example booking for postgres://db.acme.com/booking. A remote gives other attributes of the kind only through attrs.
The attrs of a service are extra attributes that have a value when the service runs on your machine. Each remote of the service must give each of them. A service that reads ${booking.api_key} then works with each choice.
Select where a service runs
Section titled “Select where a service runs”Use the same pairs as for branches. The value is remote:<name> or a URL.
lo new shop-a --template shoppinglo new shop-b --template shopping booking=remote:staginglo new shop-c --template shopping booking=https://booking.qa.acme.com \ --attr booking.api_key='${secret:booking/qa_key}'| Pair | Meaning |
|---|---|
booking=remote:staging |
The remote staging that the template lists. |
booking=https://... |
A URL that you enter. The template does not list it. |
booking-db='${secret:...}' |
A URL in a secret. It stays out of the shell history and the state, and it shows masked. |
booking=local |
The service runs on your machine at the default ref, also when the template has a default remote. |
booking=develop |
The service runs on your machine from the branch develop. |
A value with a scheme, such as https:// or postgres://, contains :. Git refuses : in a branch name, and a path starts with /, ~/ or .. So a value is a remote, a branch or a path, and not two of these.
A resource takes the same values: booking-db=remote:staging, a URL, or booking-db=local. A volume has no remote.
The default remote
Section titled “The default remote”default: staging makes staging the remote of the workspaces that select nothing. To run the service on your machine, use booking=local, a branch for the service, or --own booking. A pair for the repository, such as booking-service=develop, does not change the choice of the services of that repository that use a remote.
Attributes for a workspace
Section titled “Attributes for a workspace”Use --attr <unit>.<attribute>=<value> to give an attribute to the remote of a service or a resource in one workspace. The value replaces the one in the template. It can be ${secret:...}. This is how a URL that you enter gets the attributes that the local service has.
lo new refuses a remote that does not give an attribute that a service reads. The error names the attribute. For the example above, a URL with no --attr booking.api_key=... fails with a message about api_key.
You cannot use --share for a service that runs on a remote. lo new refuses it. A shared service cannot use a service or a resource that runs on a remote.
Check a remote
Section titled “Check a remote”ready has the same two forms as the ready of a service:
- A string that starts with
/is a path. localspace sends an HTTP GET request to the URL with that path. The remote answers when the status code is lower than 400. - Any other string is a shell command on your machine. The remote answers when the exit code is 0.
localspace checks at lo up, and each time a page or lo ls reads the status. The status of a remote is reachable or unreachable. A remote without ready has the status remote, because nothing tells if it answers.
A service that lists a remote in depends_on waits for its ready check. The wait ends when the remote answers, or after 15 seconds. If the remote does not answer, the service does not start. lo up shows it as failed, with the reason.
How localspace treats a remote
Section titled “How localspace treats a remote”| Command | Effect on a remote |
|---|---|
lo up, lo restart |
Nothing starts. lo up --service payment does not start a remote. |
lo down |
Nothing stops. |
lo rm |
It does nothing to the remote. |
lo ls, lo graph |
They show it as booking@remote:staging, or with the URL that you entered, and its status. |
lo env |
It shows the variables with the values of the remote. A secret shows as ********. |
A remote has no worktree, no port, no logs, no usage and no unit. A command that uses the process of the service, such as lo exec or lo env, tells you that the service runs on a remote.
Move a service between your machine and a remote
Section titled “Move a service between your machine and a remote”Use lo set.
lo set shop-b booking=developlo set shop-a booking=https://booking.qa.acme.comlo set shop-c --attr booking.api_key='${secret:booking/other_key}'When a service moves from your machine to a remote, localspace stops it and removes its container. It keeps the worktree and the data of its resources. When it moves back, it uses them again.
The services that read the unit restart with the new values. If the workspace is up, a service that moves back to your machine starts. If the workspace is down, it starts at the next lo up.
A new choice replaces the attributes that you gave for the previous one. lo set refuses a remote that does not give an attribute that a service reads, with the same error as lo new.
Select in the dashboard
Section titled “Select in the dashboard”The dashboard has the same choices. The form for a new workspace has a list for each service and each resource. The list has Local, each remote of the template, and Remote URL. The default remote of the template is selected at the start.
When you select a remote, the form shows the attributes of the unit with the values of the remote. A mark shows an attribute that the remote does not give. A value that you type replaces the value of the remote.
To move a service or a resource of a workspace, use Where it runs on its page. The Switch button of a repository has the same lists for the services of that repository.
A remote has a cloud icon in the tables, on the flow page and on the page of the remote. A row shows the name of the remote, or the host of a URL that you entered. It also shows the URL and the status. A remote has no logs, no terminal and no usage.
Related
Section titled “Related”- Template file gives the fields.
- References tells the value that a reference reads from a remote.
- Connect workspaces of different templates tells how an external part reaches a service of a different workspace.
- Select branches tells the forms of a pair.
- Secrets tells how to store the values that a remote uses.