@@ -17,6 +17,7 @@ package files
1717import  (
1818	"fmt" 
1919	"path/filepath" 
20+ 	"sort" 
2021	"strings" 
2122
2223	"github.com/coreos/ignition/v2/config/shared/errors" 
@@ -33,6 +34,7 @@ type Preset struct {
3334	enabled         bool 
3435	instantiatable  bool 
3536	instances       []string 
37+ 	path            string 
3638}
3739
3840// warnOnOldSystemdVersion checks the version of Systemd 
@@ -73,12 +75,12 @@ func (s *stage) createUnits(config types.Config) error {
7375				if  _ , ok  :=  presets [key ]; ok  {
7476					presets [key ].instances  =  append (presets [key ].instances , instance )
7577				} else  {
76- 					presets [key ] =  & Preset {unitName , * unit .Enabled , true , []string {instance }}
78+ 					presets [key ] =  & Preset {unitName , * unit .Enabled , true , []string {instance },  util . SystemdPresetPath ( unit ) }
7779				}
7880			} else  {
7981				key  :=  fmt .Sprintf ("%s-%s" , unit .Name , identifier )
8082				if  _ , ok  :=  presets [unit .Name ]; ! ok  {
81- 					presets [key ] =  & Preset {unit .Name , * unit .Enabled , false , []string {}}
83+ 					presets [key ] =  & Preset {unit .Name , * unit .Enabled , false , []string {},  util . SystemdPresetPath ( unit ) }
8284				} else  {
8385					return  fmt .Errorf ("%q key is already present in the presets map" , key )
8486				}
@@ -118,10 +120,11 @@ func (s *stage) createUnits(config types.Config) error {
118120	}
119121	// if we have presets then create the systemd preset file. 
120122	if  len (presets ) !=  0  {
121- 		if  err  :=  s .relabelPath (filepath .Join (s .DestDir , util .PresetPath )); err  !=  nil  {
122- 			return  err 
123- 		}
124- 		if  err  :=  s .createSystemdPresetFile (presets ); err  !=  nil  {
123+ 		// useless as it will be done later in createSystemdPresetFiles 
124+ 		// if err := s.relabelPath(filepath.Join(s.DestDir, util.PresetPath)); err != nil { 
125+ 		// 	return err 
126+ 		// } 
127+ 		if  err  :=  s .createSystemdPresetFiles (presets ); err  !=  nil  {
125128			return  err 
126129		}
127130	}
@@ -148,27 +151,27 @@ func parseInstanceUnit(unit types.Unit) (string, string, error) {
148151
149152// createSystemdPresetFile creates the presetfile for enabled/disabled 
150153// systemd units. 
151- func  (s  * stage ) createSystemdPresetFile (presets  map [string ]* Preset ) error  {
154+ func  (s  * stage ) createSystemdPresetFiles (presets  map [string ]* Preset ) error  {
152155	hasInstanceUnit  :=  false 
153- 	for  _ , value  :=  range  presets  {
154- 		unitString  :=  value .unit 
155- 		if  value .instantiatable  {
156+ 	for  _ , preset  :=  range  presets  {
157+ 		unitString  :=  preset .unit 
158+ 		if  preset .instantiatable  {
156159			hasInstanceUnit  =  true 
157160			// Let's say we have two instantiated enabled units listed under 
158161159162			// then the unitString will look like "[email protected]  foo bar"  160- 			unitString  =  fmt .Sprintf ("%s %s" , unitString , strings .Join (value .instances , " " ))
163+ 			unitString  =  fmt .Sprintf ("%s %s" , unitString , strings .Join (preset .instances , " " ))
161164		}
162- 		if  value .enabled  {
165+ 		if  preset .enabled  {
163166			if  err  :=  s .Logger .LogOp (
164- 				func () error  { return  s .EnableUnit (unitString ) },
167+ 				func () error  { return  s .EnableUnit (unitString ,  preset . path ) },
165168				"setting preset to enabled for %q" , unitString ,
166169			); err  !=  nil  {
167170				return  err 
168171			}
169172		} else  {
170173			if  err  :=  s .Logger .LogOp (
171- 				func () error  { return  s .DisableUnit (unitString ) },
174+ 				func () error  { return  s .DisableUnit (unitString ,  preset . path ) },
172175				"setting preset to disabled for %q" , unitString ,
173176			); err  !=  nil  {
174177				return  err 
@@ -183,7 +186,26 @@ func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error {
183186			return  err 
184187		}
185188	}
186- 	s .relabel (util .PresetPath )
189+ 
190+ 	//getting all paths from presets 
191+ 	var  paths  []string 
192+ 	for  _ , preset  :=  range  presets  {
193+ 		paths  =  append (paths , preset .path )
194+ 	}
195+ 	sort .Slice (paths , func (i , j  int ) bool  {
196+ 		return  paths [i ] <  paths [j ]
197+ 	})
198+ 	//running accros differents paths not to apply them s.relabalpath more than once 
199+ 	var  tmppath  string  =  "" 
200+ 	for  _ , path  :=  range  paths  {
201+ 		if  path  !=  tmppath  {
202+ 			if  err  :=  s .relabelPath (filepath .Join (s .DestDir , path )); err  !=  nil  {
203+ 				return  err 
204+ 			}
205+ 		}
206+ 		tmppath  =  path 
207+ 	}
208+ 
187209	return  nil 
188210}
189211
0 commit comments