-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgetpkg
executable file
·122 lines (105 loc) · 2.99 KB
/
getpkg
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
#!/bin/bash
# Copyright © Sébastien Luttringer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
PKG_BASE='packages'
PKG_REGEX=''
ARCH_REGEX="($(uname -m)|any)"
SSH_OPTIONS=('-oStrictHostKeyChecking=no' '-oUserKnownHostsFile=/dev/null')
usage() {
echo "usage: ${0##*/} [options] <list|get|install> [PKG_REGEX]" >&2
echo 'options:' >&2
echo " -h: host address (current: ${PKG_HOSTS[*]})" >&2
echo " -b: base path (current: $PKG_BASE)" >&2
echo " -a: arch regex (current: $ARCH_REGEX)" >&2
exit 1
}
sshinit() {
# default ssh program
SSH=ssh
SCP=scp
if [[ -n $SSH_AUTH_SOCK ]] && ssh-add -l &>/dev/null; then
# we using ssh-agent
true
elif type -P sshpass &>/dev/null; then
printf "%s 's password: " "$PKG_HOST"
read -rs SSHPASS
echo
if [[ -z "$SSHPASS" ]]; then
echo 'Empty pass, sshpass not needed'
return
fi
export SSHPASS
SSH='sshpass -e ssh'
SCP='sshpass -e scp'
else
echo 'Nor ssh-agent, nor sshpass detected. You wil tap your pass more than twice.'
fi
}
while getopts 'h:b:a:' opt; do
case $opt in
a) ARCH_REGEX=$OPTARG;;
b) PKG_BASE=$OPTARG;;
h) PKG_HOSTS=$OPTARG;;
*) usage;;
esac
done
shift $((OPTIND - 1));
(( $# >= 1 )) || usage
case $1 in
list|get|install) action="$1"; shift;;
*) action='get';;
esac
PKG_REGEX="$1"
list_tmp=$(mktemp)
for PKG_HOST in "${PKG_HOSTS[@]}"; do
echo "Connection to $PKG_HOST"
sshinit
$SSH "${SSH_OPTIONS[@]}" "$PKG_HOST" "find '$PKG_BASE' -type f \
-regextype posix-egrep -regex '.*/$PKG_REGEX.*-$ARCH_REGEX.pkg\.tar\.xz'" \
</dev/null >"$list_tmp" 2>/dev/null
if [[ -s "$list_tmp" || $? -eq 0 ]]; then
break
else
echo 'Connection failed or return an unexpected value'
fi
done
if [[ -s "$list_tmp" ]]; then
exec 3<>"$list_tmp"
while read -u 3 -r line; do
[[ $line ]] || continue
case $action in
list)
echo "$line"
;;
get)
[[ -f $(basename $line) ]] &&
echo "==> $(basename $line): already exists. skipped!" && continue
$SCP "${SSH_OPTIONS[@]}" "$PKG_HOST:$line" .
;;
install)
(( EUID == 0 )) && pacman='pacman' || pacman='sudo pacman'
pkg_tmp=$(mktemp)
$SCP "${SSH_OPTIONS[@]}" "$PKG_HOST:$line" "$pkg_tmp" && $pacman -U "$pkg_tmp"
rm -f "$pkg_tmp"
;;
esac
done
else
echo 'No match found'
fi
rm -f "$list_tmp"
# vim:set ts=2 sw=2 ft=sh noet: