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/>.
28 "ruderich.org/simon/safcm"
29 "ruderich.org/simon/safcm/cmd/safcm/config"
30 "ruderich.org/simon/safcm/rpc"
33 func (s *Sync) hostSync(conn *rpc.Conn, detectedGroups []string) error {
34 req, err := s.hostSyncReq(detectedGroups)
38 x, err := s.sendRecv(conn, req)
42 resp, ok := x.(safcm.MsgSyncResp)
44 return fmt.Errorf("unexpected response %v", x)
48 changes := s.formatChanges(resp)
50 s.log(safcm.LogInfo, true, changes)
54 return fmt.Errorf("%s", resp.Error)
59 func (s *Sync) hostSyncReq(detectedGroups []string) (
60 safcm.MsgSyncReq, error) {
62 var empty safcm.MsgSyncReq
64 groups, groupPriority, err := s.resolveHostGroups(detectedGroups)
69 // Don't leak internal group priority which is confusing
70 // without knowing the implementation details.
71 groupsSorted := make([]string, len(groups))
72 copy(groupsSorted, groups)
73 sort.Strings(groupsSorted)
74 s.logVerbosef("host groups: %s",
75 strings.Join(groupsSorted, " "))
77 // Don't leak internal priority values. Instead, order groups
79 var priorities []string
80 for x := range groupPriority {
81 priorities = append(priorities, x)
83 sort.Slice(priorities, func(i, j int) bool {
86 return groupPriority[a] > groupPriority[b]
88 s.logVerbosef("host group priorities (descending): %v",
89 strings.Join(priorities, " "))
92 allFiles := make(map[string]*safcm.File)
93 allPackagesMap := make(map[string]bool) // map to deduplicate
94 allServicesMap := make(map[string]bool) // map to deduplicate
95 var allCommands []*safcm.Command
97 for _, group := range groups {
98 // Skip non-existent group directories
99 _, err := os.Stat(group)
100 if os.IsNotExist(err) {
104 files, err := config.LoadFiles(group)
108 err = config.LoadPermissions(group, files)
112 err = config.LoadTemplates(group, files,
113 s.host.Name, groups, s.allHosts, s.allGroups)
117 err = config.LoadTriggers(group, files)
121 for k, v := range files {
122 err := s.checkFileConflict(group, k, v,
123 allFiles, groupPriority)
131 packages, err := config.LoadPackages(group)
135 for _, x := range packages {
136 allPackagesMap[x] = true
139 services, err := config.LoadServices(group)
143 for _, x := range services {
144 allServicesMap[x] = true
147 commands, err := config.LoadCommands(group)
151 allCommands = append(allCommands, commands...)
154 resolveFileDirConflicts(allFiles)
156 var allPackages []string
157 var allServices []string
158 for x := range allPackagesMap {
159 allPackages = append(allPackages, x)
161 for x := range allServicesMap {
162 allServices = append(allServices, x)
164 // Sort for deterministic results
165 sort.Strings(allPackages)
166 sort.Strings(allServices)
168 return safcm.MsgSyncReq{
169 DryRun: s.config.DryRun,
172 Packages: allPackages,
173 Services: allServices,
174 Commands: allCommands,
178 // resolveHostGroups returns the groups and group priorities of the current
180 func (s *Sync) resolveHostGroups(detectedGroups []string) (
181 []string, map[string]int, error) {
183 groups, err := config.ResolveHostGroups(s.host.Name,
184 s.allGroups, detectedGroups)
189 // Early entries in "group_priority" have higher priorities
190 groupPriority := make(map[string]int)
191 for i, x := range s.config.GroupPriority {
192 groupPriority[x] = len(s.config.GroupPriority) - i
194 // Host itself always has highest priority
195 groupPriority[s.host.Name] = math.MaxInt32
197 // Sort groups after priority and name
198 sort.Slice(groups, func(i, j int) bool {
201 if groupPriority[a] < groupPriority[b] {
203 } else if groupPriority[a] > groupPriority[b] {
210 return groups, groupPriority, nil
213 func (s *Sync) checkFileConflict(group string, path string, file *safcm.File,
214 allFiles map[string]*safcm.File, groupPriority map[string]int) error {
216 old, ok := allFiles[path]
221 newPrio := groupPriority[group]
222 oldPrio := groupPriority[old.OrigGroup]
223 if oldPrio < newPrio {
224 if old.Mode.IsDir() && file.Mode.IsDir() &&
225 old.TriggerCommands != nil {
226 s.logDebugf("files: %q: "+
227 "group %s overwrites triggers from group %s",
228 path, group, old.OrigGroup)
231 } else if oldPrio > newPrio {
232 // Should never happen, groups are sorted by priority
233 panic("invalid group priorities")
236 // Directories with default permissions and no triggers do not count
238 if file.Mode.IsDir() && file.Mode == old.Mode &&
239 config.FileModeToFullPerm(file.Mode) == 0755 &&
240 file.TriggerCommands == nil && old.TriggerCommands == nil {
244 return fmt.Errorf("groups %s and %s both provide %q\n"+
245 "Use 'group_priority' in config.yaml to declare preference",
246 group, old.OrigGroup, path)
249 func resolveFileDirConflicts(files map[string]*safcm.File) {
251 for x := range files {
252 paths = append(paths, x)
254 sort.Slice(paths, func(i, j int) bool {
255 return paths[i] < paths[j]
258 const sep = string(filepath.Separator)
260 // Remove invalid paths which can result from group_priority
261 // overriding paths from another group (e.g. "/foo" as file from one
262 // group and "/foo/bar" from another).
264 for _, x := range paths {
267 !last.Mode.IsDir() &&
268 strings.HasPrefix(file.Path, last.Path+sep) {