void HostsManager::UpdateHostsFile()

in nodemanager/core/HostsManager.cpp [69:118]


void HostsManager::UpdateHostsFile(const std::vector<HostEntry>& hostEntries)
{
    Logger::Info("Hosts file manager: update local hosts file {0}", HostsFilePath);
    std::list<std::string> unmanagedLines;
    std::ifstream ifs(HostsFilePath, std::ios::in);
    std::string line;
    while (getline(ifs, line))
    {
        std::regex entryRegex(HPCHostEntryPattern);
        std::smatch entryMatch;
        // Strip the original HPC entries
        if(!std::regex_match(line, entryMatch, entryRegex))
        {
            unmanagedLines.push_back(line);
        }
        else
        {
            Logger::Debug("Strip the existing HPC host entry: ({0}, {1})", entryMatch[2], entryMatch[1]);
        }
    }

    ifs.close();

    std::ofstream ofs(HostsFilePath);
    auto it = unmanagedLines.cbegin();
    while(it != unmanagedLines.cend())
    {
        ofs << *it++ << std::endl;
    }

    // Append the HPC entries at the end
    // Make the <NetworkType>.<NodeName> entries at the end
    for(std::size_t i=0; i<hostEntries.size(); i++)
    {
        if (hostEntries[i].HostName.find('.') == std::string::npos)
        {
            Logger::Debug("Add HPC host entry: ({0}, {1})", hostEntries[i].HostName, hostEntries[i].IPAddress);
            ofs << std::left << std::setw(24) << hostEntries[i].IPAddress << std::setw(30) << hostEntries[i].HostName << "#HPC" << std::endl;
        }
    }
    for(std::size_t i=0; i<hostEntries.size(); i++)
    {
        if (hostEntries[i].HostName.find('.') != std::string::npos)
        {
            Logger::Debug("Add HPC host entry: ({0}, {1})", hostEntries[i].HostName, hostEntries[i].IPAddress);
            ofs << std::left << std::setw(24) << hostEntries[i].IPAddress << std::setw(30) << hostEntries[i].HostName << "#HPC" << std::endl;
        }
    }
    ofs.close();
}