engine: graph, resources: Clean up log messages

The idea is to have a better user experience in the terminal.
This commit is contained in:
James Shubin
2024-08-06 15:12:10 -04:00
parent e94f39bf2c
commit 80178422db
13 changed files with 49 additions and 48 deletions

View File

@@ -192,10 +192,14 @@ func (obj *Engine) Process(ctx context.Context, vertex pgraph.Vertex) error {
} else { } else {
// run the CheckApply! // run the CheckApply!
obj.Logf("%s: CheckApply(%t)", res, !noop) if obj.Debug {
obj.Logf("%s: CheckApply(%t)", res, !noop)
}
// if this fails, don't UpdateTimestamp() // if this fails, don't UpdateTimestamp()
checkOK, err = res.CheckApply(ctx, !noop) checkOK, err = res.CheckApply(ctx, !noop)
obj.Logf("%s: CheckApply(%t): Return(%t, %s)", res, !noop, checkOK, engineUtil.CleanError(err)) if !checkOK && obj.Debug { // don't log on (checkOK == true)
obj.Logf("%s: CheckApply(%t): Return(%t, %s)", res, !noop, checkOK, engineUtil.CleanError(err))
}
} }
if checkOK && err != nil { // should never return this way if checkOK && err != nil { // should never return this way

View File

@@ -268,7 +268,7 @@ func (obj *Engine) Commit() error {
obj.wlock.Unlock() obj.wlock.Unlock()
}() }()
if obj.Debug || true { if obj.Debug {
obj.Logf("%s: Working...", v) obj.Logf("%s: Working...", v)
} }
// contains the Watch and CheckApply loops // contains the Watch and CheckApply loops

View File

