Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing the ipv4 address judgment logic #12

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ sh repoll-init.sh repoll # 密码自定义
* 管理员(dba角色)进行配置上线

# demo演示
http://repoll.club:8091/
http://43.143.240.39/
admin/admin


Expand Down
40 changes: 23 additions & 17 deletions polls/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@ class Meta:
fields = '__all__'


def get_running_ins(redis_type):
if redis_type == 'sentinel':
running_ins_time = RunningInsSentinel.objects.all()
elif redis_type == 'standalone':
running_ins_time = RunningInsStandalone.objects.all()
elif redis_type == 'cluster':
running_ins_time = RunningInsCluster.objects.all()
else:
running_ins_time = "default"
return running_ins_time


@api_view(['GET'])
@permission_classes((permissions.IsAuthenticated,))
def redisstop(request, redis_type, ins_id):
"""
TODO:硬编码密码
API接口,停止redis实例。
授权模式,当前平台内部用户
"""
if redis_type == 'sentinel':
running_ins_time = RunningInsSentinel.objects.all()
elif redis_type == 'standalone':
running_ins_time = RunningInsStandalone.objects.all()
elif redis_type == 'cluster':
running_ins_time = RunningInsCluster.objects.all()
running_ins_time = get_running_ins(redis_type)
running_ins = running_ins_time.filter(id=ins_id)
running_ins_ip = running_ins.values('redis_ip').first()
running_ins_port = running_ins.values('running_ins_port').first()
redisins = RedisStartClass(host=running_ins_ip['redis_ip'],
redis_server_ctl="/opt/repoll/redis/src/redis-cli -p {0} shutdown".format(running_ins_port['running_ins_port']))
redis_server_ctl="/opt/repoll/redis/src/redis-cli -a qZr3pet -p {0} shutdown".format(
running_ins_port['running_ins_port']))
serializer = RunningInsTimeSerializer(running_ins, many=True)
result = serializer.data[0]
if redisins.start_server():
Expand Down Expand Up @@ -62,25 +71,22 @@ def redisstart(request, redis_type, ins_id):
API接口,启动redis实例。
授权模式,当前平台内部用户
"""
if redis_type == 'sentinel':
running_ins_time = RunningInsSentinel.objects.all()
elif redis_type == 'standalone':
running_ins_time = RunningInsStandalone.objects.all()
elif redis_type == 'cluster':
running_ins_time = RunningInsCluster.objects.all()
running_ins_time = get_running_ins(redis_type)
running_ins = running_ins_time.filter(id=ins_id)
running_ins_ip = running_ins.values('redis_ip').first()
running_ins_port = running_ins.values('running_ins_port').first()
for c in running_ins:
running_ins_type = c.__dict__['redis_type']
if running_ins_type == 'Redis-Sentinel':
redisins = RedisStartClass(host=running_ins_ip['redis_ip'],
redis_server_ctl="/opt/repoll/redis/src/redis-server /opt/repoll/conf/{0}-sentinel.conf --sentinel".format(
redis_server_ctl="/opt/repoll/redis/src/redis-server /opt/repoll/conf/"
"{0}-sentinel.conf --sentinel".format(
running_ins_port['running_ins_port']))
else:
if redis_type == 'cluster':
redisins = RedisStartClass(host=running_ins_ip['redis_ip'],
redis_server_ctl="/opt/repoll/redis/src/redis-server /opt/repoll/conf/{0}-cluster.conf".format(
redis_server_ctl="/opt/repoll/redis/src/redis-server /opt/repoll/conf/"
"{0}-cluster.conf".format(
running_ins_port['running_ins_port']))
else:
redisins = RedisStartClass(host=running_ins_ip['redis_ip'],
Expand Down Expand Up @@ -121,7 +127,7 @@ def allredisins(request, redis_type=None):
try:
if redis_type == "all":
sentinel_ins = sentinel_ins_time.values('redis_ip', 'running_ins_port', 'redis_ins_alive',
'redis_ins_mem', 'redis_type', 'running_ins_name',)
'redis_ins_mem', 'redis_type', 'running_ins_name', )
stanalone_ins = stanalone_ins_time.values('redis_ip', 'running_ins_port', 'redis_ins_alive',
'redis_ins_mem', 'redis_type', 'running_ins_name')
cluster_ins = cluster_ins_time.values('redis_ip', 'running_ins_port', 'redis_ins_alive',
Expand Down Expand Up @@ -324,4 +330,4 @@ def import_ext_ins(request):
except IOError as e:
result['redis_status'] = False
return Response(result)
return Response(result)
return Response(result)
11 changes: 7 additions & 4 deletions polls/tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ipaddress

from django.core.exceptions import ValidationError
from django.utils.html import format_html
from IPy import IP
Expand All @@ -16,7 +18,8 @@ def my_custom_sql():
row_list = []
cursor = connection.cursor()
db_name = settings.DATABASES
cursor.execute(f"SELECT ip FROM {db_name['default']['NAME']}.polls_ipaddr")
# cursor.execute(f"SELECT ip FROM {db_name['default']['NAME']}.polls_ipaddr")
cursor.execute(f"SELECT ip FROM 'polls_ipaddr'")
row = cursor.fetchall()
for i in row:
row_list.append(i[0])
Expand All @@ -29,10 +32,10 @@ def judge_legal_ip(ip):
:param ip:
:return:
"""
compile_ip = re.compile('^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$')
if compile_ip.match(ip):
try:
ipaddress.IPv4Network(ip)
return True
else:
except ValueError:
return False


Expand Down