57 {
58
60 const char* chost=
getenv(
"ATLAS_CONDDB");
63 const char* chost=
getenv(
"HOSTNAME");
65
68 system("hostname --fqdn > hostnamelookup.tmp");
70 infile.open(
"hostnamelookup.tmp");
71 if (infile) {
73 } else {
75 }
76 }
77 }
78 std::cout <<
"Using machine hostname " <<
m_hostname <<
79 " for DB replica resolution" << std::endl;
80
81 const char* cfrontier=
getenv(
"FRONTIER_SERVER");
82 if (cfrontier && strcmp(cfrontier,"")!=0) {
83 std::cout << "Frontier server at " << cfrontier << " will be considered"
84 << std::endl;
86 }
87
88
89 FILE* p_inp=nullptr;
91 if (datapath!=
nullptr) p_inp=
findFile(
"dbreplica.config",datapath);
92 if (p_inp==nullptr) {
93 std::cout << "Cannot open/locate configuration file dbreplica.config"
94 << std::endl;
95 return false;
96 }
97
98 unsigned int bufsize=999;
99 char* p_buf=new char[bufsize];
100 while (!feof(p_inp)) {
101 char* p_line=fgets(p_buf,bufsize,p_inp);
102 if (p_line!=nullptr && p_line[0]!='#') {
103 std::string
buf=std::string(p_line);
104 std::string::size_type iofs1=0;
105
106 bool sequal=false;
107 std::vector<std::string> domains;
108 std::vector<std::string> servers;
109 while (iofs1<
buf.size()) {
110 std::string::size_type iofs2=
buf.find(
' ',iofs1);
111
112 if (iofs2==std::string::npos) iofs2=
buf.size()-1;
113 std::string token=
buf.substr(iofs1,iofs2-iofs1);
114
115 if (!token.empty() && token!=" ") {
116 if (token=="=") {
117 sequal=true;
118 } else if (!sequal) {
119
120 domains.push_back(std::move(token));
121 } else {
122
123
124 if (token!=
"ATLF" ||
m_frontiergen) servers.push_back(std::move(token));
125 }
126 }
127 iofs1=iofs2+1;
128 }
129
130
131 bool useit=false;
132 unsigned int bestlen=0;
133 for (std::vector<std::string>::const_iterator itr=domains.begin();
134 itr!=domains.end();++itr) {
135 std::string::size_type len=(itr->size());
136 std::string::size_type hlen=
m_hostname.size();
137 if (hlen>=len && *itr==
m_hostname.substr(hlen-len,len)) {
138 if (len>bestlen) {
139 useit=true;
140 bestlen=len;
141 }
142 }
143
144
146 std::cout <<
147 "No specific match for domain found - use default fallback"
148 << std::endl;
149 useit=true;
150 bestlen=0;
151 }
152 }
153 if (useit) {
154
155
156 for (
unsigned int i=0;
i<servers.size();++
i) {
159 }
160 }
161 }
162 }
163 fclose(p_inp);
164 delete [] p_buf;
166 " servers found for host " <<
m_hostname << std::endl;
167 return true;
168}
static FILE * findFile(const std::string &filename, const std::string &pathvar)
std::pair< std::string, int > ServerPair
std::string getenv(const std::string &variableName)
get an environment variable