cleaned up vagrantfile (#3717)

This commit is contained in:
Johnny Halfmoon 2018-11-16 15:28:29 +01:00 committed by Antoine Legrand
parent 53bde23a5e
commit 9d0786cbb0

150
Vagrantfile vendored
View file

@ -1,6 +1,8 @@
# -*- mode: ruby -*- # -*- mode: ruby -*-
# # vi: set ft=ruby : # # vi: set ft=ruby :
# For help on using kubespray with vagrant, check out docs/vagrant.md
require 'fileutils' require 'fileutils'
Vagrant.require_version ">= 2.0.0" Vagrant.require_version ">= 2.0.0"
@ -13,15 +15,16 @@ COREOS_URL_TEMPLATE = "https://storage.googleapis.com/%s.release.core-os.net/amd
DISK_UUID = Time.now.utc.to_i DISK_UUID = Time.now.utc.to_i
SUPPORTED_OS = { SUPPORTED_OS = {
"coreos-stable" => {box: "coreos-stable", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]}, "coreos-stable" => {box: "coreos-stable", user: "core", box_url: COREOS_URL_TEMPLATE % ["stable"]},
"coreos-alpha" => {box: "coreos-alpha", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]}, "coreos-alpha" => {box: "coreos-alpha", user: "core", box_url: COREOS_URL_TEMPLATE % ["alpha"]},
"coreos-beta" => {box: "coreos-beta", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]}, "coreos-beta" => {box: "coreos-beta", user: "core", box_url: COREOS_URL_TEMPLATE % ["beta"]},
"ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"}, "ubuntu1604" => {box: "generic/ubuntu1604", user: "vagrant"},
"ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"}, "ubuntu1804" => {box: "generic/ubuntu1804", user: "vagrant"},
"centos" => {box: "centos/7", user: "vagrant"}, "centos" => {box: "centos/7", user: "vagrant"},
"fedora" => {box: "fedora/28-cloud-base", user: "vagrant"}, "centos-bento" => {box: "bento/centos-7.5", user: "vagrant"},
"opensuse" => {box: "opensuse/openSUSE-42.3-x86_64", use: "vagrant"}, "fedora" => {box: "fedora/28-cloud-base", user: "vagrant"},
"opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", use: "vagrant"}, "opensuse" => {box: "opensuse/openSUSE-42.3-x86_64", user: "vagrant"},
"opensuse-tumbleweed" => {box: "opensuse/openSUSE-Tumbleweed-x86_64", user: "vagrant"},
} }
# Defaults for config options defined in CONFIG # Defaults for config options defined in CONFIG
@ -50,8 +53,6 @@ $kube_node_instances_with_disks_number = 2
$playbook = "cluster.yml" $playbook = "cluster.yml"
$local_release_dir = "/vagrant/temp"
host_vars = {} host_vars = {}
if File.exist?(CONFIG) if File.exist?(CONFIG)
@ -60,13 +61,13 @@ end
$box = SUPPORTED_OS[$os][:box] $box = SUPPORTED_OS[$os][:box]
# if $inventory is not set, try to use example # if $inventory is not set, try to use example
$inventory = File.join(File.dirname(__FILE__), "inventory", "sample") if ! $inventory $inventory = "inventory/sample" if ! $inventory
$inventory = File.absolute_path($inventory, File.dirname(__FILE__))
# if $inventory has a hosts file use it, otherwise copy over vars etc # if $inventory has a hosts.ini file use it, otherwise copy over
# to where vagrant expects dynamic inventory to be. # vars etc to where vagrant expects dynamic inventory to be
if ! File.exist?(File.join(File.dirname($inventory), "hosts")) if ! File.exist?(File.join(File.dirname($inventory), "hosts.ini"))
$vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", $vagrant_ansible = File.join(File.dirname(__FILE__), ".vagrant", "provisioners", "ansible")
"provisioners", "ansible")
FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible) FileUtils.mkdir_p($vagrant_ansible) if ! File.exist?($vagrant_ansible)
if ! File.exist?(File.join($vagrant_ansible,"inventory")) if ! File.exist?(File.join($vagrant_ansible,"inventory"))
FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory")) FileUtils.ln_s($inventory, File.join($vagrant_ansible,"inventory"))
@ -81,80 +82,60 @@ if Vagrant.has_plugin?("vagrant-proxyconf")
end end
Vagrant.configure("2") do |config| Vagrant.configure("2") do |config|
# always use Vagrants insecure key
config.ssh.insert_key = false
config.vm.box = $box config.vm.box = $box
if SUPPORTED_OS[$os].has_key? :box_url if SUPPORTED_OS[$os].has_key? :box_url
config.vm.box_url = SUPPORTED_OS[$os][:box_url] config.vm.box_url = SUPPORTED_OS[$os][:box_url]
end end
config.ssh.username = SUPPORTED_OS[$os][:user] config.ssh.username = SUPPORTED_OS[$os][:user]
# plugin conflict # plugin conflict
if Vagrant.has_plugin?("vagrant-vbguest") then if Vagrant.has_plugin?("vagrant-vbguest") then
config.vbguest.auto_update = false config.vbguest.auto_update = false
end end
# always use Vagrants insecure key
config.ssh.insert_key = false
(1..$num_instances).each do |i| (1..$num_instances).each do |i|
config.vm.define vm_name = "%s-%02d" % [$instance_name_prefix, i] do |config| config.vm.define vm_name = "%s-%01d" % [$instance_name_prefix, i] do |node|
config.vm.hostname = vm_name
node.vm.hostname = vm_name
if Vagrant.has_plugin?("vagrant-proxyconf") if Vagrant.has_plugin?("vagrant-proxyconf")
config.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || "" node.proxy.http = ENV['HTTP_PROXY'] || ENV['http_proxy'] || ""
config.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || "" node.proxy.https = ENV['HTTPS_PROXY'] || ENV['https_proxy'] || ""
config.proxy.no_proxy = $no_proxy node.proxy.no_proxy = $no_proxy
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 end
["vmware_fusion", "vmware_workstation"].each do |vmware| ["vmware_fusion", "vmware_workstation"].each do |vmware|
config.vm.provider vmware do |v| node.vm.provider vmware do |v|
v.vmx['memsize'] = $vm_memory v.vmx['memsize'] = $vm_memory
v.vmx['numvcpus'] = $vm_cpus v.vmx['numvcpus'] = $vm_cpus
end end
end end
config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] node.vm.provider :virtualbox do |vb|
$shared_folders.each do |src, dst|
config.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
end
config.vm.provider :virtualbox do |vb|
vb.gui = $vm_gui
vb.memory = $vm_memory vb.memory = $vm_memory
vb.cpus = $vm_cpus vb.cpus = $vm_cpus
vb.gui = $vm_gui
vb.linked_clone = true
end end
config.vm.provider :libvirt do |lv| node.vm.provider :libvirt do |lv|
lv.memory = $vm_memory lv.memory = $vm_memory
# Fix kernel panic on fedora 28 lv.cpus = $vm_cpus
if $os == "fedora" lv.default_prefix = 'kubespray'
lv.cpu_mode = "host-passthrough" # Fix kernel panic on fedora 28
end if $os == "fedora"
end lv.cpu_mode = "host-passthrough"
end
ip = "#{$subnet}.#{i+100}" end
host_vars[vm_name] = {
"ip": ip,
"local_release_dir" => $local_release_dir,
"download_run_once": "False",
"kube_network_plugin": $network_plugin,
"kube_network_plugin_multus": $multi_networking
}
config.vm.network :private_network, ip: ip
# Disable swap for each vm
config.vm.provision "shell", inline: "swapoff -a"
if $kube_node_instances_with_disks if $kube_node_instances_with_disks
# Libvirt # Libvirt
driverletters = ('a'..'z').to_a driverletters = ('a'..'z').to_a
config.vm.provider :libvirt do |lv| node.vm.provider :libvirt do |lv|
# always make /dev/sd{a/b/c} so that CI can ensure that # always make /dev/sd{a/b/c} so that CI can ensure that
# virtualbox and libvirt will have the same devices to use for OSDs # virtualbox and libvirt will have the same devices to use for OSDs
(1..$kube_node_instances_with_disks_number).each do |d| (1..$kube_node_instances_with_disks_number).each do |d|
@ -163,24 +144,51 @@ Vagrant.configure("2") do |config|
end end
end end
# Only execute once the Ansible provisioner, if $expose_docker_tcp
# when all the machines are up and ready. node.vm.network "forwarded_port", guest: 2375, host: ($expose_docker_tcp + i - 1), auto_correct: true
end
$forwarded_ports.each do |guest, host|
node.vm.network "forwarded_port", guest: guest, host: host, auto_correct: true
end
node.vm.synced_folder ".", "/vagrant", disabled: false, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z'] , rsync__exclude: ['.git','venv']
$shared_folders.each do |src, dst|
node.vm.synced_folder src, dst, type: "rsync", rsync__args: ['--verbose', '--archive', '--delete', '-z']
end
ip = "#{$subnet}.#{i+100}"
node.vm.network :private_network, ip: ip
# Disable swap for each vm
node.vm.provision "shell", inline: "swapoff -a"
host_vars[vm_name] = {
"ip": ip,
"kube_network_plugin": $network_plugin,
"kube_network_plugin_multus": $multi_networking,
"docker_keepcache": "1",
"download_run_once": "True",
"download_localhost": "False"
}
# Only execute the Ansible provisioner once, when all the machines are up and ready.
if i == $num_instances if i == $num_instances
config.vm.provision "ansible" do |ansible| node.vm.provision "ansible" do |ansible|
ansible.playbook = $playbook ansible.playbook = $playbook
if File.exist?(File.join(File.dirname($inventory), "hosts")) if File.exist?(File.join( $inventory, "hosts.ini"))
ansible.inventory_path = $inventory ansible.inventory_path = $inventory
end end
ansible.become = true ansible.become = true
ansible.limit = "all" ansible.limit = "all"
ansible.host_key_checking = false ansible.host_key_checking = false
ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache"] ansible.raw_arguments = ["--forks=#{$num_instances}", "--flush-cache", "--ask-become-pass"]
ansible.host_vars = host_vars ansible.host_vars = host_vars
#ansible.tags = ['download'] #ansible.tags = ['download']
ansible.groups = { ansible.groups = {
"etcd" => ["#{$instance_name_prefix}-0[1:#{$etcd_instances}]"], "etcd" => ["#{$instance_name_prefix}-[1:#{$etcd_instances}]"],
"kube-master" => ["#{$instance_name_prefix}-0[1:#{$kube_master_instances}]"], "kube-master" => ["#{$instance_name_prefix}-[1:#{$kube_master_instances}]"],
"kube-node" => ["#{$instance_name_prefix}-0[1:#{$kube_node_instances}]"], "kube-node" => ["#{$instance_name_prefix}-[1:#{$kube_node_instances}]"],
"k8s-cluster:children" => ["kube-master", "kube-node"], "k8s-cluster:children" => ["kube-master", "kube-node"],
} }
end end