Jenkins 如何自动启动 VirtualBox slave?

How can Jenkins autostart a VirtualBox slave?

我有一个 VirtualBox Jenkins slave。每次它出现故障时我都会手动启动它(例如在重新启动到 PC 后)。

有没有办法让 Jenkins 自动启动它?

简答:

是的,我们可以做到。

只需在您的 PC 中将 VitrualBox 添加为服务,并将 Jenkins Slave 作为服务添加到该 VitrualBox VM 中。

长答案:

实际上,我们在这里谈的是两件事:

  1. 如何在 VirtualBox 主机重启时auto-start VirtualBox VM。
  2. 如何auto-start jenkins slave 当这个节点客户端重启时。

或者,换句话说,我们在谈论一件事:

  1. 如何在计算机重新启动时 auto-start 一些东西。

但详细操作与您使用的OS有关。 (对于 windows,此工具可能会有所帮助:https://nssm.cc/

我有一台 Windows 主机,应该 运行 一个 Ubuntu 14.04 虚拟机詹金斯奴隶。

先决条件:您应该在 Jenkins 中定义一个 VM slave (http://YOUR-JENKINS-URL/computer/)

我是这样解决的:

1) 设置 Ubuntu VM 以在启动时自动启动一个服务,该服务 运行 是一个启动 Jenkins slave 的脚本:

  • 使用upstart到运行脚本:创建文件/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
    
  • /jenkins/run.sh:

    cd $(dirname [=11=])
    java -jar slave.jar -jnlpUrl         
    http://YOUR-JENKINS-URL/computer/NAME-OF-NODE/slave-agent.jnlp -secret 51d080f68b3d2552c977840aa8a01bb371a1b3e8b3326f36fadb497e597185ce
    
  • /Jenkins 文件夹应包含从 Jenkins 节点页面下载的 slave.jar 文件。

  • run.sh 文件中的最后两行应取自 Jenkins 节点页面。

2) 设置 Windows 以在启动时启动 VM:

  • 在 "Target:"

    中使用以下值创建一个 Windows 快捷方式
    "C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm "UbuntuVBox"
    
  • 其中 UbuntuVBox 应替换为您的 VM 名称

  • 将快捷方式放在 Windows 开始菜单的 "Startup" 文件夹下

总结:

完成上述步骤后 - 如果您重新启动 Windows 主机,它应该 运行 快捷方式,这将 运行 虚拟机。 VM 将 运行 服务 "Jenkins-slave" 并且你的 Jenkins slave 应该启动并且 运行ning.

有一个VirtualBox Plugin

  • 在需要时自动启动 VM

  • 不再需要时停止 VM

它需要 VirtualBox 网络服务 (vboxwebsrv) 来控制虚拟机(参见 chapter 9.12 of the VirtualBox documentation 获取它 运行)。