-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfdist
executable file
·157 lines (138 loc) · 2.1 KB
/
confdist
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
#!/bin/bash
# copies UNIX configuration files to other hosts
info()
{
if test ${verbose:-0} -gt 0
then
echo "$*" 1>&2
fi
}
error()
{
echo "$*" 1>&2
}
run()
{
if test "$simulate" -gt 0
then
echo "$@"
else
"$@"
fi
return $?
}
scp()
{
local scpflags=-Bq
if test "$verbose" -gt 0
then
command scp $scpflags "$@"
else
command scp $scpflags "$@" 2>/dev/null
fi
return $?
}
ssh()
{
if test "$verbose" -gt 0
then
command ssh "$@"
else
command ssh "$@" 2>/dev/null
fi
return $?
}
read_defaults()
{
hosts=
verbose=0
test -f "$HOME"/.confdist && . "$HOME"/.confdist
}
usage()
{
echo "Usage: confdist [-c <config file>] [-v] [<host>...]" 1>&2
}
read_options()
{
while getopts ":hv" flag
do
case $flag in
h)
usage
exit 0
;;
v)
verbose=$(($verbose + 1))
;;
':')
error "Missing argument to -$OPTARG"
usage
exit 2
;;
'?')
error "Unknown option -$OPTARG"
usage
exit 2
;;
*)
error "The -$flag option is not supported yet"
usage
exit 2
;;
esac
done
[ $verbose -gt 1 ] && set -x
shift `expr $OPTIND - 1`
test $# -gt 0 && hosts="$@"
}
copy_to()
{
local host=$1
local conffile
local confname
for conffile in "$HOME"/conf*.tar.gz; do
test -f "$conffile" || continue
confname=$(basename "$conffile")
info "Copying $confname to $host"
printf "Copying $confname to $host... "
scp "$conffile" $host:~
if test $? -ne 0
then
printf "failure\n"
continue
fi
printf "success\n"
done
}
install_on()
{
local host=$1
printf "Installing on $host... "
ssh $host "test -d scripts || mkdir scripts"
scp "$(dirname "$0")/confinst" $host:scripts
if test $? -ne 0
then
printf "failure copying install script to $host:scripts\n"
return 1
fi
ssh $host "scripts/confinst" -e
if test $? -ne 0
then
printf "failure running remote install script\n"
else
printf "success\n"
fi
}
distribute_from()
{
local host=$1
ssh $host confdist
}
trap "exit" INT TERM QUIT
read_defaults
read_options "$@"
for host in $hosts
do
copy_to $host && install_on $host && distribute_from $host || true
done
# vim: set ts=4 sw=4 tw=0 noet: