Write a template
A template is the description of a system. You write it one time. It lists the repositories, the services that run from them, and the resources that the services use. Then lo new makes any number of workspaces from it.
This guide shows how to write a template, and how localspace finds out how each service runs. For each field, see the template reference. For complete templates that you can copy, see the template examples.
Where templates live
Section titled “Where templates live”A template is a directory that holds one file with the name localspace.yaml. The name of the directory is the name of the template.
~/.localspace/templates/ shop/ localspace.yaml seeds/ orders.sql.gzThe templates: machine setting gives the list of template directories. The default is the templates directory next to config.yaml, that is ~/.localspace/templates. localspace refuses a template name that it finds in more than one directory.
templates: - ~/.localspace/templates - ~/work/templatesRun lo template ls to list the templates in all the directories. For each template, it shows the number of services and resources, the number of problems, and the directory. If it finds no template, it prints the directories that it searched.
Write a template
Section titled “Write a template”-
Make the directory and the file:
Terminal window mkdir -p ~/.localspace/templates/shop -
List the repositories in
repos:. Each key is a name that the services use. Each value is the path of a git repository on this machine.localspace.yaml defaults:ref: mainrepos:storefront: ~/code/storefrontorders-api: ~/code/orders-api -
Set
defaults.ref. It is the branch that a service takes when nothing else selects one. If the template has nodefaults.refand you select no branch,lo newfails. A repo that uses a different branch can set itsref:localspace.yaml repos:storefront:path: ~/code/storefrontref: release/next -
Add the services. Each service names its
repo. Setportto the port that the service listens on. Setexpose: trueif the browser must reach the service through a public URL.localspace.yaml services:storefront:repo: storefrontport: 3000expose: trueenv:ORDERS_URL: ${orders.public_url}orders:repo: orders-apiport: 8080expose: truesetup: pnpm prisma migrate deployenv:DATABASE_URL: ${orders-db.url}REDIS_URL: ${cache.url} -
Add the resources. Each resource has a
kind. A service refers to a resource in itsenvwith a reference such as${orders-db.url}.localspace.yaml resources:orders-db:kind: postgresversion: 16seed: seeds/orders.sql.gzcache:kind: redis -
Validate the template:
Terminal window lo template validate shopThe command starts nothing. It prints each problem as
file:line:column: message, in the form that editors use to jump to the line. If the template is correct, it tells you that the template is valid. -
Make a workspace from the template:
Terminal window lo new shop-a --template shoplo up shop-a
Fields of a service
Section titled “Fields of a service”| Field | Use |
|---|---|
repo |
The key in repos: that the code comes from. |
dir |
A directory in the repository, for a monorepo. It must stay in the repository. |
port |
The port that the service listens on. |
expose |
Gives the service a public URL. It is only possible with a port. |
env |
The variables of the service. Values can hold references. |
setup |
A command that runs before each start of the service, after the install that localspace found. |
install |
The install step of a process, in place of the install that localspace found. false removes the install step. |
command |
The command that starts the service. Use it for a service that runs as a process. |
run |
container or process. See How a service runs. |
image |
A prebuilt image, as an alternative to code from a repository. |
ready |
How localspace tests that the service is ready. The default is the port. A path that starts with / is an HTTP request. Other text is a shell command. |
depends_on |
The services and resources that the service must have to start. See Start order. |
localspace sets PORT in the environment of each service that has a port. A process listens on a host port that localspace selects, and gets that number in PORT.
A service with an image has no source. It cannot have repo, dir, setup, install, rebuild_on or on_switch, and it always runs as a container.
For the other fields, such as mounts, shareable, on_switch and rebuild_on, see the template reference.
References
Section titled “References”A value in env can hold a reference in the form ${...}. localspace replaces it for each workspace and for each consumer. A container and a host process get the address that works for each of them.
| Reference | Value |
|---|---|
${orders.internal_url} |
The address that a service in the workspace uses to reach orders. |
${orders.public_url} |
The public URL of the service orders. The service must have expose: true. |
${orders-db.url} |
An attribute of a resource. |
${secret:stripe/key} |
A secret. See Store secrets. |
${env:HOME} |
A variable of the environment of lo. |
${workspace.name} |
The name of the workspace. |
The template checker tests each reference. A reference to a service or resource that is not in the template, or to an attribute that it does not have, is a problem. See the full list in the references reference.
Start order
Section titled “Start order”lo up starts a service after the services that it references. Some references are only addresses, for example a webhook URL. To tell localspace which services a service must have, list them in depends_on:
services: orders: repo: orders-api depends_on: [orders-db, pricing] env: PRICING_URL: ${pricing.internal_url} ADMIN_URL: ${admin.public_url}A service with depends_on starts after the items in the list, and its other references do not change the order. lo up --service orders also starts pricing and orders-db. If pricing fails, orders does not start. See dependencies.
How a service runs
Section titled “How a service runs”A service runs as a container or as a host process. You do not have to select one. localspace looks at the source of the service:
- If you set
run: process, the service runs as a process. - If you set
run: container, the source must have aDockerfile. If it does not,lo newfails. - If you set no
runand the source has aDockerfilein its directory, the service runs as a container. - If you set no
runand there is noDockerfile, the service runs as a process.
The container engine runs containers and the process engine runs processes. See Engines.
localspace selects how a service runs when it makes the workspace. It reads the source that the service runs from. It reads the source again when lo set changes that source.
Options of a container
Section titled “Options of a container”A container service can have the options of a compose file: platform, entrypoint, args, working_dir, publish, volumes and cap_add. Use oneshot: true for a job that runs to completion, such as a migration. The services that list it in depends_on start after it stops with the exit code 0.
services: migrate: image: acme/api:latest oneshot: true args: [npm, run, migrate] api: image: acme/api:latest port: 8080 publish: [9090] depends_on: [migrate] volumes: - { source: ./config, target: /app/config, read_only: true }api publishes the port 9090 on a host port that localspace selects. A process reads that port with ${api.ports.9090}. See container options.
Change the template of a workspace
Section titled “Change the template of a workspace”lo up and lo restart read run, command, install and setup from the template again each time they start a service that does not run. When you change one of these fields, the next start of the service uses the new value. The workspace keeps its worktrees and its data, so lo rm and lo new are not necessary.
A service that runs keeps its command until it restarts. lo up then shows a warning. To apply the change, run lo restart <workspace> --service <service>.
Services without a command
Section titled “Services without a command”A process must have a command. If the template gives no command, localspace looks in the directory of the service and finds how it runs. This is discovery.
Discovery reads these files, in this order. The first match decides.
composer.jsonwithlaravel/framework: Laravel.package.json: Node.go.mod: Go.pyproject.toml,requirements.txtormanage.py: Python.Gemfile: Ruby.composer.json: PHP.
If no file matches and the source has a Dockerfile, discovery reads the base images of the Dockerfile to find the language.
For Node, discovery selects the package manager from packageManager in package.json, or from the lockfile. It finds the framework from the dependencies of the package:
| Framework | Dependency |
|---|---|
| NestJS | @nestjs/core |
| Strapi | @strapi/strapi |
| Next.js | next |
| Nuxt | nuxt |
| SvelteKit | @sveltejs/kit |
| Astro | astro |
| Vite | vite |
| Express | express |
| Fastify | fastify |
For the other languages, discovery finds these frameworks: Django, Flask and FastAPI for Python, Rails for Ruby, and Laravel for PHP. A Go service with a main.go, or with one main package in cmd/, runs with go run.
Discovery finds two commands. The install step installs the dependencies, for example pnpm install --frozen-lockfile, composer install or bundle install. The start command starts the service with the port in $PORT.
The setup of the template runs after the install step, before each start. Use it for a step such as a migration. Do not write the install again.
services: orders: repo: orders-api port: 8080 setup: pnpm prisma migrate deployIf the install step of discovery does not work for a repo, set install. A command replaces the install step. install: false removes it, and setup does all the work, for example an install at the root of a monorepo.
services: web: repo: shop dir: apps/web install: false setup: cd ../.. && pnpm install --frozen-lockfileA value in the template always wins. If you set command, localspace does not use the command that discovery found. If discovery finds no command to start a process, lo new fails and tells you to set command.
Draft a template from your repositories
Section titled “Draft a template from your repositories”lo init writes a first draft of a template. It is experimental. Review the result before you use it.
-
Go to a directory that holds your git repositories, or that is one of them.
-
Run the command:
Terminal window lo init --name shopIt reads compose files,
.envfiles, the workspace files of a monorepo, and the framework of each service. It writeslocalspace.yamlin the first template directory. -
Open the file. Each guess has a
# lo init:comment. Correct the guesses and delete the comments. -
Run
lo template validate shop.
If you give no --name, the template takes the name of the current directory. Use --dry-run to print the draft and write no file. Use --force to replace a template that has the same name.
Related
Section titled “Related”- Template examples gives complete templates to copy.
- Template reference gives each field.
- References gives each
${...}form. - Select branches shows how a workspace selects the source of each service.
- Resources tells how resources work.
- One repository shows a template in a
.localspace.yamlin a repository.