-
Notifications
You must be signed in to change notification settings - Fork 2
/
rhea_user
executable file
·68 lines (59 loc) · 1.75 KB
/
rhea_user
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
[ -z "${DRYRUN:-}" ] && DRYRUN= || DRYRUN="echo"
# 20210317WF - add user
#
# setup default groups and put ni config in .bashrc
#
# important groups on server
_USER_GROUPS=lncd,luna_wallace,kvm,docker,users
mkpass(){
# concat nwords
local n_words=3
local min_word_len=5
LC_ALL=C grep -P "[a-z]{$min_word_len}" /usr/share/dict/words |
shuf -n $n_words|
sed 's/.*/\u&/'|
tr -dC '[A-Za-z]'
}
user_dne(){
local user="$1"
grep -q "$user" /etc/passwd && echo "have $user in $_" && return 1
return 0
}
add_rhea_user(){
local user="$1"; shift
local fullname="$*"
$DRYRUN useradd -g lncd -m -G "$_USER_GROUPS" -s "$(which bash)" -c "$fullname" $user
}
set_passwd(){
local user="$1"; shift
local pass="${1:-}"
[ -z "$pass" ] && pass=$(mkpass)
echo -e "$pass\n$pass" | $DRYRUN passwd "$user"
echo "# using '$pass'"
}
add_rc(){
# add default ni settings: env (path, python, fsl, afni, ...) and bash prompt, aliases, etc
local user="$1"
echo "source /opt/ni_tools/dotfiles/bash/.bashrc" |
sudo -u "$user" $DRYRUN tee -a "/home/$user/.bashrc" >/dev/null
# R config file: make sure we look to ni_tools for packages
$DRYRUN sudo -u "$user" ln -s /opt/ni_tools/dotfiles/Rprofile/.Rprofile "/home/$user/"
}
# if running (not sourcing)
if [[ "$(caller)" == "0 "* ]]; then
# run as root
if [ "$(id -u)" -ne 0 ]; then
sudo --preserve-env=DRYRUN,USERPASS "$0" "$@"
exit $?
fi
# parse args
[ $# -lt 2 ] && echo "USAGE: USERPASS=xxx $0 user full name # if no userpass, makes one up" && exit 1
user="${1//@*/}"; shift
fullname="$*";
# already have user
user_dne "$user" || exit
add_rhea_user "$user" "$fullname"
set_passwd "$user" "${USERPASS:-}"
add_rc "$user"
fi