Skip to content
Snippets Groups Projects
Commit 6cff06b9 authored by David Auer's avatar David Auer
Browse files

Duplicate hostsfile utils for daemon

parent 67d4c829
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,9 @@
#ifndef GEKKOFS_DAEMON_UTIL_HPP
#define GEKKOFS_DAEMON_UTIL_HPP
#include <vector>
#include <string>
namespace gkfs {
namespace util {
void
......@@ -21,6 +24,9 @@ populate_hosts_file();
void
destroy_hosts_file();
std::vector<std::pair<std::string, std::string>>
read_hosts_file();
} // namespace util
} // namespace gkfs
......
......@@ -18,6 +18,8 @@
#include <fstream>
#include <iostream>
#include <regex>
using namespace std;
namespace gkfs::util {
......@@ -48,4 +50,68 @@ destroy_hosts_file() {
std::remove(GKFS_DATA->hosts_file().c_str());
}
// hosts file functions copied from daemon - need Modifications for LOG and hostsfile path source
// TODO clean that up, probably best into global utilities - TBD How do we do logging there?
/**
* Reads the daemon generator hosts file by a given path, returning hosts and URI addresses
* @param path to hosts file
* @return vector<pair<hosts, URI>>
* @throws std::runtime_error
*/
vector<pair<string, string>> load_hostfile(const std::string& path) {
//LOG(DEBUG, "Loading hosts file: \"{}\"", path);
ifstream lf(path);
if (!lf) {
throw runtime_error(fmt::format("Failed to open hosts file '{}': {}",
path, strerror(errno)));
}
vector<pair<string, string>> hosts;
const regex line_re("^(\\S+)\\s+(\\S+)$",
regex::ECMAScript | regex::optimize);
string line;
string host;
string uri;
std::smatch match;
while (getline(lf, line)) {
if (!regex_match(line, match, line_re)) {
//LOG(ERROR, "Unrecognized line format: [path: '{}', line: '{}']",
// path, line);
throw runtime_error(
fmt::format("unrecognized line format: '{}'", line));
}
host = match[1];
uri = match[2];
hosts.emplace_back(host, uri);
}
if (hosts.empty()) {
throw runtime_error("Hosts file found but no suitable addresses could be extracted");
}
// ??? extract_protocol(hosts[0].second);
return hosts;
}
vector<pair<string, string>> read_hosts_file() {
string hostfile = GKFS_DATA->hosts_file();
vector<pair<string, string>> hosts;
try {
hosts = load_hostfile(hostfile);
} catch (const exception& e) {
auto emsg = fmt::format("Failed to load hosts file: {}", e.what());
throw runtime_error(emsg);
}
if (hosts.empty()) {
throw runtime_error(fmt::format("Hostfile empty: '{}'", hostfile));
}
GKFS_DATA->spdlogger()->info("Hosts pool size: {}", hosts.size());
return hosts;
}
} // namespace gkfs::util
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment