github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/agent/mcorpc/ruby/util.go (about) 1 // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package ruby 6 7 import ( 8 "os" 9 "path/filepath" 10 "strings" 11 12 "github.com/choria-io/go-choria/internal/util" 13 agentddl "github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent" 14 ) 15 16 func (p *Provider) loadAgents(libdirs []string) { 17 p.eachAgent(libdirs, func(a *agentddl.DDL) { 18 p.agents = append(p.agents, a) 19 }) 20 } 21 22 func (p *Provider) eachAgent(libdirs []string, cb func(ddl *agentddl.DDL)) { 23 for _, dir := range libdirs { 24 agentsdir := filepath.Join(dir, "mcollective", "agent") 25 26 p.log.Debugf("Attempting to load Ruby agents from %s", agentsdir) 27 28 err := filepath.Walk(agentsdir, func(path string, info os.FileInfo, err error) error { 29 if err != nil { 30 return err 31 } 32 33 if info.IsDir() { 34 return nil 35 } 36 37 fname := info.Name() 38 extension := filepath.Ext(fname) 39 name := strings.TrimSuffix(fname, extension) 40 41 if extension != ".json" { 42 return nil 43 } 44 45 bpath := strings.TrimSuffix(path, extension) 46 rbfile := bpath + ".rb" 47 48 if !util.FileExist(rbfile) || util.FileIsDir(rbfile) { 49 return nil 50 } 51 52 if !shouldLoadAgent(name) { 53 p.log.Warnf("Ruby agents are not allowed to supply an agent called '%s', skipping", name) 54 return nil 55 } 56 57 p.log.Debugf("Attempting to load %s as an agent DDL", path) 58 ddl, err := agentddl.New(path) 59 if err != nil { 60 p.log.Errorf("Could not load ruby agent DDL %s: %s", path, err) 61 return nil 62 } 63 64 if ddl.Metadata.Provider == "" || ddl.Metadata.Provider == "ruby" { 65 cb(ddl) 66 } 67 68 return nil 69 }) 70 71 if err != nil { 72 p.log.Errorf("Could not find agents in %s: %s", dir, err) 73 } 74 } 75 } 76 77 func shouldLoadAgent(name string) bool { 78 for _, a := range denylist { 79 if a == name { 80 return false 81 } 82 } 83 84 return true 85 }