|
|
drist - drist - a remote deployment tool |
|
|
 |
git clone git://bitreich.org/drist/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/drist/ (git://bitreich.org) |
|
|
 |
Log |
|
|
 |
Files |
|
|
 |
Refs |
|
|
 |
Tags |
|
|
 |
README |
|
|
 |
LICENSE |
|
|
|
--- |
|
|
|
drist (2829B) |
|
|
|
--- |
|
|
|
1 #!/bin/sh |
|
|
|
2 |
|
|
|
3 SUDO=0 |
|
|
|
4 SUDO_BIN=sudo |
|
|
|
5 EXEC="" |
|
|
|
6 SSHONCE=0 |
|
|
|
7 TRUNCATE=0 |
|
|
|
8 TEMPDIR="" |
|
|
|
9 |
|
|
|
10 usage() { |
|
|
|
11 echo "$0 [-p] [-d] [-s [-e sudo|doas]] server [...]" |
|
|
|
12 exit 0 |
|
|
|
13 } |
|
|
|
14 |
|
|
|
15 # $1 = directory name |
|
|
|
16 # $2 = remote server |
|
|
|
17 # $3 = tempdir |
|
|
|
18 copy_files() { |
|
|
|
19 # -l = keep symlink / -D = special device |
|
|
|
20 if [ -d "${1}" ] |
|
|
|
21 then |
|
|
|
22 LIST=$(mktemp /tmp/drist-rsync.XXXXXXXXXX) |
|
|
|
23 if [ -f "$LIST" ] |
|
|
|
24 then |
|
|
|
25 printf 'Copying files from "%s" to temporary directory %s:\n' "$1" "$3" |
|
|
|
26 find "${1}"/ -type f -or -type l | cut -d '/' -f 2- | tee "${LIST}" | sed 's/^/ \//' |
|
|
|
27 rsync -t -e "ssh $SSH_PARAMS" -lD --files-from="${LIST}" "${1}/" "${2}":"/${3}" |
|
|
|
28 rm "$LIST" |
|
|
|
29 fi |
|
|
|
30 fi |
|
|
|
31 } |
|
|
|
32 |
|
|
|
33 # $1 = script filename |
|
|
|
34 # $2 = remote server |
|
|
|
35 # $3 = tempdir |
|
|
|
36 remote_script() { |
|
|
|
37 if [ -f "${1}" ] |
|
|
|
38 then |
|
|
|
39 printf 'Executing file "%s":\n' "$1" |
|
|
|
40 ssh $SSH_PARAMS "${2}" "cd ${3} && DRIST=${3}/script && |
|
|
|
41 cat - > \$DRIST && |
|
|
|
42 chmod u+x \$DRIST && |
|
|
|
43 ${EXEC} \$DRIST" < "$1" |
|
|
|
44 fi |
|
|
|
45 } |
|
|
|
46 |
|
|
|
47 # $1 = remote server |
|
|
|
48 create_temp() { |
|
|
|
49 TEMPDIR=$(ssh $SSH_PARAMS "$1" "mktemp -d ~/.drist_files_XXXXXXXXXXXXXXX") |
|
|
|
50 if [ "$TEMPDIR" = "" ]; then |
|
|
|
51 echo "mktemp error, aborting" |
|
|
|
52 exit 1 |
|
|
|
53 fi |
|
|
|
54 } |
|
|
|
55 |
|
|
|
56 # $1 = remote server |
|
|
|
57 # $2 = temporary directory |
|
|
|
58 delete_temp() { |
|
|
|
59 if echo "${2}" | grep drist_files_ >/dev/null ; then |
|
|
|
60 ssh $SSH_PARAMS "$1" "rm -fr ${2}" |
|
|
|
61 else |
|
|
|
62 echo "Problem, TEMPDIR was reset during execution, current value is = $2" |
|
|
|
63 exit 2 |
|
|
|
64 fi |
|
|
|
65 } |
|
|
|
66 |
|
|
|
67 |
|
|
|
68 # RUNTIME BEGINS HERE |
|
|
|
69 while getopts pndse: arg; do |
|
|
|
70 case ${arg} in |
|
|
|
71 d) TRUNCATE=1 ;; |
|
|
|
72 s) SUDO=1 ;; |
|
|
|
73 e) SUDO_BIN="${OPTARG}" ;; |
|
|
|
74 p) SSHONCE=1 ;; |
|
|
|
75 *) usage ;; |
|
|
|
76 esac |
|
|
|
77 done |
|
|
|
78 shift $((OPTIND - 1)) |
|
|
|
79 |
|
|
|
80 # allow to use a privilege escalation program |
|
|
|
81 if [ "$SUDO" -eq 1 ] |
|
|
|
82 then |
|
|
|
83 EXEC="$SUDO_BIN" |
|
|
|
84 fi |
|
|
|
85 |
|
|
|
86 # use ControlMaster to make connections persistent |
|
|
|
87 if [ "$SSHONCE" -eq 1 ] |
|
|
|
88 then |
|
|
|
89 SSH_PARAMS=-o"ControlMaster=auto"" "-o"ControlPath=/tmp/drist_ssh_%h_%p_%r.sock"" "-o"ControlPersist=1m" |
|
|
|
90 fi |
|
|
|
91 |
|
|
|
92 # start looping over server list |
|
|
|
93 if [ -f "$1" ] |
|
|
|
94 then |
|
|
|
95 SERVER_LIST="$(tr '\n' ' ' < $1)" |
|
|
|
96 else |
|
|
|
97 SERVER_LIST="$@" |
|
|
|
98 fi |
|
|
|
99 |
|
|
|
100 if [ "${SERVER_LIST}" = "" ] |
|
|
|
101 then |
|
|
|
102 echo "No server specified" |
|
|
|
103 exit 1 |
|
|
|
104 fi |
|
|
|
105 |
|
|
|
106 for remote_server in ${SERVER_LIST} |
|
|
|
107 do |
|
|
|
108 echo "Running on ${remote_server}" |
|
|
|
109 |
|
|
|
110 # check if host exists |
|
|
|
111 HOSTNAME=$(ssh $SSH_PARAMS "${remote_server}" "uname -n") |
|
|
|
112 if [ "$?" -ne 0 ]; then |
|
|
|
113 echo "Error while ssh ${remote_server}" |
|
|
|
114 exit 2 |
|
|
|
115 fi |
|
|
|
116 |
|
|
|
117 if [ "$TRUNCATE" -eq 1 ]; then |
|
|
|
118 HOSTNAME="${HOSTNAME%%.*}" |
|
|
|
119 fi |
|
|
|
120 |
|
|
|
121 create_temp "${remote_server}" |
|
|
|
122 copy_files "files" "${remote_server}" "$TEMPDIR" |
|
|
|
123 copy_files "files-${HOSTNAME}" "${remote_server}" "$TEMPDIR" |
|
|
|
124 remote_script "script" "${remote_server}" "$TEMPDIR" |
|
|
|
125 remote_script "script-${HOSTNAME}" "${remote_server}" "$TEMPDIR" |
|
|
|
126 delete_temp "${remote_server}" "$TEMPDIR" |
|
|
|
127 |
|
|
|
128 # close socket if persistance is actived |
|
|
|
129 if [ "$SSHONCE" -eq 1 ] |
|
|
|
130 then |
|
|
|
131 ssh $SSH_PARAMS -O exit -N "$1" |
|
|
|
132 fi |
|
|
|
133 |
|
|
|
134 unset TEMPDIR HOSTNAME |
|
|
|
135 done |
|