-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmanage_redis.sh
150 lines (136 loc) · 4.45 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
#!/bin/bash
ACTION=$1
USERNAME=$2
CONFIG_FILE="/etc/redis/$USERNAME.conf"
PASSWORD_FILE="/etc/redis/$USERNAME.pass"
LOG_DIR="/var/log/redis"
LOG_FILE="$LOG_DIR/$USERNAME.log"
SYSTEMD_SERVICE_DIR="/etc/systemd/system"
REDIS_CLI=$(which redis-cli) # Update this path based on your redis-cli location
# 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 $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 create systemd service unit
create_systemd_service() {
local username=$1
local port=$2
local config_file="/etc/redis/$username.conf"
local service_file="$SYSTEMD_SERVICE_DIR/redis-$username.service"
cat > $service_file <<EOF
[Unit]
Description=Redis instance for $username
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/bin/redis-server $config_file --daemonize yes --supervised systemd
ExecStop=/usr/libexec/redis-shutdown
Type=notify
User=$username
Group=$username
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
EOF
chmod 644 $service_file
systemctl daemon-reload
systemctl enable redis-$username
systemctl start redis-$username
}
# Function to check if a port is available
is_port_available() {
local port=$1
if ! lsof -i:$port &>/dev/null; then
return 0 # Port is available
else
return 1 # Port is in use
fi
}
# Find an available port
find_available_port() {
local port
while true; do
port=$(shuf -i 55000-60000 -n 1)
if is_port_available $port; then
echo $port
return
fi
done
}
log "Action: $ACTION, Username: $USERNAME"
case $ACTION in
start)
if [ ! -f $CONFIG_FILE ]; then
log "Creating New Redis Config For $USERNAME"
PASSWORD=$(openssl rand -base64 16)
echo $PASSWORD > $PASSWORD_FILE
cp /etc/redis/redis-template.conf $CONFIG_FILE
sed -i "s/%h/$USERNAME/g" $CONFIG_FILE
PORT=$(find_available_port)
echo "" >> $CONFIG_FILE
echo "port $PORT" >> $CONFIG_FILE
echo "requirepass $PASSWORD" >> $CONFIG_FILE
echo "maxmemory 256mb" >> $CONFIG_FILE
echo "databases 16" >> $CONFIG_FILE
mkdir -p /var/lib/redis/$USERNAME
mkdir -p $LOG_DIR # Ensure log directory is created
chown -R $USERNAME:$USERNAME /var/lib/redis/$USERNAME
chown -R $USERNAME:$USERNAME $LOG_DIR
chmod 755 /var/lib/redis/$USERNAME
chmod 755 $LOG_DIR
chown $USERNAME:$USERNAME $CONFIG_FILE $PASSWORD_FILE
chmod 644 $CONFIG_FILE $PASSWORD_FILE
else
PASSWORD=$(cat $PASSWORD_FILE)
# Retrieve the existing port number from the config file
PORT=$(grep '^port' $CONFIG_FILE | awk '{print $2}')
fi
log "Starting Redis For $USERNAME With Config $CONFIG_FILE"
# Check if the port is already in use
if is_port_available $PORT; then
create_systemd_service $USERNAME $PORT
else
echo "Port $PORT is already in use. Cannot start Redis."
log "Port $PORT is already in use. Cannot start Redis."
exit 1
fi
;;
stop)
log "Stopping Redis For $USERNAME"
sudo systemctl stop redis-$USERNAME
if [ $? -eq 0 ]; then
echo "Redis Stopped for $USERNAME"
log "Redis Stopped Successfully For $USERNAME"
systemctl disable redis-$USERNAME
rm -f $SYSTEMD_SERVICE_DIR/redis-$USERNAME.service
systemctl daemon-reload
else
echo "Failed To Stop Redis For $USERNAME"
log "Failed To Stop Redis For $USERNAME"
fi
;;
status)
if systemctl is-active --quiet redis-$USERNAME; then
PASSWORD=$(cat $PASSWORD_FILE)
PORT=$(grep '^port' $CONFIG_FILE | awk '{print $2}')
echo "Running $PORT $PASSWORD"
log "Redis is Running For $USERNAME on Port $PORT"
else
echo "inactive"
log "Redis is Inactive For $USERNAME"
fi
;;
*)
echo "Usage: $0 {start|stop|status} username"
log "Invalid action: $ACTION"
exit 1
;;
esac