@@ -33,6 +33,7 @@ type Preset struct {
33
33
enabled bool
34
34
instantiatable bool
35
35
instances []string
36
+ scope util.UnitScope
36
37
}
37
38
38
39
// warnOnOldSystemdVersion checks the version of Systemd
@@ -69,35 +70,33 @@ func (s *stage) createUnits(config types.Config) error {
69
70
if err != nil {
70
71
return err
71
72
}
72
- key := fmt .Sprintf ("%s-%s" , unitName , identifier )
73
+ key := fmt .Sprintf ("%s.%s -%s" , util . GetUnitScope ( unit ) , unitName , identifier )
73
74
if _ , ok := presets [key ]; ok {
74
75
presets [key ].instances = append (presets [key ].instances , instance )
75
76
} else {
76
- presets [key ] = & Preset {unitName , * unit .Enabled , true , []string {instance }}
77
+ presets [key ] = & Preset {unitName , * unit .Enabled , true , []string {instance }, util . GetUnitScope ( unit ) }
77
78
}
78
79
} else {
79
- key := fmt .Sprintf ("%s-%s" , unit .Name , identifier )
80
- if _ , ok := presets [unit . Name ]; ! ok {
81
- presets [key ] = & Preset {unit .Name , * unit .Enabled , false , []string {}}
80
+ key := fmt .Sprintf ("%s-%s" , unit .Key () , identifier )
81
+ if _ , ok := presets [key ]; ! ok {
82
+ presets [key ] = & Preset {unit .Name , * unit .Enabled , false , []string {}, util . GetUnitScope ( unit ) }
82
83
} else {
83
84
return fmt .Errorf ("%q key is already present in the presets map" , key )
84
85
}
85
86
}
86
87
}
87
88
if unit .Mask != nil {
88
89
if * unit .Mask { // mask: true
89
- relabelpath := ""
90
90
if err := s .Logger .LogOp (
91
91
func () error {
92
- var err error
93
- relabelpath , err = s .MaskUnit (unit )
92
+ var err error = s .MaskUnit (unit )
94
93
return err
95
94
},
96
- "masking unit %q" , unit .Name ,
95
+ "masking unit %q for scope %q " , unit .Name , string ( util . GetUnitScope ( unit )) ,
97
96
); err != nil {
98
97
return err
99
98
}
100
- s . relabel ( relabelpath )
99
+
101
100
} else { // mask: false
102
101
masked , err := s .IsUnitMasked (unit )
103
102
if err != nil {
@@ -108,7 +107,7 @@ func (s *stage) createUnits(config types.Config) error {
108
107
func () error {
109
108
return s .UnmaskUnit (unit )
110
109
},
111
- "unmasking unit %q" , unit .Name ,
110
+ "unmasking unit %q for scope %q " , unit .Name , string ( util . GetUnitScope ( unit )) ,
112
111
); err != nil {
113
112
return err
114
113
}
@@ -118,7 +117,7 @@ func (s *stage) createUnits(config types.Config) error {
118
117
}
119
118
// if we have presets then create the systemd preset file.
120
119
if len (presets ) != 0 {
121
- if err := s .createSystemdPresetFile (presets ); err != nil {
120
+ if err := s .createSystemdPresetFiles (presets ); err != nil {
122
121
return err
123
122
}
124
123
}
@@ -145,31 +144,31 @@ func parseInstanceUnit(unit types.Unit) (string, string, error) {
145
144
146
145
// createSystemdPresetFile creates the presetfile for enabled/disabled
147
146
// systemd units.
148
- func (s * stage ) createSystemdPresetFile (presets map [string ]* Preset ) error {
149
- if err := s .relabelPath (filepath .Join (s .DestDir , util .PresetPath )); err != nil {
150
- return err
151
- }
147
+ func (s * stage ) createSystemdPresetFiles (presets map [string ]* Preset ) error {
152
148
hasInstanceUnit := false
153
- for _ , value := range presets {
154
- unitString := value .unit
155
- if value .instantiatable {
149
+ for _ , preset := range presets {
150
+ if err := s .relabelPath (filepath .Join (s .DestDir , s .SystemdPresetPath (preset .scope ))); err != nil {
151
+ return err
152
+ }
153
+ unitString := preset .unit
154
+ if preset .instantiatable {
156
155
hasInstanceUnit = true
157
156
// Let's say we have two instantiated enabled units listed under
158
157
159
158
// then the unitString will look like "[email protected] foo bar"
160
- unitString = fmt .Sprintf ("%s %s" , unitString , strings .Join (value .instances , " " ))
159
+ unitString = fmt .Sprintf ("%s %s" , unitString , strings .Join (preset .instances , " " ))
161
160
}
162
- if value .enabled {
161
+ if preset .enabled {
163
162
if err := s .Logger .LogOp (
164
- func () error { return s .EnableUnit (unitString ) },
165
- "setting preset to enabled for %q" , unitString ,
163
+ func () error { return s .EnableUnit (unitString , preset . scope ) },
164
+ "setting %q preset to enabled for %q" , preset . scope , unitString ,
166
165
); err != nil {
167
166
return err
168
167
}
169
168
} else {
170
169
if err := s .Logger .LogOp (
171
- func () error { return s .DisableUnit (unitString ) },
172
- "setting preset to disabled for %q" , unitString ,
170
+ func () error { return s .DisableUnit (unitString , preset . scope ) },
171
+ "setting %q preset to disabled for %q" , preset . scope , unitString ,
173
172
); err != nil {
174
173
return err
175
174
}
@@ -191,55 +190,61 @@ func (s *stage) createSystemdPresetFile(presets map[string]*Preset) error {
191
190
// applies to the unit's dropins.
192
191
func (s * stage ) writeSystemdUnit (unit types.Unit ) error {
193
192
return s .Logger .LogOp (func () error {
194
- relabeledDropinDir := false
195
193
for _ , dropin := range unit .Dropins {
196
194
if dropin .Contents == nil {
197
195
continue
198
196
}
199
- f , err := s .FileFromSystemdUnitDropin (unit , dropin )
197
+ fetchops , err := s .FilesFromSystemdUnitDropin (unit , dropin )
200
198
if err != nil {
201
199
s .Logger .Crit ("error converting systemd dropin: %v" , err )
202
200
return err
203
201
}
204
- // trim off prefix since this needs to be relative to the sysroot
205
- if ! strings .HasPrefix (f .Node .Path , s .DestDir ) {
206
- panic (fmt .Sprintf ("Dropin path %s isn't under prefix %s" , f .Node .Path , s .DestDir ))
207
- }
208
- relabelPath := f .Node .Path [len (s .DestDir ):]
209
- if err := s .Logger .LogOp (
210
- func () error { return s .PerformFetch (f ) },
211
- "writing systemd drop-in %q at %q" , dropin .Name , f .Node .Path ,
212
- ); err != nil {
213
- return err
214
- }
215
- if ! relabeledDropinDir {
216
- s .relabel (filepath .Dir (relabelPath ))
217
- relabeledDropinDir = true
202
+ for _ , f := range fetchops {
203
+ relabeledDropinDir := false
204
+ // trim off prefix since this needs to be relative to the sysroot
205
+ if ! strings .HasPrefix (f .Node .Path , s .DestDir ) {
206
+ panic (fmt .Sprintf ("Dropin path %s isn't under prefix %s" , f .Node .Path , s .DestDir ))
207
+ }
208
+ relabelPath := f .Node .Path [len (s .DestDir ):]
209
+ if err := s .Logger .LogOp (
210
+ func () error { return s .PerformFetch (f ) },
211
+ "writing systemd drop-in %q at %q" , dropin .Name , f .Node .Path ,
212
+ ); err != nil {
213
+ return err
214
+ }
215
+ if ! relabeledDropinDir {
216
+ s .relabel (filepath .Dir (relabelPath ))
217
+ relabeledDropinDir = true
218
+ }
218
219
}
219
220
}
220
221
221
222
if cutil .NilOrEmpty (unit .Contents ) {
222
223
return nil
223
224
}
224
225
225
- f , err := s .FileFromSystemdUnit (unit )
226
+ fetchops , err := s .FilesFromSystemdUnit (unit )
226
227
if err != nil {
227
228
s .Logger .Crit ("error converting unit: %v" , err )
228
229
return err
229
230
}
230
- // trim off prefix since this needs to be relative to the sysroot
231
- if ! strings .HasPrefix (f .Node .Path , s .DestDir ) {
232
- panic (fmt .Sprintf ("Unit path %s isn't under prefix %s" , f .Node .Path , s .DestDir ))
233
- }
234
- relabelPath := f .Node .Path [len (s .DestDir ):]
235
- if err := s .Logger .LogOp (
236
- func () error { return s .PerformFetch (f ) },
237
- "writing unit %q at %q" , unit .Name , f .Node .Path ,
238
- ); err != nil {
239
- return err
231
+
232
+ for _ , f := range fetchops {
233
+ // trim off prefix since this needs to be relative to the sysroot
234
+ if ! strings .HasPrefix (f .Node .Path , s .DestDir ) {
235
+ panic (fmt .Sprintf ("Unit path %s isn't under prefix %s" , f .Node .Path , s .DestDir ))
236
+ }
237
+ relabelPath := f .Node .Path [len (s .DestDir ):]
238
+ if err := s .Logger .LogOp (
239
+ func () error { return s .PerformFetch (f ) },
240
+ "writing unit %q at %q" , unit .Name , f .Node .Path ,
241
+ ); err != nil {
242
+ return err
243
+ }
244
+
245
+ s .relabel (relabelPath )
240
246
}
241
- s .relabel (relabelPath )
242
247
243
248
return nil
244
- }, "processing unit %q" , unit .Name )
249
+ }, "processing unit %q for scope %q " , unit .Name , string ( util . GetUnitScope ( unit )) )
245
250
}
0 commit comments