-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnmctool
More file actions
executable file
·142 lines (126 loc) · 4.39 KB
/
nmctool
File metadata and controls
executable file
·142 lines (126 loc) · 4.39 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
is_sourced(){ [[ "$0" != "$BASH_SOURCE" ]]; }
msg(){ echo -e "$@" 1>&2; }
err(){ msg $'\033[00;31m'"ERROR: $@"$'\033[00m'; }
die(){ err "$@"; is_sourced || exit 1 && false; }
set_keyboard(){
local kb="${1:?Missing argument: <keyboard-layout>}";
#sudo sed -ri "/XKBLAYOUT/ s/=.*$/=\"$kb\"/" /etc/default/keyboard;
#sudo setupcon -v
sudo raspi-config nonint do_configure_keyboard ${kb}
}
declare -fx set_keyboard
nopasswd_sudo(){
local user="${1:-$SUDO_USER}"
[[ -n "$user" ]] || { die "Usage: $0 USERNAME" || return 1; }
id "$user" &>/dev/null || { die "User '$user' does not exist!" || return 1; }
msg "Adding NOPASSWD:ALL for user '$user' to sudoers file"
echo "$user ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/010_$user-nopasswd
}
declare -fx nopasswd_sudo
set_hostname(){
local newname="${1:?Missing argument: <newname>}"
local oldname="$(hostname)"
[[ -n "$newname" ]] || die "No hostname specified" || return -1
echo "Changing hostname from '$oldname' to '$newname'"
sudo hostname "$1" && {
sudo sed -i "s/$oldname/$newname/g" /etc/hosts
hostname | sudo tee /etc/hostname >/dev/null
[[ -f /var/lib/cloud/seed/nocloud-net/user-data ]] &&
sudo sed -ri "s/(hostname: ).*\$/\1$newname/" \
/var/lib/cloud/seed/nocloud-net/user-data
exec $SHELL
}
}
declare -fx set_hostname
set_prompt_colors(){
sed -ri 's/^#?(force_color_prompt=).*/\1yes/g' $target{/etc/skel,/root,/home/*}/.bashrc &> /dev/null
[[ ! -f $target/root/.bashrc ]] && cp $target{/etc/skel/.bashrc,/root/}
sed -i "s/PS1='/PS1='\\\[\\\033[01;33m\\\]/g; s/01;32m/01;31m/g" $target/root/.bashrc
}
set_timezone(){
local tz="${1:-Europe/Brussels}"
sudo timedatectl set-timezone "$tz"
}
declare -fx set_timezone
add_default_groups(){
local user="$1"
sudo usermod -aG adm,dialout,cdrom,audio,users,sudo,video,games,plugdev,input,gpio,spi,i2c,netdev "$user"
}
declare -fx add_default_groups
add_user(){
local user="${1:?Missing argument: <username>}"
[[ -n "$user" ]] || { die "Usage: $0 USERNAME" || return 1; }
id "$user" &>/dev/null && { die "User '$user' exists!" || return 1; }
sudo adduser "$user" --gecos "" --disabled-password
add_default_groups "$user"
nopasswd_sudo "$user"
sudo passwd "$user"
}
declare -fx add_user
mkvenv(){
local opts="" dir="$PWD"
while [[ -n "$1" ]]; do
case "$1" in
-s) opts+="--system-site-packages" ;;
-d) dir="$2"; shift ;;
*) local name="$1" ;;
esac
shift
done
msg "Creating new environment '${name:=venv}' in ${dir}..."
pushd "$dir" >/dev/null
python3 -m venv "$env"
"$env/bin/python3" -m pip install --upgrade pip setuptools wheel
source "$env/bin/activate"
popd >/dev/null
}
declare -fx mkvenv
update() {
tmp="$(mktemp)"
if wget -qO "${tmp}" https://raw.githubusercontent.com/nmctseb/RPi-tools/master/nmctool; then
sudo install -v -m 755 "${tmp}" /usr/local/bin/nmctool
else
err "Update failed"
return 1
fi
}
usage(){
cat <<-EOF
usage: $0 <command> [<args>]
Available commands:
add-user <name>
- add new user account
fix-groups [<user>]
- fix group memberships (gpio, i2c, ...) for current or <specified user>
hostname <newname>
- change system hostname to <newname>
keyboard <layout>
- change console keyboard layout.
<layout> is a 2 letter code, e.g. us, gb, be, nl, ...
lazy-sudo[<user>]
- add rule to enable sudo without password
timezone <tz>
- set timezone to <tz>, e.g. Europe/Brussels
new-venv [-s] <name>
- make a new Python3 virtual environment in <name>
-s: inherit system-site-packages
update
- update this tool to the latest version
EOF
}
is_sourced || {
op="$1"; shift
case "$op" in
add-user) add_user "$@" ;;
fix-groups) add_default_groups "$@" ;;
hostname) set_hostname "$@" ;;
keyboard) set_keyboard "$@" ;;
lazy-sudo) nopasswd_sudo "$@" ;;
timezone) set_timezone "$@" ;;
new-venv) mkvenv "$@" ;;
update) update "$@" ;;
help) usage "$@" ;;
*) usage 1>&2 ;;
esac
}