summaryrefslogtreecommitdiff
path: root/src/rpc.bash
blob: cceadec304ae3eab96367477325201b8bbcf49d5 (plain)
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

intersection()
{
        comm -12 "$@"
}

difference()
{
        comm -23 "$@"
}

extract_words()
{
        grep -o -E '\b\w+\b' | sort -u
}

immediate_dependencies1()
{
        intersection \
                <(compgen -A function | sort -u) \
                <(declare -f "$1" | extract_words)
}

immediate_dependencies()
{
        for f
        do
                immediate_dependencies1 "$f"
        done | sort -u
}

recursive_dependencies()
{
        seen=()
        while [ $# -gt 0 ]
        do
                seen+=("$@")
                set -- $(difference <(immediate_dependencies "$@") \
                                    <(printf '%s\n' "${seen[@]}" | sort -u))
        done
        printf '%s\n' "${seen[@]}"
}








# Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases)
# Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--')
# Input; $BASH_RPC_STDIN_IS_TTY - controls whether to allocate ssh pty.  Doesn't work.
# Input: $1 - shell script source code to run in the remote bash
# Input: $2, $3, ... - command line arguments passed to the remote bash
# Input: stdin, stdout, stderr: passed to the remote bash over ssh
# Input: "$0" - copied to remote bash's $0
# Input: "$SHELL" - remote shell to launch (better be bash)
bash_rpc_remote_run_script()
{
        local script="$1"
        shift
        exec ssh \
                "${BASH_RPC_SSH_OPTIONS[@]}" -- \
                "${BASH_RPC_REMOTE_DEST:-localhost}" \
                "$SHELL" -c "${script@Q}" "$0" "${@@Q}"
}

bash_rpc_with_ssh_option()
{
        local BASH_RPC_SSH_OPTIONS
        BASH_RPC_SSH_OPTIONS+=("$1")
        shift
        "$@"
}

# Preserves the rest of stdin for the command.
bash_rpc_eval_stdin()
{
        [ "$1" -gt 0 ] || return
        read -N"$1" -r || return

        # Prepare "$@" for script execution context.
        shift

        # Even unset REPLY to leave pristine environment.
        eval "unset REPLY; $(cat <<< "$REPLY")"
}

bash_rpc_run_function_simple()
{
        script_source=$(declare -f "$1" && printf '"$@";\n')
        bash_rpc_remote_run_script  \
                "$script_source" \
                "$@"
}

remote_run_function_notty()
{
        main=$1
        funcs=$(recursive_dependencies "$main")
        stage2_source=$(
                declare -f $funcs
                printf '"$@"\n'
        )
        {
                printf '%s' "$stage2_source"
                cat
        } |
                bash_rpc_run_function_simple \
                        bash_rpc_eval_stdin \
                                "${#stage2_source}" \
                                "$@"
}

read_stdin()
{
        REPLY=
        read -r -N2147483647 || [ "$REPLY" ]
}

remote_run_function_tty()
{
        main=$1
        funcs=$(recursive_dependencies "$main")
        stage2_source=$(
                declare -f $funcs
                printf 'TERM=%s\n' "${TERM@Q}"
                printf '"$@"\n'
        )
        (
                TERM="$stage2_source"
                export TERM
                read_stdin <<'END'
                        rpc=$TERM
                        TERM=screen
                        source <(printf '%s' "$rpc")
END
                exec ssh localhost -t -- bash -c "${REPLY@Q}" bash "$@"
        )
}

remote_run_function()
{
        if [ -t 0 ]
        then
                remote_run_function_tty "$@"
        else
                remote_run_function_notty "$@"
        fi
}