1package tools
2
3import (
4 "encoding/json"
5 "list"
6 "strings"
7
8 "github.com/xchangeee/syslet/schema/core@v0"
9)
10
11// This definition provides a shortcut to lock the given network/container/volumes.
12//
13// Locking means that we disallow removal, and for volumes, we also set reclaim policy
14// to retain so data wont be accidentally deleted.
15#SysdefLock: {
16 X1="in"!: {
17 networks: [...string]
18 containers: [...string]
19 volumes: [...string]
20 }
21 out: {
22 networks: {
23 for name in X1.networks {
24 "\(name)": spec: {
25 removalAllowed: false
26 }
27 }
28 }
29 containers: {
30 for name in X1.containers {
31 "\(name)": spec: {
32 removalAllowed: false
33 }
34 }
35 }
36 volumes: {
37 for name in X1.volumes {
38 "\(name)": spec: {
39 removalAllowed: false
40 reclaimPolicy: "Retain"
41 }
42 }
43 }
44 }
45}
46
47// This definition provides a shortcut to assign containers to networks. The network
48// name is the first level, the container name the second level.
49//
50// There is nothing special about this, you can also assign containers to networks
51// directly in the container definition, but when there are many containers and complex
52// definitions its harder to get an overview of what container is in what network.
53#SysdefAssignNetwork: {
54 X1="in"!: [string]: [...string]
55 out: {
56 containers: {
57 for network, containerList in X1
58 for name in containerList {
59 "\(name)": spec: unit: Container: Network: [
60 for net, members in X1
61 if list.Contains(members, name) {"\(net).network"},
62 ]
63 }
64 }
65 }
66}
67
68// Accepts a struct from @embed(glob=creds-*.enc.yaml,type=text,allowEmptyGlob)
69// and returns a struct with matching secret specs. The name is taken from the file
70// name, stripping the creds- prefix and the enc.yaml suffix.
71//
72// The files must be in the same directory as the CUE file: for a glob into a
73// subdirectory, @embed keeps the directory in the key, so the prefix isn't
74// stripped and the spec name contains the path.
75#SysdefSecretsFromEmbeddedFiles: {
76 X1="in"!: [string]: string
77 secrets: {
78 for path, content in X1
79 let name = strings.TrimSuffix(strings.TrimPrefix(path, "creds-"), ".enc.yaml") {
80 (name): core.#SysdefSecret & {
81 spec: ciphertext: content
82 }
83 }
84 }
85}
86
87// Renders the provided sysdef struct in syslet json format, to be consumed by
88// syslet via file or stdin.
89#SysletJsonFromSysdef: {
90 X1="in": core.#Sysdef
91 _specList: [
92 for containerName, container in X1.containers if container.enabled {
93 core.#ContainerSpec & container.spec & {name: containerName}
94 },
95 for networkName, network in X1.networks if network.enabled {
96 core.#NetworkSpec & network.spec & {name: networkName}
97 },
98 for volumeName, volume in X1.volumes if volume.enabled {
99 core.#VolumeSpec & volume.spec & {name: volumeName}
100 },
101 for buildName, build in X1.builds if build.enabled {
102 core.#BuildSpec & build.spec & {name: buildName}
103 },
104 for secretName, secret in X1.secrets if secret.enabled {
105 core.#SecretSpec & secret.spec & {name: secretName}
106 },
107 ]
108 specRendered: json.Marshal(_specList)
109}