简体   繁体   English

用于从shell管理Virtualbox的Vagrant Ruby代码

[英]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). 我在Debian shell中创建一个sh脚本,以自动化基于不同操作系统的Vagrantfile创建,例如管理存储控制器(用于Debian的Sata Controller或用于CentOS的IDE控制器)。 Trying to manage the disk size, i analyze the box default disk (vmdk), converting to vdi for Virtualbox native management. 尝试管理磁盘大小,我分析了盒子默认磁盘(vmdk),转换为vdi以进行Virtualbox本机管理。

The problem is in use Ruby code for analyze the VM data. 问题在于使用Ruby代码来分析VM数据。

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. 我在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. 问题是,我想分析机器磁盘,但是这个Ruby代码在机器创建之前执行,我需要一种方法在创建机器之后执行此操作,但是在shell中调用vagrant up句子。

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. 调用shell的ruby句子在创建机器之前正在执行,因此我需要在下载框并创建VM之后立即找到这些句子和vagrantfile磁盘自定义句子。

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. 然后从shell脚本i exec vagrant reload和我添加到Vagrantfile的提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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