Restore snapshot before build in jenkins using vir

2019-08-04 13:07发布

I'm using VirtualBox plugin on jenkins, to launch different builds retrieving sources from Git. I would like the environment on each VM to be clean by restoring a snapshot before starting them for a build.

I tried several things as https://wiki.jenkins-ci.org/display/JENKINS/Slave+Setup+Plugin. Finally I don't think it it's a good solution as it is called after the git repository being clone... But I also tried to launch the slave "via the execution of a command on master", that takes as parameter the name of the vm and calls this script:

VBoxManage registervm "C:\Users\mfauvet\VirtualBox VMs\%1\%1.vbox
VBoxManage controlvm %1 poweroff
VBoxManage snapshot %1 restore SetEnvironment
VBoxManage startvm %1
ssh 192.168.120.1 -p 1310

But I have the following error:

C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" registervm "C:\Users\mfauvet\VirtualBox VMs\Jenkins-Ubuntu13.10\Jenkins-Ubuntu13.10.vbox" 
VBoxManage.exe: error: Trying to open a VM config 'C:\Users\mfauvet\VirtualBox VMs\Jenkins-Ubuntu13.10\Jenkins-Ubuntu13.10.vbox' which has the same UUID as an existing virtual machine
VBoxManage.exe: error: Details: code E_FAIL (0x80004005), component Machine, interface IMachine, callee IUnknown
VBoxManage.exe: error: Context: "OpenMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 88 of file VBoxManageMisc.cpp

C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm Jenkins-Ubuntu13.10 poweroff 
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" snapshot Jenkins-Ubuntu13.10 restore SetEnvironment 
0%...10%...20%...30%...40%...50%...
Progress object failure: RPC_S_SERVER_UNAVAILABLE 0x800706BA
Restoring snapshot 7f7cc1a3-7128-426a-bf74-9ffb0b3fbca8

C:\Program Files (x86)\Jenkins>"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" startvm Jenkins-Ubuntu13.10 
VBoxManage.exe: error: Could not find a registered machine named 'Jenkins-Ubuntu13.10'
VBoxManage.exe: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee IUnknown
VBoxManage.exe: error: Context: "FindMachine(Bstr(pszVM).raw(), machine.asOutParam())" at line 575 of file VBoxManageMisc.cpp

Does someone know how I could solve this ? Or is there a better way to restore a snapshot on my vm before launching them via Jenkins ? Thanks for reading

1条回答
Explosion°爆炸
2楼-- · 2019-08-04 13:55

I got this working, admittedly with VMWare.

Steps I took

  1. add swarm plugin to master
  2. Setup virtual machine and add swarm client
  3. make sure client connects - will be in nodes in master
  4. take snapshot with the slave client running (or initiate it on startup)
  5. create a job on master which runs on your swarm slave
  6. add multijob plugin to master create multijob project with normal steps to revert the VM
  7. add a multijob phase
  8. add the swarm slave job into the multijob phase

When running the job, the swarm slave job will wait for the VM to start before submitting jobs to it

查看更多
登录 后发表回答