forked from sskafandri/redis-cpanel-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_redis.sh
166 lines (141 loc) · 4.56 KB
/
manage_redis.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
# Following Bash Script Is Not Being Used Anymore. Moved To RedisManager.php Class.
echo 'Functionality Was Moved To RedisManager.php Class'
# Exit the script
exit
# Define Variables
ACTION=$1
USERNAME=$2
CONFIG_DIR="/home/$USERNAME/.cpplugin/redis"
CONFIG_FILE="$CONFIG_DIR/redis.conf"
LOG_DIR="/home/$USERNAME/.cpplugin/redis/log"
LOG_FILE="$LOG_DIR/$USERNAME.log"
REDIS_CLI=$(which redis-cli) # Update this path based on your redis-cli location
USER_REDIS_DIR="/home/$USERNAME/.cpplugin/redis/data"
# Ensure log directory exists and set permissions
mkdir -p $LOG_DIR
touch $LOG_FILE # Create an empty log file if it doesn't exist
chown -R $USERNAME:$USERNAME $LOG_DIR $LOG_FILE
chmod 755 $LOG_DIR
chmod 644 $LOG_FILE
# Logging Function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE
}
# Function To Find an Available Port
find_available_port() {
local port
while true; do
port=$(shuf -i 55000-60000 -n 1)
if ! lsof -i:$port &>/dev/null; then
echo $port
return
fi
done
}
# Function To Create Redis Config If It Doesn't Exist
create_redis_config() {
local username=$1
local config_dir=$CONFIG_DIR
local config_file=$CONFIG_FILE
if [ ! -f $config_file ]; then
log "Creating New Redis Config For $username"
PASSWORD=$(openssl rand -base64 8)
mkdir -p $config_dir
touch $config_file # Create empty files if they don't exist
chown -R $username:$username $config_dir $config_file
chmod 755 $config_dir
chmod 644 $config_file
if [ -f $config_file ]; then
PORT=$(find_available_port)
{
echo "bind 127.0.0.1"
echo "port $PORT"
echo "requirepass $PASSWORD"
echo "dir $USER_REDIS_DIR"
echo "maxmemory 256mb"
echo "databases 16"
} >> $config_file
else
log "Failed to create the config file $config_file"
echo "Failed to create the config file $config_file" >&2
exit 1
fi
mkdir -p $USER_REDIS_DIR
chown -R $username:$username $USER_REDIS_DIR
chmod 755 $USER_REDIS_DIR
fi
}
# Function To Start Redis
start_redis() {
local username=$1
local config_file=$CONFIG_FILE
if [ ! -f $config_file ]; then
create_redis_config($username)
fi
# Start Redis server and capture the output
local output=$(cd $USER_REDIS_DIR && redis-server $config_file --daemonize yes 2>&1)
local status=$?
log "Redis start command: redis-server $config_file --daemonize yes"
log "Redis start command output: $output"
if [ $status -eq 0 ]; then
log "Started Redis for $username"
echo "Redis started successfully: $output"
else
log "Failed to start Redis for $username: $output"
exit 1
fi
}
# Function To Stop Redis
stop_redis() {
local username=$1
$REDIS_CLI -h 127.0.0.1 -p $(grep '^port' $CONFIG_FILE | awk '{print $2}') -a $(grep 'requirepass' $CONFIG_FILE | awk '{print $2}') shutdown
if [ $? -eq 0 ]; then
echo "Stopped Redis for $username"
log "Stopped Redis for $username"
else
echo "Failed to stop Redis for $username"
log "Failed to stop Redis for $username"
exit 1
fi
}
# Function To Check Redis Status
check_redis_status() {
local username=$1
local config_file=$CONFIG_FILE
# Check if config file exists
if [ ! -f $config_file ]; then
echo "inactive"
echo "Redis was never started for $username before. Please click on Start Redis Button."
log "Redis never started for $username because config file is missing"
return
fi
# Retrieve Port Number From The Config File
local port=$(grep '^port' $config_file | awk '{print $2}')
# Check If a Redis Process Is Running with the Specified Port
if ps aux | grep -q "[r]edis.*127.0.0.1:$port"; then
echo "Running $port $(grep 'requirepass' $config_file | awk '{print $2}')"
echo "Running - Redis Started Successfully."
log "Redis is running for $username on port $port"
else
echo "inactive"
log "Redis is inactive for $username"
fi
}
log "Action: $ACTION, Username: $USERNAME"
case $ACTION in
start)
start_redis $USERNAME
;;
stop)
stop_redis $USERNAME
;;
status)
check_redis_status $USERNAME
;;
*)
echo "Usage: $0 {start|stop|status} username"
log "Invalid action: $ACTION"
exit 1
;;
esac