提交 d6dbda87 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!89 atune: add file_config CPI and code enhancement

Merge pull request !89 from hanxinke/master
......@@ -19,6 +19,7 @@ __all__ = [
"affinity",
"bios",
"bootloader",
"file_config",
"kernel_config",
"script",
"sysctl",
......
......@@ -93,6 +93,7 @@ class IrqAffinity(Configurator):
@staticmethod
def check(config1, config2):
"""replace comma"""
config1 = config1.replace(",", "")
config2 = config2.replace(",", "")
return int(config1, base=16) == int(config2, base=16)
......@@ -34,7 +34,7 @@ class Bios(Configurator):
def _set(self, key, value):
raise NeedConfigWarning(
"Please change the BIOS configuration {key} to {val}.".format(
"please change the BIOS configuration {key} to {val}".format(
key=key, val=value))
def _get(self, key, _):
......
......@@ -90,6 +90,12 @@ class Configurator:
cfg = self._getcfg(config)
try:
ret = self._set(cfg[0], cfg[1])
except Warning as warn:
if self._user == "UT":
raise warn
LOGGER.warning("%s.%s: %s", self.__class__.__name__,
inspect.stack()[0][3], str(warn))
return warn
except Exception as err:
if self._user == "UT":
raise err
......@@ -188,6 +194,12 @@ class Configurator:
ret = self._get(key, value)
if ret is not None:
ret = ret.replace('\n', ' ').strip()
except Warning as warn:
if self._user == "UT":
raise warn
LOGGER.warning("%s.%s: %s", self.__class__.__name__,
inspect.stack()[0][3], str(warn))
return warn
except Exception as err:
if self._user == "UT":
raise err
......
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-06-18
"""
Init file.
"""
__all__ = ["fconfig"]
from . import *
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright (c) 2020 Huawei Technologies Co., Ltd.
# A-Tune is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Create: 2020-06-18
"""
The sub class of the Configurator, used to change the file config.
"""
import inspect
import logging
import os
import random
import re
import shutil
from analysis.plugin.public import GetConfigError, NeedConfigWarning
from ..common import Configurator
LOGGER = logging.getLogger(__name__)
class FileConfig(Configurator):
"""To change the file config"""
_module = "FILE_CONFIG"
_submod = "FILE_CONFIG"
_cfg_files = ["/etc/profile"]
_separator_char = "-"
def __init__(self, user=None):
Configurator.__init__(self, user)
def _set(self, key, value):
try:
if self.get(key, value) == value:
return 0
except LookupError as err:
LOGGER.debug("%s.%s: %s", self.__class__.__name__, inspect.stack()[0][3], str(err))
with open(key, 'a') as file:
file.write(value + "\n")
if key in self._cfg_files:
raise NeedConfigWarning(
"please exec source {file} to make the configuration take effect".format(file=key))
return 0
def _get(self, key, value):
if not os.path.exists(key):
raise GetConfigError("Fail to find file {}".format(key))
config_key = value.split("=")[0] if value.find("=") != -1 else value
pattern = re.compile("^" + config_key + ".*", re.ASCII | re.DOTALL)
search_obj = []
with open(key, 'r') as file:
for line in file:
result = pattern.search(line)
if result is not None:
search_obj.append(result.group(0).strip())
if len(search_obj) == 0:
raise LookupError("Fail to find {} entry in {}".format(config_key, key))
return search_obj[-1]
def _backup(self, config, rollback_info):
cfg = self._getcfg(config)
name = cfg[0].replace("/", self._separator_char)
bak_file = "{path}/{file}{sep}{ver}".format(path=rollback_info, file=name,
sep=self._separator_char,
ver=random.random())
shutil.copy(cfg[0], bak_file)
return "CPI_ROLLBACK_INFO = {}".format(bak_file)
def _resume(self, key, value):
if key != "CPI_ROLLBACK_INFO":
raise ValueError("unsupported resume type: {}".format(key))
left_index = value.rfind("/")
right_index = value.rfind(self._separator_char)
if left_index == -1 or right_index == -1:
return
cfg_file = value[left_index + 1:right_index].replace(self._separator_char, "/")
LOGGER.info("resume cfg file is %s", cfg_file)
shutil.copy(value, cfg_file)
......@@ -15,7 +15,6 @@
The sub class of the Configurator, used to change the kernel config.
"""
import gzip
import inspect
import os
import logging
import subprocess
......@@ -46,7 +45,7 @@ class KernelConfig(Configurator):
if self.get(key) == value:
return 0
raise NeedConfigWarning(
"Please change the kernel configuration {key} to {val}.".format(
"please change the kernel configuration {key} to {val}".format(
key=key, val=value))
def _get(self, key, _):
......@@ -61,9 +60,7 @@ class KernelConfig(Configurator):
pattern = re.compile("^" + key + "=(.+)", re.ASCII | re.MULTILINE)
search_obj = pattern.findall(cfgs)
if len(search_obj) != 1:
err = LookupError("not find one " + key)
LOGGER.error("%s.%s: %s", self.__class__.__name__,
inspect.stack()[0][3], str(err))
err = Warning("not find one " + key)
raise err
return search_obj[0]
......
......@@ -100,6 +100,7 @@ class Script(Configurator):
@staticmethod
def check(_, __):
"""check"""
return True
def _backup(self, config, rollback_info):
......
......@@ -44,6 +44,7 @@ class Sysctl(Configurator):
@staticmethod
def check(config1, config2):
"""substring"""
config1 = re.sub(r"\s{1,}", " ", config1)
config2 = re.sub(r"\s{1,}", " ", config2)
return config1 == config2
......@@ -48,4 +48,5 @@ class Sysfs(Configurator):
@staticmethod
def check(_, __):
"""check"""
return True
......@@ -53,6 +53,7 @@ class Systemctl(Configurator):
@staticmethod
def check(_, __):
"""check"""
return True
def _backup(self, config, _):
......
......@@ -95,6 +95,7 @@ class IoStat(Monitor):
keys = []
dev = "sd.*?"
ret = ""
resplitobj = re.compile(r'\s*\n')
device_data = {}
opts, _ = getopt.getopt(para.split(), None, ['device=', 'fields='])
......@@ -106,17 +107,15 @@ class IoStat(Monitor):
keys.append(val)
continue
rows_contents = resplitobj.split(info)
dev = "Device|" + dev
pattern = re.compile(
"^(" +
dev +
r")\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)"
r"\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)"
r"\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)"
r"\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)\ {1,}(\S*)",
re.ASCII | re.MULTILINE)
search_obj = pattern.findall(info)
if len(search_obj) < 2:
search_obj = []
pattern = re.compile("^(" + dev + r").+", re.ASCII)
for row in rows_contents:
if pattern.match(row):
search_obj.append([data for data in row.split()])
if len(search_obj) < 3:
err = LookupError("Fail to find data for {}".format(dev))
LOGGER.error("%s.%s: %s", self.__class__.__name__,
inspect.stack()[0][3], str(err))
......
......@@ -59,6 +59,6 @@ class SysInterrupts(Monitor):
nic = val
break
pattern = re.compile(r"^(\d*):{}$".format(nic.strip()), re.MULTILINE)
pattern = re.compile(r"^(\d*):{}".format(nic.strip()), re.MULTILINE)
interrupts = pattern.findall(info)
return " ".join(interrupts).strip()
......@@ -79,7 +79,7 @@ class Profile(Resource):
result["status"] = "FAILED"
result["value"] = str(real_value)
else:
if not real_value:
if real_value is None:
result["status"] = "FAILED"
result["value"] = "UNKNOWN"
else:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册