Updates
parent
8c918671bd
commit
f1541e6669
220
manifest.jps
220
manifest.jps
|
@ -72,138 +72,93 @@ 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"
|
|
||||||
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)
|
|
||||||
|
|
||||||
# Try to get last password change as fallback
|
LOG_DIR="/home/jelastic/add-sftp-user-addon/logs"
|
||||||
if [ -z "$creation_date" ]; then
|
SCRIPT_ID="$(date +%Y%m%d%H%M%S)-$$"
|
||||||
if id "$user" &>/dev/null; then
|
|
||||||
creation_date=$(chage -l "$user" 2>/dev/null | grep "Last password change" | cut -d: -f2)
|
# 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
|
||||||
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
|
# Log command execution
|
||||||
result="${result}Username: $user - Created: $creation_date\n"
|
jps_log_cmd() {
|
||||||
else
|
local cmd="$1"
|
||||||
result="${result}Username: $user - Created: Unknown\n"
|
local desc="$2"
|
||||||
fi
|
local log_file=${3:-$LOG_DIR/script_output.log}
|
||||||
done
|
|
||||||
|
jps_log "DEBUG" "Executing: $desc" "$log_file"
|
||||||
if [ -n "$result" ]; then
|
jps_log "DEBUG" "Command: $cmd" "$log_file"
|
||||||
jps_log "SUCCESS" "User information retrieved successfully" "$log_file"
|
|
||||||
echo -e "$result"
|
# Execute command and capture output and status
|
||||||
else
|
local output
|
||||||
jps_log "WARNING" "Could not retrieve user information" "$log_file"
|
output=$(eval "$cmd" 2>&1)
|
||||||
echo ""
|
local status=$?
|
||||||
fi
|
|
||||||
}
|
if [ $status -eq 0 ]; then
|
||||||
|
jps_log "DEBUG" "Command succeeded: $desc" "$log_file"
|
||||||
# Make the logging script executable
|
[ -n "$output" ] && jps_log "DEBUG" "Output: $output" "$log_file"
|
||||||
chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
|
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
|
# Log system information
|
||||||
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
|
jps_log_system_info() {
|
||||||
chmod +x /home/jelastic/add-sftp-user-addon/add-sftp.sh
|
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
|
# Make the logging script executable
|
||||||
source /home/jelastic/add-sftp-user-addon/log_helper.sh
|
chmod +x /home/jelastic/add-sftp-user-addon/log_helper.sh
|
||||||
|
|
||||||
# Log installation started
|
# Download the SFTP script
|
||||||
jps_log "INFO" "======== SFTP ADDON INSTALLATION STARTED ========" "install.log"
|
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
|
||||||
jps_log_system_info "install.log"
|
chmod +x /home/jelastic/add-sftp-user-addon/add-sftp.sh
|
||||||
|
|
||||||
# Install SFTP addon on Jelastic environment
|
# Source the logging helper
|
||||||
jps_log_cmd "mkdir -p /home/jelastic/add-sftp-user-addon/" "Creating log directory structure"
|
source /home/jelastic/add-sftp-user-addon/log_helper.sh
|
||||||
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"
|
# Log installation started
|
||||||
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 "INFO" "======== SFTP ADDON INSTALLATION STARTED ========" "install.log"
|
||||||
jps_log_cmd "chmod +x /home/jelastic/add-sftp-user-addon/*.sh" "Making SFTP script executable"
|
jps_log_system_info "install.log"
|
||||||
jps_log_cmd "echo \"$(date) - Installing SFTP addon on Jelastic environment\" >> /home/jelastic/add-sftp-user-addon/logs/script_output.log" "Logging installation"
|
|
||||||
|
# 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"
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue