首页 > 代码库 > Vagrantfile配置

Vagrantfile配置

Vagrantfile文件只会在第一次执行vagrant up时调用执行,其后如果不明确使用vagrant reload,则不会被强制重新加载。

# This is the mount point for the sync_folders of the sourceSRCMOUNT = "/hyperledger"LOCALDEV = "/local-dev"

The script is the scripts to be run

$script = <<SCRIPTset -xexport DOCKER_STORAGE_BACKEND="#{ENV[‘DOCKER_STORAGE_BACKEND‘]}"cd #{SRCMOUNT}/devenv./setup.shSCRIPT

 

baseimage_release = File.read ../.baseimage-release

 

Vagrant.require_version ">= 1.7.4"

box设置:配置虚拟机主机名

config.vm.box = "hyperledger/fabric-baseimage"

 auto download box from

config.vm.box_version = ENV[USE_LOCAL_BASEIMAGE] ? "0": baseimage_release # Vagrant does not support versioning local images, the local version is always implicitly version 0

端口转发,设置主机与虚拟机之间的端口的映射关系.要转发到虚拟机(guest)上的端口是 7050,转发的是主机(host)上的7050 端口。也就是你在访问主机上的7050端口的时候,实际上访问的是虚拟机上的 7050端口

config.vm.network :forwarded_port, guest: 7050, host: 7050 # Openchain REST servicesconfig.vm.network :forwarded_port, guest: 7051, host: 7051 # Openchain gRPC servicesconfig.vm.network :forwarded_port, guest: 7054, host: 7054 # Membership serviceconfig.vm.network :forwarded_port, guest: 7053, host: 7053 # GRPCCient gRPC services

 

同步目录:synced_folder方法的第一个参数为主机上要跟虚拟机同步的目录,第二个参数为要挂载到虚拟机上的路径。

config.vm.synced_folder "..", "#{SRCMOUNT}"  #Copy /opt/gopath/src/github.com/hyperledger/fabric folder to /hyperledgerconfig.vm.synced_folder "..", "/opt/gopath/src/github.com/hyperledger/fabric" #Copy /opt/gopath/src/github.com/hyperledger/fabric folder to /opt/gopath/src/github.com/hyperledger/fabricconfig.vm.synced_folder ENV.fetch(LOCALDEVDIR, ".."), "#{LOCALDEV}" #Copy /opt/gopath/src/github.com/hyperledger/fabric folder to /opt/gopath/src/github.com/hyperledger/fabric#/vagrant => /opt/gopath/src/github.com/hyperledger/fabric/devenv 默认配置下,虚拟机中的/vagrant目录与主机上的项目目录是同一个目录,该目录中的所有操作都会自动同步。
#注意的是,在该虚拟机上进行rm -fr /操作的时候请谨慎一些,因为在该虚拟机中,挂载了/vagrant目录,该目录是与你主机的项目共享的,删除的话会将项目删除掉。

 

内存和cpu核心

config.vm.provider :virtualbox do |vb|
  #Customize the amount of memory on the VM: vb.name = "hyperledger" vb.customize [modifyvm, :id, --memory, 4096] vb.cpus = 2 storage_backend = ENV[DOCKER_STORAGE_BACKEND] case storage_backend when nil,"","aufs","AUFS" # No extra work to be done when "btrfs","BTRFS" # Add a second disk for the btrfs volume IO.popen("VBoxManage list systemproperties") { |f| success = false while line = f.gets do # Find the directory where the machine images are stored machine_folder = line.sub(/^Default machine folder:\s*/,"") if line != machine_folder btrfs_disk = File.join(machine_folder, vb.name, btrfs.vdi) unless File.exist?(btrfs_disk) # Create the disk if it doesn‘t already exist vb.customize [createhd, --filename, btrfs_disk, --format, VDI, --size, 20 * 1024] end # Add the disk to the VM vb.customize [storageattach, :id, --storagectl, SATA Controller, --port, 1, --device, 0, --type, hdd, --medium, btrfs_disk] success = true break end end raise Vagrant::Errors::VagrantError.new, "Could not provision btrfs disk" if !success } else raise Vagrant::Errors::VagrantError.new, "Unknown storage backend type: #{storage_backend}" end

 Vagrant使用提供者(provider)来启动隔离的虚拟环境。默认的提供者是Virtualbox

The Vagrant Shell provisioner allows you to upload and execute a script within the guest machine.Reference to Shell Provisioner

config.vm.provision :shell, inline: $script #run scripts config.vm.provision :shell, path =>"boot.sh" #run scripts in boot.sh file using ssh provider method like running command ‘vagrant ssh‘

 

vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)

vagrant package --output NAME --vagrantfile FILE(当在启动Vagrant后,对于虚拟机有进行过安装环境相关的配置,如果并不希望写在Vagrant的启动shell里面每次都重新安装配置一遍,可以将当前配置好的虚拟机打包成box.(可选)设置通过NAME来指定输出的文件名/(可选)可以将Vagrantfile直接封进box中注:如果网络模式中使用 private_network 的话,在打包之前需要清除一下private_network的设置,避免不必要的错误:sudo rm -f /etc/udev/rule.d/70-persistent-net.rules

 

参考网站:

Vagrant简介和安装配置

使用vagrant部署开发环境

Vagrantfile配置