vadviktor
3/20/2017 - 6:08 PM

Learning Chef with Vagrant

Learning Chef with Vagrant

DOWNLOADS = {
  trusty: {
    chef_server: {
      url: 'https://packages.chef.io/files/stable/chef-server/12.13.0/ubuntu/14.04/chef-server-core_12.13.0-1_amd64.deb',
      sha256: 'e1c6a092f74a6b6b49b47dd92afa95be3dd9c30e6b558da5adf943a359a65997',
    },
    chef_client: {
      url: 'https://packages.chef.io/files/stable/chef/12.19.36/ubuntu/14.04/chef_12.19.36-1_amd64.deb',
      sha256: 'fbf44670ab5b76e4f1a1f5357885dafcc79e543ccbbe3264afd40c15d604b6dc',
    },
    chef_dk: {
      url: 'https://packages.chef.io/files/stable/chefdk/1.2.22/ubuntu/14.04/chefdk_1.2.22-1_amd64.deb',
      sha256: '518ecf308764c08a647ddabc6511af231affd2bf3e6526e60ef581926c8e7105',
    }
  },
  xenial: {
    chef_server: {
      url:'https://packages.chef.io/files/stable/chef-server/12.13.0/ubuntu/16.04/chef-server-core_12.13.0-1_amd64.deb',
      sha256: 'e1c6a092f74a6b6b49b47dd92afa95be3dd9c30e6b558da5adf943a359a65997',
    },
    chef_client: {
      url: 'https://packages.chef.io/files/stable/chef/12.19.36/ubuntu/16.04/chef_12.19.36-1_amd64.deb',
      sha256: 'fbf44670ab5b76e4f1a1f5357885dafcc79e543ccbbe3264afd40c15d604b6dc',
    },
    chef_dk: {
      url: 'https://packages.chef.io/files/stable/chefdk/1.2.22/ubuntu/16.04/chefdk_1.2.22-1_amd64.deb',
      sha256: '518ecf308764c08a647ddabc6511af231affd2bf3e6526e60ef581926c8e7105',
    }
  }
}

def install_chef(version, component)
  route = DOWNLOADS[version.to_sym][component.to_sym]
  <<-SCRIPT
    echo "Downloading #{component.to_s}"
    wget --no-verbose -O #{component.to_s}.deb #{route[:url]}

    echo "Verifying #{component.to_s} integrity"
    sha256sum -b #{component.to_s}.deb | grep #{route[:sha256]}

    echo "Installing #{component.to_s}"
    dpkg -i #{component.to_s}.deb
  SCRIPT
end

def set_hostname(server)
  server.vm.provision 'shell', inline: "hostname #{server.vm.hostname}"
end

Vagrant.configure("2") do |config|
  config.ssh.private_key_path = 'vagrant_rsa'

  config.vm.provider 'virtualbox' do |v|
    v.memory = 1 * 512
    v.cpus   = 1
  end

  #  _    _            _        _        _   _
  # | |  | |          | |      | |      | | (_)
  # | |  | | ___  _ __| | _____| |_ __ _| |_ _  ___  _ __
  # | |/\| |/ _ \| '__| |/ / __| __/ _` | __| |/ _ \| '_ \
  # \  /\  / (_) | |  |   <\__ \ || (_| | |_| | (_) | | | |
  #  \/  \/ \___/|_|  |_|\_\___/\__\__,_|\__|_|\___/|_| |_|
  config.vm.define 'workstation', primary: true do |ws|
    ws.vm.box = 'xenial'
    ws.vm.network 'private_network', ip: '10.0.42.100'
    ws.vm.hostname = 'workstation.chef.ikon'
    set_hostname ws
    ws.vm.provision "shell", inline: install_chef(:xenial, :chef_dk)
    ws.vm.provision "shell", inline: 'echo "10.0.42.101 server.chef.ikon" | tee -a /etc/hosts'
  end

  #  _____
  # /  ___|
  # \ `--.  ___ _ ____   _____ _ __
  #  `--. \/ _ \ '__\ \ / / _ \ '__|
  # /\__/ /  __/ |   \ V /  __/ |
  # \____/ \___|_|    \_/ \___|_|
  config.vm.define 'chef-server' do |cs|
    cs.vm.box = 'trusty'
    cs.vm.network 'private_network', ip: '10.0.42.101'
    cs.vm.hostname = 'server.chef.ikon'
    set_hostname cs
    cs.vm.provider 'virtualbox' do |v|
      v.memory = 2048
      v.cpus = 2
    end

    cs.vm.provision "shell", inline: install_chef(:trusty, :chef_server)
    script = <<-SCRIPT
      echo "Reconfiguring Chef server"
      chef-server-ctl reconfigure

      echo "Restarting Chef server"
      chef-server-ctl restart

      echo "Waiting for services"
      until (curl -sS -D - http://localhost:8000/_status) 2>/dev/null | grep "200 OK"; do sleep 5s; done

      echo "Creating a user and organization"
      chef-server-ctl user-create ikon Viktor Vad vad.viktor@gmail.com force7777 --filename chef-ikon-key.pem
      chef-server-ctl org-create econthrust "Ikon Inc." --association_user ikon --filename chef-org-key.pem

      echo "Copying keys to /vagrant/secrets"
      mkdir -p /vagrant/secrets
      cp -f /home/vagrant/chef-ikon-key.pem /vagrant/secrets
      cp -f /home/vagrant/chef-org-key.pem /vagrant/secrets
    SCRIPT
    cs.vm.provision "shell", inline: script
  end

  #  _   _           _
  # | \ | |         | |
  # |  \| | ___   __| | ___
  # | . ` |/ _ \ / _` |/ _ \
  # | |\  | (_) | (_| |  __/
  # \_| \_/\___/ \__,_|\___|
  config.vm.define 'node' do |n|
    n.vm.box = 'trusty'
    n.vm.network 'private_network', ip: '10.0.42.102'
    n.vm.hostname = 'node1.chef.ikon'
    set_hostname n
    n.vm.provision "shell", inline: install_chef(:trusty, :chef_client)
    n.vm.provision "shell", inline: 'echo "10.0.42.101 server.chef.ikon" | tee -a /etc/hosts'
  end

end