Skip to content

Commit

Permalink
Merge branch 'user-units' of github.com:Nemric/ignition into user-units
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemric committed Feb 10, 2022
2 parents 3a3942d + 987794f commit f28a6b2
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/v3_4_experimental/types/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import (
)

func (u Unit) Key() string {
<<<<<<< HEAD
=======
// Change unit Key to differenciate them by concatenating their scope and name
// to handle the case of same named units for differents scope
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
if u.Scope != nil {
return *u.Scope + "." + u.Name
} else {
Expand Down
15 changes: 15 additions & 0 deletions internal/exec/stages/files/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ func (s *stage) createUnits(config types.Config) error {
// relabelpaths := []string{}
if err := s.Logger.LogOp(
func() error {
<<<<<<< HEAD
// var err error
// relabelpaths, err = s.MaskUnit(unit)
=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
var err error = s.MaskUnit(unit)
return err
},
Expand All @@ -101,10 +104,13 @@ func (s *stage) createUnits(config types.Config) error {
return err
}

<<<<<<< HEAD
// for _, path := range relabelpaths {
// s.relabel(path[len(s.DestDir):])
// }

=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
} else { // mask: false
masked, err := s.IsUnitMasked(unit)
if err != nil {
Expand All @@ -125,10 +131,13 @@ func (s *stage) createUnits(config types.Config) error {
}
// if we have presets then create the systemd preset file.
if len(presets) != 0 {
<<<<<<< HEAD
// useless as it will be done later in createSystemdPresetFiles
// if err := s.relabelPath(filepath.Join(s.DestDir, util.PresetPath)); err != nil {
// return err
// }
=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
if err := s.createSystemdPresetFiles(presets); err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +179,10 @@ func (s *stage) createSystemdPresetFiles(presets map[string]*Preset) error {
paths[path] = true
}
}
<<<<<<< HEAD

=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
hasInstanceUnit := false
for _, preset := range presets {
unitString := preset.unit
Expand Down Expand Up @@ -205,7 +217,10 @@ func (s *stage) createSystemdPresetFiles(presets map[string]*Preset) error {
return err
}
}
<<<<<<< HEAD

=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions internal/exec/stages/files/units_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,19 @@ func TestSystemdPresetPath(t *testing.T) {
},
{
types.Unit{Name: "test.service", Scope: cfgutil.StrToPtr("user")},
<<<<<<< HEAD
"etc/systemd/user-preset/21-ignition-user.preset",
},
{
types.Unit{Name: "test.service", Scope: cfgutil.StrToPtr("global")},
"etc/systemd/user-preset/20-ignition-global.preset",
=======
"etc/systemd/user-preset/21-ignition.preset",
},
{
types.Unit{Name: "test.service", Scope: cfgutil.StrToPtr("global")},
"etc/systemd/user-preset/20-ignition.preset",
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
},
}

Expand Down
8 changes: 8 additions & 0 deletions internal/exec/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ func (u Util) SystemdUnitPaths(unit types.Unit) []string {
func (u Util) SystemdPresetPath(scope UnitScope) string {
switch scope {
case UserUnit:
<<<<<<< HEAD
return filepath.Join("etc", "systemd", "user-preset", "21-ignition-user.preset")
case SystemUnit:
return filepath.Join("etc", "systemd", "system-preset", "20-ignition.preset")
case GlobalUnit:
return filepath.Join("etc", "systemd", "user-preset", "20-ignition-global.preset")
=======
return filepath.Join("etc", "systemd", "user-preset", "21-ignition.preset")
case SystemUnit:
return filepath.Join("etc", "systemd", "system-preset", "20-ignition.preset")
case GlobalUnit:
return filepath.Join("etc", "systemd", "user-preset", "20-ignition.preset")
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
default:
return filepath.Join("etc", "systemd", "system-preset", "20-ignition.preset")
}
Expand Down
7 changes: 7 additions & 0 deletions internal/exec/util/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
)

const (
<<<<<<< HEAD
// PresetPath string = "/etc/systemd/system-preset/20-ignition.preset"
=======
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
DefaultPresetPermissions os.FileMode = 0644
)

Expand Down Expand Up @@ -97,6 +100,7 @@ func (ut Util) FilesFromSystemdUnitDropin(unit types.Unit, dropin types.Dropin)
return fetchops, nil
}

<<<<<<< HEAD
// MaskUnit writes a symlink to /dev/null to mask the specified unit and returns the path of that unit
// without the sysroot prefix
// func (ut Util) MaskUnit(unit types.Unit) ([]string, error) {
Expand All @@ -123,6 +127,9 @@ func (ut Util) FilesFromSystemdUnitDropin(unit types.Unit, dropin types.Dropin)
// return paths, nil
// }

=======
// MaskUnit writes a symlink to /dev/null to mask the specified unit
>>>>>>> 987794fb9832a7e28d5229b82cfd7bd1d6dafed6
func (ut Util) MaskUnit(unit types.Unit) error {
for _, path := range ut.SystemdUnitPaths(unit) {
unitpath, err := ut.JoinPath(path, unit.Name)
Expand Down
90 changes: 90 additions & 0 deletions tests/positive/systemd/create_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

func init() {
register.Register(register.PositiveTest, CreateSystemdService())
register.Register(register.PositiveTest, CreateSystemdUserService())
}

func CreateSystemdService() types.Test {
Expand Down Expand Up @@ -63,3 +64,92 @@ func CreateSystemdService() types.Test {
ConfigMinVersion: configMinVersion,
}
}

func CreateSystemdUserService() types.Test {
name := "systemd.unit.userunit.create"
in := types.GetBaseDisk()
out := types.GetBaseDisk()
config := `{
"ignition": { "version": "$version" },
"systemd": {
"units": [{
"name": "example.service",
"enabled": true,
"scope": "user",
"users": ["tester1", "tester2"],
"contents": "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target"
},
{
"contents": "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target",
"enabled": true,
"name": "example.service",
"scope": "global"
}]
}
}`
configMinVersion := "3.4.0-experimental"

in[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Name: "passwd",
Directory: "etc",
},
Contents: "tester1:x:44:4242::/home/tester1:/bin/false\ntester2:x:45:4242::/home/tester2:/bin/false",
},
{
Node: types.Node{
Name: "nsswitch.conf",
Directory: "etc",
},
Contents: "passwd: files\ngroup: files\nshadow: files\ngshadow: files\n",
},
})

out[0].Partitions.AddFiles("ROOT", []types.File{
{
Node: types.Node{
Name: "example.service",
Directory: "home/tester1/.config/systemd/user",
},
Contents: "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target",
},
{
Node: types.Node{
Name: "example.service",
Directory: "home/tester2/.config/systemd/user",
},
Contents: "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target",
},
{
Node: types.Node{
Name: "21-ignition.preset",
Directory: "etc/systemd/user-preset",
},
Contents: "enable example.service\n",
},

{
Node: types.Node{
Name: "example.service",
Directory: "etc/systemd/user",
},
Contents: "[Service]\nType=oneshot\nExecStart=/usr/bin/echo Hello World\n\n[Install]\nWantedBy=multi-user.target",
},
{
Node: types.Node{
Name: "20-ignition.preset",
Directory: "etc/systemd/user-preset",
},
Contents: "enable example.service\n",
},
})

return types.Test{
Name: name,
In: in,
Out: out,
Config: config,
ConfigMinVersion: configMinVersion,
}
}

0 comments on commit f28a6b2

Please sign in to comment.