remove old unused code.
This commit is contained in:
parent
688f5da3e3
commit
ee63e65e36
@ -8,7 +8,6 @@ add_library(Universal
|
||||
FunctionTraits.h
|
||||
IoContext.h IoContext.cpp
|
||||
MessageManager.h MessageManager.inl MessageManager.cpp
|
||||
ProcessUtility.h ProcessUtility.inl ProcessUtility.cpp
|
||||
Singleton.h
|
||||
SingletonProcess.h SingletonProcess.cpp
|
||||
StreamFormat.h StreamFormat.inl StreamFormat.cpp
|
||||
|
@ -1,201 +0,0 @@
|
||||
#include "ProcessUtility.h"
|
||||
#include "BoostLog.h"
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <filesystem>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#ifdef __linux__
|
||||
#include <unistd.h>
|
||||
#elif defined(_WIN32)
|
||||
#include <boost/winapi/dll.hpp>
|
||||
#endif
|
||||
|
||||
bool DockerUtility::supervisorStatus(std::string_view container, const std::string &module) {
|
||||
using namespace boost::process;
|
||||
boost::process::ipstream ips;
|
||||
auto docker = search_path("docker");
|
||||
auto ctl = boost::process::search_path("supervisorctl");
|
||||
auto env = boost::this_process::environment();
|
||||
if (env.count("USER")) {
|
||||
auto user = env["USER"].to_string();
|
||||
system(docker, args = {"exec", "-u", user, container.data(), "supervisorctl", "status", module}, std_out > ips);
|
||||
std::string line;
|
||||
if (!ips) return false;
|
||||
std::getline(ips, line);
|
||||
if (line.empty()) return false;
|
||||
boost::algorithm::trim(line);
|
||||
std::vector<std::string> s;
|
||||
boost::algorithm::split(s, line, boost::algorithm::is_any_of(" "), boost::algorithm::token_compress_on);
|
||||
if (s.size() < 3) return false;
|
||||
return s.at(2) == "RUNNING";
|
||||
} else {
|
||||
LOG(error) << "this_process did not contain USER env value.";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DockerUtility::running(std::string_view container, std::error_code &error) {
|
||||
#ifdef __linux__
|
||||
using namespace boost::process;
|
||||
boost::process::ipstream stream;
|
||||
std::ostringstream oss;
|
||||
oss << "docker ps -q --filter \"name=" << container << "\" --filter \"status=running\"";
|
||||
system(oss.str(), boost::process::std_out > stream, error);
|
||||
if (error) return false;
|
||||
bool ret = false;
|
||||
std::string line;
|
||||
while (std::getline(stream, line) && !line.empty()) {
|
||||
ret = true;
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string NativeUtility::currentExecutable() {
|
||||
// win32 GetModuleFileNameA
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
char buffer[512] = {0};
|
||||
auto status = readlink("/proc/self/exe", buffer, sizeof(buffer));
|
||||
return status == -1 ? std::string() : std::string(buffer);
|
||||
#else
|
||||
char buffer[MAX_PATH];
|
||||
boost::winapi::get_module_file_name(nullptr, buffer, MAX_PATH);
|
||||
return buffer;
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string NativeUtility::executableDirectory() {
|
||||
auto path = currentExecutable();
|
||||
#if defined(__linux__) || defined(__APPLE__)
|
||||
auto slashPos = path.find_last_of("/");
|
||||
#else
|
||||
auto slashPos = path.find_last_of("\\");
|
||||
#endif
|
||||
return path.substr(0, slashPos);
|
||||
}
|
||||
|
||||
std::string NativeUtility::applicationDataDirectory(bool create) {
|
||||
std::string directory;
|
||||
|
||||
#ifdef WIN32
|
||||
DWORD bufferSize = GetEnvironmentVariable("APPDATA", 0, 0);
|
||||
char *buffer = new char[bufferSize + 2];
|
||||
GetEnvironmentVariable("APPDATA", buffer, bufferSize);
|
||||
directory.assign(buffer);
|
||||
delete[] buffer;
|
||||
#else
|
||||
const char *result = std::getenv("XDG_CONFIG_HOME");
|
||||
if (result && strlen(result)) {
|
||||
directory = std::string(result);
|
||||
} else {
|
||||
directory = std::string(std::getenv("HOME")) + std::string("/.config/");
|
||||
}
|
||||
#endif
|
||||
auto exePath = currentExecutable();
|
||||
auto slashPos = exePath.find_last_of("\\");
|
||||
auto dotPos = exePath.find_last_of(".");
|
||||
auto appName = exePath.substr(slashPos + 1, dotPos - slashPos - 1);
|
||||
std::ostringstream oss;
|
||||
oss << directory << "/" << appName << "/";
|
||||
directory = oss.str();
|
||||
if (create) {
|
||||
try {
|
||||
std::filesystem::path path(std::filesystem::u8path(directory));
|
||||
if (!std::filesystem::exists(path)) {
|
||||
std::filesystem::create_directories(path);
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
}
|
||||
return directory;
|
||||
}
|
||||
|
||||
std::string NativeUtility::homeDirectory() {
|
||||
std::string directory;
|
||||
#ifdef WIN32
|
||||
DWORD bufferSize = GetEnvironmentVariable("USERPROFILE", 0, 0);
|
||||
char *buffer = new char[bufferSize + 2];
|
||||
GetEnvironmentVariable("USERPROFILE", buffer, bufferSize);
|
||||
directory.assign(buffer);
|
||||
delete[] buffer;
|
||||
#else
|
||||
directory = std::string(std::getenv("HOME"));
|
||||
#endif
|
||||
return directory;
|
||||
}
|
||||
|
||||
bool NativeUtility::processRunning(const std::string &nameFilter) {
|
||||
boost::process::ipstream ips;
|
||||
// for unknow reason,on gdb debug mode,the columns is very short.
|
||||
boost::process::system("ps -eF --columns 999", boost::process::std_out > ips);
|
||||
std::string line;
|
||||
std::regex regex(nameFilter);
|
||||
while (std::getline(ips, line)) {
|
||||
if (line.empty()) continue;
|
||||
// LOG(debug) << "processRunning: " << line;
|
||||
if (std::regex_search(line, regex)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DockerUtility::executeBashCommand(const std::string &command) {
|
||||
std::ostringstream oss;
|
||||
oss << "bash -c \"" << command << "\"";
|
||||
boost::process::system("bash -c \" \"");
|
||||
|
||||
// ps -aux | grep rosbridge | awk '{print $2}' | xargs kill -s 9 2>/dev/null
|
||||
}
|
||||
|
||||
void DockerUtility::execute(const std::string_view &container, const std::string_view &command) {
|
||||
std::ostringstream oss;
|
||||
auto env = boost::this_process::environment();
|
||||
auto user = env["USER"].to_string();
|
||||
oss << "docker exec -u " << user << " -d " << container << " " << command;
|
||||
using namespace boost::process;
|
||||
system(oss.str());
|
||||
}
|
||||
|
||||
void DockerUtility::kill(const std::string &nameFilter) {
|
||||
std::ostringstream oss;
|
||||
oss << "ps -aux | grep " << nameFilter << " | awk '{print $2}' | xargs kill -s 9 2>/dev/null";
|
||||
executeBashCommand(oss.str());
|
||||
}
|
||||
|
||||
bool DockerUtility::containerExisted(const std::string_view &name) {
|
||||
boost::process::ipstream ips;
|
||||
std::ostringstream oss;
|
||||
oss << "docker ps -aq --filter name=" << name;
|
||||
boost::process::system(oss.str(), boost::process::std_out > ips);
|
||||
std::string line;
|
||||
while (std::getline(ips, line)) {
|
||||
if (line.empty()) continue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DockerUtility::supervisorctl(const std::string_view &container, const std::string &module, bool isStart) {
|
||||
using namespace boost::process;
|
||||
auto docker = search_path("docker");
|
||||
auto env = boost::this_process::environment();
|
||||
auto user = env["USER"].to_string();
|
||||
system(docker, args = {"exec", "-u", user, container.data(), "supervisorctl", isStart ? "start" : "stop", module});
|
||||
}
|
||||
|
||||
void DockerUtility::stop(const std::string &nameFilter) {
|
||||
using namespace boost::process;
|
||||
ipstream ips;
|
||||
std::ostringstream oss;
|
||||
oss << "docker ps -q --filter \"name=" << nameFilter << "\" --filter \"status=running\" ";
|
||||
system(oss.str(), std_out > ips);
|
||||
std::string id;
|
||||
auto docker = search_path("docker");
|
||||
while (std::getline(ips, id) && !id.empty()) {
|
||||
system(docker, "stop", args += {id});
|
||||
}
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
#ifndef DOCKERUTILITY_H
|
||||
#define DOCKERUTILITY_H
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
|
||||
/**
|
||||
* @brief used when app run on the native machine
|
||||
*/
|
||||
class DockerUtility {
|
||||
public:
|
||||
void stop(const std::string &nameFilter);
|
||||
static bool supervisorStatus(std::string_view container, const std::string &module);
|
||||
|
||||
template <typename... Args>
|
||||
static void runScript(std::string_view container, const std::string &script, Args &&...scriptArgs);
|
||||
|
||||
static bool running(std::string_view container, std::error_code &error);
|
||||
static void executeBashCommand(const std::string &command);
|
||||
static void execute(const std::string_view &container, const std::string_view &command);
|
||||
static void kill(const std::string &nameFilter);
|
||||
static bool containerExisted(const std::string_view &name);
|
||||
|
||||
/**
|
||||
* @brief supervisorctl
|
||||
* @param module
|
||||
* @param isStart true:start,false:stop
|
||||
*/
|
||||
static void supervisorctl(const std::string_view &container, const std::string &module, bool isStart);
|
||||
|
||||
private:
|
||||
DockerUtility() = default;
|
||||
};
|
||||
|
||||
class NativeUtility {
|
||||
public:
|
||||
using ShellCommand = std::true_type;
|
||||
template <typename... Args>
|
||||
static void runScript(const std::string &script, Args &&...scriptArgs);
|
||||
static bool processRunning(const std::string &nameFilter);
|
||||
static std::string currentExecutable();
|
||||
static std::string executableDirectory();
|
||||
static std::string applicationDataDirectory(bool create = true);
|
||||
static std::string homeDirectory();
|
||||
|
||||
private:
|
||||
NativeUtility() = default;
|
||||
};
|
||||
|
||||
#include "ProcessUtility.inl"
|
||||
|
||||
#endif // DOCKERUTILITY_H
|
@ -1,48 +0,0 @@
|
||||
#ifndef __PROCESSUTILITY_INL__
|
||||
#define __PROCESSUTILITY_INL__
|
||||
|
||||
#include "BoostLog.h"
|
||||
#include "ProcessUtility.h"
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <boost/process.hpp>
|
||||
#if __cplusplus >= 201703L
|
||||
#include <filesystem>
|
||||
#elif __cplusplus >= 201402L && defined(__GNUC__)
|
||||
#include <experimental/filesystem>
|
||||
#else
|
||||
#error "C++14 or later is required"
|
||||
#endif
|
||||
|
||||
template <typename... Args>
|
||||
void DockerUtility::runScript(std::string_view container, const std::string &script, Args &&...scriptArgs) {
|
||||
using namespace boost::process;
|
||||
auto env = boost::this_process::environment();
|
||||
auto user = env["USER"].to_string();
|
||||
auto docker = search_path("docker");
|
||||
system(docker,
|
||||
args = {"exec", "-d", "-u", user, container.data(), "bash", script, std::forward<Args>(scriptArgs)...});
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void NativeUtility::runScript(const std::string &script, Args &&...scriptArgs) {
|
||||
#if __cplusplus >= 201703L
|
||||
using namespace std;
|
||||
#elif __cplusplus >= 201402L && defined(__GNUC__)
|
||||
using namespace std::experimental;
|
||||
#else
|
||||
#error "C++14 or later is required"
|
||||
#endif
|
||||
|
||||
#if defined(WIN32) || defined(ANDROID)
|
||||
LOG(info) << "DockerUtility::runScript() not supported on windows or android.";
|
||||
#else
|
||||
auto askpass = filesystem::current_path().parent_path() / "Askpass/Askpassd.AppImage";
|
||||
auto env = boost::this_process::environment();
|
||||
env["SUDO_ASKPASS"] = askpass;
|
||||
|
||||
auto bash = boost::process::search_path("bash");
|
||||
boost::process::system(bash, boost::process::args = {script, std::forward<Args>(scriptArgs)...}, env);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // __PROCESSUTILITY_INL__
|
Loading…
x
Reference in New Issue
Block a user