This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
#include <filesystem>
|
||||
#include <libassert/assert.hpp>
|
||||
#include "servers.hpp"
|
||||
#include <sys/stat.h>
|
||||
|
||||
namespace dropshell
|
||||
{
|
||||
@@ -176,49 +177,107 @@ namespace dropshell
|
||||
return trim(result);
|
||||
}
|
||||
|
||||
int configure_autocomplete()
|
||||
{
|
||||
debug << "Ensuring dropshell autocomplete is registered in ~/.bashrc..." << std::endl;
|
||||
|
||||
std::filesystem::path bashrc = localpath::current_user_home() +"/.bashrc";
|
||||
|
||||
std::string autocomplete_script = R"(
|
||||
#---DROPSHELL AUTOCOMPLETE START---
|
||||
_dropshell_completions() {
|
||||
local cur
|
||||
COMPREPLY=()
|
||||
cur="${COMP_WORDS[COMP_CWORD]}"
|
||||
|
||||
# call dropshell to get the list of possiblities for the current argument. Supply all previous arguments.
|
||||
local completions=($(dropshell autocomplete "${COMP_WORDS[@]:1:${COMP_CWORD}-1}"))
|
||||
COMPREPLY=( $(compgen -W "${completions[*]}" -- ${cur}) )
|
||||
return 0
|
||||
}
|
||||
|
||||
# Register the completion function
|
||||
complete -F _dropshell_completions dropshell
|
||||
complete -F _dropshell_completions ds
|
||||
#---DROPSHELL AUTOCOMPLETE END---
|
||||
)";
|
||||
|
||||
file_replace_or_add_segment(bashrc.string(), autocomplete_script);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int configure_localbin()
|
||||
{
|
||||
debug << "Ensuring ~/.local/bin is in the ~/.bashrc path..." << std::endl;
|
||||
|
||||
std::filesystem::path bashrc = localpath::current_user_home() +"/.bashrc";
|
||||
std::filesystem::path localbin = localpath::current_user_home() + "/.local/bin";
|
||||
std::filesystem::create_directories(localbin);
|
||||
// check if already in path
|
||||
const char* env_p = std::getenv("PATH");
|
||||
if (env_p) {
|
||||
std::string path_str = env_p;
|
||||
if (path_str.find(localbin.string()) == std::string::npos) {
|
||||
std::string pathstr="#---DROPSHELL PATH START---\nexport PATH=\""+localbin.string()+":$PATH\"\n#---DROPSHELL PATH END---\n";
|
||||
file_replace_or_add_segment(bashrc.string(), pathstr);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int update_dropshell()
|
||||
{
|
||||
maketitle("Updating dropshell on this computer...");
|
||||
|
||||
configure_localbin();
|
||||
configure_autocomplete();
|
||||
|
||||
// determine path to this executable
|
||||
std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe");
|
||||
std::filesystem::path parent_path = dropshell_path.parent_path();
|
||||
std::filesystem::path exe_path = std::filesystem::canonical("/proc/self/exe");
|
||||
std::filesystem::path parent_path = exe_path.parent_path();
|
||||
|
||||
// determine the architecture of the system
|
||||
std::string arch = shared_commands::get_arch();
|
||||
|
||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||
|
||||
// download new version, preserve permissions and ownership
|
||||
std::string bash_script;
|
||||
bash_script += "docker run --rm -v " + parent_path.string() + ":/target";
|
||||
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
||||
bash_script += " sh -c \"";
|
||||
bash_script += " curl -fsSL " + url + " -o /target/dropshell_temp &&";
|
||||
bash_script += " chmod --reference=/target/dropshell /target/dropshell_temp &&";
|
||||
bash_script += " chown --reference=/target/dropshell /target/dropshell_temp";
|
||||
bash_script += "\"";
|
||||
// check that the user that owns the exe is the current user this process is running as.
|
||||
struct stat st;
|
||||
if (stat(exe_path.c_str(), &st) != 0) {
|
||||
error << "Failed to stat dropshell executable: " << strerror(errno) << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string cmd = "bash -c '" + bash_script + "'";
|
||||
int rval = system(cmd.c_str());
|
||||
if (rval != 0)
|
||||
uid_t current_uid = getuid();
|
||||
if (st.st_uid != current_uid) {
|
||||
warning << "Current user does not own the dropshell executable. Please run as the owner to update." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
shared_commands::cLocalTempFolder local_temp_folder;
|
||||
std::filesystem::path temp_file = local_temp_folder.path() / "dropshell";
|
||||
bool download_okay = download_file(url, temp_file);
|
||||
if (!download_okay)
|
||||
{
|
||||
error << "Failed to download new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// make executable
|
||||
chmod(temp_file.c_str(), 0755);
|
||||
|
||||
// check if the new version is the same as the old version
|
||||
uint64_t new_hash = hash_file(parent_path / "dropshell_temp");
|
||||
uint64_t old_hash = hash_file(parent_path / "dropshell");
|
||||
uint64_t new_hash = hash_file(temp_file);
|
||||
uint64_t old_hash = hash_file(exe_path);
|
||||
if (new_hash == old_hash)
|
||||
{
|
||||
info << "Confirmed dropshell is the latest version." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string runvercmd = (parent_path / "dropshell").string() + " version";
|
||||
std::string runvercmd = exe_path.string() + " version";
|
||||
std::string currentver = _exec(runvercmd.c_str());
|
||||
runvercmd = (parent_path / "dropshell_temp").string() + " version";
|
||||
runvercmd = temp_file.string() + " version";
|
||||
std::string newver = _exec(runvercmd.c_str());
|
||||
|
||||
if (currentver >= newver)
|
||||
@@ -228,18 +287,12 @@ namespace dropshell
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
// move the new version to the old version.
|
||||
std::filesystem::rename(exe_path, exe_path.parent_path() / "dropshell.old");
|
||||
std::filesystem::rename(temp_file, exe_path);
|
||||
|
||||
std::string bash_script_2 = "docker run --rm -v " + parent_path.string() + ":/target gitea.jde.nz/public/debian-curl:latest " +
|
||||
"sh -c \"mv /target/dropshell_temp /target/dropshell\"";
|
||||
rval = system(bash_script_2.c_str());
|
||||
if (rval != 0)
|
||||
{
|
||||
error << "Failed to install new version of dropshell." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
info << "Successfully updated " << dropshell_path << " to the latest " << arch << " version." << std::endl;
|
||||
// remove the old version.
|
||||
std::filesystem::remove(exe_path.parent_path() / "dropshell.old");
|
||||
|
||||
// execute the new version
|
||||
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
|
||||
@@ -316,8 +369,6 @@ namespace dropshell
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
||||
return 0;
|
||||
|
||||
rval = install_local_agent();
|
||||
if (rval != 0)
|
||||
return rval;
|
||||
|
@@ -107,6 +107,22 @@ namespace dropshell
|
||||
return mPath;
|
||||
}
|
||||
|
||||
|
||||
|
||||
cLocalTempFolder::cLocalTempFolder()
|
||||
{
|
||||
mPath = std::filesystem::temp_directory_path() / random_alphanumeric_string(10);
|
||||
std::filesystem::create_directories(mPath);
|
||||
}
|
||||
cLocalTempFolder::~cLocalTempFolder()
|
||||
{
|
||||
std::filesystem::remove_all(mPath);
|
||||
}
|
||||
std::filesystem::path cLocalTempFolder::path() const
|
||||
{
|
||||
return mPath;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// get_all_services_status : SHARED COMMAND
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
@@ -1,6 +1,8 @@
|
||||
#ifndef SHARED_COMMANDS_HPP
|
||||
#define SHARED_COMMANDS_HPP
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#include "servers.hpp"
|
||||
#include "command_registry.hpp"
|
||||
#include "servers.hpp"
|
||||
@@ -40,6 +42,16 @@ namespace dropshell
|
||||
std::string mUser;
|
||||
};
|
||||
|
||||
class cLocalTempFolder
|
||||
{
|
||||
public:
|
||||
cLocalTempFolder(); // create a temp folder on the local machine
|
||||
~cLocalTempFolder(); // delete the temp folder on the local machine
|
||||
std::filesystem::path path() const; // get the path to the temp folder on the local machine
|
||||
private:
|
||||
std::filesystem::path mPath;
|
||||
};
|
||||
|
||||
bool rsync_tree_to_remote(
|
||||
const std::string &local_path,
|
||||
const std::string &remote_path,
|
||||
|
10509
source/src/contrib/httplib.hpp
Normal file
10509
source/src/contrib/httplib.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,8 @@
|
||||
#include "utils.hpp"
|
||||
#include "httplib.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
@@ -10,6 +14,7 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <cctype>
|
||||
#include <sstream>
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@@ -445,4 +450,183 @@ std::string tolower(const std::string& str) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Common utility function to make HTTP requests
|
||||
struct HttpResult {
|
||||
bool success;
|
||||
int status;
|
||||
std::string body;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
HttpResult make_http_request(const std::string& url) {
|
||||
try {
|
||||
// Parse the URL to get host and path
|
||||
std::string host;
|
||||
std::string path;
|
||||
size_t protocol_end = url.find("://");
|
||||
if (protocol_end != std::string::npos) {
|
||||
size_t host_start = protocol_end + 3;
|
||||
size_t path_start = url.find('/', host_start);
|
||||
if (path_start != std::string::npos) {
|
||||
host = url.substr(host_start, path_start - host_start);
|
||||
path = url.substr(path_start);
|
||||
} else {
|
||||
host = url.substr(host_start);
|
||||
path = "/";
|
||||
}
|
||||
} else {
|
||||
return {false, 0, "", "Invalid URL format"};
|
||||
}
|
||||
|
||||
// Create HTTP client
|
||||
httplib::Client cli(host);
|
||||
cli.set_connection_timeout(10); // 10 second timeout
|
||||
|
||||
// Make GET request
|
||||
auto res = cli.Get(path);
|
||||
if (!res) {
|
||||
return {false, 0, "", "Failed to connect to server"};
|
||||
}
|
||||
if (res->status != 200) {
|
||||
return {false, res->status, "", "HTTP request failed with status " + std::to_string(res->status)};
|
||||
}
|
||||
|
||||
return {true, res->status, res->body, ""};
|
||||
} catch (const std::exception& e) {
|
||||
return {false, 0, "", std::string("Exception: ") + e.what()};
|
||||
}
|
||||
}
|
||||
|
||||
bool download_file(const std::string &url, const std::string &destination) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to download file from URL: " << url << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
std::ofstream out_file(destination, std::ios::binary);
|
||||
if (!out_file) {
|
||||
warning << "Failed to open file for writing: " << destination << std::endl;
|
||||
return false;
|
||||
}
|
||||
out_file.write(result.body.c_str(), result.body.size());
|
||||
out_file.close();
|
||||
return true;
|
||||
} catch (const std::exception& e) {
|
||||
warning << "Failed to download file from URL: " << url << std::endl;
|
||||
warning << "Exception: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json get_json_from_url(const std::string &url) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to get JSON from URL: " << url << std::endl;
|
||||
return nlohmann::json();
|
||||
}
|
||||
|
||||
try {
|
||||
return nlohmann::json::parse(result.body);
|
||||
} catch (const nlohmann::json::parse_error& e) {
|
||||
warning << "Failed to parse JSON from URL: " << url << std::endl;
|
||||
warning << "JSON: " << result.body << std::endl;
|
||||
return nlohmann::json();
|
||||
}
|
||||
}
|
||||
|
||||
std::string get_string_from_url(const std::string &url) {
|
||||
auto result = make_http_request(url);
|
||||
if (!result.success) {
|
||||
warning << "Failed to get string from URL: " << url << std::endl;
|
||||
return std::string();
|
||||
}
|
||||
return result.body;
|
||||
}
|
||||
|
||||
bool match_line(const std::string &line, const std::string &pattern) {
|
||||
return trim(line) == trim(pattern);
|
||||
}
|
||||
|
||||
// replace or append a block of text to a file, matching first and last lines if replacing.
|
||||
// edits file in-place.
|
||||
bool file_replace_or_add_segment(std::string filepath, std::string segment)
|
||||
{
|
||||
std::string first_line = segment.substr(0, segment.find("\n"));
|
||||
|
||||
// look backwards until we get a non-empty line.
|
||||
size_t last_line_pos = segment.rfind("\n");
|
||||
while (last_line_pos != std::string::npos) {
|
||||
std::string last_line = segment.substr(last_line_pos + 1);
|
||||
if (!trim(last_line).empty()) {
|
||||
break;
|
||||
}
|
||||
last_line_pos = segment.rfind("\n", last_line_pos - 1);
|
||||
}
|
||||
std::string last_line = segment.substr(last_line_pos + 1);
|
||||
|
||||
// Read the entire file into memory
|
||||
std::ifstream input_file(filepath);
|
||||
if (!input_file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> file_lines;
|
||||
std::string line;
|
||||
while (std::getline(input_file, line)) {
|
||||
file_lines.push_back(line);
|
||||
}
|
||||
input_file.close();
|
||||
|
||||
// Try to find the matching block
|
||||
bool found_match = false;
|
||||
for (size_t i = 0; i < file_lines.size(); i++) {
|
||||
if (match_line(file_lines[i], first_line)) {
|
||||
// Found potential start, look for end
|
||||
for (size_t j = i + 1; j < file_lines.size(); j++) {
|
||||
if (match_line(file_lines[j], last_line)) {
|
||||
// Found matching block, replace it
|
||||
file_lines.erase(file_lines.begin() + i, file_lines.begin() + j + 1);
|
||||
|
||||
// Split segment into lines and insert them
|
||||
std::vector<std::string> segment_lines;
|
||||
std::istringstream segment_stream(segment);
|
||||
while (std::getline(segment_stream, line)) {
|
||||
segment_lines.push_back(line);
|
||||
}
|
||||
file_lines.insert(file_lines.begin() + i, segment_lines.begin(), segment_lines.end());
|
||||
|
||||
found_match = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found_match) break;
|
||||
}
|
||||
}
|
||||
|
||||
// If no match found, append the segment
|
||||
if (!found_match) {
|
||||
std::istringstream segment_stream(segment);
|
||||
while (std::getline(segment_stream, line)) {
|
||||
file_lines.push_back(line);
|
||||
}
|
||||
}
|
||||
|
||||
// Write back to file
|
||||
std::ofstream output_file(filepath);
|
||||
if (!output_file.is_open()) {
|
||||
std::cerr << "Error: Unable to open file for writing: " << filepath << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto& line : file_lines) {
|
||||
output_file << line << "\n";
|
||||
}
|
||||
output_file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace dropshell
|
@@ -5,6 +5,7 @@
|
||||
#include <map>
|
||||
|
||||
#include "output.hpp"
|
||||
#include "json.hpp"
|
||||
|
||||
namespace dropshell {
|
||||
|
||||
@@ -61,4 +62,11 @@ std::string get_line_wrap(std::string & src, int maxchars);
|
||||
|
||||
std::string tolower(const std::string& str);
|
||||
|
||||
bool download_file(const std::string& url, const std::string& destination);
|
||||
nlohmann::json get_json_from_url(const std::string& url);
|
||||
std::string get_string_from_url(const std::string& url);
|
||||
|
||||
// replace or append a block of text to a file, matching first and last lines if replacing.
|
||||
bool file_replace_or_add_segment(std::string filepath, std::string segment);
|
||||
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user