-
Notifications
You must be signed in to change notification settings - Fork 0
/
wemux-mgr.sh
executable file
·150 lines (117 loc) · 3.46 KB
/
wemux-mgr.sh
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
143
144
145
146
147
148
149
150
#!/usr/bin/env bash
set -e
[ -n "$DEBUG" ] && set -x
CWD=$(dirname $(readlink -f $0)); pushd ${CWD} >/dev/null
function usage() {
>&2 echo "
Usage: $(basename $0) CONTAINER COMMAND
With:
CONTAINER: the name or id of the container
COMMAND:
adduser|u USER [SSH_PUBIC_KEY_PATH]
Add a user in the Docker container in mirror mode
addkey|k USER SSH_PUBLIC_KEY_PATH
Add a SSH_PUBIC_KEY_PATH into the authorized_keys
file of the USER
setmode|m USER MODE
Set the wemux mode for the user (mirror, pair, rogue)
"
}
# Main functions
function add_user() {
local container="$1"
local user="$2"
local key_path="$3"
check_requirements "bash"
docker exec $container adduser -D -h /home/$user -s /bin/bash $user
docker exec $container passwd -u $user # Unlock the user
set_wemux_mode "$container" "$user" "mirror"
if [ -n "$key_path" ]; then
add_ssh_key "$container" "$user" "$key_path"
fi
}
function add_ssh_key() {
local container="$1"
local user="$2"
local key_path="$3"
local ssh_dir="/home/$user/.ssh"
local authorized_keys="$ssh_dir/authorized_keys"
check_user_exists $container $user
docker exec $container mkdir -p $ssh_dir
cat_file $key_path | docker exec -i $container sh -c "cat >> $authorized_keys"
docker exec $container chown -R $user:$user $ssh_dir
docker exec $container chmod -R 700 $ssh_dir
docker exec $container chmod -R 640 $authorized_keys
}
function set_wemux_mode() {
local container="$1"
local user="$2"
local mode="$3"
local supported_mode="mirror pair rogue" # Mode that can be set
check_user_exists $container $user
if [ ! "${supported_mode#$mode}" == "${supported_mode}" ]; then
echo "wemux $mode; exit" | docker exec -i $container sh -c "cat > /home/$user/.bash_profile"
else
message "error" "Wemux mode $mode unsupported. Please choose a mode in '${supported_mode}'"
fi
}
function main() {
local container="$1"
local cmd="$2"
[ $# -ge 2 ] && shift 2
try_import_shml "1.0.3"
case $cmd in
adduser|u)
local user="$1"
local key_path="$2"
add_user "$container" "$user" "$key_path";;
addkey|k)
local user="$1"
local key_path="$2"
add_ssh_key "$container" "$user" "$key_path";;
setmode|m)
local user="$1"
local mode="$2"
set_wemux_mode "$container" "$user" "$mode";;
--help|-h|*)
usage
message "error" "Unknown command '$cmd'";;
esac
}
# Libraries
function check_user_exists() {
local container="$1"
local user="$2"
docker exec $container id $user
}
function check_requirements() {
local requirements="$1"
for tool in $requirements; do
which $tool >/dev/null
done
}
# Print the content of a local file or hosted on a web server
function cat_file() {
local file_path="$1"
case $file_path in
http://*|https://*) curl $file_path;;
*) cat $file_path;;
esac
}
function try_import_shml() {
shml_version="$1"
shml_url="https://raw.githubusercontent.com/MaxCDN/shml/${shml_version}/shml.sh"
shml_filename="$(basename $shml_url)" # Name of the file to import
wget --quiet --no-clobber $shml_url | true
[ -f $shml_filename ] && source $shml_filename
}
function message() {
local msg_type="$1"
local msg="$2"
case $msg_type in
info) printf "$(fgcolor green)--> $msg$(fgcolor end)\n";;
error) printf "$(fgcolor red)Error: $msg$(fgcolor end)\n"; exit 1;;
*) printf "$(fgcolor yellow)Unknown message type $msg_type$(fgcolor end)\n";;
esac
}
main "$@"