From 18e159350b1309f1a53de7efe4d50aba2ceca387 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 11 May 2016 11:10:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(api)=20=E4=BF=AE=E6=94=B9=E5=BB=BA?= =?UTF-8?q?=E7=AB=8B=E7=9B=AE=E5=BD=95=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用bash代替python完成建立777目录的功能 --- jumpserver/api.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jumpserver/api.py b/jumpserver/api.py index a4840f206..b9732f49c 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -463,9 +463,8 @@ def mkdir(dir_name, username='', mode=0755): insure the dir exist and mode ok 目录存在,如果不存在就建立,并且权限正确 """ - if not os.path.isdir(dir_name): - os.makedirs(dir_name) - os.chmod(dir_name, mode) + cmd = 'mkdir -p %s && chmod %s %s' % (dir_name, mode, dir_name) + bash(cmd) if username: chown(dir_name, username) From 8526437c88a068eb93ae47afd4583c2c9bb64f46 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 11 May 2016 11:19:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(mkdir)=20=E4=BF=AE=E6=94=B9mkdirs?= =?UTF-8?q?=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改原来导致的bug --- connect.py | 4 ++-- jperm/utils.py | 2 +- jumpserver/api.py | 8 ++++---- juser/user_api.py | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/connect.py b/connect.py index 8e138c6c5..2c312fe25 100755 --- a/connect.py +++ b/connect.py @@ -180,8 +180,8 @@ class Tty(object): log_file_path = os.path.join(today_connect_log_dir, '%s_%s_%s' % (self.username, self.asset_name, time_start)) try: - mkdir(os.path.dirname(today_connect_log_dir), mode=0777) - mkdir(today_connect_log_dir, mode=0777) + mkdir(os.path.dirname(today_connect_log_dir), mode=777) + mkdir(today_connect_log_dir, mode=777) except OSError: logger.debug('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir)) raise ServerError('创建目录 %s 失败,请修改%s目录权限' % (today_connect_log_dir, tty_log_dir)) diff --git a/jperm/utils.py b/jperm/utils.py index d011125c3..1e4966852 100644 --- a/jperm/utils.py +++ b/jperm/utils.py @@ -42,7 +42,7 @@ def gen_keys(key="", key_path_dir=""): key_path_dir = os.path.join(KEY_DIR, 'role_key', key_basename) private_key = os.path.join(key_path_dir, 'id_rsa') public_key = os.path.join(key_path_dir, 'id_rsa.pub') - mkdir(key_path_dir, mode=0755) + mkdir(key_path_dir, mode=755) if not key: key = RSAKey.generate(2048) key.write_private_key_file(private_key) diff --git a/jumpserver/api.py b/jumpserver/api.py index b9732f49c..ca1972532 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -91,7 +91,7 @@ def get_role_key(user, role): """ user_role_key_dir = os.path.join(KEY_DIR, 'user') user_role_key_path = os.path.join(user_role_key_dir, '%s_%s.pem' % (user.username, role.name)) - mkdir(user_role_key_dir, mode=0777) + mkdir(user_role_key_dir, mode=777) if not os.path.isfile(user_role_key_path): with open(os.path.join(role.key_path, 'id_rsa')) as fk: with open(user_role_key_path, 'w') as fu: @@ -458,12 +458,12 @@ def bash(cmd): return subprocess.call(cmd, shell=True) -def mkdir(dir_name, username='', mode=0755): +def mkdir(dir_name, username='', mode=755): """ insure the dir exist and mode ok 目录存在,如果不存在就建立,并且权限正确 """ - cmd = 'mkdir -p %s && chmod %s %s' % (dir_name, mode, dir_name) + cmd = '[ ! -d %s ] && mkdir -p %s && chmod %s %s' % (dir_name, dir_name, mode, dir_name) bash(cmd) if username: chown(dir_name, username) @@ -485,7 +485,7 @@ def my_render(template, data, request): def get_tmp_dir(): seed = uuid.uuid4().hex[:4] dir_name = os.path.join('/tmp', '%s-%s' % (datetime.datetime.now().strftime('%Y%m%d-%H%M%S'), seed)) - mkdir(dir_name, mode=0777) + mkdir(dir_name, mode=777) return dir_name diff --git a/juser/user_api.py b/juser/user_api.py index 03ed65938..9ad22d25e 100644 --- a/juser/user_api.py +++ b/juser/user_api.py @@ -130,14 +130,14 @@ def gen_ssh_key(username, password='', """ logger.debug('生成ssh key, 并设置authorized_keys') private_key_file = os.path.join(key_dir, username+'.pem') - mkdir(key_dir, mode=0777) + mkdir(key_dir, mode=777) if os.path.isfile(private_key_file): os.unlink(private_key_file) ret = bash('echo -e "y\n"|ssh-keygen -t rsa -f %s -b %s -P "%s"' % (private_key_file, length, password)) if authorized_keys: auth_key_dir = os.path.join(home, username, '.ssh') - mkdir(auth_key_dir, username=username, mode=0700) + mkdir(auth_key_dir, username=username, mode=700) authorized_key_file = os.path.join(auth_key_dir, 'authorized_keys') with open(private_key_file+'.pub') as pub_f: with open(authorized_key_file, 'w') as auth_f: