mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 17 | # Version: 1.3-eng |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 18 | # |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 19 | set -o nounset |
| 20 | umask 077 |
| 21 | |
| 22 | # |
| 23 | # Settings |
| 24 | # |
Benoit Lamarche | 709ddb9 | 2016-12-12 15:34:26 +0100 | [diff] [blame] | 25 | JACK_VERSION=${JACK_VERSION:=4.19.BETA} |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 26 | JACK_HOME="${JACK_HOME:=$HOME/.jack-server}" |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 27 | JACK_CLIENT_SETTING="${JACK_CLIENT_SETTING:=$HOME/.jack-settings}" |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 28 | TMPDIR=${TMPDIR:=/tmp} |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 29 | # This is half the timeout since the script will make a second attempt collecting debugs when |
| 30 | # the first attempt fails on connection timeout |
| 31 | JACK_CONNECTION_TIMEOUT=150 |
Yohann Roussel | 5b90920 | 2016-01-04 14:01:05 +0100 | [diff] [blame] | 32 | JACK_EXTRA_CURL_OPTIONS=${JACK_EXTRA_CURL_OPTIONS:=} |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 33 | JACK_ASSERTION_ENABLED="${JACK_ASSERTION_ENABLED:=false}" |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 34 | |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 35 | |
| 36 | abort () { exit 255; } |
| 37 | |
| 38 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 39 | # |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 40 | # Load client settings |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 41 | # |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 42 | if [ -f "$JACK_CLIENT_SETTING" ]; then |
| 43 | source "$JACK_CLIENT_SETTING" |
| 44 | else |
| 45 | echo "Cannot find settings at '$JACK_CLIENT_SETTING'" >&2 |
| 46 | abort |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 47 | fi |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 48 | |
Yohann Roussel | e23f992 | 2015-11-04 21:51:25 +0100 | [diff] [blame] | 49 | |
| 50 | JACK_SERVER=${JACK_SERVER:=true} |
| 51 | JACK_MAIN_COMMAND=${JACK_MAIN_COMMAND:="java -Djava.io.tmpdir=$TMPDIR -Dfile.encoding=UTF-8 -XX:+TieredCompilation"} |
| 52 | JACK_REPOSITORY=${JACK_REPOSITORY:=} |
| 53 | |
| 54 | |
| 55 | # |
| 56 | # If not in server mode, exec jack |
| 57 | # |
| 58 | if [ "$JACK_SERVER" != "true" ]; then |
| 59 | if [ -z "$JACK_REPOSITORY" ]; then |
| 60 | echo "Running Jack without Jack server requires definition of JACK_REPOSITORY" >&2 |
| 61 | abort |
| 62 | fi |
| 63 | JACK_JAR=$JACK_REPOSITORY/jack-$JACK_VERSION.jar |
| 64 | if [ ! -r "$JACK_JAR" ]; then |
| 65 | echo "Jack jar \"$JACK_JAR\" is not readable" >&2 |
| 66 | abort |
| 67 | fi |
| 68 | |
| 69 | |
| 70 | exec $JACK_MAIN_COMMAND -jar $JACK_JAR "$@" |
| 71 | echo "Cannot succeed to launch Jack without Jack server" >&2 |
| 72 | abort |
| 73 | fi |
| 74 | |
| 75 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 76 | # |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 77 | # Prepare compilation |
| 78 | # |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 79 | JACK_PWD="$PWD" |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 80 | JACK_EXIT="$TMPDIR/jack-task-$USER-$$-exit" |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 81 | |
| 82 | # Cleanup |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 83 | trap 'rm -f "$JACK_EXIT" 2>/dev/null;' EXIT |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 84 | |
| 85 | set -o errexit |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 86 | |
| 87 | # Check that we can create temp file for exit status |
| 88 | rm -rf "$JACK_EXIT" |
| 89 | echo "" > "$JACK_EXIT" |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 90 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 91 | # |
| 92 | # Launch the compilation |
| 93 | # |
| 94 | |
Yohann Roussel | 1b697cb | 2015-04-15 15:01:33 +0200 | [diff] [blame] | 95 | set +o errexit |
| 96 | trap ERR |
| 97 | |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 98 | # put arguments in a non array variable |
| 99 | ARGS="" |
| 100 | for i in "$@"; do |
| 101 | ARGS="$ARGS $i" |
| 102 | done |
| 103 | |
| 104 | |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 105 | CURRENT_CHARSET=$(locale charmap) |
Yohann Roussel | 5b90920 | 2016-01-04 14:01:05 +0100 | [diff] [blame] | 106 | if [ -z "$CURRENT_CHARSET" ]; then |
| 107 | CHARSET_ARGUMENT= |
| 108 | else |
| 109 | CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET" |
| 110 | fi |
| 111 | |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 112 | # Check base64 availability |
| 113 | BASE64_CHECK=$((echo amFjaw==;echo LXNlcnZlcg==) | base64 --decode 2>&1) |
| 114 | |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 115 | sendRequest() { |
| 116 | RETRY_OPTION=$1 |
| 117 | shift |
| 118 | |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 119 | # Launch compilation |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 120 | exec 3>&1 |
| 121 | exec 4>&2 |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 122 | if [ "$BASE64_CHECK" = jack-server ]; then |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 123 | HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 124 | --cert "${JACK_HOME}/client.pem" \ |
| 125 | --cacert "${JACK_HOME}/server.pem" \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 126 | --output >(tee >(sed -n -e 's/^E|\(.*\)$/\1/p' | base64 --decode >&4 ) | tee >(sed -n -e 's/^X|\(.*\)$/\1/p' >>$JACK_EXIT) | sed -n -e 's/^O|\(.*\)$/\1/p' | base64 --decode >&3) \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 127 | --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \ |
| 128 | -X POST \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 129 | -H "Accept: application/vnd.jack.command-out-base64;version=1$CHARSET_ARGUMENT" \ |
| 130 | -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 131 | -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \ |
| 132 | -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 133 | -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 134 | --noproxy ${SERVER_HOST} \ |
| 135 | https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \ |
| 136 | ) |
| 137 | else |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 138 | HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 139 | --cert "${JACK_HOME}/client.pem" \ |
| 140 | --cacert "${JACK_HOME}/server.pem" \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 141 | --output >(tee >(sed -n -e 's/^E|\(.*\)$/\1/p' >&4 ) | tee >(sed -n -e 's/^X|\(.*\)$/\1/p' >>$JACK_EXIT) | sed -n -e 's/^O|\(.*\)$/\1/p' >&3) \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 142 | --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \ |
| 143 | -X POST \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 144 | -H "Accept: application/vnd.jack.command-out;version=1$CHARSET_ARGUMENT" \ |
| 145 | -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 146 | -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \ |
| 147 | -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 148 | -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \ |
Yohann Roussel | 2960f69 | 2016-09-20 17:33:21 +0200 | [diff] [blame] | 149 | --noproxy ${SERVER_HOST} \ |
| 150 | https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \ |
| 151 | ) |
| 152 | fi |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 153 | CURL_CODE=$? |
| 154 | exec 3>&- |
| 155 | exec 4>&- |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 156 | |
| 157 | set -o errexit |
| 158 | |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 159 | if [ $CURL_CODE -eq 0 ]; then |
| 160 | # No problem, let's go |
Yohann Roussel | 90c63e6 | 2016-11-18 18:12:50 +0100 | [diff] [blame] | 161 | JACK_CODE=$(cat "$JACK_EXIT") |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 162 | exit $JACK_CODE |
| 163 | elif [ $CURL_CODE -eq 7 ]; then |
| 164 | # Failed to connect |
| 165 | echo "No Jack server running. Try 'jack-admin start-server'" >&2 |
| 166 | abort |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 167 | elif [ $CURL_CODE -eq 28 ]; then |
| 168 | if [ "$RETRY_OPTION" == "" ]; then |
| 169 | echo "Connection to the Jack server timeout, retrying with debug" |
| 170 | sendRequest -v $@ |
| 171 | else |
| 172 | echo "Connection to the Jack server timeout" >&2 |
| 173 | abort |
| 174 | fi |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 175 | elif [ $CURL_CODE -eq 35 ]; then |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 176 | if [ "$RETRY_OPTION" == "" ]; then |
| 177 | echo "SSL error when connecting to the Jack server, retrying with debug" |
| 178 | sendRequest -v $@ |
| 179 | else |
| 180 | echo "SSL error when connecting to the Jack server. Try 'jack-diagnose'" >&2 |
| 181 | abort |
| 182 | fi |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 183 | elif [ $CURL_CODE -eq 58 ]; then |
Yohann Roussel | e3f18c2 | 2016-02-24 16:16:42 +0100 | [diff] [blame] | 184 | echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/client.pem. Try 'jack-diagnose'" >&2 |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 185 | abort |
| 186 | elif [ $CURL_CODE -eq 60 ]; then |
Yohann Roussel | e3f18c2 | 2016-02-24 16:16:42 +0100 | [diff] [blame] | 187 | echo "Failed to authenticate Jack server certificate. Try 'jack-diagnose'" >&2 |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 188 | abort |
| 189 | elif [ $CURL_CODE -eq 77 ]; then |
Yohann Roussel | e3f18c2 | 2016-02-24 16:16:42 +0100 | [diff] [blame] | 190 | echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/server.pem. Try 'jack-diagnose'" >&2 |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 191 | abort |
| 192 | elif [ $CURL_CODE -eq 22 ]; then |
| 193 | # Http code not OK, let's decode and abort |
| 194 | if [ $HTTP_CODE -eq 400 ]; then |
| 195 | # 400: Bad request |
| 196 | echo "Bad request, see server log" >&2 |
Yohann Roussel | b6b1f82 | 2015-07-01 16:54:15 +0200 | [diff] [blame] | 197 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 198 | else |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 199 | # Other |
Yohann Roussel | e3f18c2 | 2016-02-24 16:16:42 +0100 | [diff] [blame] | 200 | echo "Internal unknown error ($HTTP_CODE), try 'jack-diagnose' or see Jack server log" >&2 |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 201 | abort |
mikaelpeltier | 4d1fcc4 | 2015-04-07 11:15:06 +0200 | [diff] [blame] | 202 | fi |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 203 | else |
Yohann Roussel | e3f18c2 | 2016-02-24 16:16:42 +0100 | [diff] [blame] | 204 | echo "Communication error with Jack server ($CURL_CODE). Try 'jack-diagnose'" >&2 |
Yohann Roussel | 664ef35 | 2015-10-23 18:09:02 +0200 | [diff] [blame] | 205 | abort |
| 206 | fi |
Yohann Roussel | d92acea | 2016-12-14 19:36:34 +0100 | [diff] [blame^] | 207 | } |
| 208 | |
| 209 | sendRequest "" $@ |