One CUE repository can describe several hosts: one package per host, shared templates in packages of their own, and a root package that collects all hosts.
Each host package includes the templates it deploys by importing them.
This guide starts from a repository with SOPS set up as in Set up SOPS in a CUE repository, with the module example.com/infra@v0.
package syslet
import (
sysletcore "github.com/xchangeee/syslet/schema/core"
syslettools "github.com/xchangeee/syslet/schema/tools"
)
// Schema and defaults that every host's sysdef has to satisfy
#Sysdef: {
sysletcore.#Sysdef
sysletcore.#SysdefDefaults
}
// All settings of one host, see hosts.cue
#Host: {
// Host that cue cmd plan/apply deploy to
fqdn!: string
// age public key of the host, see sops.cue
agekey!: #AgeKey
// SOPS-encrypted secret files of the host, keyed by file name
secretFiles?: [string]: string
sysdef!: #Sysdef
if secretFiles != _|_ {
sysdef: secrets: (syslettools.#SysdefSecretsFromEmbeddedFiles & {in: secretFiles}).secrets
}
// Rendered spec that cue cmd plan/apply pass to syslet
specRendered: (syslettools.#SysletJsonFromSysdef & {in: sysdef}).specRendered
}
hosts: [string]: #Host
The secret files are no longer embedded here, since @embed reads files relative to the file it's in; each host package embeds its own.
Put what several hosts deploy into a definition in its own package.
It takes its settings in in and returns a sysdef fragment in out, so hosts change the settings without knowing the fragment's structure:
package site
// nginx site shared by all web hosts.
// A host includes it in its sysdef with (site.#Sysdef & {in: {...}}).out
#Sysdef: {
in: {
image: string | *"docker.io/library/nginx:1.27"
port: int | *8080
}
out: containers: site: spec: unit: Container: {
Image: in.image
PublishPort: ["\(in.port):80"]
}
}
Note
If you squint, this looks a lot like a Helm chart: settings go in, resources come out.
With CUE modules, you can even publish such definitions and reuse them across repositories, but that's out of scope here.
A host package only gets checked against #Host once it's listed here.
Since #Host is closed, every regular field of a host package has to be one of its fields; keep helper values in hidden fields such as _ports.
package syslet
import "encoding/yaml"
// age public key of an admin or a host
#AgeKey: =~"^age1"
// age public keys of all admins; every admin can decrypt the secrets of every host
sops: admins: [string]: #AgeKey
sops: admins: alice: "age1..." // your admin key
// Content of .sops.yaml, written by cue cmd sopsgen: one creation rule per
// host, so the secrets in hosts/<name>/ can be decrypted by all admins and
// that host only. Host names in hosts.cue must match their directory names.
sopsYaml: "# generated by cue cmd sopsgen, do not edit\n" + yaml.Marshal({
creation_rules: [
for name, host in hosts {
path_regex: #"^hosts/\#(name)/.*\.enc\.yaml$"#
key_groups: [{age: [for _, key in sops.admins {key}, host.agekey]}]
},
]
})
# generated by cue cmd sopsgen, do not editcreation_rules:-path_regex:^hosts/web01/.*\.enc\.yaml$key_groups:-age:-age1...-age1...-path_regex:^hosts/web02/.*\.enc\.yaml$key_groups:-age:-age1...-age1...
Commit the file, since sops reads it from the repository when you encrypt or edit a secret.
The moved secret files are still encrypted to the recipients of the old rule; re-encrypt them to the new ones: