From 1c513970f8f5e81268e998cc33ab8a93802bb722 Mon Sep 17 00:00:00 2001 From: gaoruoshu Date: Fri, 14 Jul 2023 18:47:25 +0800 Subject: [PATCH 10/11] bugfix: create file only if setting params --- atune_collector/plugin/configurator/mysql/mysql.py | 9 ++++++--- atune_collector/plugin/configurator/nginx/nginx.py | 7 ++++--- atune_collector/plugin/configurator/redis/redis.py | 7 +++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/atune_collector/plugin/configurator/mysql/mysql.py b/atune_collector/plugin/configurator/mysql/mysql.py index cfc533d..e472a1c 100644 --- a/atune_collector/plugin/configurator/mysql/mysql.py +++ b/atune_collector/plugin/configurator/mysql/mysql.py @@ -31,8 +31,10 @@ class Mysql(Configurator): self.__cmd = "" self.__file_path = "/etc/my.cnf" self.__mysqld_ind = -1 + + def _init_file(self): if not os.path.isfile(self.__file_path): - with open(self.__file_path, 'w'): + with open(self.__file_path, 'w', 0o644): pass os.chmod(self.__file_path, 0o644) self.__check_mysqld() @@ -61,7 +63,8 @@ class Mysql(Configurator): return False, ind def _set(self, key, value): - with open(self.__file_path, 'r') as f: + self._init_file() + with open(self.__file_path, 'r', 0o400) as f: lines = f.readlines() key_exist, ind = self.__check_file_exists(lines, key) @@ -71,7 +74,7 @@ class Mysql(Configurator): else: lines[ind] = new_line - with open(self.__file_path, 'w') as f: + with open(self.__file_path, 'w', 0o644) as f: f.writelines(lines) return 0 diff --git a/atune_collector/plugin/configurator/nginx/nginx.py b/atune_collector/plugin/configurator/nginx/nginx.py index 7ab619d..9d38dd0 100644 --- a/atune_collector/plugin/configurator/nginx/nginx.py +++ b/atune_collector/plugin/configurator/nginx/nginx.py @@ -42,18 +42,18 @@ class Nginx(Configurator): os.mkdir(self.__file_dir) os.chmod(self.__file_dir, 0o755) if not os.path.isfile(self.__file_path): - with open(self.__file_path, 'w'): + with open(self.__file_path, 'w', 0o644): pass os.chmod(self.__file_path, 0o644) self._set_index() def _get_lines(self): - with open(self.__file_path, 'r') as f: + with open(self.__file_path, 'r', 0o444) as f: lines = f.readlines() return lines def _set_lines(self, lines): - with open(self.__file_path, 'w') as f: + with open(self.__file_path, 'w', 0o644) as f: f.writelines(lines) def _set_index(self): @@ -209,3 +209,4 @@ def rewrite_value(value): raise SetConfigError("Failed to get cpu number") return output.stdout.decode().count("\n") + diff --git a/atune_collector/plugin/configurator/redis/redis.py b/atune_collector/plugin/configurator/redis/redis.py index c56f38e..aa14bcd 100644 --- a/atune_collector/plugin/configurator/redis/redis.py +++ b/atune_collector/plugin/configurator/redis/redis.py @@ -34,15 +34,18 @@ class Redis(Configurator): self.__re = r"^#\?\s*{key}\s* " self.__file_dir = "/etc/redis/" self.__file_path = self.__file_dir + "redis.conf" + + def _init_file(self): if not os.path.isdir(self.__file_dir): os.mkdir(self.__file_dir) os.chmod(self.__file_dir, 0o755) if not os.path.isfile(self.__file_path): - with open(self.__file_path, 'w', 0o200): + with open(self.__file_path, 'w', 0o600): pass os.chmod(self.__file_path, 0o644) def _set(self, key, value): + self._init_file() re_cmd = self.__re.format(key=key) grep_cmd = [r"grep", re_cmd, self.__file_path] out, err = self.execute_cmd(grep_cmd) @@ -51,7 +54,7 @@ class Redis(Configurator): num_lines = out.count("\n") new_line = self.__lines.format(key=key, value=value) if num_lines == 0: - with open(self.__file_path, 'a', 0o600) as f: + with open(self.__file_path, 'a', 0o644) as f: f.write(new_line + '\n') elif num_lines == 1: sed_cmd = [r"sed", "-i", r"s/{}.*$/{}/g".format(re_cmd, new_line), self.__file_path] -- 2.27.0