~popey/phablet-tools/fix-1360582

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
152
153
154
155
156
157
158
#!/bin/sh
# This program is free software: you can redistribute it and/or modify it
# under the terms of the the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the applicable version of 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, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2012 Canonical, Ltd.

export LC_ALL=C

usage() {
cat <<EOF
usage: $0 [OPTIONS]

Copies ACTIVE network manager connection into device

OPTIONS:
  -h	Show this message
  -s    Specify the serial of the device to install (see adb $ADBOPTS devices)
  -n    Select network file
  -t    Timeout for waiting on network connection to become active.
        Default=$WAIT_TIMEOUT

  --skip-setup   Skip setting up a network-manager connection on the target.
  --skip-wait    Skip waiting for the network connection to become active.

EOF
}

ADBOPTS=""
OPTION_NETWORK_FILE=""
SKIP_SETUP=""
SKIP_WAIT=""
WAIT_TIMEOUT="80s"

ARGS=$(getopt -o n:s:t:h -l "help,skip-setup,skip-wait,timeout:" -n "$0" -- "$@")
if [ $? -ne 0 ] ; then
    exit 1
fi
eval set -- "$ARGS"

while true; do
    case "$1" in
        -h|--help)
            usage
            exit 0
            ;;
        -n)
            shift;
	    OPTION_NETWORK_FILE=$1
            shift;
            ;;
	-s)
            shift;
            ADBOPTS="-s $1"
            shift;
            ;;
	-t|--timeout)
            shift;
            WAIT_TIMEOUT=$1
            shift;
            ;;
	--skip-wait)
            shift;
	    SKIP_WAIT=1
            ;;
	--skip-setup)
            shift;
	    SKIP_SETUP=1
            ;;
        --)
            shift;
            break;
            ;;
    esac
done

NETWORK_MANAGER=/etc/NetworkManager/system-connections

find_active_network() {
    wireless_adapter="$(nmcli -t -f device,type dev | egrep wireless | cut -d: -f1)"
    if [ -z "$wireless_adapter" ]
    then
        echo "No wireless adapter found, exiting"
        exit 1
    fi
   network_active_uuid="$(nmcli -t -f name,uuid,devices,vpn con status | grep $wireless_adapter | egrep ':no$'| cut -d: -f2)"
    if [ -z "$network_active_uuid" ]
    then
        echo "No active wifi network connection, exiting"
        exit 1
    fi
    network_file=$(sudo grep -H "$network_active_uuid" /etc/NetworkManager/system-connections/* | grep uuid | cut -f1 -d:)

    echo "$network_file"
}

setup_connection() {
    if [ -z "$OPTION_NETWORK_FILE" ]; then
        network_file=$(find_active_network)
    else
        network_file=$OPTION_NETWORK_FILE
    fi

    if [ ! -f "$network_file" ]
    then
        echo "Network connection file \"$network_file\" cannot be read"
        exit 1
    fi

    echo Network file is $network_file

    TMP_FILE=$(mktemp)
    CMD="grep -v mac-address"
    [ ! -r "$network_file" ] && CMD="sudo $CMD"
    $CMD "$network_file" > $TMP_FILE

    echo Provisioning network file to device
    target_network_file=$NETWORK_MANAGER/active_ws_connection.conf
    adb $ADBOPTS push $TMP_FILE $target_network_file
    adb $ADBOPTS shell "chmod 600 $target_network_file"

    rm -f $TMP_FILE

    echo
    echo Network setup complete
}

wait_for_network() {
    timeout $WAIT_TIMEOUT sh <<EOF
        while true ; do
	    # adb shell doesn't return errors from the command
	    # also, ping can be blocked like it is in the QA lab. however,
	    # the logic below will ensure that DNS resolution part of the
	    # ping worked, which should be enough to prove to us that the
	    # network stack is ready
            if adb $ADBOPTS shell ping -c1 launchpad.net | grep PING ; then
                exit 0
            fi
            done
EOF
    if [ $? -ne 0 ] ; then
        echo "Network connection failed to become active."
	exit 1
    fi
}

adb $ADBOPTS wait-for-device
[ -n "$SKIP_SETUP" ] || setup_connection
[ -n "$SKIP_WAIT" ] || wait_for_network