func (ce *CtrlEngine) upkeepFetchconf( msgDB *msgdb.MsgDB, homedir string, show bool, outfp, statfp io.Writer, ) error { netDomain, pubkeyStr, configURL := def.ConfigParams() log.Infof("fetch config for '%s'", netDomain) fmt.Fprintf(statfp, "fetch config for '%s'\n", netDomain) publicKey, err := hex.DecodeString(pubkeyStr) if err != nil { log.Error(err) } ce.config.PublicKey = publicKey ce.config.URLList = "10," + configURL ce.config.Timeout = 0 // use default timeout if err := ce.config.Update(); err != nil { return log.Error(err) } jsn, err := json.Marshal(ce.config) if err != nil { return log.Error(err) } if err := msgDB.AddValue(netDomain, string(jsn)); err != nil { return err } err = msgDB.AddValue("time."+netDomain, strconv.FormatInt(times.Now(), 10)) if err != nil { return err } // apply new configuration if err := def.InitMute(&ce.config); err != nil { return err } // format configuration nicely jsn, err = json.MarshalIndent(ce.config, "", " ") if err != nil { return log.Error(err) } // write new configuration file if err := writeConfigFile(homedir, netDomain, jsn); err != nil { return err } // show new configuration if show { fmt.Fprintf(outfp, string(jsn)+"\n") } return nil }
func (ce *CtrlEngine) getConfig(homedir string, offline bool) error { // read default config netDomain, _, _ := def.ConfigParams() jsn, err := ce.msgDB.GetValue(netDomain) if err != nil { return err } if jsn != "" { if err := json.Unmarshal([]byte(jsn), &ce.config); err != nil { return err } // apply old configuration err := def.InitMute(&ce.config) if err != nil { // init failed -> update config (which will try init again) fmt.Fprintf(ce.fileTable.StatusFP, "initialization failed, try to update config\n") if offline { return log.Error("ctrlengine: cannot fetch config in " + "--offline mode, run without") } err := ce.upkeepFetchconf(ce.msgDB, homedir, false, nil, ce.fileTable.StatusFP) if err != nil { return err } } else { // fetch new configuration, if last fetch is older than 24h timestr, err := ce.msgDB.GetValue("time." + netDomain) if err != nil { return err } if timestr != "" { t, err := strconv.ParseInt(timestr, 10, 64) if err != nil { return log.Error(err) } last := time.Now().Sub(time.Unix(t, 0)) if last > def.FetchconfMinDuration { if offline { if last > def.FetchconfMaxDuration { return log.Error("ctrlengine: configuration is " + "outdated, please run without --offline") } log.Warn("ctrlengine: cannot fetch outdated config " + "in --offline mode") fmt.Fprintf(ce.fileTable.StatusFP, "ctrlengine: cannot fetch outdated config in "+ "--offline mode\n") } else { // update config err := ce.upkeepFetchconf(ce.msgDB, homedir, false, nil, ce.fileTable.StatusFP) if err != nil { return err } } } } } } else { // no config found, fetch it if offline { return log.Error("ctrlengine: cannot fetch config in --offline mode") } fmt.Fprintf(ce.fileTable.StatusFP, "no system config found\n") err := ce.upkeepFetchconf(ce.msgDB, homedir, false, nil, ce.fileTable.StatusFP) if err != nil { return err } } return nil }