# -*- mode: ruby -*-
# vi: set ft=ruby :

module LocalCommand
    class Config < Vagrant.plugin("2", :config)
        attr_accessor :command
    end

    class Plugin < Vagrant.plugin("2")
        name "shell_local"

        config(:shell_local, :provisioner) do
            Config
        end

        provisioner(:shell_local) do
            Provisioner
        end
    end

    class Provisioner < Vagrant.plugin("2", :provisioner)
        def provision
            result = system "#{config.command}"
        end
    end
end

Vagrant.configure(2) do |config|
    config.vm.define 'node', primary: true do |node|
        node.vm.box = 'ubuntu/trusty64'

        # Bridged networks make the machine appear as another physical device on
        # your network.
        node_ip = '10.0.0.101'
        node.vm.network 'public_network',
            bridge: 'Dell GigabitEthernet',
            ip: node_ip

        # The hostname the machine should have
        node.vm.hostname = 'chef-client'
        node.vm.provider 'virtualbox' do |vb|
            vb.gui = false
            vb.name = node.vm.hostname
            vb.cpus = 2
            vb.memory = '1024'
        end

        # If we previously Vagrant up'd this IP, there'll be a signature for
        # it in our .ssh/known_hosts file for a box that no longer exists since
        # Vagrant generates a new key each time we Vagrant up.  So we'll go
        # ahead and remove that here
        node.vm.provision 'remove-old-ssh-key',
            type: 'shell_local',
            command: "ssh-keygen -R #{node_ip}"

        # Uninstall Chef (older version 11.8.2-2 is currently pre-installed on
        # the base box)
        node.vm.provision 'remove-chef-client', type: :shell, inline: <<-SHELL
            dpkg -P chef
            dpkg -P chef-zero
        SHELL
    end
end