[minor] fix configLoad bug in mig-agent

If an error ocurred during configLoad function, it might not use the
builtin conf, but a half-overwritten one. This change defers the
Config overwrite until all the error-sensitive functions are called.
This commit is contained in:
Marco Vanotti 2015-10-11 03:24:39 -03:00
Родитель 75b3c626a4
Коммит d23927487c
2 изменённых файлов: 31 добавлений и 24 удалений

Просмотреть файл

@ -123,9 +123,10 @@ func main() {
// attempt to read a local configuration file // attempt to read a local configuration file
err = configLoad(*config) err = configLoad(*config)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "[info] using builtin conf\n") fmt.Fprintf(os.Stderr, "[error] Failed to load conf from %q, err: %v\n", *config, err)
fmt.Fprintf(os.Stderr, "[info] Using builtin conf.\n")
} else { } else {
fmt.Fprintf(os.Stderr, "[info] Using external conf from %s\n", *config) fmt.Fprintf(os.Stderr, "[info] Using external conf from %q\n", *config)
} }
// if checkin mode is set in conf, enforce the mode // if checkin mode is set in conf, enforce the mode
if CHECKIN && *mode == "agent" { if CHECKIN && *mode == "agent" {

Просмотреть файл

@ -9,9 +9,10 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"mig.ninja/mig"
"time" "time"
"mig.ninja/mig"
"gopkg.in/gcfg.v1" "gopkg.in/gcfg.v1"
) )
@ -42,10 +43,30 @@ func configLoad(path string) (err error) {
} }
}() }()
var config config var config config
err = gcfg.ReadFileInto(&config, path) if err = gcfg.ReadFileInto(&config, path); err != nil {
panic(err)
}
hbf, err := time.ParseDuration(config.Agent.HeartbeatFreq)
if err != nil { if err != nil {
panic(err) panic(err)
} }
timeout, err := time.ParseDuration(config.Agent.ModuleTimeout)
if err != nil {
panic(err)
}
agentkey, err := ioutil.ReadFile(config.Certs.Key)
if err != nil {
panic(err)
}
cacert, err := ioutil.ReadFile(config.Certs.Ca)
if err != nil {
panic(err)
}
agentcert, err := ioutil.ReadFile(config.Certs.Cert)
if err != nil {
panic(err)
}
ISIMMORTAL = config.Agent.IsImmortal ISIMMORTAL = config.Agent.IsImmortal
MUSTINSTALLSERVICE = config.Agent.InstallService MUSTINSTALLSERVICE = config.Agent.InstallService
DISCOVERPUBLICIP = config.Agent.DiscoverPublicIP DISCOVERPUBLICIP = config.Agent.DiscoverPublicIP
@ -53,25 +74,10 @@ func configLoad(path string) (err error) {
LOGGINGCONF = config.Logging LOGGINGCONF = config.Logging
AMQPBROKER = config.Agent.Relay AMQPBROKER = config.Agent.Relay
APIURL = config.Agent.Api APIURL = config.Agent.Api
HEARTBEATFREQ, err = time.ParseDuration(config.Agent.HeartbeatFreq) HEARTBEATFREQ = hbf
if err != nil { MODULETIMEOUT = timeout
panic(err) CACERT = cacert
} AGENTCERT = agentcert
MODULETIMEOUT, err = time.ParseDuration(config.Agent.ModuleTimeout) AGENTKEY = agentkey
if err != nil {
panic(err)
}
CACERT, err = ioutil.ReadFile(config.Certs.Ca)
if err != nil {
panic(err)
}
AGENTCERT, err = ioutil.ReadFile(config.Certs.Cert)
if err != nil {
panic(err)
}
AGENTKEY, err = ioutil.ReadFile(config.Certs.Key)
if err != nil {
panic(err)
}
return return
} }