2

I have a VirtualBox Jenkins slave. I start it manually every time it goes down (e.g. after doing reboot to the PC).

Is there a way to make Jenkins start it automatically?

DavidS
  • 1,647
  • 1
  • 15
  • 21

3 Answers3

3

I have a Windows host that should run an Ubuntu 14.04 virtual machine Jenkins slave.

Pre-requirements: you should have a VM slave defined in Jenkins (http://YOUR-JENKINS-URL/computer/)

This is how I solved it:

1) Setup Ubuntu VM to automatically start a service on boot that runs a script to start Jenkins slave:

  • Use upstart to run the script: create a the file /etc/init/jenkins-slave.conf

    description "A job to start a Jenkins slave"
    author "Your Name"
    start on runlevel [2345]
    exec echo Starting Jenkins slave at `date` >> /var/log/jenkins-slave-job.log
    exec /jenkins/run.sh
    
  • The /jenkins/run.sh:

    cd $(dirname $0)
    java -jar slave.jar -jnlpUrl         
    http://YOUR-JENKINS-URL/computer/NAME-OF-NODE/slave-agent.jnlp -secret 51d080f68b3d2552c977840aa8a01bb371a1b3e8b3326f36fadb497e597185ce
    
  • The /Jenkins folder should contain slave.jar file downloaded from Jenkins node page.

  • The 2 last lines in the run.sh file should be taken from the Jenkins node page.

2) Setup Windows to start the VM on startup:

  • Create a Windows shourtcut with the following value in the "Target:"

    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "UbuntuVBox"
    
  • Where UbuntuVBox should be replaced with the name of your VM

  • Put the shortcut under "Startup" folder in the Windows start menu

To conclude:

After doing the above steps - if you reboot the Windows host, it should run the shortcut, that will run the VM. The VM will run the service "Jenkins-slave" and your Jenkins slave should be up and running.

DavidS
  • 1,647
  • 1
  • 15
  • 21
0

Short answer:

Yes, we can do it.

Just add VitrualBox as a service in your PC, and add Jenkins Slave as an service in that VitrualBox VM.

Long answer:

In fact, we are talking about two things here:

  1. how to auto-start a VirtualBox VM when the host of VirtualBox is restarted.
  2. how to auto-start a jenkins slave when this node client is restarted.

Or , in another word , we are talking one thing:

  1. how to auto-start some stuff when computer restarted.

But the detail operations related to the OS you are using. (For windows, this tool may help: https://nssm.cc/)

user4395104
  • 161
  • 1
  • 4
0

There is a VirtualBox Plugin that:

  • starts VMs automatically when needed

  • stops VMs when no longer needed

It requires VirtualBox web service (vboxwebsrv) to control VMs (see e.g. chapter 9.12 of the VirtualBox documentation to get it running).

jcbsv
  • 396
  • 2
  • 13
vlp
  • 6,063
  • 2
  • 18
  • 44
  • Is there a newer plugin that supports latest VirtualBox ? – Arcin B Nov 19 '19 at 15:29
  • 1
    @ArcinB Plugin seems to have been updated with VirtualBox 6 support. See the latest commits [here](https://github.com/jenkinsci/virtualbox-plugin). Some of its [forks](https://github.com/jenkinsci/virtualbox-plugin/network) also mention VirtualBox 6 support. I no longer use this plugin so can't help any more... – vlp Nov 19 '19 at 21:01