How can I add Jenkins slave nodes via the CLI? - jenkins

As per the title, in Jenkins how can I add new slave nodes to my build cluster using the CLI, or if there is not a CLI option, is there another scriptable approche that can be used?

Basic CLI instruction can be found here.
The following CLI command should get the new node configuration XML as stdin:
java -jar jenkins-cli.jar -s [JENKINS_URL] create-node [NewNodeName]
For example, if you want to copy an existing node, you can use:
java -jar jenkins-cli.jar -s [JENKINS_URL] get-node [NodeToCopyFrom] | java -jar jenkins-cli.jar -s [JENKINS_URL] create-node [NewNodeName]

Many people use the Swarm Plugin to eliminate the need to actually add slaves manually. You would need to script the install of the swarm agent of course, but that should be pretty straight forward.

Related

Create Jenkins slave from command line

I have a script that create a VM then I need to add the new VM as a slave to existing master,
How can I do that from command line in windows and from the new VM ?
Create a VM (OS = WINDOWS)
Create a Node on jenkins server from command line using jenkins cli:
config.xml | ssh -l fmud -p %JNLP_PORT% %jenkins_IPADDRESS% create-node %TARGET_WIN_NODE_NAME%
Where Config.xml could be gathered from an existing slave with command:
ssh -l fmud -p %JNLP_PORT% %jenkins_IPADDRESS% get-node %TARGET_WIN_NODE_NAME%
Note: JNLP is needed for jenkins-CLI and could be configured in jenkins administrative page.
Also, you will have to change the config file with label and name etc.
Connect the windows slave as service: Download winsw.exe
Rename it to jenkins_slave.exe
Download slave.jar (http://yourserver:port/jnlpJars/agent.jar )
Download and configure jenkins-slave.xml
Download and configure jenkins-slave.exe.config
in cmd: jenkins_slave.exe install
jenkins_slave.exe start
If it is a java slave, that is simple to automate. Like it if you find it helping or ask more specific question otherwise.

Where is jenkins.xml in jenkins docker container

We are using jenkins inside docker (version: FROM jenkins:1.651.1).
We have problems with the html publisher plugin which isn't executing javascript by default. The same issue as described here.
We want to change our jenkins.xml (just like they said) to change it's configuration. The problem is the fact we can't find it inside our container. Does it has another name inside the docker container or where can we find it? Thanks
The jenkins docker image does not contain a jenkins.xml. This looks to be a windows specific file.
$ docker run --rm jenkins find / -name 'jenkins.xml' 2>/dev/null
# returns nothing
/usr/local/bin/jenkins.sh is run when a jenkins container is started. This contains the line:
eval "exec java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS \"\$#\""
Therefore try setting the required setting in JENKINS_OPTS:
docker run -e JENKINS_OPTS="hudson.model.DirectoryBrowserSupport.CSP=" jenkins

How to have all Jenkins slave tasks executed with nice?

We have a number of Jenkins jobs which may get executed over Jenkins slaves. Is it possible to globally set the nice level of Jenkins tasks to make sure that all Jenkins tasks get executed with a higher nice level?
Yes, that's possible. The "trick" is to start the slave agent with the proper nice level already; all Jenkins processes running on that slave will inherit that.
Jenkins starts the slave agent via ssh, effectively running a command like
cd /path/to/slave/root/dir && java -jar slave.jar
On the Jenkins node config page, you can define a "Prefix Start Slave Command" and a "Suffix Start Slave Command" to have this nice-d. Set as follows:
Prefix Start Slave Command: nice -n -10 sh -c '
Suffix Start Slave Command: '
With that, the slave startup command becomes
nice -n -10 sh -c 'cd "/path/to/slave/root/dir" && java -jar slave.jar'
This assumes that your login shell is a bourne shell. For csh, you will need a different syntax. Also note that this may fail if your slave root path contains blanks.
I usually prefer to "Launch slave via execution of command on the Master", and invoke ssh myself from within a shell wrapper. Then you can select cipher and client of choice, and also setting niceness can be done without Prefix/Suffix kludges and without whitespace pitfalls.

jstack and other tools on google cloud dataflow VMs

Is there a way to run jstack on the VMs created for Dataflow jobs?
I'm trying to see where the job spends most of the CPU time and I can't find it installed.
Thanks,
G
A workaround which I found to work:
Log on to the machine
Find the docker container that runs "python -m taskrunne" using sudo docker ps
Connect to the container using sudo docker exec -i -t 9da88780f555 bash (replacing the container id with the one found in step 2)
Install openjdk-7-jdk using apt-get install openjdk-7-jdk
Find the process id of the java executable
Run /usr/bin/jstack 1437
This Github issue update includes some basic instructions for getting profiles using the --enableProfilingAgent option.
This doesn't answer the "and other tools" part of your question, but:
Dataflow workers run a local http server that you can use to get some info. Instead of using jstack you can get a thread dump with this:
curl http://localhost:8081/threadz
I'm not familiar with jstack but based on a quick Google search it looks like jstack is a tool that runs independently from the JVM and just takes a PID. So you can do the following while your job is running.
ssh into one of the VMs using gcutil ssh
Install jstack on the VM.
Run ps -aux | grep java to identify the PID of the java process.
Run jstack using the PID you identified.
Would that work for you? Are you trying to run jstack from within your code so as to profile it automatically?

Starting Cassandra from Jenkins

I'm trying to start a Cassandra instance (0.8.10) from Jenkins (latest version, 1.463).
Inside a "free-style project" job, I have a "Execute shell" build step, where I have tried a couple of approaches:
.../tools/apache-cassandra-0.8.10/bin/cassandra -f
and
.../tools/apache-cassandra-0.8.10/bin/cassandra
The first approach starts Cassandra ok, but Jenkins doesn't exit the build and keeps on building. If I stop the build, the Cassandra process dies as well.
The second approach fails because the Cassandra project dies as soon as the build finishes.
I have also tried:
.../tools/apache-cassandra-0.8.10/bin/cassandra -f &
that is kind of lame, and doesn't work anyway.
Any ideas on how to start Cassandra from Jenkins?
Try using nohup with &. Also pipe stdout and stderr to a file or /dev/null:
nohup .../tools/apache-cassandra-0.8.10/bin/cassandra -f > /dev/null 2>/dev/null &

Resources