tildetown-admin/scripts/rename_user.py

51 lines
1.0 KiB
Python
Raw Normal View History

2018-02-23 21:58:54 +00:00
#!/usr/bin/env python3
2018-02-23 22:31:25 +00:00
"""This script wraps the usermod command to allow user account renames via
sudoers."""
2018-02-23 21:58:54 +00:00
import os
import sys
import subprocess
2018-02-23 22:31:25 +00:00
def rename_user(old_username, new_username):
"""Given an old and a new username, renames user on disk with usermod.
Raises if the usermod call fails."""
2018-02-25 02:34:55 +00:00
# Rename user
2018-02-23 21:58:54 +00:00
args = [
'usermod',
'-l',
new_username,
'-m',
'-d',
os.path.join('/home', new_username),
2018-02-23 22:31:25 +00:00
old_username
2018-02-23 21:58:54 +00:00
]
subprocess.run(args, check=True)
2018-02-25 02:34:55 +00:00
# Rename their group
args = [
'groupmod',
'-n',
new_username,
old_username
]
subprocess.run(args, check=True)
2018-02-23 21:58:54 +00:00
def main(argv):
if len(argv) < 3:
print('[rename_user] Too few arguments passed.', file=sys.stderr)
return 1
try:
rename_user(argv[1], argv[2])
except subprocess.CalledProcessError as e:
print('[rename_user] {}'.format(e), file=sys.stderr)
return 2
return 0
if __name__ == '__main__':
exit(main(sys.argv))