From 641b6067cdce47c3f449d1e0f8baa04bd41efda5 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Tue, 21 Nov 2023 00:12:35 -0500 Subject: [PATCH] engine: resources: Remove net config file on down This cleans up the file we would have created when building the resource. --- engine/resources/net.go | 60 ++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/engine/resources/net.go b/engine/resources/net.go index e05cd167..84115e48 100644 --- a/engine/resources/net.go +++ b/engine/resources/net.go @@ -486,24 +486,38 @@ func (obj *NetRes) gatewayCheckApply(ctx context.Context, apply bool) (bool, err } // fileCheckApply checks and maintains the systemd-networkd unit file contents. +// TODO: replace this with a file resource if we can do so cleanly. func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error) { + // if the state is unspecified, we're done + if obj.State == "" { + return true, nil + } + // check if the unit file exists _, err := os.Stat(obj.unitFilePath) if err != nil && !os.IsNotExist(err) { return false, errwrap.Wrapf(err, "error checking file") } - // build the unit file contents from the definition - contents := obj.unitFileContents() - // check the file contents - if err == nil { - unitFile, err := ioutil.ReadFile(obj.unitFilePath) + + exists := err == nil + if obj.State == NetStateDown && !exists { + return true, nil + } + + fileContents := []byte{} + if exists { + // check the file contents + byt, err := ioutil.ReadFile(obj.unitFilePath) if err != nil { return false, errwrap.Wrapf(err, "error reading file") } - // return if the file is good - if bytes.Equal(unitFile, contents) { - return true, nil - } + fileContents = byt + } + // build the unit file contents from the definition + contents := obj.unitFileContents() + + if obj.State == NetStateUp && exists && bytes.Equal(fileContents, contents) { + return true, nil } if !apply { @@ -511,7 +525,14 @@ func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error) } obj.init.Logf("fileCheckApply(%t)", apply) - // write the file + if obj.State == NetStateDown && exists { + if err := os.Remove(obj.unitFilePath); err != nil { + return false, errwrap.Wrapf(err, "error removing configuration file") + } + return false, nil + } + + // all other situations, we write the file if err := ioutil.WriteFile(obj.unitFilePath, contents, networkdUnitFileUmask); err != nil { return false, errwrap.Wrapf(err, "error writing configuration file") } @@ -524,6 +545,13 @@ func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error) func (obj *NetRes) CheckApply(ctx context.Context, apply bool) (bool, error) { checkOK := true + // check the networkd unit file + if c, err := obj.fileCheckApply(ctx, apply); err != nil { + return false, err + } else if !c { + checkOK = false + } + // check the network device if c, err := obj.ifaceCheckApply(ctx, apply); err != nil { return false, err @@ -550,18 +578,6 @@ func (obj *NetRes) CheckApply(ctx context.Context, apply bool) (bool, error) { checkOK = false } - // if the state is unspecified, we're done - if obj.State == "" { - return checkOK, nil - } - - // check the networkd unit file - if c, err := obj.fileCheckApply(ctx, apply); err != nil { - return false, err - } else if !c { - checkOK = false - } - return checkOK, nil }