-
Notifications
You must be signed in to change notification settings - Fork 0
/
jailerctl
executable file
·218 lines (183 loc) · 5.92 KB
/
jailerctl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh
# set global variables
PROGNAME=${0##*/}
VERSION="0.1.3"
COLOUR_SET_R="\033[0;31m"
COLOUR_SET_G="\033[0;32m"
COLOUR_SET_Y="\033[0;33m"
COLOUR_SET_B="\033[0;34m"
COLOUR_SET_M="\033[0;35m"
COLOUR_SET_C="\033[0;36m"
COLOUR_SET_W="\033[0;37m"
COLOUR_END="\033[0m"
help_usage(){
cat <<EOT
Usage: jailerctl ...
help
version
remote
list
add RemoteName username@host [sudo]
remove RemoteName
rename RemoteName NewRemoteName
info [Remote]
list [Remote]
run Remote <jailer command>
EOT
exit 0
}
help_version(){
printf "%s: v%s\n" "${PROGNAME}" "${VERSION}"
}
help_tbd(){
printf "This feature is not implemented yet...\n"
}
remote_list(){
_remotes=$(ls -tr ~/.jailerctl/remotes/ 2>/dev/null)
[ -z "${_remotes}" ] && echo "No remotes available. Use \`${PROGNAME} remote add\` to add remotes" && exit 0
for remote in ${_remotes} ;
do
printf "${remote}^"
cat ~/.jailerctl/remotes/${remote}
done | column -ts^
}
remote_add(){
shift
[ "$#" -lt 2 ] && help_usage
_remoteName="$1"
_remoteConn="$2"
_remoteSudo="$3"
[ -f ~/.jailerctl/remotes/$_remoteName ] \
&& printf "${COLOUR_SET_Y}WARN:${COLOUR_END} " \
&& printf "Remote ${_remoteName} already exists\n" \
&& exit 1
( echo "${_remoteConn} ${_remoteSudo}" >> ~/.jailerctl/remotes/$_remoteName ) \
&& printf "${COLOUR_SET_G}Done!${COLOUR_END}\n"
}
remote_remove(){
shift
[ ! "$#" -eq 1 ] && help_usage
_remoteName="$1"
[ ! -f ~/.jailerctl/remotes/$_remoteName ] \
&& printf "${COLOUR_SET_Y}WARN:${COLOUR_END} " \
&& printf "Remote ${_remoteName} doesn't exists...\n" \
&& exit 1
( rm ~/.jailerctl/remotes/$_remoteName ) \
&& printf "${COLOUR_SET_G}Done!${COLOUR_END}\n"
}
remote_rename(){
help_tbd
}
remote_parse(){
shift
[ $# -eq 0 ] && remote_list
case "$1" in
list) remote_list "$@" ;;
add) remote_add "$@" ;;
remove) remote_remove "$@" ;;
rename) remote_rename "$@" ;;
esac
}
list_parse(){
shift
[ "$#" -eq 1 ] && list_do "$1" | sed -e '1s/^/HOST NAME STATE JID HOSTNAME IPv4 GW\n/'| column -t && exit 0
_remotes=$(ls -tr ~/.jailerctl/remotes/ 2>/dev/null)
[ -z "${_remotes}" ] && echo "No remotes available. Use \`${PROGNAME} remote add\` to add remotes" && exit 0
export -f list_do
echo "${_remotes}" \
| xargs -L1 -P0 sh -c 'list_do "$0"' \
| sed -e '1s/^/HOST NAME STATE JID HOSTNAME IPv4 GW\n/' \
| column -t
}
list_do(){
remoteName="$1"
SSH_OPTS="-q -o ConnectTimeout=5 -o ControlPersist=300 -o ControlMaster=yes -o ControlPath=/tmp/jailerctl.${remoteName}.ctl"
( ssh ${SSH_OPTS} $(cat ~/.jailerctl/remotes/${remoteName}) jailer list || printf "\nEHOSTUNREACH EHOSTUNREACH EHOSTUNREACH EHOSTUNREACH EHOSTUNREACH EHOSTUNREACH\n" "${remoteName}" ) | sed -e "1d" -e "s/^/${remoteName} /"
}
run_parse(){
shift
[ "$#" -lt 2 ] && help_usage
_remoteName="$1"
[ ! -f ~/.jailerctl/remotes/$_remoteName ] \
&& printf "${COLOUR_SET_Y}WARN:${COLOUR_END} " \
&& printf "Remote ${_remoteName} doesn't exists...\n" \
&& exit 1
run_do "$@"
}
run_do(){
remoteName="$1"
shift
jailerRun=$*
ssh -q -o ControlPersist=300 -o ControlMaster=yes -o ControlPath=/tmp/jailerctl.${remoteName}.ctl -t $(cat ~/.jailerctl/remotes/${remoteName}) jailer "${jailerRun}"
}
info_parse(){
shift
[ ! -z "$1" ] && info_do "$1" && exit 0
_remotes=$(ls -tr ~/.jailerctl/remotes/ 2>/dev/null)
[ -z "${_remotes}" ] && echo "No remotes available. Use \`${PROGNAME} remote add\` to add remotes" && exit 0
export -f info_do
echo "${_remotes}" \
| xargs -L1 -P0 sh -c "info_do \$0 > ~/.jailerctl/tmp/\$0"
echo "${_remotes}" \
| xargs -I% -L1 cat ~/.jailerctl/tmp/%
rm ~/.jailerctl/tmp/*
}
info_do(){
remoteName="$1"
SSH_OPTS="-q -o ConnectTimeout=5 -o ControlPersist=300 -o ControlMaster=yes -o ControlPath=/tmp/jailerctl.${remoteName}.ctl"
[ ! -f ~/.jailerctl/remotes/$remoteName ] \
&& printf "${COLOUR_SET_Y}WARN:${COLOUR_END} " \
&& printf "Remote ${remoteName} doesn't exists...\n" \
&& exit 1
ssh ${SSH_OPTS} -t $(cat ~/.jailerctl/remotes/${remoteName} | cut -d ' ' -f 1) '/bin/sh -s' <<EOS
printf "${remoteName} ->\n"
printf " Hostname : "
hostname
sec=\$(sysctl -n kern.boottime | cut -w -f 4 | tr -d ',' | xargs expr \$(date +%s) - )
days=\$(expr \${sec} / 60 / 60 / 24)
hours=\$(expr \${sec} / 60 / 60 % 24)
minutes=\$(expr \${sec} / 60 % 60)
printf " Uptime : %sd %sh %sm\n" \${days} \${hours} \${minutes}
printf " OS Version : K: %s, R: %s, U: %s\n" \$(freebsd-version -kru)
printf " Jailer Vers: %s\n" \$(sudo jailer version | head -1)
printf " CPUs : "
sysctl -n hw.ncpu
printf " Load : "
top -b | grep '^CPU:' | cut -w -f 2- | tr '\t' ' '
printf " Memory : "
top -b | grep '^Mem:' | cut -w -f 2- | tr '\t' ' '
jailer_zfs=\$(sysrc -n jailer_dir | cut -d : -f 2)
jailer_pool=\$(printf "\${jailer_zfs}" | cut -d / -f 1)
used=\$(zfs get -H -o value used \${jailer_zfs})
mountpoint=\$(zfs get -H -o value mountpoint \${jailer_zfs})
size=\$(zfs get -H -o value quota \${jailer_zfs})
[ "\$size" = "none" ] && size=\$(zpool get -H -o value size \${jailer_pool})
printf " Jailer Dir : "
printf "\${used}/\${size} @ \${mountpoint}\n"
printf " Jails : "
jls jid | wc -l | cut -w -f 2
EOS
_ssh_ret="$?"
if [ "${_ssh_ret}" -eq 255 ];
then
printf "${remoteName} ->\n EHOSTUNREACH\n"
elif [ "${_ssh_ret}" -gt 0 ];
then
printf "${remoteName} ->\n ERROR\n"
fi
}
jailerctl_main(){
[ $# -eq 0 ] && help_usage
[ ! -d ~/.jailerctl/remotes ] && echo "Creating ~/.jailerctl/remotes" && mkdir -p ~/.jailerctl/remotes
[ ! -d ~/.jailerctl/tmp ] && echo "Creating ~/.jailerctl/tmp" && mkdir -p ~/.jailerctl/tmp
case "$1" in
help) help_usage ;;
version) help_version ;;
remote) remote_parse "$@" ;;
list) list_parse "$@" ;;
run) run_parse "$@" ;;
info) info_parse "$@" ;;
*) help_usage ;;
esac
}
jailerctl_main "$@"