Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uenv image find #18

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ curl_dep = dependency('libcurl', required: true)
# the lib dependency is all of the common funtionality shared between the CLI
# and the slurm plugin.
lib_src = [
'src/uenv/cscs.cpp',
'src/site/site.cpp',
'src/uenv/env.cpp',
'src/uenv/envvars.cpp',
'src/uenv/lex.cpp',
Expand All @@ -42,6 +42,7 @@ lib_src = [
'src/uenv/parse.cpp',
'src/uenv/repository.cpp',
'src/uenv/uenv.cpp',
'src/util/curl.cpp',
'src/util/fs.cpp',
'src/util/shell.cpp',
'src/util/strings.cpp',
Expand All @@ -62,10 +63,12 @@ if uenv_cli
uenv_src = [
'src/cli/add_remove.cpp',
'src/cli/color.cpp',
'src/cli/find.cpp',
'src/cli/help.cpp',
'src/cli/image.cpp',
'src/cli/inspect.cpp',
'src/cli/ls.cpp',
'src/cli/print.cpp',
'src/cli/repo.cpp',
'src/cli/run.cpp',
'src/cli/start.cpp',
Expand Down
1 change: 0 additions & 1 deletion src/cli/add_remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <fmt/std.h>
#include <spdlog/spdlog.h>

#include <uenv/cscs.h>
#include <uenv/parse.h>
#include <uenv/repository.h>
#include <util/expected.h>
Expand Down
121 changes: 121 additions & 0 deletions src/cli/find.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// vim: ts=4 sts=4 sw=4 et

#include <string>

#include <fmt/core.h>
#include <fmt/ranges.h>
#include <fmt/std.h>
#include <nlohmann/json.hpp>
#include <spdlog/spdlog.h>

#include <site/site.h>
#include <uenv/parse.h>
#include <uenv/repository.h>
#include <util/curl.h>
#include <util/expected.h>

#include "find.h"
#include "help.h"
#include "print.h"

namespace uenv {

std::string image_find_footer();

void image_find_args::add_cli(CLI::App& cli,
[[maybe_unused]] global_settings& settings) {
auto* find_cli =
cli.add_subcommand("find", "search for uenv that can be pulled");
find_cli->add_option("uenv", uenv_description, "search term");
find_cli->add_flag("--no-header", no_header,
"print only the matching records, with no header.");
find_cli->add_option("-n,--namespace", nspace,
"the namespace in which to search (default 'deploy')");
find_cli->callback(
[&settings]() { settings.mode = uenv::cli_mode::image_find; });

find_cli->footer(image_find_footer);
}

int image_find([[maybe_unused]] const image_find_args& args,
[[maybe_unused]] const global_settings& settings) {
// find the search term that was provided by the user
uenv_label label{};
if (args.uenv_description) {
if (const auto parse = parse_uenv_label(*args.uenv_description)) {
label = *parse;
} else {
spdlog::error("invalid search term: {}", parse.error().message());
return 1;
}
}
label.system = site::get_system_name(label.system);

spdlog::info("image_find: {}::{}", args.nspace, label);

auto store = site::get_remote_listing(args.nspace);
if (!store) {
spdlog::error("unable to get a listing of the uenv", store.error());
return 1;
}

// search db for matching records
const auto result = store->query(label);
if (!result) {
spdlog::error("invalid search term: {}", store.error());
return 1;
}

// pass results to print
print_record_list(*result, args.no_header);

return 0;
}

std::string image_find_footer() {
using enum help::block::admonition;
std::vector<help::item> items{
// clang-format off
help::block{none, "Search for uenv that are available to pull." },
help::linebreak{},
help::block{xmpl, "find all uenv"},
help::block{code, "uenv image find"},
help::linebreak{},
help::block{xmpl, "find all uenv with the name prgenv-gnu"},
help::block{code, "uenv image find prgenv-gnu"},
help::linebreak{},
help::block{xmpl, "find all uenv with the name prgenv-gnu and version 24.7"},
help::block{code, "uenv image find prgenv-gnu/24.7"},
help::linebreak{},
help::block{xmpl, "find all uenv with the name prgenv-gnu, version 24.7 and release v2"},
help::block{code, "uenv image find prgenv-gnu/24.7:v2"},
help::linebreak{},
help::block{xmpl, "use the @ symbol to specify a target system name"},
help::block{code, "uenv image find prgenv-gnu@todi"},
help::block{none, "this feature is useful when using images that were built for a different system",
"than the one you are currently working on."},
help::linebreak{},
help::block{xmpl, "use the @ symbol to specify a target system name"},
help::block{code, "uenv image find prgenv-gnu@todi"},
help::block{none, "this feature is useful when using images that were built for a different system",
"than the one you are currently working on."},
help::linebreak{},
help::block{xmpl, "use the % symbol to specify a target microarchitecture (uarch)"},
help::block{code, "uenv image find prgenv-gnu%gh200"},
help::block{none, "this feature is useful on a system with multiple uarch."},
help::linebreak{},
help::block{xmpl, "list any uenv with a concrete sha256 checksum"},
help::block{code, "uenv image find 510094ddb3484e305cb8118e21cbb9c94e9aff2004f0d6499763f42bdafccfb5"},
help::linebreak{},
help::block{note, "more than one uenv might be listed if there are two uenv that refer",
"to the same underlying uenv sha256."},
help::linebreak{},
help::block{xmpl, "search for uenv by id (id is the first 16 characters of the sha256):"},
help::block{code, "uenv image find 510094ddb3484e30"},
// clang-format on
};

return fmt::format("{}", fmt::join(items, "\n"));
}

} // namespace uenv
39 changes: 39 additions & 0 deletions src/cli/find.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
// vim: ts=4 sts=4 sw=4 et

#include <string>

#include <CLI/CLI.hpp>
#include <fmt/core.h>

#include <uenv/env.h>

#include "uenv.h"

namespace uenv {

struct image_find_args {
std::optional<std::string> uenv_description;
bool no_header = false;
std::string nspace = "deploy";
void add_cli(CLI::App&, global_settings& settings);
};

int image_find(const image_find_args& args, const global_settings& settings);

} // namespace uenv

template <> class fmt::formatter<uenv::image_find_args> {
public:
// parse format specification and store it:
constexpr auto parse(format_parse_context& ctx) {
return ctx.end();
}
// format a value using stored specification:
template <typename FmtContext>
constexpr auto format(uenv::image_find_args const& opts,
FmtContext& ctx) const {
return fmt::format_to(ctx.out(), "{{uenv: '{}'}}",
opts.uenv_description);
}
};
3 changes: 3 additions & 0 deletions src/cli/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void image_args::add_cli(CLI::App& cli,
// add the `uenv image inspect` command
inspect_args.add_cli(*image_cli, settings);

// add the `uenv image find` command
find_args.add_cli(*image_cli, settings);

image_cli->footer(image_footer);
}

Expand Down
6 changes: 4 additions & 2 deletions src/cli/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <fmt/core.h>

#include "add_remove.h"
#include "find.h"
#include "inspect.h"
#include "ls.h"
#include "uenv.h"
Expand All @@ -14,10 +15,11 @@ namespace uenv {
void image_help();

struct image_args {
image_ls_args ls_args;
image_add_args add_args;
image_remove_args remove_args;
image_find_args find_args;
image_inspect_args inspect_args;
image_ls_args ls_args;
image_remove_args remove_args;
void add_cli(CLI::App&, global_settings& settings);
};

Expand Down
4 changes: 2 additions & 2 deletions src/cli/inspect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <fmt/std.h>
#include <spdlog/spdlog.h>

#include <uenv/cscs.h>
#include <site/site.h>
#include <uenv/parse.h>
#include <uenv/repository.h>
#include <util/expected.h>
Expand Down Expand Up @@ -61,7 +61,7 @@ int image_inspect([[maybe_unused]] const image_inspect_args& args,

// set label->system to the current cluster name if it has not
// already been set.
label.system = cscs::get_system_name(label.system);
label.system = site::get_system_name(label.system);

// query the repo
const auto result = store->query(label);
Expand Down
47 changes: 6 additions & 41 deletions src/cli/ls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#include <fmt/std.h>
#include <spdlog/spdlog.h>

#include <uenv/cscs.h>
#include <site/site.h>
#include <uenv/parse.h>
#include <uenv/repository.h>
#include <util/expected.h>

#include "help.h"
#include "ls.h"
#include "print.h"

namespace uenv {

Expand All @@ -23,8 +24,7 @@ void image_ls_args::add_cli(CLI::App& cli,
[[maybe_unused]] global_settings& settings) {
auto* ls_cli =
cli.add_subcommand("ls", "search for uenv that are available to run");
ls_cli->add_option("uenv", uenv_description,
"comma separated list of uenv to mount.");
ls_cli->add_option("uenv", uenv_description, "search term");
ls_cli->add_flag("--no-header", no_header,
"print only the matching records, with no header.");
ls_cli->callback(
Expand All @@ -34,9 +34,6 @@ void image_ls_args::add_cli(CLI::App& cli,
}

int image_ls(const image_ls_args& args, const global_settings& settings) {
spdlog::info("image ls {}",
args.uenv_description ? *args.uenv_description : "none");

// get the repo and handle errors if it does not exist
if (!settings.repo) {
spdlog::error(
Expand Down Expand Up @@ -65,7 +62,7 @@ int image_ls(const image_ls_args& args, const global_settings& settings) {

// set label->system to the current cluster name if it has not
// already been set.
label.system = cscs::get_system_name(label.system);
label.system = site::get_system_name(label.system);

// query the repo
const auto result = store->query(label);
Expand All @@ -74,36 +71,7 @@ int image_ls(const image_ls_args& args, const global_settings& settings) {
return 1;
}

if (result->empty()) {
if (!args.no_header) {
fmt::println("no matching uenv");
}
return 0;
}

// print the results
std::size_t w_name = std::string_view("uenv").size();
std::size_t w_sys = std::string_view("system").size();
std::size_t w_arch = std::string_view("arch").size();

for (auto& r : *result) {
w_name = std::max(
w_name, fmt::format("{}/{}:{}", r.name, r.version, r.tag).size());
w_sys = std::max(w_sys, r.system.size());
w_arch = std::max(w_arch, r.uarch.size());
}
++w_name;
++w_sys;
++w_arch;
if (!args.no_header) {
fmt::println("{:<{}}{:<{}}{:<{}}{:<17}{:<10}", "uenv", w_name, "arch",
w_arch, "system", w_sys, "id", "date");
}
for (auto& r : *result) {
auto name = fmt::format("{}/{}:{}", r.name, r.version, r.tag);
fmt::println("{:<{}}{:<{}}{:<{}}{:<17}{:s}", name, w_name, r.uarch,
w_arch, r.system, w_sys, r.id.string(), r.date);
}
print_record_list(*result, args.no_header);

return 0;
}
Expand All @@ -117,9 +85,6 @@ std::string image_ls_footer() {
help::block{xmpl, "list all uenv"},
help::block{code, "uenv image ls"},
help::linebreak{},
help::block{xmpl, "list all uenv"},
help::block{code, "uenv image ls"},
help::linebreak{},
help::block{xmpl, "list all uenv with the name prgenv-gnu"},
help::block{code, "uenv image ls prgenv-gnu"},
help::linebreak{},
Expand All @@ -144,7 +109,7 @@ std::string image_ls_footer() {
help::block{none, "this feature is useful on a system with multiple uarch."},
help::linebreak{},
help::block{xmpl, "list any uenv with a concrete sha256 checksum"},
help::block{code, "uenv image ls 510094ddb3484e305cb8118e21cbb9c94e9aff2004f0d6499763f42" "bdafccfb5"},
help::block{code, "uenv image ls 510094ddb3484e305cb8118e21cbb9c94e9aff2004f0d6499763f42bdafccfb5"},
help::linebreak{},
help::block{note, "more than one uenv might be listed if there are two uenv that refer",
"to the same underlying uenv sha256."},
Expand Down
Loading