Skip to content
Snippets Groups Projects
Select Git revision
  • 22058fa289e0e11da1d50bdd40eff0fa55f9125f
  • master default
2 results

generate_bpmn.rs

Blame
  • Vagrantfile 13.46 KiB
    # https://github.com/rubocop/ruby-style-guide
    gems_installed=false
    required_gems = ["recursive-open-struct"]
    required_gems.each do |gem|
      unless Vagrant.has_plugin? gem 
    	print "Gem '" + gem + "' is required to process the vagrantfile, do you want to install it [Y/N]? "
    	input = STDIN.gets.chomp
    	abort "Gem requirement unmeet exiting" unless input.to_s.downcase  == "y"
    	abort "Gem requirement unmeet exiting" unless system "vagrant plugin install #{gem}"
    	gems_installed = true
      end
    end
    if gems_installed 
    	exec "vagrant #{ARGV.join(' ')}"
    end
    
    require 'recursive-open-struct'
    
    # Function to check whether VM was already provisioned
    def provisioned?(vm_name:'default', provider:'virtualbox')
    	File.exist?(".vagrant/machines/#{vm_name}/#{provider}/action_provision")
    end
    
    # avoid tabs in yaml!!! YAML or Psych
    vms_hash = Psych.load <<-EOF
      group: "/srkt"
      relative_machinefolder: ../vm
      management:
        name: management
        gui: true
        memory: 1024
        cores: 1
        accelerate_3d: false
        hdd:
        hdd2: 5120
        ip: 192.168.56.10
        box: centos/8
      nodes:
        - name: kubernetes01
          gui: true
          memory: 2048
          cores: 2
          accelerate_3d: false
          hdd:
          hdd2: 5120
          ip: 192.168.56.11
          box: centos/8
        - name: kubernetes02
          gui: true
          memory: 2048
          cores: 2
          accelerate_3d: false
          hdd:
          hdd2: 5120
          ip: 192.168.56.12
          box: centos/8
        - name: kubernetes03
          gui: true
          memory: 2048
          cores: 2
          accelerate_3d: false
          hdd:
          hdd2: 5120
          ip: 192.168.56.13
          box: centos/8
    #    - name: kubernetes04
    #      gui: true
    #      memory: 2048
    #      cores: 2
    #      accelerate_3d: false
    #      hdd:
    #      hdd2: 5120
    #      ip: 192.168.56.14
    #      box: centos/8
    #    - name: ubuntu
    #      gui: true
    #      memory: 2048
    #      cores: 2
    #      accelerate_3d: false
    #      hdd:
    #      ip: 192.168.56.15
    #      box: ubuntu/focal64
    EOF
    
    #we can also load this from and external file
    #vm_hash = Psych.load(File.expand_path('../vm.yaml', __FILE__))
    #or even process some erb templating
    #vm_hash = = Psych.load(ERB.new(File.read(File.expand_path('../vm.yaml', __FILE__))).result(b))
    
    #transform into object tu access as i.e. vm.name
    vms = RecursiveOpenStruct.new(vms_hash)
    #puts vms_hash.to_s
    #puts vms.management.name
    #abort 'DEBUG: force stop'
    
    abort 'ERROR: vms.groups must start with / ' if vms.group and not vms.group.start_with?("/")
    
    Vagrant.configure("2") do |config|
    
        # This is configured this way to allow the nesting of this vagrant file from a outer multimachine vagrantfile in case it is necessary
    	# we mark it as default with primary
    	config.vm.define vms.management.name, primary: true do |subconfig|
            subconfig.vagrant.plugins = ["vagrant-disksize","vagrant-vbguest"]
    
    		subconfig.vm.box = vms.management.box
    		# http://cloud.centos.org/centos/8/x86_64/images/
    		# subconfig.vm.box_url = "http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Vagrant-8.1.1911-20200113.3.x86_64.vagrant-virtualbox.box"
    		subconfig.vm.box_url = "http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Vagrant-8.3.2011-20201204.2.x86_64.vagrant-virtualbox.box"
    		subconfig.vbguest.auto_update = false
    
    		subconfig.vm.hostname = vms.management.name
    
    		subconfig.vm.provider "virtualbox" do |vb|
    			vb.name = vms.management.name
    			vb.gui = vms.management.gui
    			vb.customize ["modifyvm", :id, "--memory", vms.management.memory]
    			vb.customize ["modifyvm", :id, "--cpus", vms.management.cores]
    			vb.customize ["modifyvm", :id, "--accelerate3d", "on"] if vms.management.accelerate_3d
    			vb.customize ["modifyvm", :id, "--groups", vms.group] unless vms.group.to_s==""
    			#https://serverfault.com/questions/453185/vagrant-virtualbox-dns-10-0-2-3-not-working?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
    			vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    			#https://betacloud.io/get-rid-of-ubuntu-xenial-16-04-cloudimg-console-log/
    			#vb.customize [ "modifyvm", :id, "--uartmode1", "disconnected" ] this works in vbox 5 but in vbox 6 it slows downs the loading of the virtual machine 
    			# en base a https://groups.google.com/forum/#!topic/vagrant-up/eZljy-bddoI lo redirecciono a NUL
    			vb.customize [ "modifyvm", :id, "--uartmode1", "file", "NUL" ] 
    			
    			# other values for graphicscontroller VBoxSVGA: The default graphics controller for new VMs that use Linux or Windows 7 or later. This graphics controller improves performance and 3D support when compared to the legacy VBoxVGA option. – VBoxVGA: Use this graphics controller for legacy guest OSes. This is the default graphics controller for Windows versions before Windows 7. – VMSVGA: Use this graphics controller to emulate a VMware SVGA graphics device. – None: Does not emulate a graphics adapter type.
    			vb.customize [ "modifyvm", :id, "--graphicscontroller", "vboxsvga" ]
    			# other values for -autostop-type disabled|savestate|poweroff|acpishutdown
    			# vb.customize [ "modifyvm", :id, "--autostop-type", "poweroff" ] but this seems to be not abailable
    			vb.customize ["setextradata", :id, "GUI/DefaultCloseAction", "Shutdown"]
    			# this set the default shutdown option to shutdown. Avoiding the dialogue to appear
    			
    			# https://github.com/hashicorp/vagrant/issues/1992
    			if vms.relative_machinefolder.to_s != "" then
    				vb.customize "pre-import", [ "setproperty", "machinefolder", File.join(File.dirname(__FILE__), vms.relative_machinefolder) ]
    				vb.customize "post-boot",  [ "setproperty", "machinefolder", "default" ]
    			end
    
    			subconfig.vm.network "private_network", ip: vms.management.ip, netmask: "255.255.255.0", auto_config: true, adapter: 2
    
    			subconfig.vm.network "forwarded_port", guest: 8001, host: 8001
    		end	
    
    		subconfig.disksize.size = vms.management.hdd unless vms.management.hdd.to_s
    
            # this forced mapping of vagrant ensure it maps to this vagrantfile folder in case we call this vagrantfile from an outer vagrantfile
    		# In this case we dissable it
    		subconfig.vm.synced_folder File.expand_path('../', __FILE__), "/vagrant", disabled: true
    
    		# we take the name of the repo, that we will use to map into the profile
    		# we have had problems in ubuntu mapping under /home/vagrant when rebooting form the machine the shared is not remounted again
    		# we have had problems in centos because it does not include guest additions https://blog.centos.org/2020/05/updated-centos-vagrant-images-available-v2004-01/, in abscense of guest additions they use rsync on machine start or reload. 
    		subconfig.vm.synced_folder File.expand_path('../', __FILE__), "/kubernetes-vagrant", type: "rsync", rsync__exclude: ".git/"		
    
    		# this adds the ssh keys on this image allowing it to passwordless access the nodes 
    		subconfig.ssh.insert_key = true
    
    		# this section manages the rsa key in the management machine, it takes from the .ssh folder, and if it is not there it takes from the management machine. 
    		# This only takes place the first time the machine is created. Latter on is not needed.
    		unless provisioned?(vm_name: vms.management.name )	
    			# In case there is a private key in the .ssh we add it to the management as id_rsa, in case it is not we will retrieve the key generated by vagrant. The public key is available inside the machine in the authorized_keys file.
    			id_rsa_path = File.expand_path('../.ssh/id_rsa', __FILE__) 
    			if File.exists?(id_rsa_path)
    				subconfig.vm.provision "file", source: id_rsa_path, destination: "/home/vagrant/.ssh/id_rsa" 
    				subconfig.vm.provision "shell", name: "fix ssh keys permissions", path: File.expand_path('../scripts/multimachine/fixIdRsaPermissions.sh', __FILE__), privileged: false
    
    				# we copy the public key
    				id_rsa_pub_path = File.expand_path('../.ssh/id_rsa.pub', __FILE__) 
    				subconfig.vm.provision "file", source: id_rsa_pub_path, destination: "/tmp/id_rsa.pub", run: 'once'
    				subconfig.vm.provision "shell", name: "Add public key", inline: "cat /tmp/id_rsa.pub >> .ssh/authorized_keys", privileged: false, run: 'once'
    			else
    				# this script saves pki information in guest and in the path of the vagrantfile
    				subconfig.vm.provision "shell", name: "Get public ssh keys if id_rsa case no present", path: File.expand_path('../scripts/multimachine/getLastPublicRsa.sh', __FILE__), privileged: false
    
    				# this script saves the ssh keys of this machine in the host in case there are no one there
    				subconfig.trigger.after :up do |trigger|
    					trigger.name = "Get pki"
    					trigger.info = "Get private and public key from the guest and store in the host"
    					trigger.ruby do |env,machine|
    						keys_folder_path = File.expand_path('.ssh', env.root_path)						 
    						unless File.exists?(keys_folder_path)
    							env.ui.info("==> #{machine.name.to_s}: creating missing .ssh folder at host")
    							Dir.mkdir(keys_folder_path) 
    						end
    						env.ui.info("==> #{machine.name.to_s}: recovering id_rsa from vagrant config")
    						vagrant_private_key=File.expand_path(".vagrant/machines/#{machine.name.to_s}/virtualbox/private_key", env.root_path)
    						private_key_path = File.expand_path('id_rsa', keys_folder_path)
    						FileUtils.cp(vagrant_private_key, private_key_path)
    						env.ui.info("==> #{machine.name.to_s}: uploading id_rsa to Guest")
    						puts `vagrant upload #{vagrant_private_key} '.ssh/id_rsa' #{machine.name.to_s}`
    						env.ui.info("==> #{machine.name.to_s}: fixing rights of id_rsa at Guest")
    						puts `vagrant ssh -c 'chmod 600 .ssh/id_rsa'`
    						public_key_path = File.expand_path('id_rsa.pub', keys_folder_path)
    						env.ui.info("==> #{machine.name.to_s}: recovering id_rsa.pub from host to guest")
    						public_key = `vagrant ssh -c 'cat ~/.ssh/id_rsa.pub`
    						File.open(public_key_path, 'w') { |file| file.write(public_key.chomp) }
    					end
    				end
    			end
    		end
    
    		subconfig.vm.provision "shell", name: "Install ansible", path: File.expand_path('../scripts/ansible/install.sh', __FILE__), run: 'once'
    
            subconfig.vm.provision "shell", name: "addHostsToEtcHosts", inline: "/bin/sh /kubernetes-vagrant/ansible/addHostsToEtcHosts.sh", run: 'never', privileged: false
    	end
    
    	vms.nodes.each do |node_hash|
    		node = RecursiveOpenStruct.new(node_hash)
    		config.vm.define node.name do |subconfig|
    			subconfig.vagrant.plugins = ["vagrant-disksize","vagrant-vbguest"]
    	
    			subconfig.vm.box = node.box
    			# http://cloud.centos.org/centos/8/x86_64/images/
    			# subconfig.vm.box_url = "http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Vagrant-8.1.1911-20200113.3.x86_64.vagrant-virtualbox.box"
    			subconfig.vm.box_url = "http://cloud.centos.org/centos/8/x86_64/images/CentOS-8-Vagrant-8.3.2011-20201204.2.x86_64.vagrant-virtualbox.box"
    			subconfig.vbguest.auto_update = false
    	
    			subconfig.vm.hostname = node.name
    	
    			subconfig.vm.provider "virtualbox" do |vb|
    				vb.name = node.name
    				vb.gui = node.gui
    				vb.customize ["modifyvm", :id, "--memory", node.memory]
    				vb.customize ["modifyvm", :id, "--cpus", node.cores]
    				vb.customize ["modifyvm", :id, "--accelerate3d", "on"] if node.accelerate_3d
    				vb.customize ["modifyvm", :id, "--groups", vms.group] unless vms.group.to_s==""
    				vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
    				vb.customize [ "modifyvm", :id, "--uartmode1", "file", "NUL" ] 
    				
    				vb.customize [ "modifyvm", :id, "--graphicscontroller", "vboxsvga" ]
    				vb.customize ["setextradata", :id, "GUI/DefaultCloseAction", "Shutdown"]
    				
    				if vms.relative_machinefolder.to_s != "" then
    					vb.customize "pre-import", [ "setproperty", "machinefolder", File.join(File.dirname(__FILE__), vms.relative_machinefolder) ]
    					vb.customize "post-boot",  [ "setproperty", "machinefolder", "default" ]
    				end
    				subconfig.vm.network "private_network", ip: node.ip, netmask: "255.255.255.0", auto_config: true, adapter: 2
    
    				#we add a second device for gluster fs NOTE: Stay away from --port 0 as it is usually the boot disk.
    				if node.hdd2.to_s != "" then
    					hd = 1
    					if vms.relative_machinefolder.to_s != "" then
    						default_machine_folder=File.join(File.dirname(__FILE__), vms.relative_machinefolder)
    					else 
    						system_properties = %x[VBoxManage list systemproperties]
    						puts system_properties
    						default_machine_folder_match = system_properties.match /Default machine folder:\s*(.*)/
    						default_machine_folder=default_machine_folder_match[1]					
    					end
    					if vms.group.to_s=="" then
    						disk = File.join(default_machine_folder, vb.name, "disk#{hd}.vdi")
    					else
    						disk = File.join(default_machine_folder, vms.group, vb.name, "disk#{hd}.vdi")
    					end
    
    					vb.customize ['createhd', '--filename', disk,'--variant', 'Standard', '--size', node.hdd2] unless File.exists?(disk)
    					vb.customize ['storageattach', :id,  '--storagectl', 'IDE', '--port', hd, '--device', 0, '--type', 'hdd', '--medium', disk] 
    				end
    			end	
    	
    			subconfig.disksize.size = node.hdd unless node.hdd.to_s == ""
    
    			# we dissable the vagrant mapping we should not work directly in this image and therefore it is not required
    			subconfig.vm.synced_folder File.expand_path('../', __FILE__), "/vagrant", disabled: true	
    
    			# we copy the public key
    			id_rsa_pub_path = File.expand_path('../.ssh/id_rsa.pub', __FILE__) 
    			subconfig.vm.provision "file", source: id_rsa_pub_path, destination: "/tmp/id_rsa.pub", run: 'once'
    			subconfig.vm.provision "shell", name: "Add public key", inline: "cat /tmp/id_rsa.pub >> .ssh/authorized_keys", privileged: false, run: 'once'
    		end  
    	end
    end