Basic debian put. Unverified.

This commit is contained in:
Kyle Isom 2023-10-23 22:44:49 -07:00
parent 4f7c5519e2
commit fd93a937a4
2 changed files with 80 additions and 1 deletions

View File

@ -53,7 +53,7 @@ include_directories(SYSTEM ${CURLPP_INCLUDE_DIRS})
### THE REAL TARGETS ###
add_executable(${PROJECT_NAME}-packager src/packager.cc ${SOURCES} ${HEADERS})
target_link_libraries(${PROJECT_NAME}-packager ${SCSL_LIBRARIES})
target_link_libraries(${PROJECT_NAME}-packager ${SCSL_LIBRARIES} ${CURLPP_LIBRARIES})
### INCLUDES ###

View File

@ -9,11 +9,21 @@
#include <iostream>
#include <string>
#include <curlpp/Easy.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>
#include <fstream>
#include <scsl/Commander.h>
#include <scsl/Flags.h>
#include <scsl/SimpleConfig.h>
const auto defaultOwner = std::string("sc");
const auto defaultDistribution = std::string("ubuntu");
const auto defaultComponent = std::string("main");
void
usage(std::ostream &outs, int exc)
{
@ -52,13 +62,82 @@ getDefaultConfigFile()
return userHome;
}
static std::string
debianEndpoint(std::string endpoint)
{
if (!endpoint.empty() && endpoint.at(0) != '/') {
endpoint.insert(endpoint.cbegin(), '/');
}
auto urlBase = scsl::SimpleConfig::GetGlobal("server");
return urlBase + "/api/packages/" +
scsl::SimpleConfig::GetGlobal("owner", defaultOwner) +
"/debian/pool/" +
scsl::SimpleConfig::GetGlobal("distribution", defaultDistribution) +
"/" +
scsl::SimpleConfig::GetGlobal("component", defaultComponent) +
endpoint;
}
static std::string
getUserPass()
{
return scsl::SimpleConfig::GetGlobal("http_user") + ":" +
scsl::SimpleConfig::GetGlobal("http_password");
}
static bool
putDebian(std::vector<std::string> args)
{
auto ok = true;
std::cout << "[+] put Debian package\n";
for (auto &arg: args) {
std::cout << "\t[*] " << arg << "\n";
}
auto url = debianEndpoint("upload");
auto userPass = getUserPass();
for (auto &arg: args) {
try {
std::list<std::string> header;
header.emplace_back("Content-Type: application/octet-stream");
curlpp::Cleanup cleaner;
curlpp::Easy request;
if (!std::filesystem::exists(arg)) {
std::cerr << "[!] " << arg << " does not exist!\n";
ok &= false;
continue;
}
auto fileSize = static_cast<long>(std::filesystem::file_size(arg));
std::ifstream debFile(arg);
if (!debFile.good()) {
std::cerr << "[!] " << arg << " does not exist!\n";
ok &= false;
continue;
}
request.setOpt(new curlpp::options::Url(url));
request.setOpt(new curlpp::options::Verbose(true));
request.setOpt(new curlpp::options::UserPwd(userPass));
request.setOpt(new curlpp::Options::ReadStream(&debFile));
request.setOpt(new curlpp::Options::InfileSize(fileSize));
request.setOpt(new curlpp::Options::Upload(true));
request.perform();
} catch (curlpp::LogicError &e) {
std::cout << e.what() << "\n";
ok &= false;
} catch (curlpp::RuntimeError &e) {
std::cerr << e.what() << "\n";
ok &= false;
}
std::cout << "[+] " << std::filesystem::path(arg).filename() << ": OK\n";
}
return false;
}