Skip to content

Instantly share code, notes, and snippets.

@yan-foto
Created September 19, 2015 13:17
Show Gist options
  • Save yan-foto/9a1b7b967832ad938f89 to your computer and use it in GitHub Desktop.
Save yan-foto/9a1b7b967832ad938f89 to your computer and use it in GitHub Desktop.
Vagrant shell provision to solve `No X-Jenkins-CLI2-Port` (Ubuntu guest)
#!/bin/bash
# This is a simple example of a shell script which is used as a Vagrant provision
# It can be used as: 'config.vm.provision "shell", path: "./jenkins.sh"'
# inside a Vagrantfile.
# The logic is simple and the implementation as well. Any suggestions/improvements
# are highly welcome.
# Default values
jenkins_home="/var/lib/jenkins"
jenkins_url="http://localhost:8080"
echo "Adding jenkins key..."
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
echo "Adding jenkins repository..."
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
echo "Installing jenkins..."
sudo apt-get update
sudo apt-get install -y jenkins
echo "Starting Jenkins..."
sudo service jenkins start
# Make sure CLI jar is created
echo "Waiting 5 seconds to have jenkins CLI initiated..."
sleep 5
jenkins_cli=$(sudo find / -type f -name 'jenkins-cli.jar' -print0)
echo "Jenkins CLI found at $jenkins_cli"
# Checking if server is up and running
sleep_interval=1
while true; do
st=$(curl -sL $jenkins_url/cli -w "%{http_code}" -o /dev/null)
if [ "$st" != "200" ]; then
echo "Jenkins server CLI not responding (status: '$st'). Sleeping $sleep_interval seconds..."
sleep $sleep_interval
((sleep_interval++))
else
break
fi
done
# Update jenkin's update center :/
echo "Update center gettign up-to-date!"
sudo mkdir -p $jenkins_home/updates
wget http://updates.jenkins-ci.org/update-center.json -qO- \
| sed '1d;$d' > $jenkins_home/updates/default.json
# An example of using CLI
echo "Installing git plugin..."
sudo java -jar "$jenkins_cli" -s "$jenkins_url" install-plugin git
@yan-foto
Copy link
Author

The loop starting on line 33 solves the following problem:

java.io.IOException: No X-Jenkins-CLI2-Port among [X-Jenkins, null, X-Hudson, X-Hudson-Theme, Content-Length, Expires, X-Jenkins-Session, Set-Cookie, Content-Type, Server, Cache-Control, X-Content-Type-Options]
    at hudson.cli.CLI.getCliTcpPort(CLI.java:290)
    at hudson.cli.CLI.<init>(CLI.java:128)
    at hudson.cli.CLIConnectionFactory.connect(CLIConnectionFactory.java:72)
    at hudson.cli.CLI._main(CLI.java:479)
    at hudson.cli.CLI.main(CLI.java:390)
    Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL: http://localhost:8080/cli
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
        at hudson.cli.FullDuplexHttpStream.<init>(FullDuplexHttpStream.java:78)
        at hudson.cli.CLI.connectViaHttp(CLI.java:158)
        at hudson.cli.CLI.<init>(CLI.java:132)
        ... 3 more

In an incremental probe-wait loop it waits until the CLI is up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment