forked from srlefevre/zfs-repl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target-config
executable file
·146 lines (124 loc) · 2.49 KB
/
target-config
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
#! /bin/bash
# creates a configuration for target system that can be piped to a file
rmt_run(){
#params:
# ssh_host
# exec
# rmt_var - where to store results
[ $# -ne 3 ] && exit 1
local ssh_host
local exec
local result_var
local out
local rc
ssh_host=$1
exec=$2
result_var=$3
out=$( ssh $ssh_host "$exec" 2> /dev/null )
rc=$?
printf -v "${result_var}" '%s' "${out}"
return $rc
}
find_rmt_bins() {
# params:
# ssh host
# list of exec to find
# results var
[ $# -ne 3 ] && exit 1
local ssh_host
local execs
local result_var
local cmd
local p
local paths
local x
local IFS
local e
local uid
ssh_host=$1
declare -a execs=($2)
result_var=$3
#get remote users id
rmt_run $ssh_host "id -u 2> /dev/null" "uid"
#build command to run
for e in ${execs[*]}; do
cmd="$cmd x=\$(which $e);test \$? -eq 0 && echo \$x || echo 'not found';"
done
# run it
rmt_run $ssh_host "$cmd" "$result_var"
#check for missing paths
x=0
IFS=$'\n'
for p in ${!result_var}; do
if [[ "${p:0:1}" != "/" ]]; then
# try to find it
guess_rmt_bin $ssh_host ${execs[$x]}
if [ -z ${_guess_rmt_bin} ]; then
p=" "
else
p="${_guess_rmt_bin}"
fi
fi
# root doesn't need sudo
if [[ ${execs[$x]} == sudo && $uid -eq 0 ]]; then
p=' '
fi
paths="${paths}${p}"$'\n'
x=$(( x + 1 ))
done
printf -v "${result_var}" '%s' "${paths}"
}
guess_rmt_bin() {
# params
# ssh host
# exec
[ $# -ne 2 ] && exit 1
local ssh_host
local ex
local path_list
local k
local cmd
local IFS
ssh_host=$1
ex=$2
path_list="/sbin /bin /usr/sbin /usr/bin /usr/gnu/bin /opt/local/bin /opt/local/sbin"
#build command to run
IFS=' '
for k in $path_list; do
cmd="$cmd if [ -x $k/$ex ]; then echo $k/$ex; exit 0; fi;"
done
# run it
rmt_run $ssh_host "$cmd" "_${FUNCNAME}"
}
usage() {
echo "Usage: $(basename $0 .sh) --host hostname"
exit 0
}
declare -a RMT_VARS=('RMT_SUDO=%s' 'RMT_ZFS="$RMT_SUDO %s"' 'RMT_GZIP=%s' 'RMT_XZCAT=%s' 'RMT_NC=%s' 'RMT_SOCAT=%s' 'RMT_MBUFFER=%s' 'RMT_CAT=%s' 'RMT_OPENSSL=%s' )
EXECS="sudo zfs gzip xzcat nc socat mbuffer cat openssl"
RMT_BINS=
CONFIG_PATH=/etc/zfs-repl
while [ $# -gt 0 ];do
case $1 in
-H|--host)
shift
HOST=$1
;;
*)
usage
;;
esac
shift
done
[ -z $HOST ] && usage
HOST_CONF=$CONFIG_PATH/$HOST.conf
CONFIG="# remote target for zfs-repl"$'\n'
find_rmt_bins $HOST "$EXECS" "RMT_BINS"
IFS=$'\n'
i=0
for v in $RMT_BINS; do
printf -v t "${RMT_VARS[$i]}" "$v"
CONFIG="${CONFIG}${t}"$'\n'
i=$(( i + 1 ))
done
echo "$CONFIG"