简体   繁体   中英

Vagrant Ruby Code for manage Virtualbox from shell

Im creating a sh script in Debian shell to automate Vagrantfile creation based in different OS, to manage the storagecontroller for example (Sata Controller for Debian or IDE controller for CentOS). Trying to manage the disk size, i analyze the box default disk (vmdk), converting to vdi for Virtualbox native management.

The problem is in use Ruby code for analyze the VM data.

storagecontroller = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollertype = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollertype0 | cut -d '=' -f 2 | sed 's/\"//g']
storagecontrollerportcount = %x[VBoxManage showvminfo $machinename'-'master --machinereadable | grep storagecontrollerportcount0 | cut -d '=' -f 2 | sed 's/\"//g']

This code works when the machine is already created and i use:

vagrant reload

but when i try to do

vagrant up

the problem is that machine doesnt exists in machines directory. I get an error in shell. Error:

Message: Errno::ENOENT: No such file or directory @ dir_chdir - /root/VirtualBox VMs/projectname/machinename-master

The thing is, i want to analize the machine disk, but this Ruby code exec before machine creation, i need a way to do this after the machine is created but when vagrant up sentence is called in shell.

This is a part of my generated code.

config.vm.define "machinename" do |machinename|
       machinename.vm.box ="debian/contrib-jessie64"
       machinename.vm.hostname = "machinename"
       machinename.vm.provider "virtualbox" do |virtualbox|
       virtualbox.memory = "2048"
       virtualbox.cpus = "8"
       virtualbox.name = "machinename-master"
       virtualbox.customize [
         "modifyvm", :id, "--groups", "/projectname"
       ]
       storagecontroller = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
       path = File.join [ENV["HOME"], 'VirtualBox VMs/','projectname','machinename-master']
       Dir.chdir("#{path}")
       storagecontroller = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollername0 | cut -d '=' -f 2 | sed 's/\"//g']
       storagecontroller.delete!("\n")
       storagecontrollertype = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollertype0 | cut -d '=' -f 2 | sed 's/\"//g']
       storagecontrollertype.delete!("\n")
       storagecontrollerportcount = %x[VBoxManage showvminfo machinename'-'master --machinereadable | grep storagecontrollerportcount0 | cut -d '=' -f 2 | sed 's/\"//g']
       storagecontrollerportcount.delete!("\n")
       require "find"
       file=Dir['*.vmdk'].first
       if !File.exist?('/root/VirtualBox VMs/projectname/machinename-master/machinename-box.vdi')
                            virtualbox.customize [
                                    "storagectl", :id,
                                    "--name", "#{storagecontroller}",
                                    "--controller", "#{storagecontrollertype}",
                                    "--portcount", "#{storagecontrollerportcount}",
                                    "--hostiocache", "on"
                            ]
                            virtualbox.customize [
                                    "clonehd", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/box-disk1.vmdk",
                                        "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/ojocuidao-box.vdi",
                                    "--format", "VDI"
                            ]
                            virtualbox.customize [
                                    "modifyhd", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/machinename-box.vdi",
                                    "--resize", 55 * 1024
                            ]
                            virtualbox.customize [
                                    "storageattach", :id,
                                    "--storagectl", "#{storagecontroller}",
                                    "--port", "0",
                                    "--device", "0",
                                    "--type", "hdd",
                                    "--nonrotational", "on",
                                    "--medium", "#{ENV["HOME"]}/VirtualBox VMs/projectname/#{virtualbox.name}/machinename-box.vdi"
                            ]

The ruby sentences for calling shell are executing before the machine is created, so i need to locate this sentences and vagrantfile disk customize sentences right after the box is downloaded and the VM is created.

Someone can help me?

I have fix this issue reading to vagrant function calling sentence argument.

Writing in ruby:

if !(ARGV[0] == "up")

then from shell script i exec vagrant reload and the provision i added to Vagrantfile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM