提交 d6c914f6 authored 作者: blu's avatar blu

get sn potential bugfix

上级 66d8dd9c
......@@ -18,6 +18,7 @@ update: 2019/09/10
#include "inc/database.h"
#include "spdlog/spdlog.h"
#include "inc/httplib.h"
#include "fs.h"
using namespace leveldb;
using namespace httplib;
......@@ -245,7 +246,7 @@ namespace LVDB {
ret = _getDB(fileName, &pdb);
if(ret < 0) {
return ret;
return -10990;
}
Status s = pdb->Get(ReadOptions(), key, &value);
......@@ -375,33 +376,40 @@ togo_end:
}
int getSn(json &info, string fileName){
const string snFileName = "/etc/devsn.cfg";
int ret = 0;
int cnt = 0;
do{
string sn;
ret = getValue(info, LVDB_KEY_SN, fileName, _validateSn);
cnt++;
}while(ret < 0 && cnt < 3);
if(ret == -10990) {
spdlog::error("database.cpp failed to get sn: {}", ret);
exit(1);
}
if(ret < 0) {
// read from text config
// /etc/devsn.cfg
string sn;
ifstream snFile ("/etc/devsn.cfg");
if(fs::exists(fs::path(snFileName))){
// sn file exist
ifstream snFile(snFileName);
if (snFile.is_open())
{
getline (snFile,sn);
snFile.close();
}else{
spdlog::error("database.cpp failed to open sn file /etc/devsn.cfg");
exit(1);
}
}
if(sn.empty()) {
// create default sn.
spdlog::warn("no valid sn in /etc/devsn.cfg, generating a random one.");
ofstream snFile(snFileName);
sn = getStrRand(8);
}else{
std::remove(sn.begin(), sn.end(), ' ');
spdlog::info("successfully read sn from /etc/devsn.cfg");
snFile << sn;
snFile.close();
}
std::remove(sn.begin(), sn.end(), ' ');
spdlog::info("successfully get sn");
info["sn"] = sn;
info["apiCloud"] = "http://127.0.0.1:8089";
spdlog::warn("new sn is: {}", sn);
......@@ -414,15 +422,23 @@ togo_end:
}else{
//
}
}else{
ofstream snFile(snFileName);
sn = info["sn"].get<string>();
snFile << sn;
snFile.close();
}
return ret;
};
}
int setSn(json &info, string fileName){
info["lastupdated"] = chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count();
ofstream snFile("/etc/devsn.cfg", ios::trunc);
snFile << info["sn"];
snFile.close();
return setValue(info, LVDB_KEY_SN, fileName, _validateSn);
};
}
// config
int _validateConfig(const json &config) {
......@@ -437,7 +453,7 @@ togo_end:
return getValue(config, key, fileName, _validateConfig);
}
};
}
int setLocalConfig(json &config, string key, string fileName){
......@@ -451,18 +467,18 @@ togo_end:
// slices
int saveSlices(json &slices, string fileName){
return 0;
};
}
int loadSlices(json &slices, string fileName){
return 0;
};
}
// log
int saveLog(json &log, json &writeOptions, string fileName){
return 0;
};
}
int readLog(json &log, json &readOptions, string fileName){
return 0;
};
}
}// namespace LVDB
......
......@@ -178,15 +178,7 @@ private:
int reloadCfg(string subModGid = "")
{
int ret = LVDB::getSn(this->info);
if(ret < 0) {
spdlog::error("evdaemon {} failed to get info", this->devSn);
return 1;
}
this->devSn = this->info["sn"];
this->daemonId = this->devSn + ":evdaemon:0";
int ret = 0;
// apply config
try {
// lock_guard<mutex> lock(cacheLock);
......@@ -952,8 +944,9 @@ public:
}
spdlog::info("evdaemon boot \n{}",info.dump());
string cmd = "hostnamectl set-hostname " + info["sn"].get<string>();
string cmd = "hostnamectl set-hostname EVB-" + info["sn"].get<string>();
system(cmd.c_str());
this->daemonId = this->devSn + ":evdaemon:0";
auto ipAddrs = netutils::getIps();
if(ipAddrs.size() == 0) {
......
......@@ -442,8 +442,8 @@ void hand_sig(int sig) {
// station mode
mgr.enableMode(2);
}else if(key_event_msg.count == 5 && key_event_msg.time * 10 >= 10 * 1000){
// clear SN && restart evdaemon
system("rm -fr /opt/lvldb && systemctl restart evdaemon");
// clear SN && reboot
system("rm -fr /opt/lvldb && reboot");
}else if((key_event_msg.count == 1 && key_event_msg.time * 10 >= 10 * 1000)){
// restart network and evdaemon
system("/sbin/ifdown -a; /sbin/ifup -a; systemctl restart evdaemon");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论