Compare commits
21 Commits
2025.0524.
...
2025.0526.
Author | SHA1 | Date | |
---|---|---|---|
84fd96e74e | |||
8eb652b84e | |||
c8c0c3289c | |||
3dc82c682c | |||
4035538ba5 | |||
8ec4976cc0 | |||
1b35f74bfe | |||
08794e6480 | |||
3b51a511a6 | |||
e1be3dff8d | |||
9375acafa9 | |||
f45baa8362 | |||
d3ceb3f4be | |||
27a2d25fb2 | |||
e7558be416 | |||
8f06fc31ae | |||
1502d6e3d2 | |||
d71ba38754 | |||
f45d9a33ed | |||
73f85769a2 | |||
9a141685de |
@ -12,12 +12,16 @@ jobs:
|
|||||||
sudo apt-get install -y openssh-server
|
sudo apt-get install -y openssh-server
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
- name: Install build dependencies
|
||||||
|
run: |
|
||||||
|
cd ${{ gitea.workspace }}/source
|
||||||
|
./install_build_prerequisites.sh
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
cd ${{ gitea.workspace }}/docker
|
cd ${{ gitea.workspace }}/source
|
||||||
./compile.sh
|
./multibuild.sh
|
||||||
- name: Test
|
- name: Test
|
||||||
run: |
|
run: |
|
||||||
cd ${{ gitea.workspace }}/docker/output
|
cd ${{ gitea.workspace }}/source/output
|
||||||
./dropshell_x86_64 list
|
./dropshell_x86_64 list
|
||||||
./dropshell_x86_64 help
|
./dropshell_x86_64 help
|
||||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -39,7 +39,6 @@ cmake_install.cmake
|
|||||||
Makefile
|
Makefile
|
||||||
|
|
||||||
# IDE specific files
|
# IDE specific files
|
||||||
.vscode/
|
|
||||||
.idea/
|
.idea/
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
17
.vscode/c_cpp_properties.json
vendored
Normal file
17
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Linux",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"${workspaceFolder}/source/build/src/autogen"
|
||||||
|
],
|
||||||
|
"defines": [],
|
||||||
|
"compilerPath": "/usr/bin/g++",
|
||||||
|
"cStandard": "c23",
|
||||||
|
"cppStandard": "c++23",
|
||||||
|
"intelliSenseMode": "linux-gcc-x64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
103
.vscode/settings.json
vendored
Normal file
103
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.inja": "jinja-html",
|
||||||
|
"*.ipp": "cpp",
|
||||||
|
"random": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"cctype": "cpp",
|
||||||
|
"clocale": "cpp",
|
||||||
|
"cmath": "cpp",
|
||||||
|
"csetjmp": "cpp",
|
||||||
|
"csignal": "cpp",
|
||||||
|
"cstdarg": "cpp",
|
||||||
|
"cstddef": "cpp",
|
||||||
|
"cstdio": "cpp",
|
||||||
|
"cstdlib": "cpp",
|
||||||
|
"cstring": "cpp",
|
||||||
|
"ctime": "cpp",
|
||||||
|
"cwchar": "cpp",
|
||||||
|
"cwctype": "cpp",
|
||||||
|
"any": "cpp",
|
||||||
|
"array": "cpp",
|
||||||
|
"atomic": "cpp",
|
||||||
|
"strstream": "cpp",
|
||||||
|
"bit": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"bitset": "cpp",
|
||||||
|
"cfenv": "cpp",
|
||||||
|
"charconv": "cpp",
|
||||||
|
"chrono": "cpp",
|
||||||
|
"cinttypes": "cpp",
|
||||||
|
"codecvt": "cpp",
|
||||||
|
"compare": "cpp",
|
||||||
|
"complex": "cpp",
|
||||||
|
"concepts": "cpp",
|
||||||
|
"condition_variable": "cpp",
|
||||||
|
"cstdint": "cpp",
|
||||||
|
"deque": "cpp",
|
||||||
|
"forward_list": "cpp",
|
||||||
|
"list": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"set": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"unordered_map": "cpp",
|
||||||
|
"unordered_set": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"exception": "cpp",
|
||||||
|
"expected": "cpp",
|
||||||
|
"algorithm": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iterator": "cpp",
|
||||||
|
"memory": "cpp",
|
||||||
|
"memory_resource": "cpp",
|
||||||
|
"numeric": "cpp",
|
||||||
|
"optional": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"regex": "cpp",
|
||||||
|
"source_location": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"system_error": "cpp",
|
||||||
|
"tuple": "cpp",
|
||||||
|
"type_traits": "cpp",
|
||||||
|
"utility": "cpp",
|
||||||
|
"fstream": "cpp",
|
||||||
|
"future": "cpp",
|
||||||
|
"initializer_list": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"iosfwd": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"mutex": "cpp",
|
||||||
|
"new": "cpp",
|
||||||
|
"numbers": "cpp",
|
||||||
|
"ranges": "cpp",
|
||||||
|
"scoped_allocator": "cpp",
|
||||||
|
"semaphore": "cpp",
|
||||||
|
"shared_mutex": "cpp",
|
||||||
|
"span": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"stdexcept": "cpp",
|
||||||
|
"stop_token": "cpp",
|
||||||
|
"streambuf": "cpp",
|
||||||
|
"thread": "cpp",
|
||||||
|
"typeindex": "cpp",
|
||||||
|
"typeinfo": "cpp",
|
||||||
|
"valarray": "cpp",
|
||||||
|
"variant": "cpp",
|
||||||
|
"format": "cpp",
|
||||||
|
"stdfloat": "cpp",
|
||||||
|
"__nullptr": "cpp",
|
||||||
|
"__node_handle": "cpp",
|
||||||
|
"__split_buffer": "cpp",
|
||||||
|
"filesystem": "cpp",
|
||||||
|
"queue": "cpp",
|
||||||
|
"stack": "cpp",
|
||||||
|
"__bit_reference": "cpp",
|
||||||
|
"__functional_base": "cpp",
|
||||||
|
"__memory": "cpp",
|
||||||
|
"locale": "cpp",
|
||||||
|
"stacktrace": "cpp",
|
||||||
|
"__locale": "cpp"
|
||||||
|
}
|
||||||
|
}
|
36
README.md
36
README.md
@ -5,15 +5,23 @@ A system management tool for server operations, written in C++.
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```
|
```
|
||||||
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/install.sh | sudo bash
|
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/install.sh | bash
|
||||||
```
|
```
|
||||||
|
|
||||||
This installs as dropshell, with a symlink ds if the ds command does not already exist.
|
This installs as dropshell for the local user, with a symbolic link ds.
|
||||||
|
You'll need to run:
|
||||||
|
```
|
||||||
|
~/.local/bin/dropshell edit
|
||||||
|
~/.local/bin/dropshell install
|
||||||
|
source ~/.bashrc
|
||||||
|
```
|
||||||
|
to configure dropshell and install the local components.
|
||||||
|
|
||||||
## Installation of Agent
|
## Remote Server Setup
|
||||||
|
|
||||||
Install the Agent on each server you wish to manage. Supports amd64 (x86_64) and arm64 (aarch64) architectures.
|
### Initial setup
|
||||||
|
|
||||||
|
Auto setup script which creates a dropshell user, and includes installing docker if not already present:
|
||||||
```
|
```
|
||||||
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/server_autosetup.sh | sudo bash
|
curl -fsSL https://gitea.jde.nz/public/dropshell/releases/download/latest/server_autosetup.sh | sudo bash
|
||||||
```
|
```
|
||||||
@ -26,7 +34,21 @@ Manual steps:
|
|||||||
1. Test ssh'ing into the server.
|
1. Test ssh'ing into the server.
|
||||||
|
|
||||||
|
|
||||||
## Install Services
|
### Configure and Use Remote Server
|
||||||
|
|
||||||
Set up a server and install a service:
|
#### Add to local dropshell configuration, and install remote agent
|
||||||
1. `ds create-server SERVERNAME`
|
Back on the dropshell host:
|
||||||
|
1. `dropshell create-server SERVERNAME`
|
||||||
|
1. `dropshell edit SERVERNAME`
|
||||||
|
1. `dropshell install SERVERNAME`
|
||||||
|
|
||||||
|
#### Install Services
|
||||||
|
Create and install a service
|
||||||
|
1. `ds template list` -- see what templates are available to install.
|
||||||
|
1. `ds create-service SERVERNAME SERVICENAME TEMPLATE`
|
||||||
|
1. `ds edit SERVERNAME SERVICENAME`
|
||||||
|
1. Edit other config files if needed.
|
||||||
|
1. `ds install SERVERNAME SERVICENAME`
|
||||||
|
1. `ds list`
|
||||||
|
|
||||||
|
The service should now be seen to be running.
|
||||||
|
33
install.sh
33
install.sh
@ -3,20 +3,14 @@ set -e
|
|||||||
|
|
||||||
# download and install dropshell
|
# download and install dropshell
|
||||||
|
|
||||||
# Check if running as root
|
|
||||||
if [ "$EUID" -ne 0 ]; then
|
|
||||||
echo "Please run this script as root (use sudo)"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# 1. Determine architecture
|
# 1. Determine architecture
|
||||||
# -----------------------------------------------------------------------------
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
ARCH=$(uname -m)
|
ARCH=$(uname -m)
|
||||||
if [[ "$ARCH" == "x86_64" ]]; then
|
if [[ "$ARCH" == "x86_64" ]]; then
|
||||||
BIN=dropshell.amd64
|
BIN=dropshell.x86_64
|
||||||
elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
|
elif [[ "$ARCH" == "aarch64" || "$ARCH" == "arm64" ]]; then
|
||||||
BIN=dropshell.arm64
|
BIN=dropshell.aarch64
|
||||||
else
|
else
|
||||||
echo "Unsupported architecture: $ARCH" >&2
|
echo "Unsupported architecture: $ARCH" >&2
|
||||||
exit 1
|
exit 1
|
||||||
@ -30,21 +24,26 @@ trap 'rm -rf "$TMPDIR"' EXIT
|
|||||||
URL="https://gitea.jde.nz/public/dropshell/releases/download/latest/$BIN"
|
URL="https://gitea.jde.nz/public/dropshell/releases/download/latest/$BIN"
|
||||||
echo "Downloading $BIN from $URL..."
|
echo "Downloading $BIN from $URL..."
|
||||||
|
|
||||||
curl -fsSL -o "$TMPDIR/dropshell" "$URL"
|
TARGET_PATH="${HOME}/.local/bin/dropshell"
|
||||||
|
|
||||||
if [ ! -f "$TMPDIR/dropshell" ]; then
|
mkdir -p "${HOME}/.local/bin"
|
||||||
|
|
||||||
|
curl -fsSL -o "$TARGET_PATH" "$URL"
|
||||||
|
|
||||||
|
if [ ! -f "$TARGET_PATH" ]; then
|
||||||
echo "Failed to download dropshell" >&2
|
echo "Failed to download dropshell" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
chmod +x "$TMPDIR/dropshell"
|
chmod +x "$TARGET_PATH"
|
||||||
|
|
||||||
cp "$TMPDIR/dropshell" /usr/local/bin/dropshell
|
if [ ! -f "${HOME}/.local/bin/ds" ]; then
|
||||||
if [ -f /usr/local/bin/ds ]; then
|
ln -s "$TARGET_PATH" "${HOME}/.local/bin/ds"
|
||||||
rm -f /usr/local/bin/ds
|
|
||||||
fi
|
fi
|
||||||
ln -s /usr/local/bin/dropshell /usr/local/bin/ds
|
|
||||||
rm -rf "$TMPDIR"
|
|
||||||
|
|
||||||
echo "dropshell installed successfully to /usr/local/bin/dropshell"
|
echo "dropshell installed successfully to $TARGET_PATH"
|
||||||
|
|
||||||
|
echo "Please:"
|
||||||
|
echo "1. run '${TARGET_PATH} edit' to edit the configuration."
|
||||||
|
echo "2. run '${TARGET_PATH} install' to install dropshell components on this computer."
|
||||||
|
echo "3. run 'source ~/.bashrc' to add to your path and autocomplete for the current shell."
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(dropshell VERSION 1.0.0 LANGUAGES CXX)
|
project(dropshell VERSION 1.0.0 LANGUAGES CXX)
|
||||||
|
|
||||||
|
# Force static linking globally
|
||||||
|
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
|
||||||
|
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
|
||||||
|
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
|
||||||
|
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
|
||||||
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -static")
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")
|
||||||
|
set(ZLIB_USE_STATIC_LIBS "ON")
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_C_STANDARD 23)
|
set(CMAKE_C_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
@ -89,7 +98,8 @@ FetchContent_Declare(
|
|||||||
GIT_TAG v2.1.5
|
GIT_TAG v2.1.5
|
||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(libassert)
|
FetchContent_MakeAvailable(libassert)
|
||||||
include(FetchContent)
|
|
||||||
|
# Add cpptrace
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
cpptrace
|
cpptrace
|
||||||
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
|
GIT_REPOSITORY https://github.com/jeremy-rifkin/cpptrace.git
|
||||||
@ -97,81 +107,28 @@ FetchContent_Declare(
|
|||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(cpptrace)
|
FetchContent_MakeAvailable(cpptrace)
|
||||||
|
|
||||||
|
# Add nlohmann/json
|
||||||
|
FetchContent_Declare(
|
||||||
|
nlohmann_json
|
||||||
|
GIT_REPOSITORY https://github.com/nlohmann/json.git
|
||||||
|
GIT_TAG v3.11.3
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(nlohmann_json)
|
||||||
|
|
||||||
# Link libraries
|
# Link libraries
|
||||||
target_link_libraries(dropshell PRIVATE
|
target_link_libraries(dropshell PRIVATE
|
||||||
libassert::assert
|
libassert::assert
|
||||||
cpptrace::cpptrace
|
cpptrace::cpptrace
|
||||||
|
nlohmann_json::nlohmann_json
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set static linking flags
|
||||||
|
set_target_properties(dropshell PROPERTIES
|
||||||
|
LINK_FLAGS "-static"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Install targets
|
# Install targets
|
||||||
install(TARGETS dropshell
|
install(TARGETS dropshell
|
||||||
RUNTIME DESTINATION bin
|
RUNTIME DESTINATION $ENV{HOME}/.local/bin
|
||||||
)
|
)
|
||||||
|
|
||||||
# Create symbolic link 'ds' pointing to 'dropshell'
|
|
||||||
install(CODE "
|
|
||||||
message(STATUS \"Checking if 'ds' command already exists...\")
|
|
||||||
execute_process(
|
|
||||||
COMMAND which ds
|
|
||||||
RESULT_VARIABLE DS_NOT_EXISTS
|
|
||||||
OUTPUT_QUIET
|
|
||||||
ERROR_QUIET
|
|
||||||
)
|
|
||||||
if(DS_NOT_EXISTS)
|
|
||||||
message(STATUS \"Command 'ds' does not exist. Creating symlink.\")
|
|
||||||
execute_process(
|
|
||||||
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
|
||||||
\${CMAKE_INSTALL_PREFIX}/bin/dropshell
|
|
||||||
\${CMAKE_INSTALL_PREFIX}/bin/ds
|
|
||||||
)
|
|
||||||
else()
|
|
||||||
message(STATUS \"Command 'ds' already exists. Skipping symlink creation.\")
|
|
||||||
endif()
|
|
||||||
")
|
|
||||||
|
|
||||||
# Install completion script
|
|
||||||
install(FILES src/dropshell-completion.bash
|
|
||||||
DESTINATION /etc/bash_completion.d
|
|
||||||
RENAME dropshell
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create a symlink for the completion script to work with 'ds' command
|
|
||||||
install(CODE "
|
|
||||||
# First check if 'ds' command exists after our installation
|
|
||||||
execute_process(
|
|
||||||
COMMAND which ds
|
|
||||||
RESULT_VARIABLE DS_NOT_EXISTS
|
|
||||||
OUTPUT_VARIABLE DS_PATH
|
|
||||||
ERROR_QUIET
|
|
||||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
||||||
)
|
|
||||||
|
|
||||||
# Only proceed if 'ds' exists
|
|
||||||
if(NOT DS_NOT_EXISTS)
|
|
||||||
# Check if 'ds' is a symlink pointing to dropshell
|
|
||||||
execute_process(
|
|
||||||
COMMAND readlink -f \${DS_PATH}
|
|
||||||
RESULT_VARIABLE READLINK_FAILED
|
|
||||||
OUTPUT_VARIABLE REAL_PATH
|
|
||||||
ERROR_QUIET
|
|
||||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
|
||||||
)
|
|
||||||
|
|
||||||
# Get the path to our dropshell binary
|
|
||||||
set(DROPSHELL_PATH \${CMAKE_INSTALL_PREFIX}/bin/dropshell)
|
|
||||||
|
|
||||||
# Check if the real path is our dropshell binary
|
|
||||||
if(NOT READLINK_FAILED AND \"\${REAL_PATH}\" STREQUAL \"\${DROPSHELL_PATH}\")
|
|
||||||
message(STATUS \"Command 'ds' exists and points to dropshell. Creating completion script symlink.\")
|
|
||||||
execute_process(
|
|
||||||
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
|
||||||
/etc/bash_completion.d/dropshell
|
|
||||||
/etc/bash_completion.d/ds
|
|
||||||
)
|
|
||||||
else()
|
|
||||||
message(STATUS \"Command 'ds' exists but doesn't point to dropshell. Skipping completion symlink.\")
|
|
||||||
endif()
|
|
||||||
else()
|
|
||||||
message(STATUS \"Command 'ds' not found. Skipping completion symlink.\")
|
|
||||||
endif()
|
|
||||||
")
|
|
||||||
|
65
source/Dockerfile
Normal file
65
source/Dockerfile
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
FROM --platform=$BUILDPLATFORM alpine:latest AS builder
|
||||||
|
|
||||||
|
# Add build arguments for platform
|
||||||
|
ARG TARGETPLATFORM
|
||||||
|
ARG BUILDPLATFORM
|
||||||
|
|
||||||
|
# Install build dependencies
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
build-base \
|
||||||
|
cmake \
|
||||||
|
git \
|
||||||
|
musl-dev \
|
||||||
|
curl \
|
||||||
|
bash \
|
||||||
|
musl \
|
||||||
|
g++ \
|
||||||
|
ninja \
|
||||||
|
linux-headers
|
||||||
|
|
||||||
|
# Install cross-compilation tools for ARM64
|
||||||
|
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
|
||||||
|
apk add --no-cache \
|
||||||
|
crossbuild-essential-arm64 \
|
||||||
|
gcc-aarch64-linux-gnu \
|
||||||
|
g++-aarch64-linux-gnu; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Set working directory
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
# Copy source files
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Configure and build
|
||||||
|
RUN mkdir -p build_static
|
||||||
|
|
||||||
|
# Set up cross-compilation environment for ARM64
|
||||||
|
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
|
||||||
|
export CC=aarch64-linux-gnu-gcc \
|
||||||
|
export CXX=aarch64-linux-gnu-g++ \
|
||||||
|
export CMAKE_TOOLCHAIN_FILE=/build/toolchain.cmake; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create toolchain file for ARM64
|
||||||
|
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
|
||||||
|
echo "set(CMAKE_SYSTEM_NAME Linux)" > toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_SYSTEM_PROCESSOR aarch64)" >> toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc)" >> toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)" >> toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)" >> toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)" >> toolchain.cmake && \
|
||||||
|
echo "set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)" >> toolchain.cmake; \
|
||||||
|
fi
|
||||||
|
|
||||||
|
RUN cmake -G Ninja -B build_static -DCMAKE_BUILD_TYPE=Release \
|
||||||
|
-DCMAKE_EXE_LINKER_FLAGS="-static" \
|
||||||
|
-DCMAKE_FIND_LIBRARY_SUFFIXES=".a" \
|
||||||
|
-DBUILD_SHARED_LIBS=OFF \
|
||||||
|
${CMAKE_TOOLCHAIN_FILE:+-DCMAKE_TOOLCHAIN_FILE=$CMAKE_TOOLCHAIN_FILE}
|
||||||
|
|
||||||
|
RUN cmake --build build_static
|
||||||
|
|
||||||
|
# Final stage that only contains the binary
|
||||||
|
FROM scratch AS dropshell
|
||||||
|
COPY --from=builder /build/build_static/dropshell /dropshell
|
@ -1,5 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
|
||||||
|
|
||||||
# Exit on error
|
# Exit on error
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
@ -15,7 +17,6 @@ if command -v nproc >/dev/null 2>&1; then
|
|||||||
JOBS=$(nproc)
|
JOBS=$(nproc)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Function to print status messages
|
# Function to print status messages
|
||||||
print_status() {
|
print_status() {
|
||||||
echo -e "${GREEN}[*] $1${NC}"
|
echo -e "${GREEN}[*] $1${NC}"
|
||||||
@ -47,57 +48,37 @@ if ! command -v cmake &> /dev/null; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if make is installed
|
# Check if Ninja is installed
|
||||||
if ! command -v make &> /dev/null; then
|
if ! command -v ninja &> /dev/null; then
|
||||||
print_error "Make is not installed. Please install Make first."
|
print_error "Ninja is not installed. Please install Ninja first."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Check if pkg-config is installed
|
# Check if ccache is installed
|
||||||
if ! command -v pkg-config &> /dev/null; then
|
if ! command -v ccache &> /dev/null; then
|
||||||
print_error "pkg-config is not installed. Please install pkg-config first."
|
print_warning "ccache is not installed. Builds will be slower without it."
|
||||||
print_warning "On Ubuntu/Debian: sudo apt-get install pkg-config"
|
print_warning "Consider installing ccache for faster builds."
|
||||||
print_warning "On Fedora: sudo dnf install pkg-config"
|
|
||||||
print_warning "On Arch: sudo pacman -S pkg-config"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if ncurses is installed
|
|
||||||
if ! pkg-config --exists ncurses; then
|
|
||||||
print_error "ncurses is not installed. Please install ncurses first."
|
|
||||||
print_warning "On Ubuntu/Debian: sudo apt-get install libncurses-dev"
|
|
||||||
print_warning "On Fedora: sudo dnf install ncurses-devel"
|
|
||||||
print_warning "On Arch: sudo pacman -S ncurses"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Configure with CMake
|
# Configure with CMake
|
||||||
print_status "Configuring with CMake..."
|
print_status "Configuring with CMake..."
|
||||||
cmake .. -DCMAKE_BUILD_TYPE=Debug
|
cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
|
||||||
#cmake .. -DCMAKE_BUILD_TYPE=Release
|
|
||||||
|
|
||||||
# Build the project
|
# Build the project
|
||||||
print_status "Building project..."
|
print_status "Building project..."
|
||||||
make -j"$JOBS"
|
ninja -j"$JOBS"
|
||||||
|
|
||||||
# Check if build was successful
|
# Check if build was successful
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
print_status "Build successful!"
|
print_status "Build successful!"
|
||||||
print_status "Binary location: $(pwd)/dropshell"
|
|
||||||
else
|
else
|
||||||
print_error "Build failed!"
|
print_error "Build failed!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
print_status "Auto-installing dropshell..."
|
print_status "Auto-installing dropshell locally..."
|
||||||
sudo make install
|
mkdir -p "${HOME}/.local/bin"
|
||||||
if [ $? -eq 0 ]; then
|
cp "$SCRIPT_DIR/build/dropshell" "${HOME}/.local/bin/dropshell"
|
||||||
print_status "Installation successful!"
|
|
||||||
else
|
|
||||||
print_error "Installation failed!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
|
||||||
# Return to original directory
|
# Return to original directory
|
||||||
cd ..
|
cd ..
|
||||||
|
@ -41,7 +41,7 @@ print_status "Detected OS: $OS $VER"
|
|||||||
case $OS in
|
case $OS in
|
||||||
"Ubuntu"|"Debian GNU/Linux")
|
"Ubuntu"|"Debian GNU/Linux")
|
||||||
# Common packages for both Ubuntu and Debian
|
# Common packages for both Ubuntu and Debian
|
||||||
PACKAGES="cmake make g++ devscripts debhelper"
|
PACKAGES="cmake make g++ devscripts debhelper build-essential upx"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
print_error "Unsupported distribution: $OS"
|
print_error "Unsupported distribution: $OS"
|
||||||
@ -85,7 +85,7 @@ done
|
|||||||
|
|
||||||
|
|
||||||
# Install other required packages
|
# Install other required packages
|
||||||
apt install -y musl-tools wget tar
|
apt install -y musl-tools wget tar ccache ninja-build
|
||||||
|
|
||||||
# Set install directory
|
# Set install directory
|
||||||
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
|
if [ -n "$SUDO_USER" ] && [ "$SUDO_USER" != "root" ]; then
|
||||||
|
@ -6,8 +6,7 @@ set -e
|
|||||||
# src/utils/createagent.cpp
|
# src/utils/createagent.cpp
|
||||||
#
|
#
|
||||||
|
|
||||||
SCRIPT_DIR=$(dirname "$0")
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
|
||||||
# check if dehydrate is installed
|
# check if dehydrate is installed
|
||||||
if ! command -v dehydrate &> /dev/null; then
|
if ! command -v dehydrate &> /dev/null; then
|
||||||
echo "dehydrate could not be found - installing"
|
echo "dehydrate could not be found - installing"
|
||||||
|
@ -1,59 +1,33 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# build amd64 and arm64 versions of dropshell, to:
|
# Create output directory
|
||||||
# build/dropshell.amd64
|
|
||||||
# build/dropshell.arm64
|
|
||||||
|
|
||||||
set -e
|
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
||||||
|
|
||||||
rm -f $SCRIPT_DIR/build_amd64/dropshell $SCRIPT_DIR/build_arm64/dropshell $SCRIPT_DIR/output/dropshell.amd64 $SCRIPT_DIR/output/dropshell.arm64
|
|
||||||
|
|
||||||
# Determine number of CPU cores for parallel build
|
|
||||||
if command -v nproc >/dev/null 2>&1; then
|
|
||||||
JOBS=$(nproc)
|
|
||||||
else
|
|
||||||
JOBS=4 # fallback default
|
|
||||||
fi
|
|
||||||
|
|
||||||
PREV_PWD=$PWD
|
|
||||||
cd $SCRIPT_DIR
|
|
||||||
|
|
||||||
# Build for amd64 (musl)
|
|
||||||
echo "Building for amd64 (musl)..."
|
|
||||||
cmake -B build_amd64 -DCMAKE_BUILD_TYPE=Release \
|
|
||||||
-DCMAKE_C_COMPILER=x86_64-linux-musl-gcc \
|
|
||||||
-DCMAKE_CXX_COMPILER=x86_64-linux-musl-g++ \
|
|
||||||
-DCMAKE_EXE_LINKER_FLAGS="-static" \
|
|
||||||
-DCMAKE_CXX_FLAGS="-march=x86-64" .
|
|
||||||
cmake --build build_amd64 --target dropshell --config Release -j"$JOBS"
|
|
||||||
mkdir -p output
|
mkdir -p output
|
||||||
cp build_amd64/dropshell output/dropshell.amd64
|
|
||||||
|
|
||||||
# Build for arm64 (musl)
|
|
||||||
echo "Building for arm64 (musl)..."
|
|
||||||
cmake -B build_arm64 -DCMAKE_BUILD_TYPE=Release \
|
|
||||||
-DCMAKE_C_COMPILER=aarch64-linux-musl-gcc \
|
|
||||||
-DCMAKE_CXX_COMPILER=aarch64-linux-musl-g++ \
|
|
||||||
-DCMAKE_EXE_LINKER_FLAGS="-static" \
|
|
||||||
-DCMAKE_CXX_FLAGS="-march=armv8-a" \
|
|
||||||
-DCMAKE_SYSTEM_PROCESSOR=aarch64 .
|
|
||||||
cmake --build build_arm64 --target dropshell --config Release -j"$JOBS"
|
|
||||||
mkdir -p output
|
|
||||||
cp build_arm64/dropshell output/dropshell.arm64
|
|
||||||
|
|
||||||
if [ ! -f output/dropshell.amd64 ]; then
|
function build_arch() {
|
||||||
echo "output/dropshell.amd64 not found!" >&2
|
local arch=$1
|
||||||
|
|
||||||
|
if [ ! -f "${HOME}/.musl-cross/${arch}-linux-musl-cross/bin/${arch}-linux-musl-c++" ]; then
|
||||||
|
echo "Musl cross compiler for ${arch} not found. Please run install_build_prerequisites.sh first."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f output/dropshell.arm64 ]; then
|
CMAKE_BUILD_TYPE=Release
|
||||||
echo "output/dropshell.arm64 not found!" >&2
|
CC="${HOME}/.musl-cross/${arch}-linux-musl-cross/bin/${arch}-linux-musl-gcc"
|
||||||
exit 1
|
CXX="${HOME}/.musl-cross/${arch}-linux-musl-cross/bin/${arch}-linux-musl-g++"
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Builds complete:"
|
BUILDDIR=build/${arch}
|
||||||
ls -lh output/dropshell.*
|
mkdir -p ${BUILDDIR}
|
||||||
|
|
||||||
cd $PREV_PWD
|
cmake -B ${BUILDDIR} -G Ninja -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_C_COMPILER=${CC} -DCMAKE_CXX_COMPILER=${CXX}
|
||||||
|
cmake --build ${BUILDDIR}
|
||||||
|
|
||||||
|
upx ${BUILDDIR}/dropshell
|
||||||
|
cp ${BUILDDIR}/dropshell output/dropshell.${arch}
|
||||||
|
}
|
||||||
|
|
||||||
|
build_arch x86_64
|
||||||
|
build_arch aarch64
|
||||||
|
|
||||||
|
echo "Static binaries have been created:"
|
||||||
|
ls -la output
|
||||||
|
@ -6,14 +6,8 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|||||||
echo "Script directory: $SCRIPT_DIR"
|
echo "Script directory: $SCRIPT_DIR"
|
||||||
|
|
||||||
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
# Check for GITEA_TOKEN_DEPLOY or GITEA_TOKEN
|
||||||
if [ -n "$GITEA_TOKEN_DEPLOY" ]; then
|
TOKEN="${GITEA_TOKEN_DEPLOY:-${GITEA_TOKEN}}"
|
||||||
TOKEN="$GITEA_TOKEN_DEPLOY"
|
[ -z "$TOKEN" ] && { echo "Neither GITEA_TOKEN_DEPLOY nor GITEA_TOKEN environment variable set!" >&2; exit 1; }
|
||||||
elif [ -n "$GITEA_TOKEN" ]; then
|
|
||||||
TOKEN="$GITEA_TOKEN"
|
|
||||||
else
|
|
||||||
echo "GITEA_TOKEN_DEPLOY or GITEA_TOKEN environment variable not set!" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
$SCRIPT_DIR/multibuild.sh
|
$SCRIPT_DIR/multibuild.sh
|
||||||
BUILD_DIR=$SCRIPT_DIR/build
|
BUILD_DIR=$SCRIPT_DIR/build
|
||||||
@ -21,21 +15,18 @@ BUILD_DIR=$SCRIPT_DIR/build
|
|||||||
OLD_PWD=$PWD
|
OLD_PWD=$PWD
|
||||||
cd $SCRIPT_DIR
|
cd $SCRIPT_DIR
|
||||||
|
|
||||||
|
# Check for required binaries
|
||||||
if [ ! -f "output/dropshell.amd64" ]; then
|
REQUIRED_BINARIES=("dropshell.x86_64" "dropshell.aarch64")
|
||||||
echo "output/dropshell.amd64 not found!" >&2
|
for binary in "${REQUIRED_BINARIES[@]}"; do
|
||||||
|
if [ ! -f "output/$binary" ]; then
|
||||||
|
echo "output/$binary not found!" >&2
|
||||||
echo "Please run multibuild.sh first." >&2
|
echo "Please run multibuild.sh first." >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
if [ ! -f "output/dropshell.arm64" ]; then
|
TAG=$("$SCRIPT_DIR/output/dropshell.x86_64" --version)
|
||||||
echo "output/dropshell.arm64 not found!" >&2
|
[ -z "$TAG" ] && echo "Failed to get version from dropshell.x86_64" >&2 && exit 1
|
||||||
echo "Please run multibuild.sh first." >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
TAG=$("$SCRIPT_DIR/output/dropshell.amd64" --version)
|
|
||||||
[ -z "$TAG" ] && echo "Failed to get version from dropshell.amd64" >&2 && exit 1
|
|
||||||
|
|
||||||
echo "Publishing dropshell version $TAG"
|
echo "Publishing dropshell version $TAG"
|
||||||
|
|
||||||
@ -70,33 +61,52 @@ RELEASE_DATA=$(cat <<EOF
|
|||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
|
|
||||||
RELEASE_ID=$(curl -s -X POST "$API_URL/releases" \
|
# Capture stdout and stderr of the curl command
|
||||||
|
CURL_OUTPUT=$(curl -X POST "$API_URL/releases" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-H "Authorization: token $TOKEN" \
|
-H "Authorization: token $TOKEN" \
|
||||||
-d "$RELEASE_DATA" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
-d "$RELEASE_DATA" 2>&1)
|
||||||
|
|
||||||
|
# Extract the release ID from the captured output
|
||||||
|
RELEASE_ID=$(echo "$CURL_OUTPUT" | grep -o '"id":[0-9]*' | head -1 | cut -d: -f2)
|
||||||
|
|
||||||
if [ -z "$RELEASE_ID" ]; then
|
if [ -z "$RELEASE_ID" ]; then
|
||||||
echo "Failed to create release on Gitea." >&2
|
echo "Failed to create release on Gitea." >&2
|
||||||
|
echo "Release ID returned: $RELEASE_ID" >&2
|
||||||
|
echo "Curl Output/Error:" >&2
|
||||||
|
echo "$CURL_OUTPUT" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Function to find file in specified locations
|
||||||
|
find_file() {
|
||||||
|
local filename="$1"
|
||||||
|
shift # remove filename from args
|
||||||
|
local locations=("$@") # grab the rest of the args as locations
|
||||||
|
|
||||||
|
for loc in "${locations[@]}"; do
|
||||||
|
if [ -f "$loc/$filename" ]; then
|
||||||
|
echo "$loc/$filename"
|
||||||
|
return 0 # Found the file, return success
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "" # Return empty string if not found
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
# Upload binaries and install.sh
|
# Upload binaries and install.sh
|
||||||
for FILE in dropshell.amd64 dropshell.arm64 install.sh server_autosetup.sh; do
|
for FILE in dropshell.x86_64 dropshell.aarch64 install.sh server_autosetup.sh; do
|
||||||
if [ -f "output/$FILE" ]; then
|
# Pass the locations directly to the find_file function
|
||||||
filetoupload="output/$FILE"
|
filetoupload=$(find_file "$FILE" "output" "../" ".")
|
||||||
elif [ -f "../$FILE" ]; then
|
if [ -z "$filetoupload" ]; then
|
||||||
filetoupload="../$FILE"
|
echo "File $FILE not found in expected locations!" >&2
|
||||||
elif [ -f "$FILE" ]; then
|
|
||||||
filetoupload="$FILE"
|
|
||||||
else
|
|
||||||
echo "File $FILE not found!" >&2
|
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Auto-detect content type
|
# Auto-detect content type
|
||||||
ctype=$(file --mime-type -b "$filetoupload")
|
ctype=$(file --mime-type -b "$filetoupload")
|
||||||
|
|
||||||
curl -s -X POST "$API_URL/releases/$RELEASE_ID/assets?name=$FILE" \
|
curl -X POST "$API_URL/releases/$RELEASE_ID/assets?name=$FILE" \
|
||||||
-H "Content-Type: $ctype" \
|
-H "Content-Type: $ctype" \
|
||||||
-H "Authorization: token $TOKEN" \
|
-H "Authorization: token $TOKEN" \
|
||||||
--data-binary @"$filetoupload"
|
--data-binary @"$filetoupload"
|
||||||
|
@ -84,7 +84,7 @@ namespace dropshell
|
|||||||
remote_command_script_file,
|
remote_command_script_file,
|
||||||
remotefile(server, user).service_env(service)}, user))
|
remotefile(server, user).service_env(service)}, user))
|
||||||
{
|
{
|
||||||
error << "Error: Required service directories not found on remote server" << std::endl;
|
error << "Required service directories not found on remote server" << std::endl;
|
||||||
info << "Is the service installed?" << std::endl;
|
info << "Is the service installed?" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -103,7 +103,7 @@ namespace dropshell
|
|||||||
std::string local_backups_dir = localpath::backups();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty())
|
if (local_backups_dir.empty())
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found" << std::endl;
|
error << "Local backups directory not found" << std::endl;
|
||||||
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -94,11 +94,11 @@ int edit_config()
|
|||||||
|
|
||||||
std::string config_file = localfile::dropshell_json();
|
std::string config_file = localfile::dropshell_json();
|
||||||
if (!edit_file(config_file, false) || !std::filesystem::exists(config_file))
|
if (!edit_file(config_file, false) || !std::filesystem::exists(config_file))
|
||||||
return die("Error: Failed to edit config file.");
|
return die("Failed to edit config file.");
|
||||||
|
|
||||||
gConfig().load_config();
|
gConfig().load_config();
|
||||||
if (!gConfig().is_config_set())
|
if (!gConfig().is_config_set())
|
||||||
return die("Error: Failed to load and parse edited config file!");
|
return die("Failed to load and parse edited config file!");
|
||||||
|
|
||||||
gConfig().save_config(true);
|
gConfig().save_config(true);
|
||||||
|
|
||||||
@ -112,7 +112,7 @@ int edit_config()
|
|||||||
int edit_server(const std::string &server_name)
|
int edit_server(const std::string &server_name)
|
||||||
{
|
{
|
||||||
if (localpath::server(server_name).empty()) {
|
if (localpath::server(server_name).empty()) {
|
||||||
std::cerr << "Error: Server not found: " << server_name << std::endl;
|
error << "Server not found: " << server_name << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <libassert/assert.hpp>
|
#include <libassert/assert.hpp>
|
||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
namespace dropshell
|
namespace dropshell
|
||||||
{
|
{
|
||||||
@ -176,49 +177,107 @@ namespace dropshell
|
|||||||
return trim(result);
|
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()
|
int update_dropshell()
|
||||||
{
|
{
|
||||||
maketitle("Updating dropshell on this computer...");
|
maketitle("Updating dropshell on this computer...");
|
||||||
|
|
||||||
|
configure_localbin();
|
||||||
|
configure_autocomplete();
|
||||||
|
|
||||||
// determine path to this executable
|
// determine path to this executable
|
||||||
std::filesystem::path dropshell_path = std::filesystem::canonical("/proc/self/exe");
|
std::filesystem::path exe_path = std::filesystem::canonical("/proc/self/exe");
|
||||||
std::filesystem::path parent_path = dropshell_path.parent_path();
|
std::filesystem::path parent_path = exe_path.parent_path();
|
||||||
|
|
||||||
// determine the architecture of the system
|
// determine the architecture of the system
|
||||||
std::string arch = shared_commands::get_arch();
|
std::string arch = shared_commands::get_arch();
|
||||||
|
|
||||||
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
std::string url = "https://gitea.jde.nz/public/dropshell/releases/download/latest/dropshell." + arch;
|
||||||
|
|
||||||
// download new version, preserve permissions and ownership
|
// check that the user that owns the exe is the current user this process is running as.
|
||||||
std::string bash_script;
|
struct stat st;
|
||||||
bash_script += "docker run --rm -v " + parent_path.string() + ":/target";
|
if (stat(exe_path.c_str(), &st) != 0) {
|
||||||
bash_script += " gitea.jde.nz/public/debian-curl:latest";
|
error << "Failed to stat dropshell executable: " << strerror(errno) << std::endl;
|
||||||
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 += "\"";
|
|
||||||
|
|
||||||
std::string cmd = "bash -c '" + bash_script + "'";
|
|
||||||
int rval = system(cmd.c_str());
|
|
||||||
if (rval != 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Failed to download new version of dropshell." << std::endl;
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// check if the new version is the same as the old version
|
||||||
uint64_t new_hash = hash_file(parent_path / "dropshell_temp");
|
uint64_t new_hash = hash_file(temp_file);
|
||||||
uint64_t old_hash = hash_file(parent_path / "dropshell");
|
uint64_t old_hash = hash_file(exe_path);
|
||||||
if (new_hash == old_hash)
|
if (new_hash == old_hash)
|
||||||
{
|
{
|
||||||
std::cout << "Confirmed dropshell is the latest version." << std::endl;
|
info << "Confirmed dropshell is the latest version." << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string runvercmd = (parent_path / "dropshell").string() + " version";
|
std::string runvercmd = exe_path.string() + " version";
|
||||||
std::string currentver = _exec(runvercmd.c_str());
|
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());
|
std::string newver = _exec(runvercmd.c_str());
|
||||||
|
|
||||||
if (currentver >= newver)
|
if (currentver >= newver)
|
||||||
@ -228,18 +287,12 @@ namespace dropshell
|
|||||||
return 0;
|
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 " +
|
// remove the old version.
|
||||||
"sh -c \"mv /target/dropshell_temp /target/dropshell\"";
|
std::filesystem::remove(exe_path.parent_path() / "dropshell.old");
|
||||||
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;
|
|
||||||
|
|
||||||
// execute the new version
|
// execute the new version
|
||||||
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
|
execlp("bash", "bash", "-c", (parent_path / "dropshell").c_str(), "install", (char *)nullptr);
|
||||||
@ -281,7 +334,7 @@ namespace dropshell
|
|||||||
info << "Installing agent for user " << user.user << " on " << server.get_server_name() << std::endl;
|
info << "Installing agent for user " << user.user << " on " << server.get_server_name() << std::endl;
|
||||||
|
|
||||||
std::string agent_path = remotepath(server.get_server_name(),user.user).agent();
|
std::string agent_path = remotepath(server.get_server_name(),user.user).agent();
|
||||||
ASSERT(agent_path == user.dir+"/agent", "Agent path does not match user directory for "+user.user+"@" + server.get_server_name() + " : " + agent_path + " != " + user.dir);
|
ASSERT(agent_path == user.dir+"/agent", "Remote agent path does not match user directory for "+user.user+"@" + server.get_server_name() + " : " + agent_path + " != " + user.dir);
|
||||||
ASSERT(!agent_path.empty(), "Agent path is empty for " + user.user + "@" + server.get_server_name());
|
ASSERT(!agent_path.empty(), "Agent path is empty for " + user.user + "@" + server.get_server_name());
|
||||||
|
|
||||||
// now create the agent.
|
// now create the agent.
|
||||||
@ -295,7 +348,7 @@ namespace dropshell
|
|||||||
bool okay = execute_ssh_command(server.get_SSH_INFO(user.user), sCommand(agent_path, "agent-install.sh",{}), cMode::Defaults | cMode::NoBB64, nullptr);
|
bool okay = execute_ssh_command(server.get_SSH_INFO(user.user), sCommand(agent_path, "agent-install.sh",{}), cMode::Defaults | cMode::NoBB64, nullptr);
|
||||||
if (!okay)
|
if (!okay)
|
||||||
{
|
{
|
||||||
error << "ERROR: Failed to install remote agent on " << server.get_server_name() << std::endl;
|
error << "Failed to install remote agent on " << server.get_server_name() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,7 +147,7 @@ void list_servers() {
|
|||||||
void show_server_details(const std::string& server_name) {
|
void show_server_details(const std::string& server_name) {
|
||||||
ServerConfig env(server_name);
|
ServerConfig env(server_name);
|
||||||
if (!env.is_valid()) {
|
if (!env.is_valid()) {
|
||||||
error << "Error: Invalid server environment file: " << server_name << std::endl;
|
error << "Invalid server environment file: " << server_name << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ namespace dropshell
|
|||||||
std::string local_backups_dir = localpath::backups();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
error << "Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,19 +150,19 @@ namespace dropshell
|
|||||||
std::string local_backups_dir = localpath::backups();
|
std::string local_backups_dir = localpath::backups();
|
||||||
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
if (local_backups_dir.empty() || !std::filesystem::exists(local_backups_dir))
|
||||||
{
|
{
|
||||||
error << "Error: Local backups directory not found: " << local_backups_dir << std::endl;
|
error << "Local backups directory not found: " << local_backups_dir << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_details->get_filename()).string();
|
std::string local_backup_file_path = (std::filesystem::path(local_backups_dir) / backup_details->get_filename()).string();
|
||||||
if (!std::filesystem::exists(local_backup_file_path))
|
if (!std::filesystem::exists(local_backup_file_path))
|
||||||
{
|
{
|
||||||
error << "Error: Backup file not found at " << local_backup_file_path << std::endl;
|
error << "Backup file not found at " << local_backup_file_path << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (backup_details->get_template_name() != service_info.template_name)
|
if (backup_details->get_template_name() != service_info.template_name)
|
||||||
{
|
{
|
||||||
error << "Error: Backup template does not match service template. Can't restore." << std::endl;
|
error << "Backup template does not match service template. Can't restore." << std::endl;
|
||||||
info << "Backup template: " << backup_details->get_template_name() << std::endl;
|
info << "Backup template: " << backup_details->get_template_name() << std::endl;
|
||||||
info << "Service template: " << service_info.template_name << std::endl;
|
info << "Service template: " << service_info.template_name << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -107,6 +107,22 @@ namespace dropshell
|
|||||||
return mPath;
|
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
|
// get_all_services_status : SHARED COMMAND
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#ifndef SHARED_COMMANDS_HPP
|
#ifndef SHARED_COMMANDS_HPP
|
||||||
#define SHARED_COMMANDS_HPP
|
#define SHARED_COMMANDS_HPP
|
||||||
|
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
#include "command_registry.hpp"
|
#include "command_registry.hpp"
|
||||||
#include "servers.hpp"
|
#include "servers.hpp"
|
||||||
@ -40,6 +42,16 @@ namespace dropshell
|
|||||||
std::string mUser;
|
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(
|
bool rsync_tree_to_remote(
|
||||||
const std::string &local_path,
|
const std::string &local_path,
|
||||||
const std::string &remote_path,
|
const std::string &remote_path,
|
||||||
|
@ -67,7 +67,7 @@ namespace dropshell
|
|||||||
{
|
{
|
||||||
if (ctx.args.size() < 2)
|
if (ctx.args.size() < 2)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Server name and service name are both required" << std::endl;
|
error << "Server name and service name are both required" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ bool config::load_config() { // load json config file.
|
|||||||
}
|
}
|
||||||
catch (nlohmann::json::parse_error& ex)
|
catch (nlohmann::json::parse_error& ex)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Failed to parse config file: " << ex.what() << std::endl;
|
error << "Failed to parse config file: " << ex.what() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
if (!mIsConfigSet)
|
if (!mIsConfigSet)
|
||||||
{
|
{
|
||||||
std::string homedir = localpath::current_user_home();
|
std::string homedir = localpath::current_user_home();
|
||||||
std::string dropshell_base = homedir + "/.local/dropshell_files";
|
std::string dropshell_base = homedir + "/.dropshell";
|
||||||
|
|
||||||
mConfig["server_definition_paths"] = {
|
mConfig["server_definition_paths"] = {
|
||||||
dropshell_base + "/servers"
|
dropshell_base + "/servers"
|
||||||
@ -81,6 +81,10 @@ bool config::save_config(bool create_aux_directories)
|
|||||||
"https://templates.dropshell.app"
|
"https://templates.dropshell.app"
|
||||||
};
|
};
|
||||||
mConfig["template_upload_token"] = "SECRETTOKEN";
|
mConfig["template_upload_token"] = "SECRETTOKEN";
|
||||||
|
|
||||||
|
mConfig["backups_path"] = {
|
||||||
|
dropshell_base + "/backups"
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
config_file << mConfig.dump(4);
|
config_file << mConfig.dump(4);
|
||||||
@ -116,14 +120,14 @@ bool config::is_agent_installed()
|
|||||||
return std::filesystem::exists(localfile::bb64());
|
return std::filesystem::exists(localfile::bb64());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> config::get_template_registry_urls() {
|
std::vector<tRegistryEntry> config::get_template_registry_urls() {
|
||||||
nlohmann::json template_registry_urls = mConfig["template_registry_URLs"];
|
nlohmann::json template_registries = mConfig["template_registries"];
|
||||||
std::vector<std::string> urls;
|
std::vector<tRegistryEntry> registries;
|
||||||
for (auto &url : template_registry_urls) {
|
for (auto ®istry : template_registries) {
|
||||||
if (url.is_string() && !url.empty())
|
if (registry.is_object() && !registry.empty())
|
||||||
urls.push_back(url);
|
registries.push_back(tRegistryEntry(registry));
|
||||||
}
|
}
|
||||||
return urls;
|
return registries;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> config::get_local_template_paths()
|
std::vector<std::string> config::get_local_template_paths()
|
||||||
@ -163,16 +167,43 @@ std::string config::get_template_create_path()
|
|||||||
return paths[0];
|
return paths[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_template_upload_url()
|
std::string config::get_backups_path()
|
||||||
{
|
{
|
||||||
std::vector<std::string> urls = get_template_registry_urls();
|
nlohmann::json backups_path = mConfig["backups_path"];
|
||||||
if (urls.empty())
|
if (backups_path.empty())
|
||||||
|
return "";
|
||||||
|
if (backups_path.is_string())
|
||||||
|
return backups_path;
|
||||||
|
warning << "backups_path is not a string: " << backups_path << std::endl;
|
||||||
return "";
|
return "";
|
||||||
return urls[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string config::get_template_upload_token() {
|
dropshell::tRegistryEntry::tRegistryEntry(nlohmann::json json)
|
||||||
return mConfig["template_upload_token"];
|
{
|
||||||
|
valid = false;
|
||||||
|
if (json.is_object() && !json.empty()) {
|
||||||
|
for (auto &[key, value] : json.items()) {
|
||||||
|
if (value.is_string() && !value.empty())
|
||||||
|
switch (switchhash(key.c_str())) {
|
||||||
|
case switchhash("name"):
|
||||||
|
name = value;
|
||||||
|
break;
|
||||||
|
case switchhash("url"):
|
||||||
|
url = value;
|
||||||
|
break;
|
||||||
|
case switchhash("token"):
|
||||||
|
token = value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
valid = (!url.empty()&&!name.empty()); // token can be empty.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tRegistryEntry::~tRegistryEntry()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -4,10 +4,23 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#define JSON_INLINE_ALL
|
#define JSON_INLINE_ALL
|
||||||
#include "json.hpp"
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
|
class tRegistryEntry {
|
||||||
|
|
||||||
|
public:
|
||||||
|
tRegistryEntry(nlohmann::json json);
|
||||||
|
~tRegistryEntry();
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
std::string url;
|
||||||
|
std::string token;
|
||||||
|
bool valid;
|
||||||
|
};
|
||||||
|
|
||||||
class config {
|
class config {
|
||||||
public:
|
public:
|
||||||
config();
|
config();
|
||||||
@ -19,14 +32,13 @@ class config {
|
|||||||
bool is_config_set() const;
|
bool is_config_set() const;
|
||||||
static bool is_agent_installed();
|
static bool is_agent_installed();
|
||||||
|
|
||||||
std::vector<std::string> get_template_registry_urls();
|
std::vector<tRegistryEntry> get_template_registry_urls();
|
||||||
std::vector<std::string> get_local_template_paths();
|
std::vector<std::string> get_local_template_paths();
|
||||||
std::vector<std::string> get_local_server_definition_paths();
|
std::vector<std::string> get_local_server_definition_paths();
|
||||||
|
|
||||||
std::string get_server_create_path();
|
std::string get_server_create_path();
|
||||||
std::string get_template_create_path();
|
std::string get_template_create_path();
|
||||||
std::string get_template_upload_url();
|
std::string get_backups_path();
|
||||||
std::string get_template_upload_token();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
nlohmann::json mConfig;
|
nlohmann::json mConfig;
|
||||||
|
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
@ -80,7 +80,7 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
std::cerr << "Error: " << e.what() << std::endl;
|
error << "Uncaught Exception: " << e.what() << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ namespace dropshell
|
|||||||
catch (const std::exception &e)
|
catch (const std::exception &e)
|
||||||
{
|
{
|
||||||
error << "Failed to parse " << server_json_path << std::endl;
|
error << "Failed to parse " << server_json_path << std::endl;
|
||||||
error << "Error: " << e.what() << std::endl;
|
error << "Exception: " << e.what() << std::endl;
|
||||||
|
|
||||||
mValid = false;
|
mValid = false;
|
||||||
}
|
}
|
||||||
@ -241,7 +241,7 @@ namespace dropshell
|
|||||||
bool okay = execute_ssh_command(sshinfo, scommand, cMode::Silent);
|
bool okay = execute_ssh_command(sshinfo, scommand, cMode::Silent);
|
||||||
if (!okay)
|
if (!okay)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Required items not found on remote server: " << file_names_str << std::endl;
|
error << "Required items not found on remote server: " << file_names_str << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -341,7 +341,7 @@ namespace dropshell
|
|||||||
std::map<std::string, std::string> env_vars;
|
std::map<std::string, std::string> env_vars;
|
||||||
if (!get_all_service_env_vars(mServerName, service_name, env_vars))
|
if (!get_all_service_env_vars(mServerName, service_name, env_vars))
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Failed to get all service env vars for " << service_name << std::endl;
|
error << "Failed to get all service env vars for " << service_name << std::endl;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,7 +358,7 @@ namespace dropshell
|
|||||||
|
|
||||||
if (sc.empty())
|
if (sc.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Failed to construct command for " << service_name << " " << command << std::endl;
|
error << "Failed to construct command for " << service_name << " " << command << std::endl;
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
return sc;
|
return sc;
|
||||||
@ -388,7 +388,7 @@ namespace dropshell
|
|||||||
ServerConfig env(server_name);
|
ServerConfig env(server_name);
|
||||||
if (!env.is_valid())
|
if (!env.is_valid())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Invalid server environment file: " << entry.path().string() << std::endl;
|
error << "Invalid server environment file: " << entry.path().string() << std::endl;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
servers.push_back(env);
|
servers.push_back(env);
|
||||||
@ -406,7 +406,7 @@ namespace dropshell
|
|||||||
std::string server_existing_dir = localpath::server(server_name);
|
std::string server_existing_dir = localpath::server(server_name);
|
||||||
if (!server_existing_dir.empty())
|
if (!server_existing_dir.empty())
|
||||||
{
|
{
|
||||||
error << "Error: Server name already exists: " << server_name << std::endl;
|
error << "Server name already exists: " << server_name << std::endl;
|
||||||
info << "Current server path: " << server_existing_dir << std::endl;
|
info << "Current server path: " << server_existing_dir << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -415,7 +415,7 @@ namespace dropshell
|
|||||||
auto lsdp = gConfig().get_local_server_definition_paths();
|
auto lsdp = gConfig().get_local_server_definition_paths();
|
||||||
if (lsdp.empty() || lsdp[0].empty())
|
if (lsdp.empty() || lsdp[0].empty())
|
||||||
{
|
{
|
||||||
error << "Error: Local server definition path not found" << std::endl;
|
error << "Local server definition path not found" << std::endl;
|
||||||
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -434,7 +434,9 @@ namespace dropshell
|
|||||||
server_env_file << " \"USER\": \"" << user << "\"," << std::endl;
|
server_env_file << " \"USER\": \"" << user << "\"," << std::endl;
|
||||||
server_env_file << " \"DIR\": \"" << "/home/" + user << "/.dropshell\"" << std::endl;
|
server_env_file << " \"DIR\": \"" << "/home/" + user << "/.dropshell\"" << std::endl;
|
||||||
server_env_file << " }" << std::endl;
|
server_env_file << " }" << std::endl;
|
||||||
server_env_file << " ]" << std::endl;
|
server_env_file << " ]," << std::endl;
|
||||||
|
server_env_file << " \"HAS_DOCKER\": \"true\"," << std::endl;
|
||||||
|
server_env_file << " \"DOCKER_ROOTLESS\": \"false\"" << std::endl;
|
||||||
server_env_file << "}" << std::endl;
|
server_env_file << "}" << std::endl;
|
||||||
server_env_file.close();
|
server_env_file.close();
|
||||||
|
|
||||||
|
@ -34,8 +34,8 @@ namespace dropshell
|
|||||||
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||||
if (local_server_definition_paths.empty())
|
if (local_server_definition_paths.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: No local server definition paths found" << std::endl;
|
error << "No local server definition paths found" << std::endl;
|
||||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ namespace dropshell
|
|||||||
auto service_info = get_service_info(server_name, service_name);
|
auto service_info = get_service_info(server_name, service_name);
|
||||||
if (service_info.local_template_path.empty())
|
if (service_info.local_template_path.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Service not found: " << service_name << std::endl;
|
error << "Service not found: " << service_name << std::endl;
|
||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ namespace dropshell
|
|||||||
auto service_info = get_service_info(server_name, service_name);
|
auto service_info = get_service_info(server_name, service_name);
|
||||||
if (service_info.local_template_path.empty())
|
if (service_info.local_template_path.empty())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Service not found: " << service_name << std::endl;
|
error << "Service not found: " << service_name << std::endl;
|
||||||
return backups;
|
return backups;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,7 +211,7 @@ namespace dropshell
|
|||||||
|
|
||||||
if (localpath::service(server_name, service_name).empty() || !fs::exists(localpath::service(server_name, service_name)))
|
if (localpath::service(server_name, service_name).empty() || !fs::exists(localpath::service(server_name, service_name)))
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Service not found: " << service_name << " on server " << server_name << std::endl;
|
error << "Service not found: " << service_name << " on server " << server_name << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,7 +265,7 @@ namespace dropshell
|
|||||||
template_info tinfo = gTemplateManager().get_template_info(it->second);
|
template_info tinfo = gTemplateManager().get_template_info(it->second);
|
||||||
if (!tinfo.is_set())
|
if (!tinfo.is_set())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Template '" << it->second << "' not found" << std::endl;
|
error << "Template '" << it->second << "' not found" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +163,7 @@
|
|||||||
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
ASSERT(mLoaded && mSources.size() > 0, "Template manager not loaded, or no template sources found.");
|
||||||
template_source_interface* source = get_source(template_name);
|
template_source_interface* source = get_source(template_name);
|
||||||
if (!source) {
|
if (!source) {
|
||||||
std::cerr << "Error: Template '" << template_name << "' not found" << std::endl;
|
error << "Template '" << template_name << "' not found" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return source->template_command_exists(template_name, command);
|
return source->template_command_exists(template_name, command);
|
||||||
@ -175,21 +175,21 @@
|
|||||||
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
std::vector<std::string> local_server_definition_paths = gConfig().get_local_server_definition_paths();
|
||||||
|
|
||||||
if (local_server_definition_paths.empty()) {
|
if (local_server_definition_paths.empty()) {
|
||||||
std::cerr << "Error: No local server definition paths found" << std::endl;
|
error << "No local server definition paths found" << std::endl;
|
||||||
std::cerr << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
info << "Run 'dropshell edit' to configure DropShell" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto info = get_template_info(template_name);
|
auto tinfo = get_template_info(template_name);
|
||||||
if (info.is_set()) {
|
if (tinfo.is_set()) {
|
||||||
std::cerr << "Error: Template '" << template_name << "' already exists at " << info.locationID() << std::endl;
|
error << "Template '" << template_name << "' already exists at " << tinfo.locationID() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto local_template_paths = gConfig().get_local_template_paths();
|
auto local_template_paths = gConfig().get_local_template_paths();
|
||||||
if (local_template_paths.empty()) {
|
if (local_template_paths.empty()) {
|
||||||
std::cerr << "Error: No local template paths found" << std::endl;
|
error << "No local template paths found" << std::endl;
|
||||||
std::cerr << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
|
info << "Run 'dropshell edit' to add one to the DropShell config" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string new_template_path = local_template_paths[0] + "/" + template_name;
|
std::string new_template_path = local_template_paths[0] + "/" + template_name;
|
||||||
@ -200,7 +200,7 @@
|
|||||||
// 2. Copy the example template from the system templates directory
|
// 2. Copy the example template from the system templates directory
|
||||||
auto example_info = gTemplateManager().get_template_info("example-nginx");
|
auto example_info = gTemplateManager().get_template_info("example-nginx");
|
||||||
if (!example_info.is_set()) {
|
if (!example_info.is_set()) {
|
||||||
std::cerr << "Error: Example template not found" << std::endl;
|
error << "Example template not found" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
std::string example_template_path = example_info.local_template_path();
|
std::string example_template_path = example_info.local_template_path();
|
||||||
@ -222,7 +222,7 @@
|
|||||||
std::string replacement_line = "TEMPLATE=" + template_name;
|
std::string replacement_line = "TEMPLATE=" + template_name;
|
||||||
std::string service_env_path = new_template_path + "/config/" + filenames::template_info_env;
|
std::string service_env_path = new_template_path + "/config/" + filenames::template_info_env;
|
||||||
if (!replace_line_in_file(service_env_path, search_string, replacement_line)) {
|
if (!replace_line_in_file(service_env_path, search_string, replacement_line)) {
|
||||||
std::cerr << "Error: Failed to replace TEMPLATE= line in the " << filenames::template_info_env <<" file" << std::endl;
|
error << "Failed to replace TEMPLATE= line in the " << filenames::template_info_env <<" file" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +278,7 @@
|
|||||||
bool template_manager::required_file(std::string path, std::string template_name)
|
bool template_manager::required_file(std::string path, std::string template_name)
|
||||||
{
|
{
|
||||||
if (!std::filesystem::exists(path)) {
|
if (!std::filesystem::exists(path)) {
|
||||||
std::cerr << "Error: " << path << " file not found in template - REQUIRED." << template_name << std::endl;
|
error << path << " file not found in template - REQUIRED." << template_name << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -323,7 +323,7 @@
|
|||||||
std::filesystem::path path = template_path + "/" + file;
|
std::filesystem::path path = template_path + "/" + file;
|
||||||
auto perms = std::filesystem::status(path).permissions();
|
auto perms = std::filesystem::status(path).permissions();
|
||||||
if ((perms & std::filesystem::perms::owner_exec) == std::filesystem::perms::none)
|
if ((perms & std::filesystem::perms::owner_exec) == std::filesystem::perms::none)
|
||||||
std::cerr << "Error: " << file << " is not executable" << std::endl;
|
error << file << " is not executable" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,18 +347,18 @@
|
|||||||
// determine template name.
|
// determine template name.
|
||||||
auto it = all_env_vars.find("TEMPLATE");
|
auto it = all_env_vars.find("TEMPLATE");
|
||||||
if (it == all_env_vars.end()) {
|
if (it == all_env_vars.end()) {
|
||||||
std::cerr << "Error: TEMPLATE variable not found in " << template_path << std::endl;
|
error << "TEMPLATE variable not found in " << template_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string env_template_name = it->second;
|
std::string env_template_name = it->second;
|
||||||
if (env_template_name.empty()) {
|
if (env_template_name.empty()) {
|
||||||
std::cerr << "Error: TEMPLATE variable is empty in " << template_path << std::endl;
|
error << "TEMPLATE variable is empty in " << template_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (env_template_name != template_name) {
|
if (env_template_name != template_name) {
|
||||||
std::cerr << "Error: TEMPLATE variable is wrong in " << template_path << std::endl;
|
error << "TEMPLATE variable is wrong in " << template_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,8 +4,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
|
#include "config.hpp"
|
||||||
#define JSON_INLINE_ALL
|
#define JSON_INLINE_ALL
|
||||||
#include "json.hpp"
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
@ -50,7 +51,7 @@ class template_source_interface {
|
|||||||
|
|
||||||
class template_source_registry : public template_source_interface {
|
class template_source_registry : public template_source_interface {
|
||||||
public:
|
public:
|
||||||
template_source_registry(std::string URL) : mURL(URL) {}
|
template_source_registry(tRegistryEntry registry) : mRegistry(registry) {}
|
||||||
|
|
||||||
~template_source_registry() {}
|
~template_source_registry() {}
|
||||||
|
|
||||||
@ -59,11 +60,11 @@ class template_source_registry : public template_source_interface {
|
|||||||
template_info get_template_info(const std::string& template_name);
|
template_info get_template_info(const std::string& template_name);
|
||||||
bool template_command_exists(const std::string& template_name,const std::string& command);
|
bool template_command_exists(const std::string& template_name,const std::string& command);
|
||||||
|
|
||||||
std::string get_description() { return "Registry: " + mURL; }
|
std::string get_description() { return "Registry: " + mRegistry.name + " (" + mRegistry.url + ")"; }
|
||||||
private:
|
private:
|
||||||
std::filesystem::path get_cache_dir();
|
std::filesystem::path get_cache_dir();
|
||||||
private:
|
private:
|
||||||
std::string mURL;
|
tRegistryEntry mRegistry;
|
||||||
std::vector<nlohmann::json> mTemplates; // cached list.
|
std::vector<nlohmann::json> mTemplates; // cached list.
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -7,30 +7,27 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace localfile
|
||||||
|
{
|
||||||
|
|
||||||
namespace localfile {
|
std::string dropshell_json()
|
||||||
|
{
|
||||||
std::string dropshell_json() {
|
return localpath::dropshell_dir() + "/" + filenames::dropshell_json;
|
||||||
// Try ~/.config/dropshell/dropshell.json
|
|
||||||
std::string homedir = localpath::current_user_home();
|
|
||||||
if (!homedir.empty()) {
|
|
||||||
fs::path user_path = fs::path(homedir) / ".config" / "dropshell" / filenames::dropshell_json;
|
|
||||||
return user_path.string();
|
|
||||||
}
|
|
||||||
return std::string();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string server_json(const std::string &server_name) {
|
std::string server_json(const std::string &server_name)
|
||||||
|
{
|
||||||
std::string serverpath = localpath::server(server_name);
|
std::string serverpath = localpath::server(server_name);
|
||||||
return (serverpath.empty() ? "" : (fs::path(serverpath) / filenames::server_json).string());
|
return (serverpath.empty() ? "" : (fs::path(serverpath) / filenames::server_json).string());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string service_env(const std::string &server_name, const std::string &service_name) {
|
std::string service_env(const std::string &server_name, const std::string &service_name)
|
||||||
|
{
|
||||||
std::string servicepath = localpath::service(server_name, service_name);
|
std::string servicepath = localpath::service(server_name, service_name);
|
||||||
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::service_env).string());
|
return (servicepath.empty() ? "" : (fs::path(servicepath) / filenames::service_env).string());
|
||||||
}
|
}
|
||||||
@ -51,13 +48,20 @@ namespace localfile {
|
|||||||
return localpath::agent_local() + "/bb64";
|
return localpath::agent_local() + "/bb64";
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace localfile
|
} // namespace localfile
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
namespace localpath
|
||||||
|
{
|
||||||
|
|
||||||
namespace localpath {
|
std::string dropshell_dir()
|
||||||
std::string server(const std::string &server_name) {
|
{
|
||||||
|
return current_user_home() + "/.dropshell";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string server(const std::string &server_name)
|
||||||
|
{
|
||||||
for (std::filesystem::path dir : gConfig().get_local_server_definition_paths())
|
for (std::filesystem::path dir : gConfig().get_local_server_definition_paths())
|
||||||
if (fs::exists(dir / server_name))
|
if (fs::exists(dir / server_name))
|
||||||
return dir / server_name;
|
return dir / server_name;
|
||||||
@ -65,28 +69,23 @@ namespace localpath {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string service(const std::string &server_name, const std::string &service_name) {
|
std::string service(const std::string &server_name, const std::string &service_name)
|
||||||
|
{
|
||||||
std::string serverpath = localpath::server(server_name);
|
std::string serverpath = localpath::server(server_name);
|
||||||
return ((serverpath.empty() || service_name.empty()) ? "" : (serverpath+"/"+service_name));
|
return ((serverpath.empty() || service_name.empty()) ? "" : (serverpath + "/" + service_name));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string remote_versions(const std::string &server_name, const std::string &service_name)
|
|
||||||
{
|
|
||||||
std::string template_cache_path = localpath::template_cache();
|
|
||||||
return ((template_cache_path.empty() || service_name.empty()) ? "" :
|
|
||||||
(template_cache_path+"/remote_versions/"+service_name+".json"));
|
|
||||||
}
|
|
||||||
std::string agent_local()
|
std::string agent_local()
|
||||||
{
|
{
|
||||||
return current_user_home()+"/.local/dropshell_agent/agent-local";
|
return dropshell_dir() + "/agent-local";
|
||||||
}
|
}
|
||||||
std::string agent_remote()
|
std::string agent_remote()
|
||||||
{
|
{
|
||||||
return current_user_home() + "/.local/dropshell_agent/agent-remote";
|
return dropshell_dir() + "/agent-remote";
|
||||||
}
|
}
|
||||||
std::string current_user_home()
|
std::string current_user_home()
|
||||||
{
|
{
|
||||||
char * homedir = std::getenv("HOME");
|
char *homedir = std::getenv("HOME");
|
||||||
if (homedir)
|
if (homedir)
|
||||||
{
|
{
|
||||||
std::filesystem::path homedir_path(homedir);
|
std::filesystem::path homedir_path(homedir);
|
||||||
@ -96,37 +95,32 @@ namespace localpath {
|
|||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string dropshell_files()
|
|
||||||
{
|
|
||||||
return current_user_home() + "/.local/dropshell_files";
|
|
||||||
return std::string();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string backups()
|
std::string backups()
|
||||||
{
|
{
|
||||||
return dropshell_files() + "/backups";
|
if (!gConfig().is_config_set())
|
||||||
|
return "";
|
||||||
|
return gConfig().get_backups_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string temp_files()
|
std::string temp_files()
|
||||||
{
|
{
|
||||||
return dropshell_files() + "/temp_files";
|
return dropshell_dir() + "/temp_files";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string template_cache()
|
std::string template_cache()
|
||||||
{
|
{
|
||||||
return dropshell_files() + "template_cache";
|
return dropshell_dir() + "/template_cache";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool create_directories()
|
bool create_directories()
|
||||||
{
|
{
|
||||||
std::vector<std::filesystem::path> paths = {
|
std::vector<std::filesystem::path> paths = {
|
||||||
dropshell_files(),
|
dropshell_dir(),
|
||||||
agent_local(),
|
agent_local(),
|
||||||
agent_remote(),
|
agent_remote(),
|
||||||
template_cache(),
|
template_cache(),
|
||||||
backups(),
|
backups(),
|
||||||
temp_files()
|
temp_files()};
|
||||||
};
|
|
||||||
for (auto &p : gConfig().get_local_server_definition_paths())
|
for (auto &p : gConfig().get_local_server_definition_paths())
|
||||||
paths.push_back(p);
|
paths.push_back(p);
|
||||||
|
|
||||||
@ -139,7 +133,7 @@ namespace localpath {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace localpath
|
} // namespace localpath
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------
|
||||||
// remote paths
|
// remote paths
|
||||||
@ -159,21 +153,26 @@ namespace localpath {
|
|||||||
// |-- service.env (default service config)
|
// |-- service.env (default service config)
|
||||||
// |-- (other config files for specific server&service)
|
// |-- (other config files for specific server&service)
|
||||||
|
|
||||||
|
remotefile::remotefile(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
||||||
remotefile::remotefile(const std::string &server_name, const std::string &user) :
|
|
||||||
mServer_name(server_name), mUser(user) {}
|
|
||||||
|
|
||||||
std::string remotefile::service_env(const std::string &service_name) const
|
std::string remotefile::service_env(const std::string &service_name) const
|
||||||
{
|
{
|
||||||
return remotepath(mServer_name,mUser).service_config(service_name) + "/" + filenames::service_env;
|
return remotepath(mServer_name, mUser).service_config(service_name) + "/" + filenames::service_env;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
remotepath::remotepath(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
remotepath::remotepath(const std::string &server_name, const std::string &user) : mServer_name(server_name), mUser(user) {}
|
||||||
|
|
||||||
std::string remotepath::DROPSHELL_DIR() const
|
std::string remotepath::DROPSHELL_DIR() const
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return ServerConfig(mServer_name).get_user_dir(mUser);
|
return ServerConfig(mServer_name).get_user_dir(mUser);
|
||||||
|
} catch (const std::exception &e)
|
||||||
|
{
|
||||||
|
error << "Failed to get remote dropshell directory for " << mServer_name << "@" << mUser << std::endl;
|
||||||
|
error << "Exception: " << e.what() << std::endl;
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string remotepath::services() const
|
std::string remotepath::services() const
|
||||||
@ -218,22 +217,21 @@ namespace localpath {
|
|||||||
return (dsp.empty() ? "" : (dsp + "/agent"));
|
return (dsp.empty() ? "" : (dsp + "/agent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------
|
||||||
|
// Utility functions
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------
|
std::string get_parent(const std::filesystem::path path)
|
||||||
// Utility functions
|
{
|
||||||
|
|
||||||
std::string get_parent(const std::filesystem::path path)
|
|
||||||
{
|
|
||||||
if (path.empty())
|
if (path.empty())
|
||||||
return std::string();
|
return std::string();
|
||||||
return path.parent_path().string();
|
return path.parent_path().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string get_child(const std::filesystem::path path)
|
std::string get_child(const std::filesystem::path path)
|
||||||
{
|
{
|
||||||
if (path.empty())
|
if (path.empty())
|
||||||
return std::string();
|
return std::string();
|
||||||
return path.filename().string();
|
return path.filename().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
||||||
|
@ -11,19 +11,14 @@ namespace dropshell {
|
|||||||
//------------------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------------------
|
||||||
// local user config directories
|
// local user config directories
|
||||||
|
|
||||||
// ~/.config/dropshell/dropshell.json
|
// ~/.dropshell
|
||||||
|
// |-- dropshell.json
|
||||||
// ~/.local/dropshell_agent
|
|
||||||
// |-- agent-local
|
// |-- agent-local
|
||||||
// |-- agent-install.sh
|
// |-- agent-install.sh
|
||||||
// |-- bb64 (only used locally, as it's for the local machine's architecture!)
|
// |-- bb64 (only used locally, as it's for the local machine's architecture!)
|
||||||
// |-- template_example
|
// |-- template_example
|
||||||
// |-- agent-remote
|
// |-- agent-remote
|
||||||
// |-- (remote agent files, including _allservicesstatus.sh)
|
// |-- (remote agent files, including _allservicesstatus.sh)
|
||||||
|
|
||||||
// ~/.local/dropshell_files
|
|
||||||
// |-- backups
|
|
||||||
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
|
|
||||||
// |-- temp_files
|
// |-- temp_files
|
||||||
// |-- template_cache
|
// |-- template_cache
|
||||||
// |-- templates
|
// |-- templates
|
||||||
@ -35,6 +30,10 @@ namespace dropshell {
|
|||||||
// | |-- .template_info.env
|
// | |-- .template_info.env
|
||||||
// | |-- (...other service config files...)
|
// | |-- (...other service config files...)
|
||||||
|
|
||||||
|
// backups_path
|
||||||
|
// |-- katie-_-squashkiwi-_-squashkiwi-test-_-2025-04-28_21-23-59.tgz
|
||||||
|
|
||||||
|
|
||||||
// server_definition_path
|
// server_definition_path
|
||||||
// |-- <server_name>
|
// |-- <server_name>
|
||||||
// |-- server.json
|
// |-- server.json
|
||||||
@ -53,7 +52,6 @@ namespace dropshell {
|
|||||||
} // namespace filenames.
|
} // namespace filenames.
|
||||||
|
|
||||||
namespace localfile {
|
namespace localfile {
|
||||||
// ~/.config/dropshell/dropshell.json
|
|
||||||
std::string dropshell_json();
|
std::string dropshell_json();
|
||||||
std::string server_json(const std::string &server_name);
|
std::string server_json(const std::string &server_name);
|
||||||
std::string service_env(const std::string &server_name, const std::string &service_name);
|
std::string service_env(const std::string &server_name, const std::string &service_name);
|
||||||
@ -63,16 +61,15 @@ namespace dropshell {
|
|||||||
} // namespace localfile
|
} // namespace localfile
|
||||||
|
|
||||||
namespace localpath {
|
namespace localpath {
|
||||||
|
std::string dropshell_dir();
|
||||||
|
|
||||||
std::string server(const std::string &server_name);
|
std::string server(const std::string &server_name);
|
||||||
std::string service(const std::string &server_name, const std::string &service_name);
|
std::string service(const std::string &server_name, const std::string &service_name);
|
||||||
|
|
||||||
std::string remote_versions(const std::string &server_name, const std::string &service_name);
|
|
||||||
|
|
||||||
std::string agent_local();
|
std::string agent_local();
|
||||||
std::string agent_remote();
|
std::string agent_remote();
|
||||||
std::string current_user_home();
|
std::string current_user_home();
|
||||||
|
|
||||||
std::string dropshell_files();
|
|
||||||
std::string backups();
|
std::string backups();
|
||||||
std::string temp_files();
|
std::string temp_files();
|
||||||
std::string template_cache();
|
std::string template_cache();
|
||||||
|
@ -183,7 +183,7 @@ namespace dropshell
|
|||||||
|
|
||||||
if (!rval && !hasFlag(mode, cMode::Silent))
|
if (!rval && !hasFlag(mode, cMode::Silent))
|
||||||
{
|
{
|
||||||
error << "Error: Failed to execute ssh command:" << std::endl;
|
error << "Failed to execute ssh command:" << std::endl;
|
||||||
debug << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path) << std::endl;
|
debug << ssh_cmd.str() + " " + remote_command.construct_cmd(remote_bb64_path) << std::endl;
|
||||||
}
|
}
|
||||||
return rval;
|
return rval;
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#define XXH_INLINE_ALL
|
#define XXH_INLINE_ALL
|
||||||
#include "contrib/xxhash.hpp"
|
#include "contrib/xxhash.hpp"
|
||||||
|
|
||||||
|
#include "output.hpp"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@ -13,7 +15,7 @@ uint64_t hash_file(const std::string &path) {
|
|||||||
// Create hash state
|
// Create hash state
|
||||||
XXH64_state_t* const state = XXH64_createState();
|
XXH64_state_t* const state = XXH64_createState();
|
||||||
if (state == nullptr) {
|
if (state == nullptr) {
|
||||||
std::cerr << "Failed to create hash state" << std::endl;
|
error << "Failed to create hash state" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +26,7 @@ uint64_t hash_file(const std::string &path) {
|
|||||||
// Open file
|
// Open file
|
||||||
std::ifstream file(path, std::ios::binary);
|
std::ifstream file(path, std::ios::binary);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
std::cerr << "Failed to open file: " << path << std::endl;
|
error << "Failed to open file: " << path << std::endl;
|
||||||
XXH64_freeState(state);
|
XXH64_freeState(state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -34,7 +36,7 @@ uint64_t hash_file(const std::string &path) {
|
|||||||
char buffer[buffer_size];
|
char buffer[buffer_size];
|
||||||
while (file.read(buffer, buffer_size)) {
|
while (file.read(buffer, buffer_size)) {
|
||||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||||
std::cerr << "Failed to update hash" << std::endl;
|
error << "Failed to update hash" << std::endl;
|
||||||
XXH64_freeState(state);
|
XXH64_freeState(state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -43,7 +45,7 @@ uint64_t hash_file(const std::string &path) {
|
|||||||
// Handle any remaining bytes
|
// Handle any remaining bytes
|
||||||
if (file.gcount() > 0) {
|
if (file.gcount() > 0) {
|
||||||
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
if (XXH64_update(state, buffer, file.gcount()) == XXH_ERROR) {
|
||||||
std::cerr << "Failed to update hash" << std::endl;
|
error << "Failed to update hash" << std::endl;
|
||||||
XXH64_freeState(state);
|
XXH64_freeState(state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -59,14 +61,14 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
|||||||
// Create hash state
|
// Create hash state
|
||||||
XXH64_state_t* const state = XXH64_createState();
|
XXH64_state_t* const state = XXH64_createState();
|
||||||
if (state == nullptr) {
|
if (state == nullptr) {
|
||||||
std::cerr << "Failed to create hash state" << std::endl;
|
error << "Failed to create hash state" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize state with seed 0
|
// Initialize state with seed 0
|
||||||
XXH64_hash_t const seed = 0; /* or any other value */
|
XXH64_hash_t const seed = 0; /* or any other value */
|
||||||
if (XXH64_reset(state, seed) == XXH_ERROR) {
|
if (XXH64_reset(state, seed) == XXH_ERROR) {
|
||||||
std::cerr << "Failed to reset hash state" << std::endl;
|
error << "Failed to reset hash state" << std::endl;
|
||||||
XXH64_freeState(state);
|
XXH64_freeState(state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -81,7 +83,7 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (const std::filesystem::filesystem_error& e) {
|
} catch (const std::filesystem::filesystem_error& e) {
|
||||||
std::cerr << "Filesystem error: " << e.what() << std::endl;
|
error << "Filesystem error: " << e.what() << std::endl;
|
||||||
XXH64_freeState(state);
|
XXH64_freeState(state);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -94,7 +96,7 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
|||||||
|
|
||||||
uint64_t hash_path(const std::string &path) {
|
uint64_t hash_path(const std::string &path) {
|
||||||
if (!std::filesystem::exists(path)) {
|
if (!std::filesystem::exists(path)) {
|
||||||
std::cerr << "Path does not exist: " << path << std::endl;
|
error << "Path does not exist: " << path << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,28 +105,28 @@ uint64_t hash_path(const std::string &path) {
|
|||||||
} else if (std::filesystem::is_regular_file(path)) {
|
} else if (std::filesystem::is_regular_file(path)) {
|
||||||
return hash_file(path);
|
return hash_file(path);
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Path is neither a file nor a directory: " << path << std::endl;
|
error << "Path is neither a file nor a directory: " << path << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hash_demo(const std::string & path)
|
void hash_demo(const std::string & path)
|
||||||
{
|
{
|
||||||
std::cout << "Hashing path: " << path << std::endl;
|
info << "Hashing path: " << path << std::endl;
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
XXH64_hash_t hash = hash_path(path);
|
XXH64_hash_t hash = hash_path(path);
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||||
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
info << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
int hash_demo_raw(const std::string & path)
|
int hash_demo_raw(const std::string & path)
|
||||||
{
|
{
|
||||||
if (!std::filesystem::exists(path)) {
|
if (!std::filesystem::exists(path)) {
|
||||||
std::cout << 0 <<std::endl; return 1;
|
info << 0 <<std::endl; return 1;
|
||||||
}
|
}
|
||||||
XXH64_hash_t hash = hash_path(path);
|
XXH64_hash_t hash = hash_path(path);
|
||||||
std::cout << hash << std::endl;
|
info << hash << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,187 +0,0 @@
|
|||||||
// __ _____ _____ _____
|
|
||||||
// __| | __| | | | JSON for Modern C++
|
|
||||||
// | | |__ | | | | | | version 3.12.0
|
|
||||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
||||||
//
|
|
||||||
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
|
||||||
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
|
||||||
|
|
||||||
#include <cstdint> // int64_t, uint64_t
|
|
||||||
#include <map> // map
|
|
||||||
#include <memory> // allocator
|
|
||||||
#include <string> // string
|
|
||||||
#include <vector> // vector
|
|
||||||
|
|
||||||
// #include <nlohmann/detail/abi_macros.hpp>
|
|
||||||
// __ _____ _____ _____
|
|
||||||
// __| | __| | | | JSON for Modern C++
|
|
||||||
// | | |__ | | | | | | version 3.12.0
|
|
||||||
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
||||||
//
|
|
||||||
// SPDX-FileCopyrightText: 2013 - 2025 Niels Lohmann <https://nlohmann.me>
|
|
||||||
// SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// This file contains all macro definitions affecting or depending on the ABI
|
|
||||||
|
|
||||||
#ifndef JSON_SKIP_LIBRARY_VERSION_CHECK
|
|
||||||
#if defined(NLOHMANN_JSON_VERSION_MAJOR) && defined(NLOHMANN_JSON_VERSION_MINOR) && defined(NLOHMANN_JSON_VERSION_PATCH)
|
|
||||||
#if NLOHMANN_JSON_VERSION_MAJOR != 3 || NLOHMANN_JSON_VERSION_MINOR != 12 || NLOHMANN_JSON_VERSION_PATCH != 0
|
|
||||||
#warning "Already included a different version of the library!"
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define NLOHMANN_JSON_VERSION_MAJOR 3 // NOLINT(modernize-macro-to-enum)
|
|
||||||
#define NLOHMANN_JSON_VERSION_MINOR 12 // NOLINT(modernize-macro-to-enum)
|
|
||||||
#define NLOHMANN_JSON_VERSION_PATCH 0 // NOLINT(modernize-macro-to-enum)
|
|
||||||
|
|
||||||
#ifndef JSON_DIAGNOSTICS
|
|
||||||
#define JSON_DIAGNOSTICS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef JSON_DIAGNOSTIC_POSITIONS
|
|
||||||
#define JSON_DIAGNOSTIC_POSITIONS 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
||||||
#define JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_DIAGNOSTICS
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS _diag
|
|
||||||
#else
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_DIAGNOSTIC_POSITIONS
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS _dp
|
|
||||||
#else
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON _ldvcmp
|
|
||||||
#else
|
|
||||||
#define NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_NO_VERSION 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Construct the namespace ABI tags component
|
|
||||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c) json_abi ## a ## b ## c
|
|
||||||
#define NLOHMANN_JSON_ABI_TAGS_CONCAT(a, b, c) \
|
|
||||||
NLOHMANN_JSON_ABI_TAGS_CONCAT_EX(a, b, c)
|
|
||||||
|
|
||||||
#define NLOHMANN_JSON_ABI_TAGS \
|
|
||||||
NLOHMANN_JSON_ABI_TAGS_CONCAT( \
|
|
||||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTICS, \
|
|
||||||
NLOHMANN_JSON_ABI_TAG_LEGACY_DISCARDED_VALUE_COMPARISON, \
|
|
||||||
NLOHMANN_JSON_ABI_TAG_DIAGNOSTIC_POSITIONS)
|
|
||||||
|
|
||||||
// Construct the namespace version component
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch) \
|
|
||||||
_v ## major ## _ ## minor ## _ ## patch
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(major, minor, patch) \
|
|
||||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT_EX(major, minor, patch)
|
|
||||||
|
|
||||||
#if NLOHMANN_JSON_NAMESPACE_NO_VERSION
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_VERSION
|
|
||||||
#else
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_VERSION \
|
|
||||||
NLOHMANN_JSON_NAMESPACE_VERSION_CONCAT(NLOHMANN_JSON_VERSION_MAJOR, \
|
|
||||||
NLOHMANN_JSON_VERSION_MINOR, \
|
|
||||||
NLOHMANN_JSON_VERSION_PATCH)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Combine namespace components
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b) a ## b
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_CONCAT(a, b) \
|
|
||||||
NLOHMANN_JSON_NAMESPACE_CONCAT_EX(a, b)
|
|
||||||
|
|
||||||
#ifndef NLOHMANN_JSON_NAMESPACE
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE \
|
|
||||||
nlohmann::NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
|
||||||
NLOHMANN_JSON_ABI_TAGS, \
|
|
||||||
NLOHMANN_JSON_NAMESPACE_VERSION)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_BEGIN \
|
|
||||||
namespace nlohmann \
|
|
||||||
{ \
|
|
||||||
inline namespace NLOHMANN_JSON_NAMESPACE_CONCAT( \
|
|
||||||
NLOHMANN_JSON_ABI_TAGS, \
|
|
||||||
NLOHMANN_JSON_NAMESPACE_VERSION) \
|
|
||||||
{
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef NLOHMANN_JSON_NAMESPACE_END
|
|
||||||
#define NLOHMANN_JSON_NAMESPACE_END \
|
|
||||||
} /* namespace (inline namespace) NOLINT(readability/namespace) */ \
|
|
||||||
} // namespace nlohmann
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief namespace for Niels Lohmann
|
|
||||||
@see https://github.com/nlohmann
|
|
||||||
@since version 1.0.0
|
|
||||||
*/
|
|
||||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief default JSONSerializer template argument
|
|
||||||
|
|
||||||
This serializer ignores the template arguments and uses ADL
|
|
||||||
([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl))
|
|
||||||
for serialization.
|
|
||||||
*/
|
|
||||||
template<typename T = void, typename SFINAE = void>
|
|
||||||
struct adl_serializer;
|
|
||||||
|
|
||||||
/// a class to store JSON values
|
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/
|
|
||||||
template<template<typename U, typename V, typename... Args> class ObjectType =
|
|
||||||
std::map,
|
|
||||||
template<typename U, typename... Args> class ArrayType = std::vector,
|
|
||||||
class StringType = std::string, class BooleanType = bool,
|
|
||||||
class NumberIntegerType = std::int64_t,
|
|
||||||
class NumberUnsignedType = std::uint64_t,
|
|
||||||
class NumberFloatType = double,
|
|
||||||
template<typename U> class AllocatorType = std::allocator,
|
|
||||||
template<typename T, typename SFINAE = void> class JSONSerializer =
|
|
||||||
adl_serializer,
|
|
||||||
class BinaryType = std::vector<std::uint8_t>, // cppcheck-suppress syntaxError
|
|
||||||
class CustomBaseClass = void>
|
|
||||||
class basic_json;
|
|
||||||
|
|
||||||
/// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document
|
|
||||||
/// @sa https://json.nlohmann.me/api/json_pointer/
|
|
||||||
template<typename RefStringType>
|
|
||||||
class json_pointer;
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@brief default specialization
|
|
||||||
@sa https://json.nlohmann.me/api/json/
|
|
||||||
*/
|
|
||||||
using json = basic_json<>;
|
|
||||||
|
|
||||||
/// @brief a minimal map-like container that preserves insertion order
|
|
||||||
/// @sa https://json.nlohmann.me/api/ordered_map/
|
|
||||||
template<class Key, class T, class IgnoredLess, class Allocator>
|
|
||||||
struct ordered_map;
|
|
||||||
|
|
||||||
/// @brief specialization that maintains the insertion order of object keys
|
|
||||||
/// @sa https://json.nlohmann.me/api/ordered_json/
|
|
||||||
using ordered_json = basic_json<nlohmann::ordered_map>;
|
|
||||||
|
|
||||||
NLOHMANN_JSON_NAMESPACE_END
|
|
||||||
|
|
||||||
#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_
|
|
@ -1,4 +1,8 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
#include "httplib.hpp"
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
@ -10,6 +14,7 @@
|
|||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cctype>
|
#include <cctype>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
@ -35,7 +40,7 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
|
|||||||
std::string line;
|
std::string line;
|
||||||
|
|
||||||
if (!input_file.is_open()) {
|
if (!input_file.is_open()) {
|
||||||
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
|
error << "Unable to open file: " << file_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +55,7 @@ bool replace_line_in_file(const std::string& file_path, const std::string& searc
|
|||||||
std::ofstream output_file(file_path);
|
std::ofstream output_file(file_path);
|
||||||
if (!output_file.is_open())
|
if (!output_file.is_open())
|
||||||
{
|
{
|
||||||
std::cerr << "Error: Unable to open file: " << file_path << std::endl;
|
error << "Unable to open file: " << file_path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (const auto& modified_line : file_lines)
|
for (const auto& modified_line : file_lines)
|
||||||
@ -151,7 +156,7 @@ int str2int(const std::string &str)
|
|||||||
try {
|
try {
|
||||||
return std::stoi(str);
|
return std::stoi(str);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
std::cerr << "Error: Invalid integer string: [" << str << "]" << std::endl;
|
error << "Invalid integer string: [" << str << "]" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -308,7 +313,8 @@ std::string requote(std::string str) {
|
|||||||
|
|
||||||
|
|
||||||
int die(const std::string & msg) {
|
int die(const std::string & msg) {
|
||||||
std::cerr << msg << std::endl;
|
error << "Fatal error:" << std::endl;
|
||||||
|
error << msg << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -445,4 +451,203 @@ std::string tolower(const std::string& str) {
|
|||||||
return result;
|
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)
|
||||||
|
{
|
||||||
|
// Create a backup of the original file
|
||||||
|
std::string backup_path = filepath + ".bak";
|
||||||
|
try {
|
||||||
|
std::filesystem::copy_file(filepath, backup_path, std::filesystem::copy_options::overwrite_existing);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "Error creating backup file: " << e.what() << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle empty segment
|
||||||
|
if (segment.empty()) {
|
||||||
|
error << "Empty segment provided" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// split the segment into lines
|
||||||
|
std::vector<std::string> segment_lines = split(segment, "\n");
|
||||||
|
// remove empty lines
|
||||||
|
segment_lines.erase(std::remove_if(segment_lines.begin(), segment_lines.end(), [](const std::string& line) {
|
||||||
|
return trim(line).empty();
|
||||||
|
}), segment_lines.end());
|
||||||
|
// remove any lines that are just whitespace
|
||||||
|
segment_lines.erase(std::remove_if(segment_lines.begin(), segment_lines.end(), [](const std::string& line) { return trim(line).empty(); }), segment_lines.end());
|
||||||
|
|
||||||
|
// check that the segment has at least two lines
|
||||||
|
if (segment_lines.size() < 2) {
|
||||||
|
error << "Segment must contain at least two non-empty lines" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the entire file into memory
|
||||||
|
std::ifstream input_file(filepath);
|
||||||
|
if (!input_file.is_open()) {
|
||||||
|
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();
|
||||||
|
|
||||||
|
// Store original file size for verification
|
||||||
|
size_t original_size = file_lines.size();
|
||||||
|
if (original_size == 0) {
|
||||||
|
warning << "File is empty" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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], segment_lines[0])) {
|
||||||
|
// Found potential start, look for end
|
||||||
|
for (size_t j = i + 1; j < file_lines.size(); j++) {
|
||||||
|
if (match_line(file_lines[j], segment_lines[segment_lines.size() - 1])) {
|
||||||
|
// Found matching block, replace it
|
||||||
|
file_lines.erase(file_lines.begin() + i, file_lines.begin() + j + 1);
|
||||||
|
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) {
|
||||||
|
file_lines.insert(file_lines.end(), segment_lines.begin(), segment_lines.end());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write back to file
|
||||||
|
std::ofstream output_file(filepath);
|
||||||
|
if (!output_file.is_open()) {
|
||||||
|
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();
|
||||||
|
|
||||||
|
// If everything succeeded, remove the backup
|
||||||
|
try {
|
||||||
|
std::filesystem::remove(backup_path);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
warning << "Could not remove backup file: " << e.what() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
@ -5,6 +5,7 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
#include "output.hpp"
|
#include "output.hpp"
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace dropshell {
|
namespace dropshell {
|
||||||
|
|
||||||
@ -61,4 +62,17 @@ std::string get_line_wrap(std::string & src, int maxchars);
|
|||||||
|
|
||||||
std::string tolower(const std::string& str);
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
constexpr unsigned int switchhash(const char *s, int off = 0)
|
||||||
|
{
|
||||||
|
return !s[off] ? 5381 : (switchhash(s, off + 1) * 33) ^ s[off];
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dropshell
|
} // namespace dropshell
|
Reference in New Issue
Block a user