Skip to content

Instantly share code, notes, and snippets.

@hokkey
Last active March 2, 2016 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hokkey/f29492503b485ea8d414 to your computer and use it in GitHub Desktop.
Save hokkey/f29492503b485ea8d414 to your computer and use it in GitHub Desktop.
Customized Vagrantfile
# Size of the CoreOS cluster created by Vagrant
$num_instances=1
# Used to fetch a new discovery token for a cluster of size $num_instances
$new_discovery_url="https://discovery.etcd.io/new?size=#{$num_instances}"
# Change basename of the VM
# The default value is "core", which results in VMs named starting with
# "core-01" through to "core-${num_instances}".
$instance_name_prefix='core'
# Change the version of CoreOS to be installed
# To deploy a specific version, simply set $image_version accordingly.
# For example, to deploy version 709.0.0, set $image_version="709.0.0".
# The default value is "current", which points to the current version
# of the selected channel
$image_version = 'current'
# Official CoreOS channel from which updates should be downloaded
$update_channel='stable'
# Log the serial consoles of CoreOS VMs to log/
# Enable by setting value to true, disable with false
# WARNING: Serial logging is known to result in extremely high CPU usage with
# VirtualBox, so should only be used in debugging situations
#$enable_serial_logging=false
# Enable port forwarding of Docker TCP socket
# Set to the TCP port you want exposed on the *host* machine, default is 2375
# If 2375 is used, Vagrant will auto-increment (e.g. in the case of $num_instances > 1)
# You can then use the docker tool locally by setting the following env var:
# export DOCKER_HOST='tcp://127.0.0.1:2375'
#$expose_docker_tcp=2375
# Enable NFS sharing of your home directory ($HOME) to CoreOS
# It will be mounted at the same path in the VM as on the host.
# Example: /Users/foobar -> /Users/foobar
#$share_home=false
# Customize VMs
$vm_gui = false
$vm_memory = 3072
$vm_cpus = 2
# Share additional folders to the CoreOS VMs
# For example,
# $shared_folders = {'/path/on/host' => '/path/on/guest', '/home/foo/app' => '/app'}
# or, to map host folders to guest folders of the same name,
# $shared_folders = Hash[*['/home/foo/app1', '/home/foo/app2'].map{|d| [d, d]}.flatten]
$shared_folders = {'.' => '/home/core/dockerfiles'}
# Enable port forwarding from guest(s) to host machine, syntax is: { 80 => 8080 }, auto correction is enabled by default.
#$forwarded_ports = {}
# 外部ストレージ
$attached_storages = [
{:file => 'internal_data.vdi', :size => 100*1024, :port => 1, :device => 0, :dev => 'sdb', :dest => '/mnt/data'},
{:file => 'internal_image.vdi', :size => 100*1024, :port => 1, :device => 1, :dev => 'sdc', :dest => '/var/lib/docker'}
]
# ストレージをフォーマット
def format_storage(dev)
fdisk = <<-EOF
(echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk /dev/#{dev}
mkfs -t ext4 /dev/#{dev}1
EOF
return fdisk
end
# 内部ネットワークipのベース値
$ip_base = 50
# 公開ネットワーク設定
$bridge = 'en0: Ethernet'
$mac_address = '080027408ff0'
# ストレージをフォーマットしてマウント
def format_storage(dev, dest)
fdisk = <<-EOF
(echo n; echo p; echo 1; echo ; echo ; echo w) | fdisk /dev/#{dev}
mkfs -t ext4 /dev/#{dev}1
mount -t ext4 /dev/#{dev}1 #{dest}
EOF
return fdisk
end
# docker_composeを起動する
$docker_compose_up = <<-EOF
cd /home/core/dockerfiles
docker-compose up -d
EOF
# -*- mode: ruby -*-
# # vi: set ft=ruby :
require 'fileutils'
Vagrant.require_version ">= 1.6.0"
CLOUD_CONFIG_PATH = File.join(File.dirname(__FILE__), "user-data")
CONFIG = File.join(File.dirname(__FILE__), "config.rb")
# Defaults for config options defined in CONFIG
$num_instances = 1
$instance_name_prefix = "core"
$update_channel = "stable"
$enable_serial_logging = false
$share_home = false
$vm_gui = false
$vm_memory = 1024
$vm_cpus = 1
$shared_folders = {}
$forwarded_ports = {}
# Attempt to apply the deprecated environment variable NUM_INSTANCES to
# $num_instances while allowing config.rb to override it
if ENV["NUM_INSTANCES"].to_i > 0 && ENV["NUM_INSTANCES"]
$num_instances = ENV["NUM_INSTANCES"].to_i
end
if File.exist?(CONFIG)
require CONFIG
end
# Use old vb_xxx config variables when set
def vm_gui
$vb_gui.nil? ? $vm_gui : $vb_gui
end
def vm_memory
$vb_memory.nil? ? $vm_memory : $vb_memory
end
def vm_cpus
$vb_cpus.nil? ? $vm_cpus : $vb_cpus
end
Vagrant.configure("2") do |config|
# always use Vagrants insecure key
config.ssh.insert_key = false
config.vm.box = "coreos-%s" % $update_channel
if $image_version != "current"
config.vm.box_version = $image_version
end
config.vm.box_url = "http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant.json" % [$update_channel, $image_version]
["vmware_fusion", "vmware_workstation"].each do |vmware|
config.vm.provider vmware do |v, override|
override.vm.box_url = "http://%s.release.core-os.net/amd64-usr/%s/coreos_production_vagrant_vmware_fusion.json" % [$update_channel, $image_version]
end
end
config.vm.provider :virtualbox do |v|
# On VirtualBox, we don't have guest additions or a functional vboxsf
# in CoreOS, so tell Vagrant that so it can be smarter.
v.check_guest_additions = false
v.functional_vboxsf = false
end
# plugin conflict
if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = false
end
(1..$num_instances).each do |i|
config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config|
config.vm.hostname = vm_name
if $enable_serial_logging
logdir = File.join(File.dirname(__FILE__), "log")
FileUtils.mkdir_p(logdir)
serialFile = File.join(logdir, "%s-serial.txt" % vm_name)
FileUtils.touch(serialFile)
["vmware_fusion", "vmware_workstation"].each do |vmware|
config.vm.provider vmware do |v, override|
v.vmx["serial0.present"] = "TRUE"
v.vmx["serial0.fileType"] = "file"
v.vmx["serial0.fileName"] = serialFile
v.vmx["serial0.tryNoRxLoss"] = "FALSE"
end
end
config.vm.provider :virtualbox do |vb, override|
vb.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
vb.customize ["modifyvm", :id, "--uartmode1", serialFile]
end
end
if $expose_docker_tcp
config.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
end
$forwarded_ports.each do |guest, host|
config.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
end
["vmware_fusion", "vmware_workstation"].each do |vmware|
config.vm.provider vmware do |v|
v.gui = vm_gui
v.vmx['memsize'] = vm_memory
v.vmx['numvcpus'] = vm_cpus
end
end
config.vm.provider :virtualbox do |vb|
vb.gui = vm_gui
vb.memory = vm_memory
vb.cpus = vm_cpus
end
# IPの重複を避ける
ip = "172.17.8.#{i+$ip_base}"
config.vm.network :private_network, ip: ip
# Uncomment below to enable NFS for sharing the host machine into the coreos-vagrant VM.
#config.vm.synced_folder ".", "/home/core/share", id: "core", :nfs => true, :mount_options => ['nolock,vers=3,udp']
$shared_folders.each_with_index do |(host_folder, guest_folder), index|
config.vm.synced_folder host_folder.to_s, guest_folder.to_s, id: "core-share%02d" % index, nfs: true, mount_options: ['nolock,vers=3,udp']
end
if $share_home
config.vm.synced_folder ENV['HOME'], ENV['HOME'], id: "home", :nfs => true, :mount_options => ['nolock,vers=3,udp']
end
if File.exist?(CLOUD_CONFIG_PATH)
config.vm.provision :file, :source => "#{CLOUD_CONFIG_PATH}", :destination => "/tmp/vagrantfile-user-data"
config.vm.provision :shell, :inline => "mv /tmp/vagrantfile-user-data /var/lib/coreos-vagrant/", :privileged => true
end
# ここから追加設定
# ネットワーク設定
#config.vm.network :public_network, bridge: $bridge, mac: $mac_address
# 外部ストレージの作成と追加
if not File.exist?($attached_storages[0][:file]) then
init_storage = format_storage($attached_storages[0][:dev])
config.vm.provision :shell, :inline => init_storage, :privileged => true
end
if not File.exist?($attached_storages[1][:file]) then
init_storage = format_storage($attached_storages[1][:dev])
config.vm.provision :shell, :inline => init_storage, :privileged => true
end
config.vm.provider :virtualbox do |vb|
for data in $attached_storages
# 新規ストレージ作成
if not File.exist?(data[:file]) then
vb.customize ['createhd', '--filename', data[:file], '--size', data[:size]]
end
# ストレージをアタッチ
vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', data[:port], '--device', data[:device], '--type', 'hdd', '--medium', data[:file], '--setuuid', '']
end
end
# docker-composeをインストール
config.vm.provision :shell, :inline => $install_docker_compose, :privileged => true
# docker-composeを実行(常に)
config.vm.provision :shell, :inline => $docker_compose_up, :run => 'always'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment