engine: resources: Remove net config file on down
This cleans up the file we would have created when building the resource.
This commit is contained in:
@@ -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.
|
// 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) {
|
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
|
// check if the unit file exists
|
||||||
_, err := os.Stat(obj.unitFilePath)
|
_, err := os.Stat(obj.unitFilePath)
|
||||||
if err != nil && !os.IsNotExist(err) {
|
if err != nil && !os.IsNotExist(err) {
|
||||||
return false, errwrap.Wrapf(err, "error checking file")
|
return false, errwrap.Wrapf(err, "error checking file")
|
||||||
}
|
}
|
||||||
// build the unit file contents from the definition
|
|
||||||
contents := obj.unitFileContents()
|
exists := err == nil
|
||||||
// check the file contents
|
if obj.State == NetStateDown && !exists {
|
||||||
if err == nil {
|
return true, nil
|
||||||
unitFile, err := ioutil.ReadFile(obj.unitFilePath)
|
}
|
||||||
|
|
||||||
|
fileContents := []byte{}
|
||||||
|
if exists {
|
||||||
|
// check the file contents
|
||||||
|
byt, err := ioutil.ReadFile(obj.unitFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errwrap.Wrapf(err, "error reading file")
|
return false, errwrap.Wrapf(err, "error reading file")
|
||||||
}
|
}
|
||||||
// return if the file is good
|
fileContents = byt
|
||||||
if bytes.Equal(unitFile, contents) {
|
}
|
||||||
return true, nil
|
// 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 {
|
if !apply {
|
||||||
@@ -511,7 +525,14 @@ func (obj *NetRes) fileCheckApply(ctx context.Context, apply bool) (bool, error)
|
|||||||
}
|
}
|
||||||
obj.init.Logf("fileCheckApply(%t)", apply)
|
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 {
|
if err := ioutil.WriteFile(obj.unitFilePath, contents, networkdUnitFileUmask); err != nil {
|
||||||
return false, errwrap.Wrapf(err, "error writing configuration file")
|
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) {
|
func (obj *NetRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
|
||||||
checkOK := true
|
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
|
// check the network device
|
||||||
if c, err := obj.ifaceCheckApply(ctx, apply); err != nil {
|
if c, err := obj.ifaceCheckApply(ctx, apply); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
@@ -550,18 +578,6 @@ func (obj *NetRes) CheckApply(ctx context.Context, apply bool) (bool, error) {
|
|||||||
checkOK = false
|
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
|
return checkOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user