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

get sn potential bugfix

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