/
misc_utils.cc
174 lines (159 loc) · 4.98 KB
/
misc_utils.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <base/misc_utils.h>
#include <base/logging.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "base/sandesh/version_types.h"
#include "base/logging.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
const std::string MiscUtils::ContrailVersionCmd = "/usr/bin/contrail-version";
const map<MiscUtils::BuildModule, string> MiscUtils::BuildModuleNames =
MiscUtils::MapInit();
SandeshTraceBufferPtr VersionTraceBuf(SandeshTraceBufferCreate(
VERSION_TRACE_BUF, 500));
string MiscUtils::BaseName(string filename) {
size_t pos = filename.find_last_of('/');
if (pos != string::npos) {
return filename.substr((pos+1));
}
return filename;
}
void MiscUtils::LogVersionInfo(const string build_info, Category::type categ) {
VERSION_TRACE(VersionInfoTrace, build_info);
if (!LoggingDisabled()) {
VERSION_LOG(VersionInfoLog, categ, build_info);
}
}
bool MiscUtils::GetVersionInfoInternal(const string &cmd, string &rpm_version,
string &build_num) {
FILE *fp;
char line[512];
fp = popen(cmd.c_str(), "r");
if (fp == NULL) {
return false;
}
char *ptr = fgets(line, sizeof(line), fp);
if (ptr == NULL) {
pclose(fp);
return false;
}
pclose(fp);
ptr = strchr(line, '\n');
if (ptr != NULL) {
*ptr = '\0';
}
istringstream iss(line);
if (iss) {
iss >> rpm_version;
if (iss) {
iss >> build_num;
} else {
return false;
}
} else {
return false;
}
return true;
}
bool MiscUtils::GetContrailVersionInfo(BuildModule id, string &rpm_version,
string &build_num) {
bool ret;
stringstream cmd;
//Initialize the version info here. Overide its value on finding version
rpm_version.assign("unknown");
build_num.assign("unknown");
ifstream f(ContrailVersionCmd.c_str());
if (!f.good()) {
f.close();
return false;
}
f.close();
cmd << ContrailVersionCmd << " " << BuildModuleNames.at(id)
<< " | tail -1 | awk '{ print $2 \" \" $3 }'";
ret = GetVersionInfoInternal(cmd.str(), rpm_version, build_num);
return ret;
}
bool MiscUtils::GetBuildInfo(BuildModule id, const string &build_info,
string &result) {
string rpm_version;
string build_num;
bool ret = GetContrailVersionInfo(id, rpm_version, build_num);
rapidjson::Document d;
if (d.Parse<0>(const_cast<char *>(build_info.c_str())).HasParseError()) {
result = build_info;
return false;
}
rapidjson::Value& fields = d["build-info"];
if (!fields.IsArray()) {
result = build_info;
return false;
}
fields[0u].AddMember("build-id", const_cast<char *>(rpm_version.c_str()),
d.GetAllocator());
fields[0u].AddMember("build-number", const_cast<char *>(build_num.c_str()),
d.GetAllocator());
rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
d.Accept(writer);
result = strbuf.GetString();
return ret;
}
bool MiscUtils::GetPlatformInfo(std::string &distro, std::string &code_name) {
FILE *fp;
char line[512];
fp = popen("cat /etc/*release", "r");
if (fp == NULL) {
return false;
}
std::string result = "";
while (!feof(fp)) {
if (fgets(line, 512, fp) != NULL) {
result += line;
}
}
// parse the strings for centos 6.4, 6.5, trusty, precise..
if (result.find("trusty") != std::string::npos) {
distro = "Ubuntu";
code_name = "Trusty";
} else if (result.find("precise") != std::string::npos) {
distro = "Ubuntu";
code_name = "Precise";
} else if (result.find("rhel") != std::string::npos) {
distro = "rhel";
code_name = "7.0";
} else if (result.find("CentOS distro 6.4") != std::string::npos) {
distro = "CentOS";
code_name = "6.4";
} else if (result.find("CentOS distro 6.5") != std::string::npos) {
distro = "CentOS";
code_name = "6.5";
} else if (result.find("CentOS Linux release 7") != std::string::npos) {
distro = "CentOS";
code_name = "7.1.1503";
} else {
return false;
}
return true;
}
bool MiscUtils::IsCqlSupported() {
std::string distro, code_name;
if (!MiscUtils::GetPlatformInfo(distro, code_name)) {
LOG(ERROR,"Unable to get platform information");
exit(1);
}
if (code_name.find("Precise") != std::string::npos ||
code_name.find("6.4") != std::string::npos ||
code_name.find("6.5") != std::string::npos) {
return false;
}
return true;
}