@@ -206,7 +206,6 @@ func (obj *AugeasRes) checkApplySet(ctx context.Context, apply bool, ag *augeas.
// CheckApply method for Augeas resource. // CheckApply method for Augeas resource.
func (obj *AugeasRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *AugeasRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply: %s", obj.File)
// By default we do not set any option to augeas, we use the defaults. // By default we do not set any option to augeas, we use the defaults.
opts := augeas.None opts := augeas.None
if obj.Lens != "" { if obj.Lens != "" {

View File

@@ -638,7 +638,7 @@ func (obj *AwsEc2Res) snsWatch(ctx context.Context) error {
// CheckApply method for AwsEc2 resource. // CheckApply method for AwsEc2 resource.
func (obj *AwsEc2Res) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *AwsEc2Res) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply(%t)", apply) obj.init.Logf("CheckApply(%t)", apply) // XXX: replace with logf on change
// find the instance we need to check // find the instance we need to check
instance, err := describeInstanceByName(obj.client, obj.prependName()) instance, err := describeInstanceByName(obj.client, obj.prependName())

View File

@@ -605,7 +605,7 @@ func (obj *FileRes) fileCheckApply(ctx context.Context, apply bool, src io.ReadS
} }
// FIXME: respect obj.Recurse here... // FIXME: respect obj.Recurse here...
// there is a dir here, where we want a file... // there is a dir here, where we want a file...
obj.init.Logf("fileCheckApply: removing (force): %s", cleanDst) obj.init.Logf("removing (force): %s", cleanDst)
if err := os.RemoveAll(cleanDst); err != nil { // dangerous ;) if err := os.RemoveAll(cleanDst); err != nil { // dangerous ;)
return "", false, err return "", false, err
} }
@@ -651,7 +651,7 @@ func (obj *FileRes) fileCheckApply(ctx context.Context, apply bool, src io.ReadS
return sha256sum, false, nil return sha256sum, false, nil
} }
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("fileCheckApply: apply: %v -> %s", src, dst) obj.init.Logf("apply: %v -> %s", src, dst)
} }
dstClose() // unlock file usage so we can write to it dstClose() // unlock file usage so we can write to it
@@ -672,12 +672,12 @@ func (obj *FileRes) fileCheckApply(ctx context.Context, apply bool, src io.ReadS
// TODO: should we offer a way to cancel the copy on ^C ? // TODO: should we offer a way to cancel the copy on ^C ?
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("fileCheckApply: copy: %v -> %s", src, dst) obj.init.Logf("copy: %v -> %s", src, dst)
} }
if n, err := io.Copy(dstFile, src); err != nil { if n, err := io.Copy(dstFile, src); err != nil {
return sha256sum, false, err return sha256sum, false, err
} else if obj.init.Debug { } else if obj.init.Debug {
obj.init.Logf("fileCheckApply: copied: %v", n) obj.init.Logf("copied: %v", n)
} }
return sha256sum, false, dstFile.Sync() return sha256sum, false, dstFile.Sync()
} }
@@ -704,7 +704,7 @@ func (obj *FileRes) dirCheckApply(ctx context.Context, apply bool) (bool, error)
// the path exists and is not a directory // the path exists and is not a directory
// delete the file if force is given // delete the file if force is given
if err == nil && !fileInfo.IsDir() { if err == nil && !fileInfo.IsDir() {
obj.init.Logf("dirCheckApply: removing (force): %s", obj.getPath()) obj.init.Logf("removing (force): %s", obj.getPath())
if err := os.Remove(obj.getPath()); err != nil { if err := os.Remove(obj.getPath()); err != nil {
return false, err return false, err
} }
@@ -720,6 +720,7 @@ func (obj *FileRes) dirCheckApply(ctx context.Context, apply bool) (bool, error)
if obj.Recurse { if obj.Recurse {
// TODO: add recurse limit here // TODO: add recurse limit here
obj.init.Logf("mkdir -p -m %s", mode)
return false, os.MkdirAll(obj.getPath(), mode) return false, os.MkdirAll(obj.getPath(), mode)
} }
@@ -733,7 +734,7 @@ func (obj *FileRes) dirCheckApply(ctx context.Context, apply bool) (bool, error)
// with the exception that a sync *can* convert a file to a dir, or vice-versa. // with the exception that a sync *can* convert a file to a dir, or vice-versa.
func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst string, excludes []string) (bool, error) { func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst string, excludes []string) (bool, error) {
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("syncCheckApply: %s -> %s", src, dst) obj.init.Logf("sync: %s -> %s", src, dst)
} }
// an src of "" is now supported, if dst is a dir // an src of "" is now supported, if dst is a dir
if dst == "" { if dst == "" {
@@ -755,12 +756,12 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
if !srcIsDir && !dstIsDir && src != "" { if !srcIsDir && !dstIsDir && src != "" {
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("syncCheckApply: %s -> %s", src, dst) obj.init.Logf("sync: %s -> %s", src, dst)
} }
fin, err := os.Open(src) fin, err := os.Open(src)
if err != nil { if err != nil {
if obj.init.Debug && os.IsNotExist(err) { // if we get passed an empty src if obj.init.Debug && os.IsNotExist(err) { // if we get passed an empty src
obj.init.Logf("syncCheckApply: missing src: %s", src) obj.init.Logf("missing src: %s", src)
} }
return false, err return false, err
} }
@@ -782,7 +783,9 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
return false, err return false, err
} }
smartSrc = mapPaths(srcFiles) smartSrc = mapPaths(srcFiles)
obj.init.Logf("syncCheckApply: srcFiles: %v", printFiles(smartSrc)) if obj.init.Debug {
obj.init.Logf("srcFiles: %v", printFiles(smartSrc))
}
} }
dstFiles, err := ReadDir(dst) dstFiles, err := ReadDir(dst)
@@ -790,7 +793,9 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
return false, err return false, err
} }
smartDst := mapPaths(dstFiles) smartDst := mapPaths(dstFiles)
obj.init.Logf("syncCheckApply: dstFiles: %v", printFiles(smartDst)) if obj.init.Debug {
obj.init.Logf("dstFiles: %v", printFiles(smartDst))
}
for relPath, fileInfo := range smartSrc { for relPath, fileInfo := range smartSrc {
absSrc := fileInfo.AbsPath // absolute path absSrc := fileInfo.AbsPath // absolute path
@@ -814,7 +819,7 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
if absCleanDst == "" || absCleanDst == "/" { if absCleanDst == "" || absCleanDst == "/" {
return false, fmt.Errorf("don't want to remove root") // safety return false, fmt.Errorf("don't want to remove root") // safety
} }
obj.init.Logf("syncCheckApply: removing (force): %s", absCleanDst) obj.init.Logf("removing (force): %s", absCleanDst)
if err := os.Remove(absCleanDst); err != nil { if err := os.Remove(absCleanDst); err != nil {
return false, err return false, err
} }
@@ -822,7 +827,7 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
} }
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("syncCheckApply: mkdir -m %s '%s'", fileInfo.Mode(), absDst) obj.init.Logf("mkdir -m %s '%s'", fileInfo.Mode(), absDst)
} }
if err := os.Mkdir(absDst, fileInfo.Mode()); err != nil { if err := os.Mkdir(absDst, fileInfo.Mode()); err != nil {
return false, err return false, err
@@ -833,11 +838,11 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
} }
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("syncCheckApply: recurse: %s -> %s", absSrc, absDst) obj.init.Logf("recurse: %s -> %s", absSrc, absDst)
} }
if obj.Recurse { if obj.Recurse {
if c, err := obj.syncCheckApply(ctx, apply, absSrc, absDst, excludes); err != nil { // recurse if c, err := obj.syncCheckApply(ctx, apply, absSrc, absDst, excludes); err != nil { // recurse
return false, errwrap.Wrapf(err, "syncCheckApply: recurse failed") return false, errwrap.Wrapf(err, "recurse failed")
} else if !c { // don't let subsequent passes make this true } else if !c { // don't let subsequent passes make this true
checkOK = false checkOK = false
} }
@@ -882,7 +887,7 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
if isExcluded(absDst) { // skip removing excluded files if isExcluded(absDst) { // skip removing excluded files
continue continue
} }
obj.init.Logf("syncCheckApply: removing: %s", absCleanDst) obj.init.Logf("removing: %s", absCleanDst)
if apply { if apply {
if err := os.RemoveAll(absCleanDst); err != nil { // dangerous ;) if err := os.RemoveAll(absCleanDst); err != nil { // dangerous ;)
return false, err return false, err
@@ -892,16 +897,16 @@ func (obj *FileRes) syncCheckApply(ctx context.Context, apply bool, src, dst str
continue continue
} }
_ = absSrc _ = absSrc
//obj.init.Logf("syncCheckApply: recurse rm: %s -> %s", absSrc, absDst) //obj.init.Logf("recurse rm: %s -> %s", absSrc, absDst)
//if c, err := obj.syncCheckApply(ctx, apply, absSrc, absDst, excludes); err != nil { //if c, err := obj.syncCheckApply(ctx, apply, absSrc, absDst, excludes); err != nil {
// return false, errwrap.Wrapf(err, "syncCheckApply: recurse rm failed") // return false, errwrap.Wrapf(err, "recurse rm failed")
//} else if !c { // don't let subsequent passes make this true //} else if !c { // don't let subsequent passes make this true
// checkOK = false // checkOK = false
//} //}
//if isExcluded(absDst) { // skip removing excluded files //if isExcluded(absDst) { // skip removing excluded files
// continue // continue
//} //}
//obj.init.Logf("syncCheckApply: removing: %s", absCleanDst) //obj.init.Logf("removing: %s", absCleanDst)
//if apply { // safety //if apply { // safety
// if err := os.Remove(absCleanDst); err != nil { // if err := os.Remove(absCleanDst); err != nil {
// return false, err // return false, err
@@ -948,7 +953,7 @@ func (obj *FileRes) stateCheckApply(ctx context.Context, apply bool) (bool, erro
if p == "/" { if p == "/" {
return false, fmt.Errorf("don't want to remove root") // safety return false, fmt.Errorf("don't want to remove root") // safety
} }
obj.init.Logf("stateCheckApply: removing: %s", p) obj.init.Logf("removing: %s", p)
// TODO: add recurse limit here // TODO: add recurse limit here
if obj.Recurse { if obj.Recurse {
return false, os.RemoveAll(p) // dangerous ;) return false, os.RemoveAll(p) // dangerous ;)
@@ -1053,13 +1058,13 @@ func (obj *FileRes) sourceCheckApply(ctx context.Context, apply bool) (bool, err
} }
} }
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("syncCheckApply: excludes: %+v", excludes) obj.init.Logf("excludes: %+v", excludes)
} }
// XXX: should this work with obj.Purge && obj.Source != "" or not? // XXX: should this work with obj.Purge && obj.Source != "" or not?
checkOK, err := obj.syncCheckApply(ctx, apply, obj.Source, obj.getPath(), excludes) checkOK, err := obj.syncCheckApply(ctx, apply, obj.Source, obj.getPath(), excludes)
if err != nil { if err != nil {
obj.init.Logf("syncCheckApply: error: %v", err) obj.init.Logf("error: %v", err)
return false, err return false, err
} }
@@ -1187,7 +1192,7 @@ func (obj *FileRes) chownCheckApply(ctx context.Context, apply bool) (bool, erro
return false, nil return false, nil
} }
obj.init.Logf("chown %s:%s %s", obj.Owner, obj.Group, obj.getPath()) obj.init.Logf("chown %s:%s", obj.Owner, obj.Group)
return false, os.Chown(obj.getPath(), expectedUID, expectedGID) return false, os.Chown(obj.getPath(), expectedUID, expectedGID)
} }
@@ -1222,7 +1227,7 @@ func (obj *FileRes) chmodCheckApply(ctx context.Context, apply bool) (bool, erro
return false, nil return false, nil
} }
obj.init.Logf("chmod %s %s", obj.Mode, obj.getPath()) obj.init.Logf("chmod %s", obj.Mode)
return false, os.Chmod(obj.getPath(), mode) return false, os.Chmod(obj.getPath(), mode)
} }

