blob: 6705df62b25b0802caf65194f5e595b8a77f2cff [file] [log] [blame]
mikaelpeltier4d1fcc42015-04-07 11:15:06 +02001#!/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 Rousseld92acea2016-12-14 19:36:34 +010017# Version: 1.3-eng
Yohann Roussel1b697cb2015-04-15 15:01:33 +020018#
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020019set -o nounset
20umask 077
21
22#
23# Settings
24#
Benoit Lamarchef54a2412017-07-19 18:46:48 +020025JACK_VERSION=${JACK_VERSION:=4.32.CANDIDATE}
Yohann Rousselb6b1f822015-07-01 16:54:15 +020026JACK_HOME="${JACK_HOME:=$HOME/.jack-server}"
Yohann Roussel90c63e62016-11-18 18:12:50 +010027JACK_CLIENT_SETTING="${JACK_CLIENT_SETTING:=$HOME/.jack-settings}"
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020028TMPDIR=${TMPDIR:=/tmp}
Yohann Rousseld92acea2016-12-14 19:36:34 +010029# This is half the timeout since the script will make a second attempt collecting debugs when
30# the first attempt fails on connection timeout
31JACK_CONNECTION_TIMEOUT=150
Yohann Roussel5b909202016-01-04 14:01:05 +010032JACK_EXTRA_CURL_OPTIONS=${JACK_EXTRA_CURL_OPTIONS:=}
Yohann Roussel2960f692016-09-20 17:33:21 +020033JACK_ASSERTION_ENABLED="${JACK_ASSERTION_ENABLED:=false}"
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020034
Yohann Roussel90c63e62016-11-18 18:12:50 +010035
36abort () { exit 255; }
37
38
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020039#
Yohann Rousselb6b1f822015-07-01 16:54:15 +020040# Load client settings
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020041#
Yohann Roussel90c63e62016-11-18 18:12:50 +010042if [ -f "$JACK_CLIENT_SETTING" ]; then
43 source "$JACK_CLIENT_SETTING"
44else
45 echo "Cannot find settings at '$JACK_CLIENT_SETTING'" >&2
46 abort
Yohann Rousselb6b1f822015-07-01 16:54:15 +020047fi
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020048
Yohann Roussele23f9922015-11-04 21:51:25 +010049
50JACK_SERVER=${JACK_SERVER:=true}
51JACK_MAIN_COMMAND=${JACK_MAIN_COMMAND:="java -Djava.io.tmpdir=$TMPDIR -Dfile.encoding=UTF-8 -XX:+TieredCompilation"}
52JACK_REPOSITORY=${JACK_REPOSITORY:=}
53
54
55#
56# If not in server mode, exec jack
57#
58if [ "$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
73fi
74
75
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020076#
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020077# Prepare compilation
78#
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020079JACK_PWD="$PWD"
Yohann Roussel90c63e62016-11-18 18:12:50 +010080JACK_EXIT="$TMPDIR/jack-task-$USER-$$-exit"
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020081
82# Cleanup
Yohann Roussel90c63e62016-11-18 18:12:50 +010083trap 'rm -f "$JACK_EXIT" 2>/dev/null;' EXIT
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020084
85set -o errexit
Yohann Roussel90c63e62016-11-18 18:12:50 +010086
87# Check that we can create temp file for exit status
88rm -rf "$JACK_EXIT"
89echo "" > "$JACK_EXIT"
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020090
mikaelpeltier4d1fcc42015-04-07 11:15:06 +020091#
92# Launch the compilation
93#
94
Yohann Roussel1b697cb2015-04-15 15:01:33 +020095set +o errexit
96trap ERR
97
Yohann Rousselb6b1f822015-07-01 16:54:15 +020098# put arguments in a non array variable
99ARGS=""
100for i in "$@"; do
101 ARGS="$ARGS $i"
102done
103
104
Yohann Rousselb6b1f822015-07-01 16:54:15 +0200105CURRENT_CHARSET=$(locale charmap)
Yohann Roussel5b909202016-01-04 14:01:05 +0100106if [ -z "$CURRENT_CHARSET" ]; then
107 CHARSET_ARGUMENT=
108else
109 CHARSET_ARGUMENT=";charset=$CURRENT_CHARSET"
110fi
111
Yohann Roussel2960f692016-09-20 17:33:21 +0200112# Check base64 availability
113BASE64_CHECK=$((echo amFjaw==;echo LXNlcnZlcg==) | base64 --decode 2>&1)
114
Yohann Rousseld92acea2016-12-14 19:36:34 +0100115sendRequest() {
116RETRY_OPTION=$1
117shift
118
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200119# Launch compilation
Yohann Roussel664ef352015-10-23 18:09:02 +0200120exec 3>&1
121exec 4>&2
Yohann Roussel2960f692016-09-20 17:33:21 +0200122if [ "$BASE64_CHECK" = jack-server ]; then
Yohann Rousseld92acea2016-12-14 19:36:34 +0100123 HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \
Yohann Roussel2960f692016-09-20 17:33:21 +0200124 --cert "${JACK_HOME}/client.pem" \
125 --cacert "${JACK_HOME}/server.pem" \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100126 --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 Roussel2960f692016-09-20 17:33:21 +0200127 --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \
128 -X POST \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100129 -H "Accept: application/vnd.jack.command-out-base64;version=1$CHARSET_ARGUMENT" \
130 -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel2960f692016-09-20 17:33:21 +0200131 -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \
132 -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100133 -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel2960f692016-09-20 17:33:21 +0200134 --noproxy ${SERVER_HOST} \
135 https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \
136 )
137else
Yohann Rousseld92acea2016-12-14 19:36:34 +0100138 HTTP_CODE=$(curl -f $JACK_EXTRA_CURL_OPTIONS $RETRY_OPTION \
Yohann Roussel2960f692016-09-20 17:33:21 +0200139 --cert "${JACK_HOME}/client.pem" \
140 --cacert "${JACK_HOME}/server.pem" \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100141 --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 Roussel2960f692016-09-20 17:33:21 +0200142 --no-buffer --write-out '%{http_code}' --silent --connect-timeout $JACK_CONNECTION_TIMEOUT \
143 -X POST \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100144 -H "Accept: application/vnd.jack.command-out;version=1$CHARSET_ARGUMENT" \
145 -F "cli=$ARGS;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel2960f692016-09-20 17:33:21 +0200146 -F "version=$JACK_VERSION;type=application/vnd.jack.select-exact;version=1" \
147 -F "pwd=$JACK_PWD;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel90c63e62016-11-18 18:12:50 +0100148 -F "assert=$JACK_ASSERTION_ENABLED;type=text/plain$CHARSET_ARGUMENT" \
Yohann Roussel2960f692016-09-20 17:33:21 +0200149 --noproxy ${SERVER_HOST} \
150 https://${SERVER_HOST}:$SERVER_PORT_SERVICE/jack \
151 )
152fi
Yohann Roussel664ef352015-10-23 18:09:02 +0200153CURL_CODE=$?
154exec 3>&-
155exec 4>&-
Yohann Roussel90c63e62016-11-18 18:12:50 +0100156
157set -o errexit
158
Yohann Roussel664ef352015-10-23 18:09:02 +0200159if [ $CURL_CODE -eq 0 ]; then
160 # No problem, let's go
Yohann Roussel90c63e62016-11-18 18:12:50 +0100161 JACK_CODE=$(cat "$JACK_EXIT")
Yohann Roussel664ef352015-10-23 18:09:02 +0200162 exit $JACK_CODE
163elif [ $CURL_CODE -eq 7 ]; then
164 # Failed to connect
165 echo "No Jack server running. Try 'jack-admin start-server'" >&2
166 abort
Yohann Rousseld92acea2016-12-14 19:36:34 +0100167elif [ $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 Roussel664ef352015-10-23 18:09:02 +0200175elif [ $CURL_CODE -eq 35 ]; then
Yohann Rousseld92acea2016-12-14 19:36:34 +0100176 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 Roussel664ef352015-10-23 18:09:02 +0200183elif [ $CURL_CODE -eq 58 ]; then
Yohann Roussele3f18c22016-02-24 16:16:42 +0100184 echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/client.pem. Try 'jack-diagnose'" >&2
Yohann Roussel664ef352015-10-23 18:09:02 +0200185 abort
186elif [ $CURL_CODE -eq 60 ]; then
Yohann Roussele3f18c22016-02-24 16:16:42 +0100187 echo "Failed to authenticate Jack server certificate. Try 'jack-diagnose'" >&2
Yohann Roussel664ef352015-10-23 18:09:02 +0200188 abort
189 elif [ $CURL_CODE -eq 77 ]; then
Yohann Roussele3f18c22016-02-24 16:16:42 +0100190 echo "Failed to contact Jack server: Problem reading ${JACK_HOME}/server.pem. Try 'jack-diagnose'" >&2
Yohann Roussel664ef352015-10-23 18:09:02 +0200191 abort
192elif [ $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 Rousselb6b1f822015-07-01 16:54:15 +0200197 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200198 else
Yohann Roussel664ef352015-10-23 18:09:02 +0200199 # Other
Yohann Roussele3f18c22016-02-24 16:16:42 +0100200 echo "Internal unknown error ($HTTP_CODE), try 'jack-diagnose' or see Jack server log" >&2
Yohann Roussel664ef352015-10-23 18:09:02 +0200201 abort
mikaelpeltier4d1fcc42015-04-07 11:15:06 +0200202 fi
Yohann Roussel664ef352015-10-23 18:09:02 +0200203else
Yohann Roussele3f18c22016-02-24 16:16:42 +0100204 echo "Communication error with Jack server ($CURL_CODE). Try 'jack-diagnose'" >&2
Yohann Roussel664ef352015-10-23 18:09:02 +0200205 abort
206fi
Yohann Rousseld92acea2016-12-14 19:36:34 +0100207}
208
Benoit Lamarchef6a22752016-12-16 11:07:40 +0100209sendRequest "" $@