puppet: Clean up the GAPI and remove log package

This uses the proper facilities which makes things a bit more uniform.
This commit is contained in:
James Shubin
2018-04-19 01:56:31 -04:00
parent 9969286224
commit 86a9181e9b
2 changed files with 31 additions and 33 deletions

View File

@@ -20,7 +20,6 @@ package puppet
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings" "strings"
"sync" "sync"
@@ -246,7 +245,10 @@ func (obj *GAPI) Graph() (*pgraph.Graph, error) {
if !obj.initialized { if !obj.initialized {
return nil, fmt.Errorf("%s: GAPI is not initialized", Name) return nil, fmt.Errorf("%s: GAPI is not initialized", Name)
} }
config := obj.ParseConfigFromPuppet() config, err := obj.ParseConfigFromPuppet()
if err != nil {
return nil, err
}
if config == nil { if config == nil {
return nil, fmt.Errorf("function ParseConfigFromPuppet returned nil") return nil, fmt.Errorf("function ParseConfigFromPuppet returned nil")
} }
@@ -297,7 +299,7 @@ func (obj *GAPI) Next() chan gapi.Next {
return return
} }
log.Printf("%s: Generating new graph...", Name) obj.data.Logf("generating new graph...")
if obj.data.NoStreamWatch { if obj.data.NoStreamWatch {
pChan = nil pChan = nil
} else { } else {

View File

@@ -22,12 +22,13 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"log"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
"github.com/purpleidea/mgmt/yamlgraph" "github.com/purpleidea/mgmt/yamlgraph"
errwrap "github.com/pkg/errors"
) )
const ( const (
@@ -37,25 +38,22 @@ const (
Debug = false // FIXME: integrate with global debug flag Debug = false // FIXME: integrate with global debug flag
) )
func runPuppetCommand(cmd *exec.Cmd) ([]byte, error) { func (obj *GAPI) runPuppetCommand(cmd *exec.Cmd) ([]byte, error) {
if Debug { if obj.data.Debug {
log.Printf("%s: running command: %v", Name, cmd) obj.data.Logf("running command: %v", cmd)
} }
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
log.Printf("%s: Error opening pipe to puppet command: %v", Name, err) return nil, errwrap.Wrapf(err, "error opening pipe to puppet command")
return nil, err
} }
stderr, err := cmd.StderrPipe() stderr, err := cmd.StderrPipe()
if err != nil { if err != nil {
log.Printf("%s: Error opening error pipe to puppet command: %v", Name, err) return nil, errwrap.Wrapf(err, "error opening error pipe to puppet command")
return nil, err
} }
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
log.Printf("%s: Error starting puppet command: %v", Name, err) return nil, errwrap.Wrapf(err, "error starting puppet command")
return nil, err
} }
// XXX: the current implementation is likely prone to fail // XXX: the current implementation is likely prone to fail
@@ -66,22 +64,21 @@ func runPuppetCommand(cmd *exec.Cmd) ([]byte, error) {
var count int var count int
count, err = stdout.Read(data) count, err = stdout.Read(data)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
log.Printf("%s: Error reading YAML data from puppet: %v", Name, err) obj.data.Logf("error reading YAML data from puppet: %v", err)
return nil, err return nil, err
} }
// Slicing down to the number of actual bytes is important, the YAML parser // Slicing down to the number of actual bytes is important, the YAML parser
// will choke on an oversized slice. http://stackoverflow.com/a/33726617/3356612 // will choke on an oversized slice. http://stackoverflow.com/a/33726617/3356612
result = append(result, data[0:count]...) result = append(result, data[0:count]...)
} }
if Debug { if obj.data.Debug {
log.Printf("%s: read %d bytes of data from puppet", Name, len(result)) obj.data.Logf("read %d bytes of data from puppet", len(result))
} }
for scanner := bufio.NewScanner(stderr); scanner.Scan(); { for scanner := bufio.NewScanner(stderr); scanner.Scan(); {
log.Printf("%s: (output) %v", Name, scanner.Text()) obj.data.Logf("(output) %v", scanner.Text())
} }
if err := cmd.Wait(); err != nil { if err := cmd.Wait(); err != nil {
log.Printf("%s: Error: puppet command did not complete: %v", Name, err) return nil, errwrap.Wrapf(err, "error waiting for puppet command to complete")
return nil, err
} }
return result, nil return result, nil
@@ -89,7 +86,7 @@ func runPuppetCommand(cmd *exec.Cmd) ([]byte, error) {
// ParseConfigFromPuppet returns the graph configuration structure from the mode // ParseConfigFromPuppet returns the graph configuration structure from the mode
// and input values, including possibly some file and directory paths. // and input values, including possibly some file and directory paths.
func (obj *GAPI) ParseConfigFromPuppet() *yamlgraph.GraphConfig { func (obj *GAPI) ParseConfigFromPuppet() (*yamlgraph.GraphConfig, error) {
var args []string var args []string
switch obj.Mode { switch obj.Mode {
case "agent": case "agent":
@@ -100,7 +97,7 @@ func (obj *GAPI) ParseConfigFromPuppet() *yamlgraph.GraphConfig {
args = []string{"mgmtgraph", "print", "--code", obj.puppetString} args = []string{"mgmtgraph", "print", "--code", obj.puppetString}
case "dir": case "dir":
// TODO: run the code from the obj.puppetDir directory path // TODO: run the code from the obj.puppetDir directory path
return nil // XXX: not implemented return nil, fmt.Errorf("not implemented") // XXX: not implemented
default: default:
panic(fmt.Sprintf("%s: unhandled case: %s", Name, obj.Mode)) panic(fmt.Sprintf("%s: unhandled case: %s", Name, obj.Mode))
} }
@@ -111,23 +108,22 @@ func (obj *GAPI) ParseConfigFromPuppet() *yamlgraph.GraphConfig {
cmd := exec.Command("puppet", args...) cmd := exec.Command("puppet", args...)
log.Printf("%s: launching translator", Name) obj.data.Logf("launching translator")
var config yamlgraph.GraphConfig var config yamlgraph.GraphConfig
if data, err := runPuppetCommand(cmd); err != nil { if data, err := obj.runPuppetCommand(cmd); err != nil {
return nil return nil, errwrap.Wrapf(err, "could not run puppet command")
} else if err := config.Parse(data); err != nil { } else if err := config.Parse(data); err != nil {
log.Printf("%s: Error: Could not parse YAML output with Parse: %v", Name, err) return nil, errwrap.Wrapf(err, "could not parse YAML output")
return nil
} }
return &config return &config, nil
} }
// RefreshInterval returns the graph refresh interval from the puppet configuration. // RefreshInterval returns the graph refresh interval from the puppet configuration.
func (obj *GAPI) refreshInterval() int { func (obj *GAPI) refreshInterval() int {
if Debug { if obj.data.Debug {
log.Printf("%s: determining graph refresh interval", Name) obj.data.Logf("determining graph refresh interval")
} }
var cmd *exec.Cmd var cmd *exec.Cmd
if obj.puppetConf != "" { if obj.puppetConf != "" {
@@ -136,17 +132,17 @@ func (obj *GAPI) refreshInterval() int {
cmd = exec.Command("puppet", "config", "print", "runinterval") cmd = exec.Command("puppet", "config", "print", "runinterval")
} }
log.Printf("%s: inspecting runinterval configuration", Name) obj.data.Logf("inspecting runinterval configuration")
interval := 1800 interval := 1800
data, err := runPuppetCommand(cmd) data, err := obj.runPuppetCommand(cmd)
if err != nil { if err != nil {
log.Printf("%s: could not determine configured run interval (%v), using default of %v", Name, err, interval) obj.data.Logf("could not determine configured run interval (%v), using default of %v", err, interval)
return interval return interval
} }
result, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 0) result, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 0)
if err != nil { if err != nil {
log.Printf("%s: error reading numeric runinterval value (%v), using default of %v", Name, err, interval) obj.data.Logf("error reading numeric runinterval value (%v), using default of %v", err, interval)
return interval return interval
} }