mirror of https://github.com/jetkvm/kvm.git
fix(dhcp): watch directory instead of file to catch fsnotify.Create event
This commit is contained in:
parent
ea350a63f3
commit
cf9e90c4a6
|
@ -4,6 +4,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
|
@ -49,6 +50,21 @@ func NewDHCPClient(options *DHCPClientOptions) *DHCPClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *DHCPClient) getWatchPaths() []string {
|
||||||
|
watchPaths := make(map[string]interface{})
|
||||||
|
watchPaths[filepath.Dir(c.leaseFile)] = nil
|
||||||
|
|
||||||
|
if c.pidFile != "" {
|
||||||
|
watchPaths[filepath.Dir(c.pidFile)] = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
paths := make([]string, 0)
|
||||||
|
for path := range watchPaths {
|
||||||
|
paths = append(paths, path)
|
||||||
|
}
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
// Run starts the DHCP client and watches the lease file for changes.
|
// Run starts the DHCP client and watches the lease file for changes.
|
||||||
// this isn't a blocking call, and the lease file is reloaded when a change is detected.
|
// this isn't a blocking call, and the lease file is reloaded when a change is detected.
|
||||||
func (c *DHCPClient) Run() error {
|
func (c *DHCPClient) Run() error {
|
||||||
|
@ -67,12 +83,14 @@ func (c *DHCPClient) Run() error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event, ok := <-watcher.Events:
|
case event, ok := <-watcher.Events:
|
||||||
if !ok {
|
if !ok || !(event.Has(fsnotify.Write) || event.Has(fsnotify.Create)) {
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
if event.Has(fsnotify.Write) || event.Has(fsnotify.Create) {
|
|
||||||
|
if event.Name == c.leaseFile {
|
||||||
c.logger.Debug().
|
c.logger.Debug().
|
||||||
Str("event", event.Name).
|
Str("event", event.Op.String()).
|
||||||
|
Str("path", event.Name).
|
||||||
Msg("udhcpc lease file updated, reloading lease")
|
Msg("udhcpc lease file updated, reloading lease")
|
||||||
_ = c.loadLeaseFile()
|
_ = c.loadLeaseFile()
|
||||||
}
|
}
|
||||||
|
@ -85,13 +103,15 @@ func (c *DHCPClient) Run() error {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err = watcher.Add(c.leaseFile)
|
for _, path := range c.getWatchPaths() {
|
||||||
if err != nil {
|
err = watcher.Add(path)
|
||||||
c.logger.Error().
|
if err != nil {
|
||||||
Err(err).
|
c.logger.Error().
|
||||||
Str("path", c.leaseFile).
|
Err(err).
|
||||||
Msg("failed to watch lease file")
|
Str("path", path).
|
||||||
return err
|
Msg("failed to watch directory")
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: update udhcpc pid file
|
// TODO: update udhcpc pid file
|
||||||
|
|
Loading…
Reference in New Issue