diff --git a/apps/common/utils/common.py b/apps/common/utils/common.py index 44920a339..66b6aee5c 100644 --- a/apps/common/utils/common.py +++ b/apps/common/utils/common.py @@ -389,3 +389,10 @@ def static_or_direct(logo_path): return static(logo_path) else: return logo_path + + +def make_dirs(name, mode=None, exist_ok=False): + """ 默认权限设置为 0o755 """ + if mode is None: + mode = 0o755 + return os.makedirs(name, mode=mode, exist_ok=exist_ok) diff --git a/apps/jumpserver/settings/logging.py b/apps/jumpserver/settings/logging.py index 81b22b0fc..ec2b3d9c2 100644 --- a/apps/jumpserver/settings/logging.py +++ b/apps/jumpserver/settings/logging.py @@ -154,4 +154,4 @@ if CONFIG.SYSLOG_ADDR != '' and len(CONFIG.SYSLOG_ADDR.split(':')) == 2: }) if not os.path.isdir(LOG_DIR): - os.makedirs(LOG_DIR) + os.makedirs(LOG_DIR, mode=0o755) diff --git a/apps/ops/models/adhoc.py b/apps/ops/models/adhoc.py index 0d1b32ba5..cd088b7f6 100644 --- a/apps/ops/models/adhoc.py +++ b/apps/ops/models/adhoc.py @@ -11,7 +11,7 @@ from django.conf import settings from django.utils import timezone from django.utils.translation import ugettext_lazy as _ -from common.utils import get_logger, lazyproperty +from common.utils import get_logger, lazyproperty, make_dirs from common.utils.translate import translate_value from common.db.fields import ( JsonListTextField, JsonDictCharField, EncryptJsonDictCharField, @@ -274,7 +274,7 @@ class AdHocExecution(OrgModelMixin): dt = datetime.datetime.now().strftime('%Y-%m-%d') log_dir = os.path.join(settings.PROJECT_DIR, 'data', 'ansible', dt) if not os.path.exists(log_dir): - os.makedirs(log_dir) + make_dirs(log_dir) return os.path.join(log_dir, str(self.id) + '.log') def start_runner(self): diff --git a/apps/ops/utils.py b/apps/ops/utils.py index a01543b95..c86a6aa08 100644 --- a/apps/ops/utils.py +++ b/apps/ops/utils.py @@ -4,7 +4,7 @@ import uuid from django.utils.translation import ugettext_lazy as _ -from common.utils import get_logger, get_object_or_none +from common.utils import get_logger, get_object_or_none, make_dirs from common.tasks import send_mail_async from orgs.utils import org_aware_func from jumpserver.const import PROJECT_DIR @@ -78,5 +78,5 @@ def get_task_log_path(base_path, task_id, level=2): rel_path = os.path.join(*task_id[:level], task_id + '.log') path = os.path.join(base_path, rel_path) - os.makedirs(os.path.dirname(path), exist_ok=True) + make_dirs(os.path.dirname(path), exist_ok=True) return path diff --git a/apps/terminal/utils/session_replay.py b/apps/terminal/utils/session_replay.py index f1b061cb0..1e2bbad53 100644 --- a/apps/terminal/utils/session_replay.py +++ b/apps/terminal/utils/session_replay.py @@ -8,7 +8,7 @@ from django.core.files.storage import default_storage import jms_storage -from common.utils import get_logger +from common.utils import get_logger, make_dirs from ..models import ReplayStorage @@ -56,7 +56,7 @@ def download_session_replay(session): target_path = os.path.join(default_storage.base_location, local_path) target_dir = os.path.dirname(target_path) if not os.path.isdir(target_dir): - os.makedirs(target_dir, exist_ok=True) + make_dirs(target_dir, exist_ok=True) ok, err = storage.download(session_path, target_path) if not ok: diff --git a/jms b/jms index bc230b268..267d78447 100755 --- a/jms +++ b/jms @@ -35,6 +35,7 @@ except ImportError as e: try: from jumpserver.const import CONFIG from common.utils.file import download_file + from common.utils import make_dirs except ImportError as e: print("Import error: {}".format(e)) print("Could not find config file, `cp config_example.yml config.yml`") @@ -50,8 +51,8 @@ logging.basicConfig( logger = logging.getLogger() try: - os.makedirs(os.path.join(BASE_DIR, "data", "static")) - os.makedirs(os.path.join(BASE_DIR, "data", "media")) + make_dirs(os.path.join(BASE_DIR, "data", "static")) + make_dirs(os.path.join(BASE_DIR, "data", "media")) except: pass