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/rpc"
32 func (s *Sync) hostSync(conn *rpc.Conn, detectedGroups []string) error {
33 req, err := s.hostSyncReq(detectedGroups)
37 x, err := s.sendRecv(conn, req)
41 resp, ok := x.(safcm.MsgSyncResp)
43 return fmt.Errorf("unexpected response %v", x)
47 changes := s.formatChanges(resp)
49 s.logf(safcm.LogInfo, true, "%s", changes)
53 return fmt.Errorf("%s", resp.Error)
58 func (s *Sync) hostSyncReq(detectedGroups []string) (
59 safcm.MsgSyncReq, error) {
61 var empty safcm.MsgSyncReq
63 groups, groupPriority, err := s.resolveHostGroups(detectedGroups)
68 // Don't leak internal group order which is confusing without
69 // knowing the implementation details.
70 groupsSorted := make([]string, len(groups))
71 copy(groupsSorted, groups)
72 sort.Strings(groupsSorted)
73 s.logVerbosef("host groups: %s",
74 strings.Join(groupsSorted, " "))
76 // Don't leak internal priority values. Instead, order groups
78 var priorities []string
79 for x := range groupPriority {
80 priorities = append(priorities, x)
82 sort.Slice(priorities, func(i, j int) bool {
85 return groupPriority[a] < groupPriority[b]
87 s.logVerbosef("host group priorities (desc. order): %v",
88 strings.Join(priorities, " "))
91 allFiles := make(map[string]*safcm.File)
92 allPackagesMap := make(map[string]bool) // map to deduplicate
93 allServicesMap := make(map[string]bool) // map to deduplicate
94 var allCommands []string
96 for _, group := range groups {
97 // Skip non-existent group directories
98 _, err := os.Stat(group)
99 if os.IsNotExist(err) {
103 files, err := config.LoadFiles(group)
107 err = config.LoadPermissions(group, files)
111 err = config.LoadTemplates(group, files,
112 s.host.Name, groups, s.allHosts, s.allGroups)
116 err = config.LoadTriggers(group, files)
120 for k, v := range files {
121 err := s.checkFileConflict(group, k, v,
122 allFiles, groupPriority)
130 packages, err := config.LoadPackages(group)
134 for _, x := range packages {
135 allPackagesMap[x] = true
138 services, err := config.LoadServices(group)
142 for _, x := range services {
143 allServicesMap[x] = true
146 commands, err := config.LoadCommands(group)
150 allCommands = append(allCommands, commands...)
153 resolveFileDirConflicts(allFiles)
155 var allPackages []string
156 var allServices []string
157 for x := range allPackagesMap {
158 allPackages = append(allPackages, x)
160 for x := range allServicesMap {
161 allServices = append(allServices, x)
163 // Sort for deterministic results
164 sort.Strings(allPackages)
165 sort.Strings(allServices)
167 return safcm.MsgSyncReq{
168 DryRun: s.config.DryRun,
171 Packages: allPackages,
172 Services: allServices,
173 Commands: allCommands,
177 // resolveHostGroups returns the groups and group priorities of the current
179 func (s *Sync) resolveHostGroups(detectedGroups []string) (
180 []string, map[string]int, error) {
182 groups, err := config.ResolveHostGroups(s.host.Name,
183 s.allGroups, detectedGroups)
188 // Early entries have higher priorities
189 groupPriority := make(map[string]int)
190 for i, x := range s.config.GroupOrder {
191 groupPriority[x] = i + 1
193 // Host itself always has highest priority
194 groupPriority[s.host.Name] = -1
196 // Sort groups after priority and name
197 sort.Slice(groups, func(i, j int) bool {
200 if groupPriority[a] > groupPriority[b] {
202 } else if groupPriority[a] < groupPriority[b] {
209 return groups, groupPriority, nil
212 func (s *Sync) checkFileConflict(group string, path string, file *safcm.File,
213 allFiles map[string]*safcm.File, groupPriority map[string]int) error {
215 old, ok := allFiles[path]
220 newPrio := groupPriority[group]
221 oldPrio := groupPriority[old.OrigGroup]
222 if oldPrio > newPrio {
223 if old.Mode.IsDir() && file.Mode.IsDir() &&
224 old.TriggerCommands != nil {
225 s.logDebugf("files: %q: "+
226 "group %s overwrites triggers from group %s",
227 path, group, old.OrigGroup)
230 } else if oldPrio < newPrio {
231 // Should never happen, groups are sorted by priority
232 panic("invalid group priorities")
235 // Directories with default permissions and no triggers do not count
237 if file.Mode.IsDir() && file.Mode == old.Mode &&
238 config.FileModeToFullPerm(file.Mode) == 0755 &&
239 file.TriggerCommands == nil && old.TriggerCommands == nil {
243 return fmt.Errorf("groups %s and %s both provide file %q\n"+
244 "Use 'group_order' in config.yaml to declare preference",
245 group, old.OrigGroup, path)
248 func resolveFileDirConflicts(files map[string]*safcm.File) {
250 for x := range files {
251 paths = append(paths, x)
253 sort.Slice(paths, func(i, j int) bool {
254 return paths[i] < paths[j]
257 const sep = string(filepath.Separator)
259 // Remove invalid paths which can result from group_order overriding
260 // paths from another group (e.g. "/foo" as file from one group and
261 // "/foo/bar" from another).
263 for _, x := range paths {
266 !last.Mode.IsDir() &&
267 strings.HasPrefix(file.Path, last.Path+sep) {