main
Anthony 2025-04-10 23:26:58 +08:00
parent 8c918671bd
commit f1541e6669
2 changed files with 95 additions and 132 deletions

View File

@ -72,138 +72,93 @@ onInstall:
# Create a function for structured logging
cat > /home/jelastic/add-sftp-user-addon/log_helper.sh << 'EOF'
#!/bin/bash
LOG_DIR="/home/jelastic/add-sftp-user-addon/logs"
SCRIPT_ID="$(date +%Y%m%d%H%M%S)-$$"
# Main logging function
jps_log() {
local level=${1:-INFO}
local message=${2}
local log_file=${3:-$LOG_DIR/script_output.log}
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$log_file"
# Also log to stdout
echo "[$level] $message"
# Log errors to error log
if [[ "$level" == "ERROR" || "$level" == "WARNING" ]]; then
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/errors/$(basename "$log_file")"
fi
# Log successful operations
if [[ "$level" == "INFO" || "$level" == "SUCCESS" ]]; then
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/operations/$(basename "$log_file")"
fi
}
# Log command execution
jps_log_cmd() {
local cmd="$1"
local desc="$2"
local log_file=${3:-$LOG_DIR/script_output.log}
jps_log "DEBUG" "Executing: $desc" "$log_file"
jps_log "DEBUG" "Command: $cmd" "$log_file"
# Execute command and capture output and status
local output
output=$(eval "$cmd" 2>&1)
local status=$?
if [ $status -eq 0 ]; then
jps_log "DEBUG" "Command succeeded: $desc" "$log_file"
[ -n "$output" ] && jps_log "DEBUG" "Output: $output" "$log_file"
else
jps_log "ERROR" "Command failed ($status): $desc" "$log_file"
jps_log "ERROR" "Error output: $output" "$log_file"
fi
return $status
}
# Log system information
jps_log_system_info() {
local log_file=${1:-$LOG_DIR/script_output.log}
jps_log "DEBUG" "============= SYSTEM INFORMATION =============" "$log_file"
jps_log "DEBUG" "Operating System: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | tr -d '\"')" "$log_file"
jps_log "DEBUG" "Kernel: $(uname -r)" "$log_file"
jps_log "DEBUG" "SSH Version: $(ssh -V 2>&1)" "$log_file"
jps_log "DEBUG" "SSH Status: $(systemctl status sshd | grep Active | awk '{print $2}')" "$log_file"
jps_log "DEBUG" "=============================================" "$log_file"
}
# Function to get detailed user information including creation date
# This replaces the functionality of the retired userlogs.sh script
jps_get_user_info() {
local log_file=${1:-$LOG_DIR/list_users.log}
jps_log "INFO" "Retrieving detailed user information" "$log_file"
# Get SFTP users
local users=$(find /home/sftpusers -maxdepth 1 -mindepth 1 -type d -exec basename {} \;)
if [ -z "$users" ]; then
jps_log "INFO" "No SFTP users found" "$log_file"
return 0
fi
jps_log "INFO" "Found users, retrieving creation dates" "$log_file"
local result=""
# Process each user
for user in $users; do
# Get creation date from directory timestamp
local creation_date=$(stat -c "%y" "/home/sftpusers/$user" 2>/dev/null | cut -d. -f1)
#!/bin/bash
# Try to get last password change as fallback
if [ -z "$creation_date" ]; then
if id "$user" &>/dev/null; then
creation_date=$(chage -l "$user" 2>/dev/null | grep "Last password change" | cut -d: -f2)
LOG_DIR="/home/jelastic/add-sftp-user-addon/logs"
SCRIPT_ID="$(date +%Y%m%d%H%M%S)-$$"
# Main logging function
jps_log() {
local level=${1:-INFO}
local message=${2}
local log_file=${3:-$LOG_DIR/script_output.log}
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$log_file"
# Also log to stdout
echo "[$level] $message"
# Log errors to error log
if [[ "$level" == "ERROR" || "$level" == "WARNING" ]]; then
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/errors/$(basename "$log_file")"
fi
fi
# Log successful operations
if [[ "$level" == "INFO" || "$level" == "SUCCESS" ]]; then
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/operations/$(basename "$log_file")"
fi
}
if [ -n "$creation_date" ]; then
result="${result}Username: $user - Created: $creation_date\n"
else
result="${result}Username: $user - Created: Unknown\n"
fi
done
if [ -n "$result" ]; then
jps_log "SUCCESS" "User information retrieved successfully" "$log_file"
echo -e "$result"
else
jps_log "WARNING" "Could not retrieve user information" "$log_file"
echo ""
fi
}
# Make the logging script executable
chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
# Log command execution
jps_log_cmd() {
local cmd="$1"
local desc="$2"
local log_file=${3:-$LOG_DIR/script_output.log}
jps_log "DEBUG" "Executing: $desc" "$log_file"
jps_log "DEBUG" "Command: $cmd" "$log_file"
# Execute command and capture output and status
local output
output=$(eval "$cmd" 2>&1)
local status=$?
if [ $status -eq 0 ]; then
jps_log "DEBUG" "Command succeeded: $desc" "$log_file"
[ -n "$output" ] && jps_log "DEBUG" "Output: $output" "$log_file"
else
jps_log "ERROR" "Command failed ($status): $desc" "$log_file"
jps_log "ERROR" "Error output: $output" "$log_file"
fi
return $status
}
# Download the SFTP script
wget https://deploy-proxy.mightybox.io/addons/add-sftp-user/raw/branch/main/add-sftp.sh -O /home/jelastic/add-sftp-user-addon/add-sftp.sh
chmod +x /home/jelastic/add-sftp-user-addon/add-sftp.sh
# Log system information
jps_log_system_info() {
local log_file=${1:-$LOG_DIR/script_output.log}
jps_log "DEBUG" "============= SYSTEM INFORMATION =============" "$log_file"
jps_log "DEBUG" "Operating System: $(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | tr -d '\"')" "$log_file"
jps_log "DEBUG" "Kernel: $(uname -r)" "$log_file"
jps_log "DEBUG" "SSH Version: $(ssh -V 2>&1)" "$log_file"
jps_log "DEBUG" "SSH Status: $(systemctl status sshd | grep Active | awk '{print $2}')" "$log_file"
jps_log "DEBUG" "=============================================" "$log_file"
}
EOF
# Source the logging helper
source /home/jelastic/add-sftp-user-addon/log_helper.sh
# Make the logging script executable
chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
# Log installation started
jps_log "INFO" "======== SFTP ADDON INSTALLATION STARTED ========" "install.log"
jps_log_system_info "install.log"
# Download the SFTP script
wget https://deploy-proxy.mightybox.io/addons/add-sftp-user/raw/branch/main/add-sftp.sh -O /home/jelastic/add-sftp-user-addon/add-sftp.sh
chmod +x /home/jelastic/add-sftp-user-addon/add-sftp.sh
# Install SFTP addon on Jelastic environment
jps_log_cmd "mkdir -p /home/jelastic/add-sftp-user-addon/" "Creating log directory structure"
jps_log_cmd "mkdir -p /home/jelastic/add-sftp-user-addon/logs" "Creating log directory structure"
jps_log_cmd "touch /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Creating script_output.log"
jps_log_cmd "wget https://deploy-proxy.mightybox.io/addons/add-sftp-user/raw/branch/main/add-sftp.sh -O /home/jelastic/add-sftp-user-addon/add-sftp.sh" "Downloading SFTP script"
jps_log_cmd "chmod +x /home/jelastic/add-sftp-user-addon/*.sh" "Making SFTP script executable"
jps_log_cmd "echo \"$(date) - Installing SFTP addon on Jelastic environment\" >> /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Logging installation"
# Source the logging helper
source /home/jelastic/add-sftp-user-addon/log_helper.sh
# Log installation started
jps_log "INFO" "======== SFTP ADDON INSTALLATION STARTED ========" "install.log"
jps_log_system_info "install.log"
# Install SFTP addon on Jelastic environment
jps_log_cmd "mkdir -p /home/jelastic/add-sftp-user-addon/" "Creating log directory structure"
jps_log_cmd "mkdir -p /home/jelastic/add-sftp-user-addon/logs" "Creating log directory structure"
jps_log_cmd "touch /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Creating script_output.log"
jps_log_cmd "wget https://deploy-proxy.mightybox.io/addons/add-sftp-user/raw/branch/main/add-sftp.sh -O /home/jelastic/add-sftp-user-addon/add-sftp.sh" "Downloading SFTP script"
jps_log_cmd "chmod +x /home/jelastic/add-sftp-user-addon/*.sh" "Making SFTP script executable"
jps_log_cmd "echo \"$(date) - Installing SFTP addon on Jelastic environment\" >> /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Logging installation"
- cmd[cp]:
user: root
@ -530,19 +485,20 @@ actions:
# Log list users action
jps_log "INFO" "======== LISTING SFTP USERS ========" "list_users.log"
# Note: This functionality replaces the retired userlogs.sh script
# with a more efficient and integrated approach
# List users with proper error handling
jps_log "INFO" "Retrieving list of SFTP users" "list_users.log"
# Get detailed user information
USER_INFO=$(jps_get_user_info "list_users.log")
# Use a safer approach to listing
USERS_LIST=$(ls -ld /home/sftpusers/* 2>/dev/null | grep -v "total" | awk '{printf "Username: %s - Created: %s %s %s\n", substr($9, 17), $6, $7, $8}')
# Output the result
if [ -z "$USER_INFO" ]; then
# Check if any users were found
if [ -z "$USERS_LIST" ]; then
jps_log "WARNING" "No SFTP users found" "list_users.log"
echo ""
else
jps_log "SUCCESS" "Retrieved list of SFTP users" "list_users.log"
echo "$USER_INFO"
jps_log "DEBUG" "Found users: $(echo "$USERS_LIST" | wc -l)" "list_users.log"
echo "$USERS_LIST"
fi
jps_log "INFO" "======== USER LISTING COMPLETED ========" "list_users.log"

View File

@ -0,0 +1,7 @@
#!/bin/bash
getent passwd | awk -F: '/^user/ {print $1}' | while read -r user; do
# Attempt to get the user's creation date from the shadow file
creation_date=$(sudo chage -l "$user" | grep 'Last password change' | cut -d: -f2)
echo "$user was created on $creation_date"
done