1 // "sync" sub-command: sync files
3 // Copyright (C) 2021 Simon Ruderich
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "ruderich.org/simon/safcm"
28 "ruderich.org/simon/safcm/cmd/safcm/config"
29 "ruderich.org/simon/safcm/frontend"
30 "ruderich.org/simon/safcm/rpc"
33 func (s *Sync) hostSync(conn *rpc.Conn, detectedGroups []string) error {
34 req, err := s.hostSyncReq(detectedGroups)
38 resp, err := s.loop.HostSyncMsg(s, conn, req)
44 c := frontend.Changes{
45 DryRun: s.config.DryRun,
46 Quiet: s.config.Quiet,
49 changes := c.FormatChanges(resp)
51 s.log(safcm.LogInfo, true, changes)
55 return fmt.Errorf("%s", resp.Error)
60 func (s *Sync) hostSyncReq(detectedGroups []string) (
61 safcm.MsgSyncReq, error) {
63 var empty safcm.MsgSyncReq
65 groups, groupPriority, err := s.resolveHostGroups(detectedGroups)
70 // Don't leak internal group priority which is confusing
71 // without knowing the implementation details.
72 groupsSorted := make([]string, len(groups))
73 copy(groupsSorted, groups)
74 sort.Strings(groupsSorted)
75 s.logVerbosef("host groups: %s",
76 strings.Join(groupsSorted, " "))
78 // Don't leak internal priority values. Instead, order groups
80 var priorities []string
81 for x := range groupPriority {
82 priorities = append(priorities, x)
84 sort.Slice(priorities, func(i, j int) bool {
87 return groupPriority[a] > groupPriority[b]
89 s.logVerbosef("host group priorities (descending): %v",
90 strings.Join(priorities, " "))
93 allFiles := make(map[string]*safcm.File)
94 allPackagesMap := make(map[string]bool) // map to deduplicate
95 allServicesMap := make(map[string]bool) // map to deduplicate
96 var allCommands []*safcm.Command
98 for _, group := range groups {
99 // Skip non-existent group directories
100 _, err := os.Stat(group)
101 if os.IsNotExist(err) {
105 files, err := config.LoadFiles(group)
109 err = config.LoadPermissions(group, files)
113 err = config.LoadTemplates(group, files,
114 s.host.Name, groups, s.allHosts, s.allGroups)
118 err = config.LoadTriggers(group, files)
122 for k, v := range files {
123 err := s.checkFileConflict(group, k, v,
124 allFiles, groupPriority)
132 packages, err := config.LoadPackages(group)
136 for _, x := range packages {
137 allPackagesMap[x] = true
140 services, err := config.LoadServices(group)
144 for _, x := range services {
145 allServicesMap[x] = true
148 commands, err := config.LoadCommands(group)
152 allCommands = append(allCommands, commands...)
155 resolveFileDirConflicts(allFiles)
157 var allPackages []string
158 var allServices []string
159 for x := range allPackagesMap {
160 allPackages = append(allPackages, x)
162 for x := range allServicesMap {
163 allServices = append(allServices, x)
165 // Sort for deterministic results
166 sort.Strings(allPackages)
167 sort.Strings(allServices)
169 return safcm.MsgSyncReq{
170 DryRun: s.config.DryRun,
173 Packages: allPackages,
174 Services: allServices,
175 Commands: allCommands,
179 // resolveHostGroups returns the groups and group priorities of the current
181 func (s *Sync) resolveHostGroups(detectedGroups []string) (
182 []string, map[string]int, error) {
184 groups, err := config.ResolveHostGroups(s.host.Name,
185 s.allGroups, detectedGroups)
190 // Early entries in "group_priority" have higher priorities
191 groupPriority := make(map[string]int)
192 for i, x := range s.config.GroupPriority {
193 groupPriority[x] = len(s.config.GroupPriority) - i
195 // Host itself always has highest priority
196 groupPriority[s.host.Name] = math.MaxInt32
198 // Sort groups after priority and name
199 sort.Slice(groups, func(i, j int) bool {
202 if groupPriority[a] < groupPriority[b] {
204 } else if groupPriority[a] > groupPriority[b] {
211 return groups, groupPriority, nil
214 func (s *Sync) checkFileConflict(group string, path string, file *safcm.File,
215 allFiles map[string]*safcm.File, groupPriority map[string]int) error {
217 old, ok := allFiles[path]
222 newPrio := groupPriority[group]
223 oldPrio := groupPriority[old.OrigGroup]
224 if oldPrio < newPrio {
225 if old.Mode.IsDir() && file.Mode.IsDir() &&
226 old.TriggerCommands != nil {
227 s.logDebugf("files: %q: "+
228 "group %s overwrites triggers from group %s",
229 path, group, old.OrigGroup)
232 } else if oldPrio > newPrio {
233 // Should never happen, groups are sorted by priority
234 panic("invalid group priorities")
237 // Directories with default permissions and no triggers do not count
239 if file.Mode.IsDir() && file.Mode == old.Mode &&
240 config.FileModeToFullPerm(file.Mode) == 0755 &&
241 file.TriggerCommands == nil && old.TriggerCommands == nil {
245 return fmt.Errorf("groups %s and %s both provide %q\n"+
246 "Use 'group_priority' in config.yaml to declare preference",
247 group, old.OrigGroup, path)
250 func resolveFileDirConflicts(files map[string]*safcm.File) {
252 for x := range files {
253 paths = append(paths, x)
255 sort.Slice(paths, func(i, j int) bool {
256 return paths[i] < paths[j]
259 // Slash separated paths are used for the configuration
262 // Remove invalid paths which can result from group_priority
263 // overriding paths from another group (e.g. "/foo" as file from one
264 // group and "/foo/bar" from another).
266 for _, x := range paths {
269 !last.Mode.IsDir() &&
270 strings.HasPrefix(file.Path, last.Path+sep) {