remotecfg.in in remote-config

at master

1#!/bin/sh
2if [ "$1" = "-s" ]
3then
4 sudo=1
5 host=$2
6 config=$3
7else
8 host=$1
9 config=$2
10fi
11
12# Read secret string
13# This function cribbed from:
14# https://stackoverflow.com/questions/3980668/how-to-get-a-password-from-a-shell-script-without-echoing
15read_secret()
16{
17 # Disable echo.
18 stty -echo
19
20 # Set up trap to ensure echo is enabled before exiting if the script
21 # is terminated while echo is disabled.
22 trap 'stty echo' EXIT
23
24 # Read secret.
25 read "$@"
26
27 # Enable echo.
28 stty echo
29 trap - EXIT
30
31 # Print a newline because the newline entered by the user after
32 # entering the passcode is not echoed. This ensures that the
33 # next line of output begins at a new line.
34 echo
35}
36
37message()
38{
39 if [ -t 1 ]
40 then
41 printf '\033[1;32m==> \033[1;37m%s\033[0m\n' "$1"
42 else
43 echo $1
44 fi
45}
46
47if [ -n "$sudo" ]
48then
49 echo -n "SUDO Password: "
50 read_secret password
51fi
52
53message "Staging files for transfer"
54mkdir transfer
55cp -r files/$config/* transfer
56
57if [ -f secrets/$config ]
58then
59 secrets=1
60 message "Decrypting secrets"
61 gpg --quiet --output transfer/secrets --decrypt secrets/$config
62
63 if [ -d templates/$config ]
64 then
65 . transfer/secrets
66 for f in `find templates/$config -type f`
67 do
68 file=`echo $f | sed "s#templates/$config/##"`
69 sedcmd=`head -n1 $f`
70 eval sedcmd=$sedcmd
71 cat $f | awk 'BEGIN{getline}{print}' | sed $sedcmd > transfer/$file
72 done
73 fi
74fi
75
76message "Creating remote command"
77cat <<-EOF > transfer/cmd
78 #!/bin/sh
79 exec 2>&1
80 [ -f secrets ] && . secrets
81 `cat {{ prefix }}/common.sh`
82 remotenum=$$
83 `cat remote/$config`
84EOF
85chmod a+x transfer/cmd
86
87message "Creating remote temporary directory"
88ssh -T $host mkdir /tmp/remote.$$
89
90message "Recursively copying files/$config to $host"
91scp -r transfer/* $host:/tmp/remote.$$/ > /dev/null 2>&1
92
93message "Running remote command"
94if [ -n "$sudo" ]
95then
96 ssh -T $host "echo -n \"$password\" | sudo -S /tmp/remote.$$/cmd 2>/dev/null"
97else
98 ssh -T $host /tmp/remote.$$/cmd
99fi
100
101message "Cleaning up"
102ssh -T $host rm -r /tmp/remote.$$
103rm -r transfer