Merge: + dhcp: use --workdir value for "leases.db" file path

Close #935

* commit '8e08cddf647226d5e8e5f41307b60274a3f232e5':
  + dhcp: use --workdir value for "leases.db" file path
This commit is contained in:
Simon Zolin 2019-08-19 16:55:42 +03:00
commit 5b555f95ff
4 changed files with 11 additions and 4 deletions

View File

@ -37,10 +37,10 @@ func (s *Server) dbLoad() {
s.leases = nil
s.IPpool = make(map[[4]byte]net.HardwareAddr)
data, err := ioutil.ReadFile(dbFilename)
data, err := ioutil.ReadFile(s.conf.DBFilePath)
if err != nil {
if !os.IsNotExist(err) {
log.Error("DHCP: can't read file %s: %v", dbFilename, err)
log.Error("DHCP: can't read file %s: %v", s.conf.DBFilePath, err)
}
return
}
@ -99,10 +99,10 @@ func (s *Server) dbStore() {
return
}
err = file.SafeWrite(dbFilename, data)
err = file.SafeWrite(s.conf.DBFilePath, data)
if err != nil {
log.Error("DHCP: can't store lease table on disk: %v filename: %s",
err, dbFilename)
err, s.conf.DBFilePath)
return
}
log.Info("DHCP: stored %d leases in DB", len(leases))

View File

@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net"
"path/filepath"
"strings"
"sync"
"time"
@ -38,6 +39,8 @@ type ServerConfig struct {
RangeStart string `json:"range_start" yaml:"range_start"`
RangeEnd string `json:"range_end" yaml:"range_end"`
LeaseDuration uint `json:"lease_duration" yaml:"lease_duration"` // in seconds
WorkDir string `json:"-" yaml:"-"`
DBFilePath string `json:"-" yaml:"-"` // path to DB file
// IP conflict detector: time (ms) to wait for ICMP reply.
// 0: disable
@ -97,6 +100,7 @@ func (s *Server) Init(config ServerConfig) error {
func (s *Server) setConfig(config ServerConfig) error {
s.conf = config
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)
iface, err := net.InterfaceByName(config.InterfaceName)
if err != nil {

View File

@ -21,6 +21,7 @@ func check(t *testing.T, result bool, msg string) {
// . Handle Request message (lease commit)
func TestDHCP(t *testing.T) {
var s = Server{}
s.conf.DBFilePath = dbFilename
var p, p2 dhcp4.Packet
var hw net.HardwareAddr
var lease *Lease
@ -156,6 +157,7 @@ func misc(t *testing.T, s *Server) {
// Leases database store/load
func TestDB(t *testing.T) {
var s = Server{}
s.conf.DBFilePath = dbFilename
var p dhcp4.Packet
var hw1, hw2 net.HardwareAddr
var lease *Lease

View File

@ -75,6 +75,7 @@ func handleDHCPSetConfig(w http.ResponseWriter, r *http.Request) {
return
}
newconfig.ServerConfig.WorkDir = config.ourWorkingDir
err = config.dhcpServer.CheckConfig(newconfig.ServerConfig)
if err != nil {
httpError(w, http.StatusBadRequest, "Invalid DHCP configuration: %s", err)