add task to check repo

This commit is contained in:
xuezhixin 2024-11-11 17:49:28 +08:00
parent ee7c2db9dc
commit 366e35b879
2 changed files with 277 additions and 1 deletions

View File

@ -0,0 +1,272 @@
From 0e6f8d8f1bb7dba15a206b7a02c9dc70ad9b1aba Mon Sep 17 00:00:00 2001
From: xuezhixin <xuezhixin@uniontech.com>
Date: Mon, 13 Nov 2023 16:39:51 +0800
Subject: [PATCH] =?UTF-8?q?agent=E6=A3=80=E6=B5=8Brepo=E4=BB=BB=E5=8A=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
sysmig_agent/check.py | 93 ------------------------
sysmig_agent/short_task.py | 144 +++++++++++++++++++++++++++++++++++++
2 files changed, 144 insertions(+), 93 deletions(-)
diff --git a/sysmig_agent/check.py b/sysmig_agent/check.py
index 141a734..1a21c52 100644
--- a/sysmig_agent/check.py
+++ b/sysmig_agent/check.py
@@ -178,99 +178,6 @@ def check_user(data):
return re_data
-def init_remove_oldrepo():
- backup_comment = '#This is a yum repository file that was disabled . <Migration to UiniontechOS> \
- \n'
- path = '/etc/yum.repos.d/'
- repos = os.listdir(path)
- for repo in repos:
- path_file = path+'/'+repo
- if not os.path.isfile(path_file):
- continue
- if not re.search('repo$',repo):
- continue
- with open(path_file, 'r') as fsrc:
- content = fsrc.read()
- with open(path_file+'.disabled','w') as fdst:
- fdst.write(repo+'\n'+backup_comment+content)
- fdst.close()
- fsrc.close()
- os.remove(path_file)
-
-
-#初始化repo文件
-def initRepoFile(baseurl):
- os_version_ret = platform.dist()
- version = os_version_ret[1].split('.',-1)
- reposdir = '/etc/yum.repos.d/'
- h = 0
- if re.match('file:',baseurl):
- str0, path = baseurl.split('://',1)
- path = '/' + path.strip('/') + '/'
- else:
- h = 1
- if re.fullmatch('8',version[0]):
- path_appstream = baseurl+'/AppStream'
- path_baseos = baseurl+'/BaseOS'
- path_310 = baseurl+'/kernel-3.10'
- path_419 = baseurl+'/kernel419'
- path_510 = baseurl+'/kernel510'
-
- repostr_uos = '''[UniontechOS-AppStream]\nname = UniontechOS AppStream\nbaseurl = '''+path_appstream.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-BaseOS]\nname = UniontechOS BaseOS\nbaseurl = '''+path_baseos.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-kernel-4.19.0]\nname = UniontechOS Kernel-4.19.0\nbaseurl = '''+path_419.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n\n[UniontechOS-kernel-5.10.0]\nname = UniontechOS Kernel-5.10.0\nbaseurl = '''+path_510.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n\n
-'''
- else:
- path_310 = baseurl+'/kernel-3.10'
- repostr_uos = '''[UniontechOS-AppStream]\nname = UniontechOS AppStream\nbaseurl = '''+baseurl.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-kernel-3.10.0]\nname = UniontechOS Kernel-3.10.0\nbaseurl = '''+path_310.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n
- '''
- repofile = os.path.join(reposdir, 'switch-to-uos.repo')
- with open(repofile,'w') as f_repo:
- f_repo.write(repostr_uos)
- f_repo.close()
-
-
-#检测repo文件创建缓存
-def checkRepoMakeCache():
- os.system('yum clean all')
- os.system('yum makecache')
- os_version_ret = platform.dist()
- os_arch = platform.machine()
- version = os_version_ret[1].split('.',-1)
- ret = os.path.exists('/var/cache/dnf/UniontechOS-AppStream.solv')
- if ret:
- ret = os.path.exists('/var/cache/dnf/UniontechOS-BaseOS.solv')
- if ret or re.fullmatch('7',version[0]):
- return 0
- else:
- return 1
- else:
- if re.fullmatch('7',version[0]):
- ret = os.path.exists('/var/cache/yum/%s/7/UniontechOS-AppStream/repomd.xml' % os_arch)
- if ret:
- return 0
- return 1
-
-
-#检测repo
-def check_repo(data_j):
- uos_sysmig_conf = json.loads(getSysMigConf())
- AGENT_IP = json.loads(uos_sysmig_conf).get('agentip').strip()[1:-1]
- baseurl = json.loads(data_j).get('repo_pwd')
- keylist = None
- valuelist = None
- data = None
- init_remove_oldrepo()
- initRepoFile(baseurl)
- state = checkRepoMakeCache()
-
- if state == 0:
- keylist = ['ip','res','data']
- valuelist = [AGENT_IP,state,'连接成功']
- else:
- data = '下载失败,请检查您的软件源'
- keylist = ['ip','res','error']
- valuelist = [AGENT_IP,state,data]
- return list_to_json(keylist,valuelist)
-
def check_os_kernel(data):
uos_sysmig_conf = json.loads(getSysMigConf())
diff --git a/sysmig_agent/short_task.py b/sysmig_agent/short_task.py
index 11dd9ef..4e4dae3 100644
--- a/sysmig_agent/short_task.py
+++ b/sysmig_agent/short_task.py
@@ -138,3 +138,147 @@ def get_agent_os():
AGENT_OS = os_version_ret[0] + version[0]
return AGENT_OS
+
+
+def init_remove_oldrepo():
+ backup_comment = '#This is a yum repository file that was disabled . <Migration to UiniontechOS> \
+ \n'
+ path = '/etc/yum.repos.d/'
+ repos = os.listdir(path)
+ for repo in repos:
+ path_file = path+'/'+repo
+ if not os.path.isfile(path_file):
+ continue
+ if not re.search('repo$',repo):
+ continue
+ with open(path_file, 'r') as fsrc:
+ content = fsrc.read()
+ with open(path_file+'.disabled','w') as fdst:
+ fdst.write(repo+'\n'+backup_comment+content)
+ fdst.close()
+ fsrc.close()
+ os.remove(path_file)
+
+
+#初始化repo文件
+def initRepoFile(baseurl):
+ os_version_ret = platform.dist()
+ version = os_version_ret[1].split('.',-1)
+ reposdir = '/etc/yum.repos.d/'
+ h = 0
+ if re.match('file:',baseurl):
+ str0, path = baseurl.split('://',1)
+ path = '/' + path.strip('/') + '/'
+ else:
+ h = 1
+ if re.fullmatch('8',version[0]):
+ path_appstream = baseurl+'/AppStream'
+ path_baseos = baseurl+'/BaseOS'
+ path_310 = baseurl+'/kernel-3.10'
+ path_419 = baseurl+'/kernel419'
+ path_510 = baseurl+'/kernel510'
+
+ repostr_uos = '''[UniontechOS-AppStream]\nname = UniontechOS AppStream\nbaseurl = '''+path_appstream.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-BaseOS]\nname = UniontechOS BaseOS\nbaseurl = '''+path_baseos.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-kernel-4.19.0]\nname = UniontechOS Kernel-4.19.0\nbaseurl = '''+path_419.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n\n[UniontechOS-kernel-5.10.0]\nname = UniontechOS Kernel-5.10.0\nbaseurl = '''+path_510.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n\n
+'''
+ else:
+ path_310 = baseurl+'/kernel-3.10'
+ repostr_uos = '''[UniontechOS-AppStream]\nname = UniontechOS AppStream\nbaseurl = '''+baseurl.strip('\n')+'''\nenabled = 1\ngpgcheck = 0\n\n[UniontechOS-kernel-3.10.0]\nname = UniontechOS Kernel-3.10.0\nbaseurl = '''+path_310.strip('\n')+'''\nenabled = 0\ngpgcheck = 0\nskip_if_unavailable = 1\n
+ '''
+ repofile = os.path.join(reposdir, 'switch-to-uos.repo')
+ with open(repofile,'w') as f_repo:
+ f_repo.write(repostr_uos)
+ f_repo.close()
+
+
+#检测repo文件创建缓存
+def checkRepoMakeCache():
+ os.system('yum clean all')
+ os.system('yum makecache')
+ os_version_ret = platform.dist()
+ os_arch = platform.machine()
+ version = os_version_ret[1].split('.',-1)
+ ret = os.path.exists('/var/cache/dnf/UniontechOS-AppStream.solv')
+ if ret:
+ ret = os.path.exists('/var/cache/dnf/UniontechOS-BaseOS.solv')
+ if ret or re.fullmatch('7',version[0]):
+ return 0
+ else:
+ return 1
+ else:
+ if re.fullmatch('7',version[0]):
+ ret = os.path.exists('/var/cache/yum/%s/7/UniontechOS-AppStream/repomd.xml' % os_arch)
+ if ret:
+ return 0
+ return 1
+
+
+# 检测centos 8 系统上的 repo文件联通
+def checkRepoFileHttp(baseurl):
+ try:
+ with request.urlopen(baseurl) as file:
+ # print(file.status)
+ # print(file.reason)
+ if re.match('OK', file.reason):
+ return 0
+ except Exception as e:
+ if re.match('HTTP Error 404|[Errno 2]', str(e)):
+ return 1
+ elif re.match('[Errno 21]', str(e)):
+ return 0
+ else:
+ return 1
+
+
+def repoFileCheck(baseurl):
+ if re.match('file\:\/\/', baseurl):
+ path = re.sub('file://', '', baseurl)
+ if os.path.exists(path):
+ return 0
+ else:
+ return 1
+ try:
+ dst_status = urllib.request.urlopen(baseurl, timeout=5).code
+ return 0
+ except Exception as err:
+ return 1
+ pass
+
+def check_repo(data):
+ agent_os = get_agent_os()
+ if '7' in agent_os:
+ agent_os = 'centos7'
+ elif '8' in agent_os:
+ agent_os = 'centos8'
+ os_type = agent_os + '_' + platform.machine().strip('')
+ baseurl = json.loads(data).get(os_type)
+ if not baseurl:
+ baseurl = '1'
+ task_id = json.loads(data).get('task_id')
+ # 更新SQL任务状态
+ statue = 1
+ sql_task_statue(statue, task_id)
+ # 发送消息给Server更新任务流状态
+ post_server('task_start', task_id)
+ # 初始化去除旧的repo文件
+ init_remove_oldrepo()
+ # 传递baseurl配置repo文件
+ initRepoFile(baseurl)
+ # 建立软件源缓存,判断软件源是否可用
+ state = checkRepoMakeCache()
+ sql = ''
+ repo_state = -1
+ if state == 0:
+ repo_state = repoFileCheck(baseurl)
+ else:
+ repo_state = 1
+
+ sql = "UPDATE agent_info SET repo_status = {} WHERE agent_ip = '{}';".format(repo_state, get_local_ip())
+ try:
+ ret = DBHelper().execute(sql)
+ statue = 2
+ except:
+ statue = 2
+ sql_task_statue(statue, task_id)
+ sql_task_statue(statue, task_id)
+ post_server('task_close', task_id)
+ return 'success'
--
2.20.1

View File

@ -1,6 +1,6 @@
Name: migration-tools
Version: 1.0.3
Release: 16
Release: 17
License: MulanPSL-2.0
Summary: A tool to help users migrate the Centos system to the UOS system and openEuler system
Source0: %{name}-%{version}.tar.gz
@ -23,6 +23,7 @@ Patch013: 0013-set-agent-migration-task.patch
Patch014: 0014-compare-with-request-agent-ip.patch
Patch015: 0015-agent-sends-the-task-after-receving-the-request.patch
Patch016: 0016-export-migration-log.patch
Patch017: 0017-add-task-to-check-repo.patch
BuildArch: noarch
@ -122,6 +123,9 @@ rm -rf /usr/bin/migration-tools
%endif
%changelog
* Mon Nov 11 2024 xuezhixin <xuezhixin@uniontech.com> - 1.0.3-17
- 0017-add-task-to-check-repo.patch
* Mon Nov 11 2024 xuezhixin <xuezhixin@uniontech.com> - 1.0.3-16
- 0016-export-migration-log.patch