File: remote.exec

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (88 lines) | stat: -rwxr-xr-x 2,018 bytes parent folder | download | duplicates (2)
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
#!/bin/bash -e

show_help() {
    echo "usage: remote.exec [--user <USER>] [--pass <PASS>] <CMD>"
    echo ""
    echo "Available options:"
    echo "  -h --help   show this help message."
    echo ""
}

_load_config() {
    local CFG_FILE
    CFG_FILE="$(remote.setup get-config-path)"
    if [ ! -f "$CFG_FILE" ]; then
        echo "remote.exec: config file \"$CFG_FILE\" not found, please run remote.setup command first"
        return 1
    fi
    # shellcheck disable=SC1090
    . "$CFG_FILE"
}

_get_pass() {
    local SSH_PASS
    if [ -n "$TESTS_REMOTE_PASS" ]; then
        echo "sshpass -p $TESTS_REMOTE_PASS"
    fi
}

_get_cert() {
    if [ -n "$TESTS_REMOTE_PASS" ]; then
        return
    elif [ -n "$TESTS_REMOTE_CERT" ]; then
        echo "-i $TESTS_REMOTE_CERT"
    fi
}

remote_exec() {
    local user pass
    local timeout=10
    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)
                show_help
                exit
                ;;
            --user)
                user="$2"
                shift 2
                ;;
            --pass)
                pass="$2"
                shift 2
                ;;
            --timeout)
                timeout="$2"
                shift 2
                ;;
            -*)
                echo "remote.exec: unknown option $1" >&2
                exit 1
                ;;
            *)
                break    
                ;;
        esac
    done

    _load_config
    if [ -n "$user" ]; then
        TESTS_REMOTE_USER="$user"
    fi
    if [ -n "$pass" ]; then
        TESTS_REMOTE_PASS="$pass"
    fi

    local SSH_PASS SSH_CERT
    SSH_PASS="$(_get_pass)"
    SSH_CERT="$(_get_cert)"

    # shellcheck disable=SC2153,SC2086
    $SSH_PASS ssh $SSH_CERT -p "$TESTS_REMOTE_PORT" -o LogLevel=ERROR -o ServerAliveInterval=10 -o ConnectTimeout="$timeout" -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$TESTS_REMOTE_USER"@"$TESTS_REMOTE_HOST" "$@"
}

main() {    
    remote_exec "$@"
}

main "$@"