Verified Commit 2e2fb821 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add `cli` directory for utility programs

The directory currently includes the following helper programs:

- `scord_ping`: A helper program to check if a `scord` server is active
  on a certain address.
- `scord_query`: A helper program to query a `scord` server for
  information on active jobs.
parent 4ee1445e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -319,6 +319,7 @@ add_compile_options("-Wall" "-Wextra" "-Werror" "$<$<CONFIG:RELEASE>:-O3>")
add_compile_definitions("$<$<CONFIG:DEBUG,ASan>:SCORD_DEBUG_BUILD>")
add_compile_definitions("$<$<CONFIG:DEBUG,ASan>:LOGGER_ENABLE_DEBUG>")

add_subdirectory(cli)
add_subdirectory(etc)
add_subdirectory(src)
add_subdirectory(plugins)
+4 −4
Original line number Diff line number Diff line
@@ -295,12 +295,12 @@ Which should produce output similar to the following:
[2021-11-19 10:30:30.066151] [scord] [131119] [info]
[2021-11-19 10:30:30.066161] [scord] [131119] [info] [[ Start up successful, awaiting requests... ]]
```
Now we can use one of the example programs to send a `ping` RPC to Scord:

```bash
Now we can use the `scord_ping` CLI program packaged with the service to
send a `ping` RPC to Scord:

cd $HOME/scord/build/examples
./ADM_ping ofi+tcp://192.168.0.111:52000
```bash
scord_ping ofi+tcp://192.168.0.111:52000
```

And the server logs should update with an entry similar the following one:

cli/CMakeLists.txt

0 → 100644
+53 −0
Original line number Diff line number Diff line
################################################################################
# Copyright 2021-2023, Barcelona Supercomputing Center (BSC), Spain            #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of scord.                                                  #
#                                                                              #
# scord is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# scord is distributed in the hope that it will be useful,                     #
# but WITHOUT ANY WARRANTY; without even the implied warranty of               #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                #
# GNU General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with scord.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

# scord_ping: ping a remote scord server
add_executable(scord_ping)

target_sources(scord_ping
  PRIVATE
  scord_ping.cpp
)

target_link_libraries(scord_ping
  PUBLIC fmt::fmt CLI11::CLI11 libscord)

install(TARGETS scord_ping
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# scord_query: query a remote scord server
add_executable(scord_query)

target_sources(scord_query
  PRIVATE
  scord_query.cpp
)

target_link_libraries(scord_query
  PUBLIC fmt::fmt CLI11::CLI11 libscord)

install(TARGETS scord_query
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

cli/scord_ping.cpp

0 → 100644
+83 −0
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2021-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of scord.
 *
 * scord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * scord is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with scord.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#include <fmt/format.h>
#include <filesystem>
#include <CLI/CLI.hpp>
#include <scord/scord.hpp>

struct ping_config {
    std::string progname;
    std::string server_address;
};

ping_config
parse_command_line(int argc, char* argv[]) {

    ping_config cfg;

    cfg.progname = std::filesystem::path{argv[0]}.filename().string();

    CLI::App app{"Scord ping client", cfg.progname};

    app.add_option("-s,--server", cfg.server_address, "Server address")
            ->option_text("ADDRESS")
            ->required();

    try {
        app.parse(argc, argv);
        return cfg;
    } catch(const CLI::ParseError& ex) {
        std::exit(app.exit(ex));
    }
}

auto
parse_address(const std::string& address) {
    const auto pos = address.find("://");
    if(pos == std::string::npos) {
        throw std::runtime_error(fmt::format("Invalid address: {}", address));
    }

    const auto protocol = address.substr(0, pos);
    return std::make_pair(protocol, address);
}


int
main(int argc, char* argv[]) {

    using namespace std::chrono_literals;

    ping_config cfg = parse_command_line(argc, argv);

    try {
        const auto [protocol, address] = parse_address(cfg.server_address);
        ping(scord::server{protocol, address});
        fmt::print("Ping succeeded!\n");
    } catch(const std::exception& ex) {
        fmt::print(stderr, "Ping failed: {}\n", ex.what());
        return EXIT_FAILURE;
    }
}

cli/scord_query.cpp

0 → 100644
+96 −0
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2021-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of scord.
 *
 * scord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * scord is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with scord.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#include <fmt/format.h>
#include <filesystem>
#include <CLI/CLI.hpp>
#include <scord/scord.hpp>

struct query_config {
    std::string progname;
    std::string server_address;
    std::uint32_t job_id{};
};

query_config
parse_command_line(int argc, char* argv[]) {

    query_config cfg;

    cfg.progname = std::filesystem::path{argv[0]}.filename().string();

    CLI::App app{"Scord metainfo client", cfg.progname};

    app.add_option("-s,--server", cfg.server_address, "Server address")
            ->option_text("ADDRESS")
            ->required();
    app.add_option("job_id", cfg.job_id, "Job ID")->required();

    try {
        app.parse(argc, argv);
        return cfg;
    } catch(const CLI::ParseError& ex) {
        std::exit(app.exit(ex));
    }
}

auto
parse_address(const std::string& address) {
    const auto pos = address.find("://");
    if(pos == std::string::npos) {
        throw std::runtime_error(fmt::format("Invalid address: {}", address));
    }

    const auto protocol = address.substr(0, pos);
    return std::make_pair(protocol, address);
}


int
main(int argc, char* argv[]) {

    using namespace std::chrono_literals;

    query_config cfg = parse_command_line(argc, argv);

    try {
        const auto [protocol, address] = parse_address(cfg.server_address);

        scord::server srv{protocol, address};

        scord::job_info info =
                scord::query(scord::server{protocol, address}, cfg.job_id);

        fmt::print(stdout,
                   "Job metadata:\n"
                   "  adhoc_controller_address: {}\n"
                   "  io_procs: {}\n",
                   info.adhoc_controller_address(), info.io_procs());


    } catch(const std::exception& ex) {
        fmt::print(stderr, "Error: {}\n", ex.what());
        return EXIT_FAILURE;
    }
}
Loading