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,13 +72,13 @@ onInstall:
# Create a function for structured logging # Create a function for structured logging
cat > /home/jelastic/add-sftp-user-addon/log_helper.sh << 'EOF' cat > /home/jelastic/add-sftp-user-addon/log_helper.sh << 'EOF'
#!/bin/bash #!/bin/bash
LOG_DIR="/home/jelastic/add-sftp-user-addon/logs" LOG_DIR="/home/jelastic/add-sftp-user-addon/logs"
SCRIPT_ID="$(date +%Y%m%d%H%M%S)-$$" SCRIPT_ID="$(date +%Y%m%d%H%M%S)-$$"
# Main logging function # Main logging function
jps_log() { jps_log() {
local level=${1:-INFO} local level=${1:-INFO}
local message=${2} local message=${2}
local log_file=${3:-$LOG_DIR/script_output.log} local log_file=${3:-$LOG_DIR/script_output.log}
@ -98,10 +98,10 @@ jps_log() {
if [[ "$level" == "INFO" || "$level" == "SUCCESS" ]]; then if [[ "$level" == "INFO" || "$level" == "SUCCESS" ]]; then
echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/operations/$(basename "$log_file")" echo "[$SCRIPT_ID] $timestamp [$level] $message" >> "$LOG_DIR/operations/$(basename "$log_file")"
fi fi
} }
# Log command execution # Log command execution
jps_log_cmd() { jps_log_cmd() {
local cmd="$1" local cmd="$1"
local desc="$2" local desc="$2"
local log_file=${3:-$LOG_DIR/script_output.log} local log_file=${3:-$LOG_DIR/script_output.log}
@ -123,10 +123,10 @@ jps_log_cmd() {
fi fi
return $status return $status
} }
# Log system information # Log system information
jps_log_system_info() { jps_log_system_info() {
local log_file=${1:-$LOG_DIR/script_output.log} local log_file=${1:-$LOG_DIR/script_output.log}
jps_log "DEBUG" "============= SYSTEM INFORMATION =============" "$log_file" jps_log "DEBUG" "============= SYSTEM INFORMATION =============" "$log_file"
@ -135,75 +135,30 @@ jps_log_system_info() {
jps_log "DEBUG" "SSH Version: $(ssh -V 2>&1)" "$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" "SSH Status: $(systemctl status sshd | grep Active | awk '{print $2}')" "$log_file"
jps_log "DEBUG" "=============================================" "$log_file" jps_log "DEBUG" "=============================================" "$log_file"
} }
EOF
# Function to get detailed user information including creation date # Make the logging script executable
# This replaces the functionality of the retired userlogs.sh script chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
jps_get_user_info() {
local log_file=${1:-$LOG_DIR/list_users.log}
jps_log "INFO" "Retrieving detailed user information" "$log_file" # 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
# Get SFTP users # Source the logging helper
local users=$(find /home/sftpusers -maxdepth 1 -mindepth 1 -type d -exec basename {} \;) source /home/jelastic/add-sftp-user-addon/log_helper.sh
if [ -z "$users" ]; then # Log installation started
jps_log "INFO" "No SFTP users found" "$log_file" jps_log "INFO" "======== SFTP ADDON INSTALLATION STARTED ========" "install.log"
return 0 jps_log_system_info "install.log"
fi
jps_log "INFO" "Found users, retrieving creation dates" "$log_file" # Install SFTP addon on Jelastic environment
local result="" 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"
# Process each user jps_log_cmd "touch /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Creating script_output.log"
for user in $users; do 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"
# Get creation date from directory timestamp jps_log_cmd "chmod +x /home/jelastic/add-sftp-user-addon/*.sh" "Making SFTP script executable"
local creation_date=$(stat -c "%y" "/home/sftpusers/$user" 2>/dev/null | cut -d. -f1) jps_log_cmd "echo \"$(date) - Installing SFTP addon on Jelastic environment\" >> /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Logging installation"
# 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)
fi
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
# 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
# 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]: - cmd[cp]:
user: root user: root
@ -530,19 +485,20 @@ actions:
# Log list users action # Log list users action
jps_log "INFO" "======== LISTING SFTP USERS ========" "list_users.log" jps_log "INFO" "======== LISTING SFTP USERS ========" "list_users.log"
# Note: This functionality replaces the retired userlogs.sh script # List users with proper error handling
# with a more efficient and integrated approach jps_log "INFO" "Retrieving list of SFTP users" "list_users.log"
# Get detailed user information # Use a safer approach to listing
USER_INFO=$(jps_get_user_info "list_users.log") 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 # Check if any users were found
if [ -z "$USER_INFO" ]; then if [ -z "$USERS_LIST" ]; then
jps_log "WARNING" "No SFTP users found" "list_users.log" jps_log "WARNING" "No SFTP users found" "list_users.log"
echo "" echo ""
else else
jps_log "SUCCESS" "Retrieved list of SFTP users" "list_users.log" 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 fi
jps_log "INFO" "======== USER LISTING COMPLETED ========" "list_users.log" 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