View File

@@ -135,8 +135,6 @@ func (obj *GroupRes) Watch(ctx context.Context) error {
// CheckApply method for Group resource. // CheckApply method for Group resource.
func (obj *GroupRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *GroupRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply(%t)", apply)
// check if the group exists // check if the group exists
exists := true exists := true
group, err := user.LookupGroup(obj.Name()) group, err := user.LookupGroup(obj.Name())

View File

@@ -238,12 +238,8 @@ func (obj *HTTPFlagRes) Watch(ctx context.Context) error {
// CheckApply never has anything to do for this resource, so it always succeeds. // CheckApply never has anything to do for this resource, so it always succeeds.
func (obj *HTTPFlagRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *HTTPFlagRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
if obj.init.Debug {
obj.init.Logf("CheckApply")
}
if obj.init.Debug || true { // XXX: maybe we should always do this? if obj.init.Debug || true { // XXX: maybe we should always do this?
obj.init.Logf("CheckApply: value: %+v", obj.value) obj.init.Logf("value: %+v", obj.value)
} }
// TODO: can we send an empty (nil) value to show it has been removed? // TODO: can we send an empty (nil) value to show it has been removed?

View File

@@ -277,8 +277,6 @@ func (obj *KVRes) lessThanCheck(value string) (bool, error) {
// CheckApply method for Password resource. Does nothing, returns happy! // CheckApply method for Password resource. Does nothing, returns happy!
func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply(%t)", apply)
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
defer wg.Wait() // this must be above the defer cancel() call defer wg.Wait() // this must be above the defer cancel() call
ctx, cancel := context.WithTimeout(ctx, kvCheckApplyTimeout) ctx, cancel := context.WithTimeout(ctx, kvCheckApplyTimeout)
@@ -296,7 +294,7 @@ func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
if val, exists := obj.init.Recv()["value"]; exists && val.Changed { if val, exists := obj.init.Recv()["value"]; exists && val.Changed {
// if we received on Value, and it changed, wooo, nothing to do. // if we received on Value, and it changed, wooo, nothing to do.
obj.init.Logf("CheckApply: `value` was updated!") obj.init.Logf("`value` was received!")
} }
value, exists, err := obj.kvGet(ctx, obj.getKey()) value, exists, err := obj.kvGet(ctx, obj.getKey())
@@ -321,8 +319,11 @@ func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
if !apply { if !apply {
return false, nil return false, nil
} }
err := obj.kvDel(ctx, obj.getKey()) if err := obj.kvDel(ctx, obj.getKey()); err != nil {
return false, errwrap.Wrapf(err, "error during del") return false, errwrap.Wrapf(err, "error during del")
}
obj.init.Logf("`value` was deleted!")
return false, nil
} }
if !apply { if !apply {
@@ -332,6 +333,7 @@ func (obj *KVRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
if err := obj.kvSet(ctx, obj.getKey(), *obj.Value); err != nil { if err := obj.kvSet(ctx, obj.getKey(), *obj.Value); err != nil {
return false, errwrap.Wrapf(err, "error during set") return false, errwrap.Wrapf(err, "error during set")
} }
obj.init.Logf("`value` was changed!")
return false, nil return false, nil
} }

View File

@@ -253,7 +253,7 @@ func (obj *NspawnRes) CheckApply(ctx context.Context, apply bool) (bool, error)
// be stopped or the state matches we're done // be stopped or the state matches we're done
if !exists && obj.State == stopped || properties["State"] == obj.State { if !exists && obj.State == stopped || properties["State"] == obj.State {
if obj.init.Debug { if obj.init.Debug {
obj.init.Logf("CheckApply() in valid state") obj.init.Logf("in valid state")
} }
return true, nil return true, nil
} }
@@ -263,7 +263,7 @@ func (obj *NspawnRes) CheckApply(ctx context.Context, apply bool) (bool, error)
return false, nil return false, nil
} }
obj.init.Logf("CheckApply() applying '%s' state", obj.State) obj.init.Logf("applying '%s' state", obj.State)
// use the embedded svc to apply the correct state // use the embedded svc to apply the correct state
if _, err := obj.svc.CheckApply(ctx, apply); err != nil { if _, err := obj.svc.CheckApply(ctx, apply); err != nil {
return false, errwrap.Wrapf(err, "nested svc failed") return false, errwrap.Wrapf(err, "nested svc failed")

View File

@@ -96,10 +96,9 @@ func (obj *PrintRes) Watch(ctx context.Context) error {
// CheckApply method for Print resource. Does nothing, returns happy! // CheckApply method for Print resource. Does nothing, returns happy!
func (obj *PrintRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *PrintRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply: %t", apply)
if val, exists := obj.init.Recv()["msg"]; exists && val.Changed { if val, exists := obj.init.Recv()["msg"]; exists && val.Changed {
// if we received on Msg, and it changed, log message // if we received on Msg, and it changed, log message
obj.init.Logf("CheckApply: Received `msg` of: %s", obj.Msg) obj.init.Logf("received `msg` of: %s", obj.Msg)
} }
var refresh = obj.init.Refresh() var refresh = obj.init.Refresh()

View File

@@ -161,7 +161,7 @@ func (obj *TestRes) Watch(ctx context.Context) error {
func (obj *TestRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *TestRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
expectRecv := []string{} expectRecv := []string{}
for key, val := range obj.init.Recv() { for key, val := range obj.init.Recv() {
obj.init.Logf("CheckApply: Received `%s`, changed: %t", key, val.Changed) obj.init.Logf("received `%s`, changed: %t", key, val.Changed)
expectRecv = append(expectRecv, key) expectRecv = append(expectRecv, key)
} }

View File

@@ -182,8 +182,6 @@ func (obj *UserRes) Watch(ctx context.Context) error {
// CheckApply method for User resource. // CheckApply method for User resource.
func (obj *UserRes) CheckApply(ctx context.Context, apply bool) (bool, error) { func (obj *UserRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
obj.init.Logf("CheckApply(%t)", apply)
var exists = true var exists = true
usr, err := user.Lookup(obj.Name()) usr, err := user.Lookup(obj.Name())
if err != nil { if err != nil {

View File

@@ -140,7 +140,7 @@ func (obj *ValueRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
checkOK := false checkOK := false
if val, exists := obj.init.Recv()["any"]; exists && val.Changed { if val, exists := obj.init.Recv()["any"]; exists && val.Changed {
// if we received on Any, and it changed, invalidate the cache! // if we received on Any, and it changed, invalidate the cache!
obj.init.Logf("CheckApply: received on `any`") obj.init.Logf("received on `any`")
obj.isSet = true // we received something obj.isSet = true // we received something
obj.cachedAny = obj.Any obj.cachedAny = obj.Any
received = true // we'll always need to send below when we recv received = true // we'll always need to send below when we recv