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,
|
||||
|
Reference in New Issue
Block a user