From 9ca73dac309859364b326b82ef78d49da87fff6a Mon Sep 17 00:00:00 2001 From: RogerWork Date: Wed, 31 Jan 2024 18:14:03 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0=E4=BA=91=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=92=8C=E8=BF=9C=E7=A8=8B=E5=8A=9E=E5=85=AC=E6=A0=87?= =?UTF-8?q?=E8=AF=86=E7=9A=84=E9=80=BB=E8=BE=91=202.=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=8E=B7=E5=8F=96git=E8=BF=9C=E7=A8=8B=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=203.=20=E5=A2=9E=E5=8A=A0websocket?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dingxin_toolbox_drf/asgi.py | 14 +++++++++-- dingxin_toolbox_drf/settings.py | 18 ++++++++++++--- dingxin_toolbox_drf/urls.py | 5 ++++ update/consumers.py | 21 +++++++++++++++++ ...020_cinema_is_cloud_cinema_remote_label.py | 23 +++++++++++++++++++ update/models.py | 2 ++ update/serializers.py | 2 +- update/utils/get_version.py | 6 +++-- update/utils/git_util.py | 20 +++++++++++----- update/views.py | 15 ++++++++---- 10 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 update/consumers.py create mode 100644 update/migrations/0020_cinema_is_cloud_cinema_remote_label.py diff --git a/dingxin_toolbox_drf/asgi.py b/dingxin_toolbox_drf/asgi.py index 94b169b..fb66d19 100644 --- a/dingxin_toolbox_drf/asgi.py +++ b/dingxin_toolbox_drf/asgi.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) + } + +) diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index e2a359f..5677c80 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -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')] + } + } +} diff --git a/dingxin_toolbox_drf/urls.py b/dingxin_toolbox_drf/urls.py index 41b09a6..b5a6aa1 100644 --- a/dingxin_toolbox_drf/urls.py +++ b/dingxin_toolbox_drf/urls.py @@ -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()) +] diff --git a/update/consumers.py b/update/consumers.py new file mode 100644 index 0000000..d5c18b6 --- /dev/null +++ b/update/consumers.py @@ -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}') + diff --git a/update/migrations/0020_cinema_is_cloud_cinema_remote_label.py b/update/migrations/0020_cinema_is_cloud_cinema_remote_label.py new file mode 100644 index 0000000..b762eac --- /dev/null +++ b/update/migrations/0020_cinema_is_cloud_cinema_remote_label.py @@ -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'), + ), + ] diff --git a/update/models.py b/update/models.py index 7c8a49d..f200ced 100644 --- a/update/models.py +++ b/update/models.py @@ -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 diff --git a/update/serializers.py b/update/serializers.py index 732965e..a9b92bc 100644 --- a/update/serializers.py +++ b/update/serializers.py @@ -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): diff --git a/update/utils/get_version.py b/update/utils/get_version.py index 0e8e077..72b568e 100644 --- a/update/utils/get_version.py +++ b/update/utils/get_version.py @@ -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) diff --git a/update/utils/git_util.py b/update/utils/git_util.py index 633bcda..362ab01 100644 --- a/update/utils/git_util.py +++ b/update/utils/git_util.py @@ -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() diff --git a/update/views.py b/update/views.py index 3380570..97be231 100644 --- a/update/views.py +++ b/update/views.py @@ -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()