diff options
author | xiubuzhe <xiubuzhe@sina.com> | 2023-10-08 20:59:00 +0800 |
---|---|---|
committer | xiubuzhe <xiubuzhe@sina.com> | 2023-10-08 20:59:00 +0800 |
commit | 1dac2263372df2b85db5d029a45721fa158a5c9d (patch) | |
tree | 0365f9c57df04178a726d7584ca6a6b955a7ce6a /lib/sunhpc/commands/sync/users | |
parent | b494be364bb39e1de128ada7dc576a729d99907e (diff) | |
download | sunhpc-1dac2263372df2b85db5d029a45721fa158a5c9d.tar.gz sunhpc-1dac2263372df2b85db5d029a45721fa158a5c9d.tar.bz2 sunhpc-1dac2263372df2b85db5d029a45721fa158a5c9d.zip |
first add files
Diffstat (limited to 'lib/sunhpc/commands/sync/users')
-rw-r--r-- | lib/sunhpc/commands/sync/users/__init__.py | 21 | ||||
-rw-r--r-- | lib/sunhpc/commands/sync/users/plugin_00_fixmaster.py | 19 | ||||
-rw-r--r-- | lib/sunhpc/commands/sync/users/plugin_05_share.py | 20 | ||||
-rw-r--r-- | lib/sunhpc/commands/sync/users/plugin_10_fixusers.py | 121 |
4 files changed, 181 insertions, 0 deletions
diff --git a/lib/sunhpc/commands/sync/users/__init__.py b/lib/sunhpc/commands/sync/users/__init__.py new file mode 100644 index 0000000..b9c50e9 --- /dev/null +++ b/lib/sunhpc/commands/sync/users/__init__.py @@ -0,0 +1,21 @@ +#coding:utf-8 + +import sunhpc +class Command(sunhpc.commands.sync.command): + """ + Update all user-related files (e.g., /etc/passwd, /etc/shadow, etc.) + on all known hosts. Also, restart autofs on all known hosts. + + <example cmd='sync users'> + Send all user info to all known hosts. + </example> + """ + def run(self, params, args): + + # + # fix /etc/passwd + # + self.runPlugins() + + # Encrypt file to /etc/safe.d directory. + self.command('create.security.users') diff --git a/lib/sunhpc/commands/sync/users/plugin_00_fixmaster.py b/lib/sunhpc/commands/sync/users/plugin_00_fixmaster.py new file mode 100644 index 0000000..e8bf082 --- /dev/null +++ b/lib/sunhpc/commands/sync/users/plugin_00_fixmaster.py @@ -0,0 +1,19 @@ +#coding:utf-8 + +import os +import sunhpc +class Plugin(sunhpc.commands.Plugin): + """Relocates home directories to location on file server and fixes autofs.share""" + + def run(self, args): + """修复auto.share文件""" + + # 默认auto.master数据 + share_mount = '/share /etc/auto.share --timeout=1200' + homes_mount = '/home /etc/auto.home --timeout=1200' + auto_master = '/etc/auto.master' + + if self.cmd.matchText(auto_master, share_mount): + with open(auto_master, 'w') as f: + f.write('%s\n' % share_mount) + f.write('%s\n' % homes_mount) diff --git a/lib/sunhpc/commands/sync/users/plugin_05_share.py b/lib/sunhpc/commands/sync/users/plugin_05_share.py new file mode 100644 index 0000000..94e6d94 --- /dev/null +++ b/lib/sunhpc/commands/sync/users/plugin_05_share.py @@ -0,0 +1,20 @@ +#coding:utf-8 + +import os +import sunhpc +class Plugin(sunhpc.commands.Plugin): + """Relocates home directories to location on file server and fixes autofs.share""" + + def run(self, args): + """修复auto.share文件""" + + # 默认auto.share数据 + hostname = '%s.%s' % (self.db.getFrontendName(), + self.db.getHostAttr('localhost', 'Kickstart_PrivateDNSDomain')) + + shared = '/etc/auto.share' + content = 'apps %s:/export/&' % (hostname) + + if self.cmd.matchText(shared, content): + with open(shared, 'w') as f: + f.write('%s\n' % content) diff --git a/lib/sunhpc/commands/sync/users/plugin_10_fixusers.py b/lib/sunhpc/commands/sync/users/plugin_10_fixusers.py new file mode 100644 index 0000000..ae083d9 --- /dev/null +++ b/lib/sunhpc/commands/sync/users/plugin_10_fixusers.py @@ -0,0 +1,121 @@ +#coding:utf-8 + +import os +import sunhpc +class Plugin(sunhpc.commands.Plugin): + """Relocates home directories to location on file server and fixes autofs.share""" + + def run(self, args): + """修复auto.home文件""" + + auto_users, pwd_users, new_users = [], [], [] + + # 先读取autofs已经挂载的用户名称 + auto_home = '/etc/auto.home' + if os.path.exists(auto_home): + with open(auto_home, 'r') as f: + for li in f.readlines(): + auto_users.append(li.split()[0]) + + # 去重排序 + auto_users = sorted(list(set(auto_users))) + + # 获取工作目录 /works/home + default_dir = '/export/home/' + + # fix /etc/default/useradd command + userhome = 'HOME=%s' % default_dir[:-1] + useradd = '/etc/default/useradd' + if self.cmd.matchText(useradd, userhome): + data = [] + with open(useradd, 'r') as fe: + for line in fe: + if line.startswith('HOME='): + data.append(userhome) + continue + + data.append(line.strip()) + + with open(useradd, 'w') as f: + f.write('\n'.join(data)) + + # 读取/etc/passwd文件新用户. + fe = open('/etc/passwd', 'r') + for line in fe.readlines(): + + l = line[:-1].split(':') + if len(l) < 6: continue + + username = l[0] + homedirs = l[5] + + # 提取工作目录等于'/export/home' + if homedirs[:len(default_dir)] == default_dir: + pwd_users.append(username) + + # 过滤掉uid < 1000用户并且用户不在auto_users列表中. + # auto_users列表中用户不需要使用usermod命令再次切换 + if self.handler_uid(line) and username not in auto_users: + auto_users.append(username) + + fe.close() + + # cluster.local + # 获取控制节点的域名称 + hostname = self.db.getHostAttr('localhost', 'Info_HomeDirSrv') + if not hostname: + hostname = '%s.%s' % (self.db.getFrontendName(), + self.db.getHostAttr('localhost', 'Kickstart_PrivateDNSDomain')) + + # 如果有自定义参数,则添加. + options = self.db.getHostAttr('localhost', 'Info_HomeDirOptions') + if options: + options = '\t-' + options + else: + options = "" + + # 清空/etc/auto.home内容. + #open(auto_home, 'w').close() + + # 修正用户主目录. + for user in pwd_users: + cmd = '/usr/sbin/usermod -d %s %s' % (os.path.join('/home', user), user) + for line in os.popen(cmd).readlines(): + self.cmd.addText(line) + + # auto gen authorized_keys + rootssh = os.path.join(default_dir, user, '.ssh') + if not os.path.exists(rootssh): + os.makedirs(rootssh) + + rootrsa = os.path.join(rootssh, 'id_rsa') + if not os.path.exists(rootrsa): + self.cmd.command('repair.users.authorized', [user]) + + content = [] + # 合并autofs用户和passwd新用户添加到new_users中. + new_users.extend(auto_users) + new_users.extend(pwd_users) + new_users = sorted(list(set(new_users))) + for user in new_users: + # 更新这个 auto.home 文件. + # /export/home/dell + new_user_dir = os.path.join(default_dir, user) + + # dell cluster.local:/export/home/dell + autofs_entry = '%s%s\t%s:%s\n' % (user, options, hostname, new_user_dir) + content.append(autofs_entry) + + with open(auto_home, 'w') as f: + f.write(''.join(content)) + + def handler_uid(self, x): + l = x.split(':') + if int(l[2]) < 1000: + return False + if l[0] in self.avoid_uname(): + return False + return True + + def avoid_uname(self): + return ['nobody', 'nobody4', 'noaccess', 'nfsnobody'] |