This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Create new "aktualizr-get" command #1276
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
add_executable(aktualizr-get main.cc get.cc) | ||
target_link_libraries(aktualizr-get aktualizr_static_lib ${AKTUALIZR_EXTERNAL_LIBS}) | ||
|
||
install(TARGETS aktualizr-get RUNTIME DESTINATION bin COMPONENT aktualizr-get) | ||
|
||
add_aktualizr_test(NAME aktualizr_get | ||
SOURCES get.cc get_test.cc | ||
PROJECT_WORKING_DIRECTORY) | ||
|
||
aktualizr_source_file_checks(main.cc get.cc get.h get_test.cc) | ||
|
||
# vim: set tabstop=4 shiftwidth=4 expandtab: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "get.h" | ||
#include "crypto/keymanager.h" | ||
#include "http/httpclient.h" | ||
|
||
std::string aktualizrGet(Config &config, const std::string url) { | ||
auto storage = INvStorage::newStorage(config.storage); | ||
storage->importData(config.import); | ||
|
||
auto client = std_::make_unique<HttpClient>(); | ||
KeyManager keys(storage, config.keymanagerConfig()); | ||
keys.copyCertsToCurl(*client); | ||
auto resp = client->get(url, HttpInterface::kNoLimit); | ||
if (resp.http_status_code != 200) { | ||
throw std::runtime_error("Unable to get " + url + ": HTTP_" + std::to_string(resp.http_status_code) + "\n" + | ||
resp.body); | ||
} | ||
return resp.body; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef AKTUALIZR_GET_HELPERS | ||
#define AKTUALIZR_GET_HELPERS | ||
|
||
#include "config/config.h" | ||
|
||
std::string aktualizrGet(Config &config, std::string url); | ||
|
||
#endif // AKTUALIZR_GET_HELPERS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <boost/process.hpp> | ||
|
||
#include "get.h" | ||
#include "test_utils.h" | ||
|
||
static std::string server = "http://127.0.0.1:"; | ||
|
||
TEST(aktualizr_get, good) { | ||
Config config; | ||
TemporaryDirectory dir; | ||
config.storage.path = dir.Path(); | ||
|
||
std::string body = aktualizrGet(config, server + "/path/1/2/3"); | ||
EXPECT_EQ("{\"path\": \"/path/1/2/3\"}", body); | ||
} | ||
|
||
#ifndef __NO_MAIN__ | ||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
|
||
std::string port = TestUtils::getFreePort(); | ||
server += port; | ||
boost::process::child server_process("tests/fake_http_server/fake_test_server.py", port); | ||
TestUtils::waitForServer(server + "/"); | ||
return RUN_ALL_TESTS(); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <unistd.h> | ||
#include <iostream> | ||
|
||
#include <openssl/ssl.h> | ||
#include <boost/filesystem.hpp> | ||
#include <boost/program_options.hpp> | ||
|
||
#include "config/config.h" | ||
#include "get.h" | ||
|
||
#include "utilities/aktualizr_version.h" | ||
|
||
namespace bpo = boost::program_options; | ||
|
||
bpo::variables_map parse_options(int argc, char *argv[]) { | ||
bpo::options_description description("aktualizr-get command line options"); | ||
// clang-format off | ||
// Try to keep these options in the same order as Config::updateFromCommandLine(). | ||
// The first three are commandline only. | ||
description.add_options() | ||
("help,h", "print usage") | ||
("version,v", "Current aktualizr version") | ||
("config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(), "configuration file or directory") | ||
("loglevel", bpo::value<int>(), "set log level 0-5 (trace, debug, info, warning, error, fatal)") | ||
("url,u", bpo::value<std::string>(), "url to get"); | ||
// clang-format on | ||
|
||
bpo::variables_map vm; | ||
std::vector<std::string> unregistered_options; | ||
try { | ||
bpo::basic_parsed_options<char> parsed_options = bpo::command_line_parser(argc, argv).options(description).run(); | ||
bpo::store(parsed_options, vm); | ||
bpo::notify(vm); | ||
if (vm.count("help") != 0) { | ||
std::cout << description << '\n'; | ||
exit(EXIT_SUCCESS); | ||
} | ||
|
||
} catch (const bpo::required_option &ex) { | ||
// print the error and append the default commandline option description | ||
std::cout << ex.what() << std::endl << description; | ||
exit(EXIT_FAILURE); | ||
} catch (const bpo::error &ex) { | ||
std::cout << ex.what() << std::endl; | ||
std::cout << description; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
return vm; | ||
} | ||
|
||
int main(int argc, char *argv[]) { | ||
logger_init(isatty(1) == 1); | ||
logger_set_threshold(boost::log::trivial::info); | ||
|
||
bpo::variables_map commandline_map = parse_options(argc, argv); | ||
|
||
int r = EXIT_FAILURE; | ||
try { | ||
if (geteuid() != 0) { | ||
LOG_WARNING << "\033[31mRunning as non-root and may not work as expected!\033[0m\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that true? I don't think it applies to this tool. |
||
} | ||
|
||
Config config(commandline_map); | ||
if (config.logger.loglevel <= boost::log::trivial::debug) { | ||
SSL_load_error_strings(); | ||
} | ||
|
||
std::string body = aktualizrGet(config, commandline_map["url"].as<std::string>()); | ||
std::cout << body; | ||
|
||
r = EXIT_SUCCESS; | ||
} catch (const std::exception &ex) { | ||
LOG_ERROR << ex.what(); | ||
} | ||
return r; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be useful to either print something here or at least have a comment to explain what the tool is for.