Model other kinds of system › Describe infrastructure

Model other kinds of system

Infrastructure

Describe machines, networks and services in the model, check them with everything else, and write the files other tools apply. archec applies nothing, keeps no state, and never holds a secret's value.

A first host#

archec new my-server --template infra-host
cd my-server
archec check main.arche
archec gen main.arche --target linux-host -o out

The words come from the infra pack:

project Server { uses infra "^0.1" }
include "infra/pack.arche"

linux-host web-1 "Web server" {
  hostname "web-1"; cpus 2; memory 4GiB; disk 40GiB; image "debian-13"; in-subnet front
  os-user app { home "/srv/app" }
  service-unit api { start "/usr/bin/node /srv/app/server.js"; user "app"; needs-secret api-key }
  site shop { address "shop.example.com"; proxy 8080 }
}
firewall-rule web-in { ports "80, 443"; source "0.0.0.0/0"; opens web-1 }
firewall-rule ssh-in { ports "22"; source "203.0.113.7"; opens web-1 }

0.0.0.0/0 is IPv4 alone. Open IPv6 with a rule of its own, from ::/0.

A shop on a cloud, step by step#

examples/infra is a shop: a network, a machine behind a load balancer, pictures in a bucket, and two environments. Production has one machine more.

1. Check it. An environment is a variant, so name one:

cd examples/infra
archec check main.arche --variant prod --strict
archec status main.arche --variant prod

2. Break it. Open SSH to everyone, with source "0.0.0.0/0" on the rule ssh:

firewall-rule ssh: an administration or database port is open to the internet:
close it, narrow its source, or say why with `accepted`

archec gen now refuses to write. Put 203.0.113.7 back.

3. Write the files, a folder for each environment:

archec gen main.arche --variant prod --target openstack -o out/prod

4. Choose no environment, and nothing is written: the files would hold staging and production at once.

5. Rename the bucket. Name it media, with was "pictures", and name it so where it is used. The new files move the resource. Without was, the plan would destroy the bucket, and archec infra plan refuses that.

6. Plan it, with OpenTofu or Terraform and a cloud of yours: OpenStack.

What is checked#

The pack's checks are evidence for its requirements, so archec status lists each one on each element. They read the model, not a running system.

  • A check that fails is a warning for archec check, and an error for a provider: it writes nothing. A check that cannot be said (an address that does not read) stops it too.
  • An administration port open to the internet is refused however it is written: 20-30, 80,22, 0.0.0.0/1. accepted "why" on the rule allows it, and the reason stays in the model.

A check of your own has the same functions: inside, overlaps, addresses and closure (kinds).

Secrets#

A secret says where a value is kept: env:NAME, file:/path or vault:path#key. The linux-host provider reads the first two. It takes env: values from the environment of the run that sets the host up, keeps them in a file only root reads, and keeps them on later runs.

The Linux host provider#

linux-host writes one folder for each host, for Debian and Ubuntu:

  • setup.sh, safe to run again: the last line says how many things changed;
  • files/: systemd units and timers, an nftables firewall that lets in only what a rule opens, Caddy sites, SSH settings;
  • cloud-init.yaml, when the host needs no secret and no file of yours.
sudo API_KEY=… sh setup.sh --files .
  • Every file is checked before it replaces the one there (nft -c, caddy validate, sshd -t, a config-file's own validate).
  • A firewall with no rule for SSH is refused. --option ssh-elsewhere=true says the host is reached otherwise.
  • Password login is turned off only when the machine has an authorized key.
  • A run that stops halfway finishes what it had to reload the next time.
  • A value that would be another setting, or a command, is refused.

OpenStack, with OpenTofu or Terraform#

openstack writes main.tf.json, which both tools read, and elements.json, which says the element each resource is of. archec runs neither tool.

archec gen main.arche --target openstack -o out
cd out/openstack
tofu init && tofu plan -out plan.bin
tofu show -json plan.bin > plan.json
archec infra plan ../../main.arche --plan plan.json --dir .
tofu apply plan.bin
  • Flavors, image ids, the key pair and the external network are variables: your cloud has its own.
  • A machine's disk and a bucket have prevent_destroy.
  • An element renamed with was "old-name" is moved, not destroyed and made again.
  • Managed databases and backups are not written: OpenStack clouds differ there. Each is a note.

archec infra plan shows each change on its element, and refuses:

  • a plan that destroys what holds data, whether the model has it or not. --allow-data-loss <address> allows one;
  • a plan that is not of the model as it is, or is of only some resources;
  • a plan that stopped on an error, or that says something archec does not know.

Drift#

archec infra drift main.arche --plan plan.json --dir out/openstack

It lists what changed on the cloud, what is missing and what is there without being in the model, and records it. archec status then shows it under the requirements of each element's kind, and out of date once the model changes. It says what the plan saw: run it again for what is there now.

Reading a state#

tofu show -json > state.json
archec infra import state.json -o imported.arche

An OpenStack state as a model to start from. What the state does not hold is marked TODO, what is not read is listed at the end, and no value marked sensitive is read.

Environments#

An environment is a variant. An element says the ones it is in, and a number may be a parameter each one sets:

parameter CPUS { value 1 }
variant staging { set CPUS = 1 }
variant prod { set CPUS = 4 }

machine-node web-2 { variants prod; cpus CPUS; … }

With environments in the model and none chosen, a provider writes nothing: the files would hold them all. Give each its own folder: archec gen main.arche --variant prod --target openstack -o out/prod.

Cost#

monthly 12 on what costs, and an infra-budget that pays-for it. archec looks up no price.

With the model's software#

Written What it says
part node: Node { runs-on web-1 } the machine a processor of a deployment is
database Store { kept-in db } the managed database it is kept in
container web … { reads-secret db-pw } a secret, in the variable its env:NAME names
container web … { exposes http } an exposed-port others reach

For --target k8s, a secret is a secretKeyRef, an exposed port a Service (and an Ingress when it has a host), and a scaling of the services pack an autoscaler. A container's DB_PASSWORD written in the model is the warning container-secret.

Writing a provider#

A provider is a generator that provides infrastructure. Its capabilities are the kinds it writes:

generator my-cloud {
  provides infrastructure
  script "provider.ts"
  capabilities machine-node, infra-network
}

It reads elements, elementKinds, relations and elementLinks of the generator model. An element of the pack that it does not write is a note.

Limits#

  • What the model no longer says is not removed from the machine: a service taken out of the model stays until you remove it.
  • The firewall file replaces every nftables rule of the machine, those of Docker or fail2ban included, when it changes.
  • linux-host was run on Ubuntu 24.04; Debian is the same family, and was not run.
  • openstack is accepted by terraform validate. No cloud was set up with it.
  • The pack's checks see an element's own subnet and rules, not whole paths from the internet. closure is there for a check of your own.
  • A relation can go from a container, not to one.
  • What an element links to is the same in every environment: an element one environment adds cannot join a load balancer or a budget the others have.