Commit 962c130f authored by David Auer's avatar David Auer
Browse files

Duplicate hostsfile utils for daemon

parent 1b3b8afa
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -14,6 +14,9 @@
#ifndef GEKKOFS_DAEMON_UTIL_HPP
#define GEKKOFS_DAEMON_UTIL_HPP

#include <vector>
#include <string>

namespace gkfs {
namespace utils {
void
@@ -21,6 +24,9 @@ populate_hosts_file();

void
destroy_hosts_file();

std::vector<std::pair<std::string, std::string>>
read_hosts_file();
} // namespace utils
} // namespace gkfs

+66 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@
#include <fstream>
#include <iostream>

#include <regex>

using namespace std;

namespace gkfs::utils {
@@ -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::utils