-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
788 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ type Preset struct { | |
enabled bool | ||
instantiatable bool | ||
instances []string | ||
scope util.UnitScope | ||
} | ||
|
||
// warnOnOldSystemdVersion checks the version of Systemd | ||
|
@@ -69,35 +70,33 @@ func (s *stage) createUnits(config types.Config) error { | |
if err != nil { | ||
return err | ||
} | ||
key := fmt.Sprintf("%s-%s", unitName, identifier) | ||
key := fmt.Sprintf("%s.%s-%s", util.GetUnitScope(unit), unitName, identifier) | ||
if _, ok := presets[key]; ok { | ||
presets[key].instances = append(presets[key].instances, instance) | ||
} else { | ||
presets[key] = &Preset{unitName, *unit.Enabled, true, []string{instance}} | ||
presets[key] = &Preset{unitName, *unit.Enabled, true, []string{instance}, util.GetUnitScope(unit)} | ||
} | ||
} else { | ||
key := fmt.Sprintf("%s-%s", unit.Name, identifier) | ||
if _, ok := presets[unit.Name]; !ok { | ||
presets[key] = &Preset{unit.Name, *unit.Enabled, false, []string{}} | ||
key := fmt.Sprintf("%s-%s", unit.Key(), identifier) | ||
if _, ok := presets[key]; !ok { | ||
presets[key] = &Preset{unit.Name, *unit.Enabled, false, []string{}, util.GetUnitScope(unit)} | ||
} else { | ||
return fmt.Errorf("%q key is already present in the presets map", key) | ||
} | ||
} | ||
} | ||
if unit.Mask != nil { | ||
if *unit.Mask { // mask: true | ||
relabelpath := "" | ||
if err := s.Logger.LogOp( | ||
func() error { | ||
var err error | ||
relabelpath, err = s.MaskUnit(unit) | ||
var err error = s.MaskUnit(unit) | ||
return err | ||
}, | ||
"masking unit %q", unit.Name, | ||
"masking unit %q for scope %q", unit.Name, string(util.GetUnitScope(unit)), | ||
); err != nil { | ||
return err | ||
} | ||
s.relabel(relabelpath) | ||
|
||
} else { // mask: false | ||
masked, err := s.IsUnitMasked(unit) | ||
if err != nil { | ||
|
@@ -108,7 +107,7 @@ func (s *stage) createUnits(config types.Config) error { | |
func() error { | ||
return s.UnmaskUnit(unit) | ||
}, | ||
"unmasking unit %q", unit.Name, | ||
"unmasking unit %q for scope %q", unit.Name, string(util.GetUnitScope(unit)), | ||
); err != nil { | ||
return err | ||
} | ||
|
@@ -118,7 +117,7 @@ func (s *stage) createUnits(config types.Config) error { | |
} | ||
// if we have presets then create the systemd preset file. | ||
if len(presets) != 0 { | ||
if err := s.createSystemdPresetFile(presets); err != nil { | ||
if err := s.createSystemdPresetFiles(presets); err != nil { | ||
return err | ||
} | ||
} | ||
|
@@ -145,31 +144,31 @@ func parseInstanceUnit(unit types.Unit) (string, string, error) { | |
|
||
// createSystemdPresetFile creates the presetfile for enabled/disabled | ||
// systemd units. | ||
func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error { | ||
if err := s.relabelPath(filepath.Join(s.DestDir, util.PresetPath)); err != nil { | ||
return err | ||
} | ||
func (s *stage) createSystemdPresetFiles(presets map[string]*Preset) error { | ||
hasInstanceUnit := false | ||
for _, value := range presets { | ||
unitString := value.unit | ||
if value.instantiatable { | ||
for _, preset := range presets { | ||
if err := s.relabelPath(filepath.Join(s.DestDir, s.SystemdPresetPath(preset.scope))); err != nil { | ||
return err | ||
} | ||
unitString := preset.unit | ||
if preset.instantiatable { | ||
hasInstanceUnit = true | ||
// Let's say we have two instantiated enabled units listed under | ||
// the systemd units i.e. [email protected], [email protected] | ||
// then the unitString will look like "[email protected] foo bar" | ||
unitString = fmt.Sprintf("%s %s", unitString, strings.Join(value.instances, " ")) | ||
unitString = fmt.Sprintf("%s %s", unitString, strings.Join(preset.instances, " ")) | ||
} | ||
if value.enabled { | ||
if preset.enabled { | ||
if err := s.Logger.LogOp( | ||
func() error { return s.EnableUnit(unitString) }, | ||
"setting preset to enabled for %q", unitString, | ||
func() error { return s.EnableUnit(unitString, preset.scope) }, | ||
"setting %q preset to enabled for %q", preset.scope, unitString, | ||
); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := s.Logger.LogOp( | ||
func() error { return s.DisableUnit(unitString) }, | ||
"setting preset to disabled for %q", unitString, | ||
func() error { return s.DisableUnit(unitString, preset.scope) }, | ||
"setting %q preset to disabled for %q", preset.scope, unitString, | ||
); err != nil { | ||
return err | ||
} | ||
|
@@ -191,55 +190,61 @@ func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error { | |
// applies to the unit's dropins. | ||
func (s *stage) writeSystemdUnit(unit types.Unit) error { | ||
return s.Logger.LogOp(func() error { | ||
relabeledDropinDir := false | ||
for _, dropin := range unit.Dropins { | ||
if dropin.Contents == nil { | ||
continue | ||
} | ||
f, err := s.FileFromSystemdUnitDropin(unit, dropin) | ||
fetchops, err := s.FilesFromSystemdUnitDropin(unit, dropin) | ||
if err != nil { | ||
s.Logger.Crit("error converting systemd dropin: %v", err) | ||
return err | ||
} | ||
// trim off prefix since this needs to be relative to the sysroot | ||
if !strings.HasPrefix(f.Node.Path, s.DestDir) { | ||
panic(fmt.Sprintf("Dropin path %s isn't under prefix %s", f.Node.Path, s.DestDir)) | ||
} | ||
relabelPath := f.Node.Path[len(s.DestDir):] | ||
if err := s.Logger.LogOp( | ||
func() error { return s.PerformFetch(f) }, | ||
"writing systemd drop-in %q at %q", dropin.Name, f.Node.Path, | ||
); err != nil { | ||
return err | ||
} | ||
if !relabeledDropinDir { | ||
s.relabel(filepath.Dir(relabelPath)) | ||
relabeledDropinDir = true | ||
for _, f := range fetchops { | ||
relabeledDropinDir := false | ||
// trim off prefix since this needs to be relative to the sysroot | ||
if !strings.HasPrefix(f.Node.Path, s.DestDir) { | ||
panic(fmt.Sprintf("Dropin path %s isn't under prefix %s", f.Node.Path, s.DestDir)) | ||
} | ||
relabelPath := f.Node.Path[len(s.DestDir):] | ||
if err := s.Logger.LogOp( | ||
func() error { return s.PerformFetch(f) }, | ||
"writing systemd drop-in %q at %q", dropin.Name, f.Node.Path, | ||
); err != nil { | ||
return err | ||
} | ||
if !relabeledDropinDir { | ||
s.relabel(filepath.Dir(relabelPath)) | ||
relabeledDropinDir = true | ||
} | ||
} | ||
} | ||
|
||
if cutil.NilOrEmpty(unit.Contents) { | ||
return nil | ||
} | ||
|
||
f, err := s.FileFromSystemdUnit(unit) | ||
fetchops, err := s.FilesFromSystemdUnit(unit) | ||
if err != nil { | ||
s.Logger.Crit("error converting unit: %v", err) | ||
return err | ||
} | ||
// trim off prefix since this needs to be relative to the sysroot | ||
if !strings.HasPrefix(f.Node.Path, s.DestDir) { | ||
panic(fmt.Sprintf("Unit path %s isn't under prefix %s", f.Node.Path, s.DestDir)) | ||
} | ||
relabelPath := f.Node.Path[len(s.DestDir):] | ||
if err := s.Logger.LogOp( | ||
func() error { return s.PerformFetch(f) }, | ||
"writing unit %q at %q", unit.Name, f.Node.Path, | ||
); err != nil { | ||
return err | ||
|
||
for _, f := range fetchops { | ||
// trim off prefix since this needs to be relative to the sysroot | ||
if !strings.HasPrefix(f.Node.Path, s.DestDir) { | ||
panic(fmt.Sprintf("Unit path %s isn't under prefix %s", f.Node.Path, s.DestDir)) | ||
} | ||
relabelPath := f.Node.Path[len(s.DestDir):] | ||
if err := s.Logger.LogOp( | ||
func() error { return s.PerformFetch(f) }, | ||
"writing unit %q at %q", unit.Name, f.Node.Path, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
s.relabel(relabelPath) | ||
} | ||
s.relabel(relabelPath) | ||
|
||
return nil | ||
}, "processing unit %q", unit.Name) | ||
}, "processing unit %q for scope %q", unit.Name, string(util.GetUnitScope(unit))) | ||
} |
Oops, something went wrong.