1. 添加云版本和远程办公标识的逻辑

2. 优化获取git远程版本的逻辑
3. 增加websocket的支持
main
RogerWork 10 months ago
parent d4f76925ba
commit 9ca73dac30
  1. 14
      dingxin_toolbox_drf/asgi.py
  2. 18
      dingxin_toolbox_drf/settings.py
  3. 5
      dingxin_toolbox_drf/urls.py
  4. 21
      update/consumers.py
  5. 23
      update/migrations/0020_cinema_is_cloud_cinema_remote_label.py
  6. 2
      update/models.py
  7. 2
      update/serializers.py
  8. 6
      update/utils/get_version.py
  9. 20
      update/utils/git_util.py
  10. 15
      update/views.py

@ -10,7 +10,17 @@ https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from .urls import websocket_urlpatterns
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dingxin_toolbox_drf.settings')
# os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dingxin_toolbox_drf.settings')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
application = get_asgi_application()
# application = get_asgi_application()
application = ProtocolTypeRouter(
{
'http': get_asgi_application(),
'websocket': URLRouter(websocket_urlpatterns)
}
)

@ -41,12 +41,13 @@ INSTALLED_APPS = [
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'channels',
'daphne',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders', # coreapi生成接口文档
'rest_framework.authtoken', # Token验证应用
'django_filters',
'channels',
'update',
'mock',
]
@ -83,7 +84,7 @@ TEMPLATES = [
WSGI_APPLICATION = 'dingxin_toolbox_drf.wsgi.application'
# 设置ASGI
ASGI_APPLICATION = ''
ASGI_APPLICATION = 'dingxin_toolbox_drf.asgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
@ -112,7 +113,7 @@ CACHES = {
}
REST_FRAMEWORK_EXTENSIONS = {
'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60*1, # 缓存时间
'DEFAULT_CACHE_RESPONSE_TIMEOUT': 60 * 1, # 缓存时间
'DEFAULT_USE_CACHE': 'default', # 缓存存储
'DEFAULT_OBJECT_CACHE_KEY_FUNC': 'rest_framework_extensions.utils.default_object_cache_key_func',
'DEFAULT_LIST_CACHE_KEY_FUNC': 'rest_framework_extensions.utils.default_list_cache_key_func',
@ -217,3 +218,14 @@ CORS_ORIGIN_ALLOW_ALL = True
# DATE_FORMAT = '%Y-%m-%d' # 设置日期格式为年/月/日
# TIME_FORMAT = '%H:%M:%S' # 设置时间格式为小时:分钟
# DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" # 设置日期时间格式为年/月/日 小时:分钟
# Channel 设置
CHANNEL_LAYERS = {
"default": {
"BACKEND": "channels_redis.core.RedisChannelLayer",
"CONFIG": {
"hosts": [("127.0.0.1", 6379)],
# "hosts": [os.environ.get('REDIS_URL', 'redis://127.0.0.1:6379/1')]
}
}
}

@ -18,6 +18,7 @@ from django.contrib import admin
from django.urls import path, include
from rest_framework.documentation import include_docs_urls
from rest_framework.authtoken.views import obtain_auth_token
from update import consumers
urlpatterns = [
path('admin/', admin.site.urls),
@ -26,3 +27,7 @@ urlpatterns = [
path('', include('mock.urls')),
path('docs/', include_docs_urls(title='接口文档')),
]
websocket_urlpatterns = [
path('ws/update/', consumers.UpdateConsumer.as_asgi())
]

@ -0,0 +1,21 @@
from channels.generic.websocket import WebsocketConsumer
import json
class UpdateConsumer(WebsocketConsumer):
def connect(self):
self.accept()
self.send('你已经连接成功')
def receive(self, text_data=None, bytes_data=None):
if text_data is None:
self.send('你发了啥')
if text_data == 'update':
self.send(json.dumps({'code': '111'}))
if text_data == 'close':
self.send('白白')
self.close()
self.disconnect('500')
else:
self.send(f'你收到的消息是{text_data}')

@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2024-01-31 07:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('update', '0019_alter_clientrelease_sub_ver'),
]
operations = [
migrations.AddField(
model_name='cinema',
name='is_cloud',
field=models.CharField(default='', help_text='是否是云版本', max_length=10, null=True, verbose_name='是否是云版本'),
),
migrations.AddField(
model_name='cinema',
name='remote_label',
field=models.CharField(default='', help_text='远程办公id', max_length=20, null=True, verbose_name='远程办公id'),
),
]

@ -16,6 +16,8 @@ class Cinema(BaseModels):
sys_ver = models.CharField(verbose_name='鼎新系统版本', max_length=50, null=False, help_text='鼎新系统版本')
client_ver = models.CharField(verbose_name='鼎新客户端版本', max_length=50, null=False, help_text='鼎新客户端版本')
user = models.CharField(verbose_name='使用人', max_length=10, null=True, help_text='当前使用人')
is_cloud = models.CharField(verbose_name='是否是云版本', max_length=10, null=True, default='', help_text='是否是云版本')
remote_label = models.CharField(verbose_name='远程办公id', max_length=20, null=True, default='', help_text='远程办公id')
def __str__(self):
return self.ip

@ -9,7 +9,7 @@ class CinemaSerializer(serializers.ModelSerializer):
model = Cinema
fields = (
'id', 'name', 'ip', 'zz_num', 'inner_id', 'db_user', 'db_pwd', 'sys_ver', 'client_ver', 'user',
'update_time')
'update_time', 'is_cloud', 'remote_label')
class CinemaUserSerializer(serializers.ModelSerializer):

@ -51,7 +51,7 @@ class GetVersion(object):
return
else:
cinema_config = q.get()
sql_str = "SELECT cs.cinema_id, cs.cinema_name, cs.cinema_num, cv.server_version, cv.client_version FROM cinema_set cs LEFT JOIN cinema_version cv ON 1=1;"
sql_str = "SELECT cs.*, cv.server_version, cv.client_version FROM cinema_set cs LEFT JOIN cinema_version cv ON 1=1;"
ip = cinema_config.get('ip')
db_config = {
'host': ip,
@ -71,7 +71,9 @@ class GetVersion(object):
'sys_ver': res['server_version'],
'client_ver': res['client_version'],
# 'update_time': datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')
'update_time': timezone.now()
'update_time': timezone.now(),
'is_cloud': '云版本' if res.get('dx_sys_version', '--') == 'CLOUD' else '--',
'remote_label': res.get('cinema_label', '--') if res.get('cinema_label', '--') != '' else '--'
}
print(ip, update_data, db_config)
cinema_obj = Cinema.objects.filter(ip=ip).update(**update_data)

@ -39,17 +39,18 @@ class GitUtil:
# 获取指定版本的最新代码
def checkout_release(self):
local_repo = Repo(self.local_code_path)
r = local_repo.git.checkout(self.short_release)
print(r)
r = local_repo.git.pull()
print(r)
checkout_result = local_repo.git.checkout(self.short_release)
print("checkout", checkout_result)
pull_result = local_repo.git.pull()
print("pull", pull_result)
return pull_result
# 复制cine.sql到本地路径cine_xxx_org.sql,然后生成新数据库名称的cine_xxx.sql
# 如果cine.sql和cine_xxx_org.sql文件md5一致则跳过
# 如果cine.sql和cine_xxx_org.sql文件md5不一致,则重新拷贝cine_xxx_org.sql并生成cine_xxx.sql
# 如果生成新的cine_xxx.sql则返回True,否则返回False
def create_cine_sql(self):
self.checkout_release()
result = self.checkout_release()
org_path = os.path.join(self.local_code_path, 'install', 'cine.sql')
target_org_path = os.path.join(self.cine_sql_path, f'{self.db_name}_org.sql')
target_path = os.path.join(self.cine_sql_path, f'{self.db_name}.sql')
@ -85,7 +86,7 @@ class GitUtil:
f.close()
target_file.writelines(target_lines)
target_file.close()
return True
return {'git_pull': result}
# 将生成的cine.sql写入数据库
def write_cine_sql_by_mysql(self):
@ -149,6 +150,7 @@ class GitDbUtil:
# 从git中获取版本列表后和数据库对比,添加新的版本到数据库
def set_release_to_db(self):
local_repo = Repo(self.local_code_path)
local_repo.git.execute('git remote update origin --prune') # 更新远程列表
local_repo.git.execute('git fetch --all')
release = []
for remote in local_repo.remotes:
@ -172,6 +174,12 @@ class GitDbUtil:
}
Release.objects.create(**data)
for local_rel in saved_release_version_list:
if local_rel in release:
pass
else:
Release.objects.filter(release=local_rel).update(status='0')
# if __name__ == '__main__':
# git_util = GitUtil()
# # git_util.clone()

@ -93,6 +93,16 @@ def get_git_version(request):
def update_cine(request):
req = request.GET.dict()
cinema_ip = req.get('ip')
update_status = {
'git': {'result': 'success', 'msg': ''},
'setup': {'result': 'success', 'msg': ''},
'db_compare': {'result': 'success', 'msg': ''},
'teardown': {'result': 'success', 'msg': ''},
'config': {'result': 'success', 'msg': ''},
'client': {'result': 'success', 'msg': ''},
}
short_release = req.get('version')
cmd_list = json.loads(req.get('cmd'))
print(cinema_ip, short_release, cmd_list)
@ -101,13 +111,10 @@ def update_cine(request):
# short_release = '2.0.33.0338_Release'
# cmd_list = [10, 11, 12, 13, 14, 15, 16, 17, 19, 20]
for cmd in cmd_list:
print(type(cmd))
# 获取并写入cine.sql
print('获取并写入cine.sql')
git_util = GitUtil(short_release)
git_util.handle_create_cine()
git_result = git_util.handle_create_cine()
# 执行setup
print('执行setup')
update_cmd = UpdateCommandUtil()

Loading…
Cancel
Save