~phablet-team/phablet-tools/trunk

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
159
160
161
162
163
164
165
166
#!/bin/sh -e
# 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) 2014 Canonical, Ltd.

usage () {
cat <<EOF
usage: $0 COMMAND TICKET-NUMBER [DEVICE-PASSWORD]
$1

COMMANDS:
    host-install        Deprecated. Please use host-upgrade instead.
    device-install      Deprecated. Please use device-upgrade instead.
    host-upgrade        Upgrades your host machine with packages from the silo.
    device-upgrade      Upgrades your connected device with packages from the silo.
    host-purge          Uses ppa-purge to uninstall the silo contents from the host machine.
    device-purge        Not implemented.
EOF
exit 1
}

loudly () {
    echo adb shell "$@"
    adb shell "$@"
}

# List all packages available in PPA $NAME, and all packages installed locally
_apt_ppa_manifest() {
    adb shell 'sed --quiet --expression "s/^Package: //p" /var/lib/apt/lists/*ppa-service*'$NAME'*Packages'  | tr -d '\r'
    adb shell dpkg --get-selections | grep --invert-match deinstall | tr -d '\r' | tr : '\t' | cut --field 1
}

# Lists binary packages installed from PPA $NAME.
apt_ppa_manifest() {
    _apt_ppa_manifest | sort | uniq --repeated
}

# List packages names & versions which were installed from PPA $NAME.
apt_ppa_versions() {
    for binary in $(apt_ppa_manifest); do
        # shellcheck disable=SC2046
        echo $(adb shell "apt-cache policy $binary" | head -2)
    done
}

[ -x /usr/bin/sudo               ] || { echo "Please install 'sudo'"; exit 1; }
[ -x /usr/bin/add-apt-repository ] || { echo "Please install 'software-properties-common'"; exit 1; }

if [ "$(basename $0)" = "citrain" ]; then
    echo "WARNING: citrain tool is deprecated. it is now called bileto."
fi

DIR=$(dirname "$0")
if [ -f "$DIR/shell-adb-common.sh" ]; then
    . "$DIR/shell-adb-common.sh"
else
    . "/usr/share/phabletutils/shell-adb-common.sh"
fi

# Defaults
PPA="ppa:ci-train-ppa-service"

# Read the first positional argument.
COMMAND="$1"
[ $# -gt 0 ] && shift || usage "Missing COMMAND"

# Check that silo number is really a number.
echo "$1" | egrep -q "^[0-9]+$" || usage "TICKET-NUMBER not a number."

# Read the second positional argument.
TICKET="$1"
[ $# -gt 0 ] && shift || usage "Missing TICKET-NUMBER."

NAME="$(curl https://requests.ci-train.ubuntu.com/v1/ticket/$TICKET | grep --perl-regexp --only-matching '"siloname":\s?"\K[^"]+' | sed --quiet --expression '1s#\w\+/##p')"

case "$COMMAND" in
    host-upgrade)
        set -eux
        sudo add-apt-repository "$PPA/$NAME"
        sudo apt-get update -qq
        sudo apt-get dist-upgrade --yes
        ;;

    device-upgrade)
        check_devices
        # Read the third positional argument.
        PASSWORD="$1"
        [ $# -gt 0 ] && shift || usage "Missing DEVICE-PASSWORD"

        SERIES=$(adb shell lsb_release -cs | tr -d '\r')
        DISTRO=$(adb shell lsb_release -is | tr -d '\r' | tr '[:upper:]' '[:lower:]' | tr ' ' '-')

        loudly egrep ^deb /etc/apt/sources.list.d/\*.list
        loudly "echo -e '#\x21/bin/sh\necho $PASSWORD' >/tmp/askpass.sh"
        loudly chmod +x /tmp/askpass.sh
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A touch /userdata/.adb_onlock
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A mount -o remount,rw /

        # Stable images have stable-snapshot PPA but sometimes we need to pull deps from overlay:
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A add-apt-repository -y "$PPA/$DISTRO/stable-phone-overlay"
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A add-apt-repository -y "$PPA/$DISTRO/$NAME"

        loudly "test -e /usr/sbin/policy-rc.d && cp /usr/sbin/policy-rc.d /tmp/policy-rc.d"
        loudly "echo 'exit 101' | SUDO_ASKPASS=/tmp/askpass.sh sudo -A tee /usr/sbin/policy-rc.d"
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A chmod +x /usr/sbin/policy-rc.d
        loudly "echo -e 'Package: *\nPin: release o=*$NAME*\nPin-Priority: 1100\n\nPackage: *\nPin: release a=$SERIES*\nPin-Priority: 50' | SUDO_ASKPASS=/tmp/askpass.sh sudo -A tee /etc/apt/preferences.d/extra-ppas.pref"
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get update

        # We need to put lxc-android-config on hold here as it's the only
        # package which must be upgrade by hand through the recovery. See
        # https://wiki.ubuntu.com/Touch/Testing/lxc-android-config for details.
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-mark hold lxc-android-config

        before="$(mktemp /tmp/bileto-before-XXX.txt)"
        apt_ppa_versions > "$before"

        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get dist-upgrade --yes --force-yes
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-get autoremove --yes --force-yes

        after="$(mktemp /tmp/bileto-after-XXX.txt)"
        apt_ppa_versions > "$after"

        loudly "test ! -e /tmp/policy-rc.d && SUDO_ASKPASS=/tmp/askpass.sh sudo -A rm /usr/sbin/policy-rc.d"
        loudly "test -e /tmp/policy-rc.d && SUDO_ASKPASS=/tmp/askpass.sh sudo -A mv /tmp/policy-rc.d /usr/sbin/policy-rc.d"
        loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A apt-mark unhold lxc-android-config

        if diff "$before" "$after"; then
            echo "ERROR: No new packages installed! Skipping reboot, please investigate why installation failed."
            echo
            echo "PPA manifest before installation:"
            cat "$before"
            echo
            echo "PPA manifest after installation:"
            cat "$after"
        else
            loudly SUDO_ASKPASS=/tmp/askpass.sh sudo -A reboot
        fi
        rm --force "$before" "$after"
        ;;

    host-purge)
        set -x
        sudo ppa-purge "$PPA/$NAME"
        ;;

    device-purge)
        echo "Unfortunately purging from the device is unsupported because"
        echo "ppa-purge is not installed by default in the images."
        echo "However, the silo packages go away next time you flash the device."
        ;;

    *)
        usage "Unknown COMMAND."
        ;;
esac