This commit is contained in:
Your Name
2025-04-21 10:25:28 +12:00
parent 093dee3fd3
commit 8b807da346
5 changed files with 79 additions and 78 deletions

View File

View File

@ -1,10 +1,51 @@
#!/bin/bash
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/src/version.sh"
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to create directory with proper ownership
mkdir_with_ownership() {
local target_dir="$1"
local current_dir="$target_dir"
local parent_dir
local owner
local group
local created_dirs=()
# Find the lowest existing parent directory
while [ ! -d "$current_dir" ] && [ "$current_dir" != "/" ]; do
created_dirs+=("$current_dir")
parent_dir="$(dirname "$current_dir")"
current_dir="$parent_dir"
done
# If we found an existing directory, get its ownership
if [ -d "$current_dir" ]; then
owner=$(stat -c %U "$current_dir")
group=$(stat -c %G "$current_dir")
else
echo -e "${RED}Error: Could not find any existing parent directory${NC}"
return 1
fi
# Create the directory and set ownership for all created directories
if mkdir -p "$target_dir"; then
# Set ownership for all created directories in reverse order
for ((i=${#created_dirs[@]}-1; i>=0; i--)); do
chown "$owner:$group" "${created_dirs[$i]}"
done
return 0
else
echo -e "${RED}Error: Failed to create directory $target_dir${NC}"
return 1
fi
}
# Check if USER_DEFINITIONS is provided
if [ -z "$1" ]; then
echo -e "${RED}Error: USER_DEFINITIONS path is required${NC}"
@ -16,10 +57,12 @@ fi
USER_DEFINITIONS="$1"
# Script locations
SCRIPT_SOURCE="$(pwd)/src/dropshell.sh"
SCRIPT_SOURCE="$SCRIPT_DIR/src/dropshell.sh"
SCRIPT_DEST="/usr/local/bin/dropshell"
COMPLETION_SOURCE="$(pwd)/src/dropshell-completion.bash"
COMPLETION_SOURCE="$SCRIPT_DIR/src/dropshell-completion.bash"
COMPLETION_DEST="/etc/bash_completion.d/dropshell"
EXAMPLE_DIR="$SCRIPT_DIR/.example"
DROPSHELL_OPT="/opt/dropshell"
# Check if running with sudo/root
if [ "$EUID" -ne 0 ]; then
@ -41,34 +84,43 @@ if [ ! -f "$COMPLETION_SOURCE" ]; then
exit 1
fi
# Create user definitions directory if it doesn't exist
if [ ! -d "$USER_DEFINITIONS" ]; then
# Create user definitions directory structure
echo "Creating user definitions directory structure..."
mkdir -p "$USER_DEFINITIONS/servers"
mkdir -p "$USER_DEFINITIONS/templates"
# Create example files
mkdir -p "$USER_DEFINITIONS/servers/example.com"
cat > "$USER_DEFINITIONS/services/example.com/server.json" << 'EOF'
{
"ssh_address": "localhost",
"ssh_port": 22,
"ssh_user": "$(whoami)"
}
EOF
# cat > "$USER_DEFINITIONS/services/example.com/server.json" << 'EOF'
# {
# "ssh_address": "localhost",
# "ssh_port": 22,
# "ssh_user": "$(whoami)"
# }
# EOF
echo "Creating user definitions directory..."
mkdir_with_ownership "$USER_DEFINITIONS" || exit 1
fi
# Check if example directory exists and copy example files if needed
if [ ! -d "$EXAMPLE_DIR" ]; then
echo -e "${RED}Error: Example directory not found at $EXAMPLE_DIR${NC}"
echo "Please ensure the .example directory exists in the project root"
exit 1
fi
# Check and create servers directory
if [ ! -d "$USER_DEFINITIONS/servers" ]; then
echo "Copying example files..."
mkdir_with_ownership "$USER_DEFINITIONS/servers" || exit 1
cp -ra "$EXAMPLE_DIR"/servers/* "$USER_DEFINITIONS/servers"
fi
# Check and create usertemplates directory
if [ ! -d "$USER_DEFINITIONS/usertemplates" ]; then
echo "Copying example files..."
mkdir_with_ownership "$USER_DEFINITIONS/usertemplates" || exit 1
cp -ra "$EXAMPLE_DIR"/usertemplates/* "$USER_DEFINITIONS/usertemplates"
fi
# install user directory.
echo "Creating symbolic link for /opt/dropshell/user..."
mkdir -p "$DROPSHELL_OPT" || exit 1
if [ -e "$DROPSHELL_OPT/user" ]; then
rm -rf "$DROPSHELL_OPT/user"
fi
ln -s "$USER_DEFINITIONS" "$DROPSHELL_OPT/user"
# Create symbolic link
echo "Creating symbolic link..."
echo "Creating symbolic link for running from $SCRIPT_DEST..."
if [ -L "$SCRIPT_DEST" ]; then
rm "$SCRIPT_DEST"
fi
@ -81,7 +133,7 @@ chmod +x "$COMPLETION_SOURCE"
# Install bash completion
echo "Installing bash completion..."
if [ ! -d "/etc/bash_completion.d" ]; then
mkdir -p "/etc/bash_completion.d"
mkdir_with_ownership "/etc/bash_completion.d" || exit 1
fi
# Install completion script
@ -98,8 +150,5 @@ EOF
echo "source $COMPLETION_DEST" >> ~/.bashrc
echo -e "${GREEN}Installation completed successfully!${NC}"
echo "User definitions directory created at: $USER_DEFINITIONS"
echo
echo "Please run 'source ~/.bashrc' or start a new terminal to enable completion"
exit 0

View File

@ -38,8 +38,6 @@ print_usage() {
echo " version - Show version information"
echo " status - Check system status"
echo " servers - List configured servers"
echo " services - List configured services"
echo " templates - List available templates"
echo
echo "Options:"
echo " -v, --verbose Enable verbose output"
@ -64,47 +62,7 @@ check_status() {
# Function to list servers
list_servers() {
echo -e "${GREEN}Configured Servers:${NC}"
if [ -d "$USER_DEFINITIONS/servers" ]; then
for server in "$USER_DEFINITIONS/servers"/*.json; do
if [ -f "$server" ]; then
name=$(jq -r '.name' "$server")
host=$(jq -r '.host' "$server")
echo "- $name ($host)"
fi
done
else
echo "No servers configured"
fi
}
# Function to list services
list_services() {
echo -e "${GREEN}Configured Services:${NC}"
if [ -d "$USER_DEFINITIONS/services" ]; then
for service in "$USER_DEFINITIONS/services"/*.json; do
if [ -f "$service" ]; then
name=$(jq -r '.name' "$service")
echo "- $name"
fi
done
else
echo "No services configured"
fi
}
# Function to list templates
list_templates() {
echo -e "${GREEN}Available Templates:${NC}"
if [ -d "$USER_DEFINITIONS/templates" ]; then
for template in "$USER_DEFINITIONS/templates"/*.sh; do
if [ -f "$template" ]; then
echo "- $(basename "$template")"
fi
done
else
echo "No templates available"
fi
}
# Main script logic
@ -121,12 +79,6 @@ case "$1" in
"servers")
list_servers
;;
"services")
list_services
;;
"templates")
list_templates
;;
"")
echo -e "${RED}Error: No command provided${NC}"
print_usage