1open module Service
2
3import "site.pkl" as site
4
5const nomadHostPort = "{{ if regexMatch \":\" .Address }}[{{ .Address }}]{{ else }}{{ .Address }}{{ end }}:{{ .Port }}"
6
7/// One HTTP listener and its public route.
8class Http {
9 containerPort: UInt16
10 hostPort: UInt16?
11 loopback: Boolean = false
12 subdomain: String?
13 authRole: String?
14 /// Backend header set from the authenticated OIDC preferred username.
15 userHeader: String?
16 /// Identity headers copied from a valid SSO session; anonymous requests continue.
17 identityHeaders: Mapping<String, String> = new {}
18 /// Set X-Real-Ip from Caddy's observed client address before proxying.
19 forwardRealIp: Boolean = false
20 /// Require HTTPS when browsers send cookies issued by this route.
21 secureCookies: Boolean = false
22 /// Generated secret whose value becomes the private proxy header name.
23 identityProofSecret: String?
24 /// Additional hosts routed to this container without SSO headers.
25 plainHostnames: Listing<String> = new {}
26 hostname: String? = read?("prop:hostname") ?? if (subdomain != null) "\(subdomain).\(site.domain)" else null
27 tlsInternal: Boolean = site.tlsInternal
28 checkPath: String = "/"
29 /// Internal Prometheus endpoint collected by the home server dashboard.
30 metricsPath: String?
31 checkHeaders: Mapping<String, String> = new {}
32 /// API paths authenticated by a sibling task, bypassing the browser login proxy.
33 apiPaths: Mapping<String, String> = new {}
34 /// Request path to a file or directory in this service's folder.
35 overrideFiles: Mapping<String, String> = new {}
36 /// HTML inserted before </head> for the listed page paths.
37 headHtml: Mapping<String, String> = new {}
38}
39
40/// One TCP listener published through Nomad.
41class Tcp {
42 name: String
43 containerPort: UInt16
44 hostPort: UInt16?
45 loopback: Boolean = true
46}
47
48/// A container mount, keyed by its absolute path inside the container.
49class Volume {
50 /// Host source; absent means the same path beneath this service's data root.
51 src: String?
52 /// File or directory relative to this service's assets folder; copied into a read-only mount.
53 config: String?
54 readOnly: Boolean = config != null
55}
56
57/// A persistent value generated when the service is first provisioned.
58class GeneratedSecret {
59 bytes: Int(isBetween(16, 128)) = 32
60}
61
62/// Provider-owned resource request; the alias names its allocated secret.
63open class Requirement {
64 alias: String
65 fixed provider: String
66 fixed kind: String
67}
68
69/// One Podman task. A service can contain one or several of these.
70class Container {
71 /// Use either an image or a build directory in this service's release folder.
72 image: String?
73 build: String?
74 entrypoint: String?
75 http: Http?
76 tcp: Listing<Tcp> = new {}
77 volumes: Mapping<String, Volume> = new {}
78 /// Podman tmpfs mounts for data that must not persist in service storage.
79 tmpfs: Listing<String> = new {}
80 /// `${secret.own.key}` and `${secret.alias.key}` resolve from Nomad variables.
81 env: Mapping<String, String> = new {}
82 /// Nomad template for environment values resolved after allocation.
83 envTemplate: String?
84 /// Nomad templates rendered privately and mounted read-only at absolute container paths.
85 secretFiles: Mapping<String, String> = new {}
86 args: Listing<String> = new {}
87 capAdd: Listing<String> = new {}
88 readOnlyRoot: Boolean = false
89 killTimeout: String?
90 devices: Listing<String> = new {}
91 extraHosts: Listing<String> = new {}
92 hostNetwork: Boolean = false
93 /// Nomad CNI `sandbox` network: public internet and PostgreSQL only, no other host or LAN address.
94 restrictedEgress: Boolean = false
95 imageUser: Boolean = false
96 /// Prestart tasks finish first; prestart sidecars stay up for the main tasks.
97 lifecycle: "main"|"prestart"|"prestartSidecar" = "main"
98 rootGroup: Boolean = false
99 cpu: Int = 200
100 memory: Int = 512
101}
102
103/// Stable internal ID supplied from the service definition name by the caller.
104id: String = read?("prop:serviceId")
105/// Stable numeric owner assigned by the deployment tool.
106uid: Int = read("prop:uid").toInt()
107/// Name shown in the management UI and service listings.
108name: String
109tagline: String = ""
110launcher: Boolean = true
111/// Discovery group; absent uses the public route's auth role.
112access: String?
113/// Allows site-specific services to stay out of deployments where they would cause side effects.
114enabled: Boolean = true
115/// Simple replaces one allocation; overlapped runs a canary alongside it.
116rollout: String = "simple"
117/// Fresh previews create empty storage and new secrets instead of forking production.
118stageIsolation: "clone"|"fresh" = "clone"
119/// Include the managed dataset in deployment backup snapshots.
120snapshots: Boolean = true
121/// Time allowed for a new allocation to pass its service checks.
122healthyDeadline: String?
123/// Delay before persistent health-check failures may restart a running allocation.
124healthRestartGrace: String?
125/// Repeatable service configuration run after a healthy deployment.
126setup: String?
127/// Service-owned data preparation before its container starts.
128prepare: String?
129/// Handler for input requests owned by this service.
130provide: String?
131
132/// Use this for one container; it becomes the "app" task in the output.
133container: Container?
134
135/// Use this instead of `container` when the service has named tasks.
136containers: Mapping<String, Container>?
137
138secrets: Mapping<String, GeneratedSecret> = new {}
139/// Secret names supplied from outside the release, in import-file line order.
140requiredSecrets: Listing<String> = new {}
141requirements: Listing<Requirement> = new {}
142/// Service startup dependency with no resource to allocate.
143dependsOn: Listing<String> = new {}
144/// Resource service.name emitted by this app's trace exporter.
145traceServiceName: String?
146/// Metrics sent by the app over OTLP instead of a Prometheus HTTP endpoint.
147metricsPushed: Boolean = false
148
149local deploymentContainers: Mapping<String, Container> =
150 if (!enabled)
151 new Mapping {}
152 else if (container != null && containers != null)
153 throw("Declare either container or containers, not both")
154 else if (container != null)
155 new Mapping { ["app"] = container!! }
156 else if (containers != null && !containers!!.isEmpty)
157 containers!!
158 else if (!requirements.isEmpty)
159 new Mapping {}
160 else
161 throw("Declare a container or an input")
162
163local vmStartupGrace: String? = if (read?("env:SNOWGLOBE_VM_ACCEL") == "qemu") "60m" else null
164
165output {
166 renderer = new JsonRenderer {}
167 value = new {
168 id = module.id
169 name = module.name
170 tagline = module.tagline
171 launcher = module.launcher
172 access = module.access
173 rollout = module.rollout
174 stageIsolation = module.stageIsolation
175 snapshots = module.snapshots
176 healthyDeadline = vmStartupGrace ?? module.healthyDeadline
177 healthRestartGrace = vmStartupGrace ?? module.healthRestartGrace
178 hostRoot = "\(site.root)/prod/\(module.id)"
179 stagingRoot = "\(site.root)/staging"
180 pool = site.pool
181 cloverRoot = site.cloverRoot
182 cloverGid = site.cloverGid
183 mediaRoot = site.mediaRoot
184 ownerEmail = site.ownerEmail
185 containers = deploymentContainers
186 setup = module.setup
187 prepare = module.prepare
188 provide = module.provide
189 // External mounts do not require this service's dataset.
190 hasManagedVolumes = deploymentContainers.toMap().values.any((task) ->
191 task.volumes.toMap().values.any((volume) -> volume.src == null && volume.config == null))
192 secrets = module.secrets
193 requiredSecrets = module.requiredSecrets
194 requirements = module.requirements
195 dependsOn = module.dependsOn
196 traceServiceName = module.traceServiceName
197 metricsPushed = module.metricsPushed
198 }
199}