Create VM with Vagrantfile

Allows to create Ubuntu or CentOS VM and mounts $GOPATH from Host machine into Guest machine .

0

Votes

# -*- mode: ruby -*-
# vi: set ft=ruby :
# This file is intended to create VM and mount $GOPATH from host machine into guest machine to utilize all existing codebase into VM.

$syncTarget = "/vagrant"
$gopath = "/home/vagrant/go"

Vagrant.configure("2") do |config|

  config.vm.box_check_update = false

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "8192"
    vb.cpus = 2
    vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 10000 ]
  end

  config.vm.network "private_network", ip: "192.168.33.10"
  config.vm.synced_folder ".", "#{$syncTarget}", type: "nfs", nfs_udp: false, nfs_export: true

  config.vm.provision "shell", inline: <<-SHEL
    mkdir -p /home/vagrant/go/{bin,pkg,src}
    ln -s #{$syncTarget}/src/k8s.io /home/vagrant/go/src/
    chown -h vagrant:vagrant #{$gopath}/*
  SHEL

  config.vm.define "ubuntu" do |os|

    os.vm.synced_folder ".", "#{$syncTarget}", type: "nfs", nfs_export: true
    os.vm.box = "ubuntu/xenial64"
    os.vm.provision "shell", inline: <<-SHELL
      apt-get update
      apt-get install -y gcc make mercurial
      apt-get install -f -y
      apt-get install -y pkg-config zip g++ zlib1g-dev unzip
      apt-get install -y iptables libltdl7 libseccomp2
      apt-get install -y apt-transport-https ca-certificates curl software-properties-common
      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
      apt-get update
      apt-get install -y docker-ce
      usermod -aG docker vagrant
      tar -C /usr/local -xzf #{$syncTarget}/vagrant-binaries/go1.11.1.linux-amd64.tar.gz
      #{$syncTarget}/vagrant-binaries/bazel-0.19.1-installer-linux-x86_64.sh --prefix=/home/vagrant
      sh -c "echo 'export GOPATH=#{$gopath}' >> /etc/profile"
      sh -c "echo 'export k8s=#{$gopath}/src/k8s.io/kubernetes' >> /etc/profile"
      sh -c "echo 'export PATH=#{$gopath}/src/k8s.io/kubernetes/_output/bin:#{$gopath}/src/k8s.io/kubernetes/third_party/etcd:#{$gopath}/bin:\${PATH}:/usr/local/go/bin:\${GOPATH}/bin' >> /etc/profile"
      sh -c "echo 'export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig"
    SHELL
  end

  config.vm.define "centos" do |os|
    os.vm.box = "centos/7"
    #os.vm.box = "minimal/centos7"
    #os.vm.box_version = "7.0"
    os.vm.provision "shell", inline: <<-STL
      yum -y install gcc make mercurial wget patch
      yum install -y pkg-config zip g++ zlib1g-dev unzip
      yum install -y libstdc++-static
      tar -C /usr/local -xzf #{$syncTarget}/vagrant-binaries/go1.11.1.linux-amd64.tar.gz
      sh -c "echo 'export GOPATH=#{$gopath}' >> /etc/profile"
      sh -c "echo 'export k8s=#{$gopath}/src/k8s.io/kubernetes' >> /etc/profile"
      sh -c "echo 'export PATH=#{$gopath}/src/k8s.io/kubernetes/_output/bin:#{$gopath}/src/k8s.io/kubernetes/third_party/etcd:#{$gopath}/bin:\${PATH}:/usr/local/go/bin:\${GOPATH}/bin' >> /etc/profile"
      sh -c "echo 'export KUBECONFIG=/var/run/kubernetes/admin.kubeconfig"
      #{$syncTarget}/vagrant-binaries/bazel-0.19.1-installer-linux-x86_64.sh --prefix=/home/vagrant
      yum install -y yum-utils device-mapper-persistent-data lvm2
      yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
      yum install -y docker-ce
      usermod -aG docker vagrant

      #https://github.com/docker/for-linux/issues/475#issuecomment-438119843
      #systemctl start docker
    STL

    #selinuxoff/on
    os.vm.provision "shell", inline: "sed -i 's/.*SELINUX=disabled.*/SELINUX=enforcing/' /etc/selinux/config"  

  end
end

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png