Skip to content

Commit 12e2c3d

Browse files
committed
Move systemd spec functions to separate file
Also removes a type we weren't using and adds additional helpers to reduce boiler plate. Signed-off-by: Brian Goff <[email protected]>
1 parent e643fc8 commit 12e2c3d

File tree

3 files changed

+80
-64
lines changed

3 files changed

+80
-64
lines changed

frontend/rpm/template.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (w *specWrapper) Install() fmt.Stringer {
441441
return b
442442
}
443443

444-
copyArtifact := func(root, p string, cfg dalec.ArtifactConfig) {
444+
copyArtifact := func(root, p string, cfg *dalec.ArtifactConfig) {
445445
targetDir := filepath.Join(root, cfg.SubPath)
446446
fmt.Fprintln(b, "mkdir -p", targetDir)
447447

@@ -458,13 +458,13 @@ func (w *specWrapper) Install() fmt.Stringer {
458458
binKeys := dalec.SortMapKeys(w.Spec.Artifacts.Binaries)
459459
for _, p := range binKeys {
460460
cfg := w.Spec.Artifacts.Binaries[p]
461-
copyArtifact(`%{buildroot}/%{_bindir}`, p, cfg)
461+
copyArtifact(`%{buildroot}/%{_bindir}`, p, &cfg)
462462
}
463463

464464
manKeys := dalec.SortMapKeys(w.Spec.Artifacts.Manpages)
465465
for _, p := range manKeys {
466466
cfg := w.Spec.Artifacts.Manpages[p]
467-
copyArtifact(`%{buildroot}/%{_mandir}`, p, cfg)
467+
copyArtifact(`%{buildroot}/%{_mandir}`, p, &cfg)
468468
}
469469

470470
createArtifactDir := func(root, p string, cfg dalec.ArtifactDirConfig) {
@@ -495,14 +495,14 @@ func (w *specWrapper) Install() fmt.Stringer {
495495
dataFileKeys := dalec.SortMapKeys(w.Spec.Artifacts.DataDirs)
496496
for _, k := range dataFileKeys {
497497
df := w.Spec.Artifacts.DataDirs[k]
498-
copyArtifact(`%{buildroot}/%{_datadir}`, k, df)
498+
copyArtifact(`%{buildroot}/%{_datadir}`, k, &df)
499499
}
500500
}
501501

502502
configKeys := dalec.SortMapKeys(w.Spec.Artifacts.ConfigFiles)
503503
for _, c := range configKeys {
504504
cfg := w.Spec.Artifacts.ConfigFiles[c]
505-
copyArtifact(`%{buildroot}/%{_sysconfdir}`, c, cfg)
505+
copyArtifact(`%{buildroot}/%{_sysconfdir}`, c, &cfg)
506506
}
507507

508508
if w.Spec.Artifacts.Systemd != nil {
@@ -524,14 +524,14 @@ func (w *specWrapper) Install() fmt.Stringer {
524524
for _, d := range docKeys {
525525
cfg := w.Spec.Artifacts.Docs[d]
526526
root := filepath.Join(`%{buildroot}/%{_docdir}`, w.Name)
527-
copyArtifact(root, d, cfg)
527+
copyArtifact(root, d, &cfg)
528528
}
529529

530530
licenseKeys := dalec.SortMapKeys(w.Spec.Artifacts.Licenses)
531531
for _, l := range licenseKeys {
532532
cfg := w.Spec.Artifacts.Licenses[l]
533533
root := filepath.Join(`%{buildroot}/%{_licensedir}`, w.Name)
534-
copyArtifact(root, l, cfg)
534+
copyArtifact(root, l, &cfg)
535535
}
536536

537537
b.WriteString("\n")

spec.go

-57
Original file line numberDiff line numberDiff line change
@@ -146,46 +146,6 @@ type Artifacts struct {
146146
// TODO: other types of artifacts (libexec, etc)
147147
}
148148

149-
type SystemdConfiguration struct {
150-
// Units is a list of systemd units to include in the package.
151-
Units map[string]SystemdUnitConfig `yaml:"units,omitempty" json:"units,omitempty"`
152-
// Dropins is a list of systemd drop in files that should be included in the package
153-
Dropins map[string]SystemdDropinConfig `yaml:"dropins,omitempty" json:"dropins,omitempty"`
154-
}
155-
156-
type SystemdUnitConfig struct {
157-
// Name is the name systemd unit should be copied under.
158-
// Nested paths are not supported. It is the user's responsibility
159-
// to name the service with the appropriate extension, i.e. .service, .timer, etc.
160-
Name string `yaml:"name,omitempty" json:"name"`
161-
162-
// Enable is used to enable the systemd unit on install
163-
// This determines what will be written to a systemd preset file
164-
Enable bool `yaml:"enable,omitempty" json:"enable"`
165-
}
166-
167-
func (s SystemdUnitConfig) Artifact() ArtifactConfig {
168-
return ArtifactConfig{
169-
SubPath: "",
170-
Name: s.Name,
171-
}
172-
}
173-
174-
type SystemdDropinConfig struct {
175-
// Name is file or dir name to use for the artifact in the package.
176-
// If empty, the file or dir name from the produced artifact will be used.
177-
Name string `yaml:"name,omitempty" json:"name,omitempty"`
178-
// Unit is the name of the systemd unit that the dropin files should be copied under.
179-
Unit string `yaml:"unit" json:"unit"` // the unit named foo.service maps to the directory foo.service.d
180-
}
181-
182-
func (s SystemdDropinConfig) Artifact() ArtifactConfig {
183-
return ArtifactConfig{
184-
SubPath: fmt.Sprintf("%s.d", s.Unit),
185-
Name: s.Name,
186-
}
187-
}
188-
189149
// CreateArtifactDirectories describes various directories that should be created on install.
190150
// CreateArtifactDirectories represents different directory paths that are common to RPM systems.
191151
type CreateArtifactDirectories struct {
@@ -222,23 +182,6 @@ func (a *ArtifactConfig) ResolveName(path string) string {
222182
return filepath.Base(path)
223183
}
224184

225-
// ServiceConfig is the configuration for a service to include in the package.
226-
type ServiceConfig struct {
227-
Name string `yaml:"name" json:"name" jsonschema:"omitempty"`
228-
229-
// Some services don't support restarting, in which case this should be set to true
230-
NoRestart bool `yaml:"noRestart,omitempty" json:"noRestart,omitempty"`
231-
232-
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`
233-
}
234-
235-
func (s ServiceConfig) Artifact() ArtifactConfig {
236-
return ArtifactConfig{
237-
SubPath: "",
238-
Name: s.Name,
239-
}
240-
}
241-
242185
// IsEmpty is used to determine if there are any artifacts to include in the package.
243186
func (a *Artifacts) IsEmpty() bool {
244187
if len(a.Binaries) > 0 {

systemd.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package dalec
2+
3+
import (
4+
"fmt"
5+
"maps"
6+
"strings"
7+
)
8+
9+
type SystemdConfiguration struct {
10+
// Units is a list of systemd units to include in the package.
11+
Units map[string]SystemdUnitConfig `yaml:"units,omitempty" json:"units,omitempty"`
12+
// Dropins is a list of systemd drop in files that should be included in the package
13+
Dropins map[string]SystemdDropinConfig `yaml:"dropins,omitempty" json:"dropins,omitempty"`
14+
}
15+
16+
type SystemdUnitConfig struct {
17+
// Name is the name systemd unit should be copied under.
18+
// Nested paths are not supported. It is the user's responsibility
19+
// to name the service with the appropriate extension, i.e. .service, .timer, etc.
20+
Name string `yaml:"name,omitempty" json:"name"`
21+
22+
// Enable is used to enable the systemd unit on install
23+
// This determines what will be written to a systemd preset file
24+
Enable bool `yaml:"enable,omitempty" json:"enable"`
25+
}
26+
27+
func (s SystemdUnitConfig) Artifact() *ArtifactConfig {
28+
return &ArtifactConfig{
29+
SubPath: "",
30+
Name: s.Name,
31+
}
32+
}
33+
34+
func (s SystemdUnitConfig) ResolveName(name string) string {
35+
return s.Artifact().ResolveName(name)
36+
}
37+
38+
// Splitname resolves a unit name and then gives its unit base name.
39+
// E.g. for `foo.socket` this would be `foo` and `socket`.
40+
func (s SystemdUnitConfig) SplitName(name string) (string, string) {
41+
name = s.ResolveName(name)
42+
base, other, _ := strings.Cut(name, ".")
43+
return base, other
44+
}
45+
46+
type SystemdDropinConfig struct {
47+
// Name is file or dir name to use for the artifact in the package.
48+
// If empty, the file or dir name from the produced artifact will be used.
49+
Name string `yaml:"name,omitempty" json:"name,omitempty"`
50+
// Unit is the name of the systemd unit that the dropin files should be copied under.
51+
Unit string `yaml:"unit" json:"unit"` // the unit named foo.service maps to the directory foo.service.d
52+
}
53+
54+
func (s SystemdDropinConfig) Artifact() *ArtifactConfig {
55+
return &ArtifactConfig{
56+
SubPath: fmt.Sprintf("%s.d", s.Unit),
57+
Name: s.Name,
58+
}
59+
}
60+
61+
func (s *SystemdConfiguration) GetUnits() map[string]SystemdUnitConfig {
62+
if s == nil {
63+
return nil
64+
}
65+
return maps.Clone(s.Units)
66+
}
67+
68+
func (s *SystemdConfiguration) GetDropins() map[string]SystemdDropinConfig {
69+
if s == nil {
70+
return nil
71+
}
72+
return maps.Clone(s.Dropins)
73+
}

0 commit comments

Comments
 (0)