Created
June 24, 2019 18:42
-
-
Save kdevo/64d4ca10fe95a501ffc4eae84b61f6c2 to your computer and use it in GitHub Desktop.
[ISE] [EIT] Vagrantfile for automatic VM provisioning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
require 'json' | |
VM_NAME = "ise-eit-vm" | |
VM_CONFIG_FILE = "#{VM_NAME}.json" | |
DEFAULTS = {'cpus' => 4, 'memory' => 4092, 'headless' => true, | |
'additional_disk_size' => 0, 'additional_disk_path' => "./additional-disk.vdi", | |
'private_network_ip' => "192.168.42.42"} | |
unless File.file?(VM_CONFIG_FILE) | |
puts "No VM configuration file found. Generating defaults..." | |
File.write(VM_CONFIG_FILE, JSON.pretty_generate(DEFAULTS)) | |
puts "Generated new VM configuration file '#{VM_CONFIG_FILE}'. Change according to your needs!" | |
puts "After you changed a value in this file, reload vagrant using `vagrant reload --provision`." | |
puts " Origin of this Vagrantfile: git.io/ise-eit ✨ Have fun!" | |
end | |
VM_CONFIG = JSON.load(File.new(VM_CONFIG_FILE)) | |
USE_ADDITIONAL_DISK = VM_CONFIG["additional_disk_size"] > 0 | |
puts "Got #{VM_CONFIG["cpus"]} CPUs and #{VM_CONFIG["memory"]} MB of RAM. Headless: #{VM_CONFIG["headless"]}." | |
if USE_ADDITIONAL_DISK | |
puts "This VM uses an additional disk with a size of #{VM_CONFIG["additional_disk_size"]} MB under #{VM_CONFIG["additional_disk_path"]} (uses the VMs 2nd port of the SCSI controller)" | |
end | |
Vagrant.configure("2") do |config| | |
# Docs: https://docs.vagrantup.com | |
config.vm.box = "ubuntu/bionic64" | |
config.vm.network "private_network", ip: "192.168.42.42" | |
config.vm.hostname = VM_NAME | |
config.vm.provider "virtualbox" do |vb| | |
vb.name = VM_NAME | |
vb.gui = !VM_CONFIG["headless"] | |
vb.cpus = VM_CONFIG["cpus"] | |
vb.memory = VM_CONFIG["memory"] | |
if !File.exist?(VM_CONFIG['additional_disk_path']) && USE_ADDITIONAL_DISK | |
# Create a 10 GB dynamic HDD: | |
vb.customize ['createhd', '--filename', VM_CONFIG['additional_disk_path'], '--variant', "Standard", '--size', VM_CONFIG["additional_disk_size"]] | |
end | |
# Insert SCSI controller (not needed because there already exists one for this specific VM): | |
# vb.customize ['storagectl', :id, '--name', "SCSI", '--add', "sata", '--controller', "IntelAHCI"] | |
if USE_ADDITIONAL_DISK | |
# Attach HDD: | |
vb.customize ['storageattach', :id, '--storagectl', "SCSI", '--port', 2, '--device', 0, '--type', "hdd", '--medium', VM_CONFIG['additional_disk_path']] | |
end | |
end | |
config.vm.provision "shell", name: "General", reset: true, inline: <<-SHELL | |
sudo apt-get update -y -q | |
sudo apt-get upgrade -y -q | |
# Generic | |
sudo apt-get install -y -q tmux vim htop | |
# sudo apt-get install -y -q python | |
# Users | |
sudo useradd -m itsadmin | |
echo "itsadmin:itsadmin" | sudo chpasswd | |
# Docker | |
curl -sSL https://get.docker.com | sh | |
# Docker: Add users to group | |
sudo usermod -aG docker itsadmin | |
sudo usermod -aG docker vagrant | |
SHELL | |
unless VM_CONFIG["headless"] | |
config.vm.provision "shell", name: "GUI-related", inline: <<-SHELL | |
sudo apt-get install -y -q i3-wm xinit firefox dmenu | |
# X11 forwarding | |
sudo apt-get install -y -q x11-xserver-utils | |
SHELL | |
config.trigger.after :up do |trigger| | |
trigger.name = "Tips: i3-wm" | |
trigger.info = 'A minimal GUI setup has been performed. After logging in, type `startx` to start i3-wm, a minimal window manager (WIN+D to open dmenu-launcher).' | |
end | |
config.trigger.after :up do |trigger| | |
trigger.name = "Tips: X11-forwarding" | |
trigger.info = 'X11-server has (also) been installed. If you prefer X11-forwarding, you can use it now (read ssh config using `vagrant ssh-config`).' | |
end | |
end | |
config.trigger.after :up do |trigger| | |
trigger.name = "Tips: Logging" | |
trigger.info = 'You can start terminal recording via script. Example: script /vagrant/eit-practicum.log' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment