Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should allow extend Vagrantfile with Vagrantfile-ext.rb #246

Closed
hoatle opened this issue Feb 9, 2017 · 1 comment
Closed

should allow extend Vagrantfile with Vagrantfile-ext.rb #246

hoatle opened this issue Feb 9, 2017 · 1 comment

Comments

@hoatle
Copy link
Member

hoatle commented Feb 9, 2017

Using vagrant_config_override.json is for normal users.

With Vagrantfile-ext.rb, we can give users power users to customize at the highest level.

This is required when vagrant_config.json is not enough to customize the VM.

Related: https://groups.google.com/forum/#!topic/vagrant-up/g68oH8-JrCE

For example, now users want to use https://github.com/devopsgroup-io/vagrant-hostmanager it's impossible with vagrant_config_override.json

Note: users are only allowed to use vagrant_config_override.json to customize the vagrant config for now.

Use Vagrantfile-ext.rb if available. Note: don't use this if you don't know what you're going to do, usually using vagrant_config_override.json is enough in most cases.

Note: allows users to choose the path and the name for Vagrantfile-override on the vagrant_config_override.json file.

The loading order:

  • Vagrantfile
  • vagrant_config.json
  • vagrant_config_override.json (optional)
  • Vagrantfile.ext.rb (optional)

And users can optionally add vagrant_config_ext.json, vagrant_config_ext_override.json if they want, however, this is rather complicated, don't do this if you don't know what you're going to do.

When this issue is resolved, users can use Vagrantfile-ext.rb.

@hoatle hoatle added this to the v0.5.0-b1 milestone Feb 9, 2017
@hoatle hoatle self-assigned this Feb 9, 2017
@hoatle hoatle changed the title should allow extend Vagrantfile with Vagrantfile-override should allow extend Vagrantfile with Vagrantfile-ext.rb Feb 9, 2017
@hoatle
Copy link
Member Author

hoatle commented Feb 9, 2017

the use case is: now I want to use https://github.com/devopsgroup-io/vagrant-hostmanager but it's impossible for now (waits for #186).

Luckily, it's possible now with Vagrantfile-ext.rb

I just need to create Vagrantfile-ext.rb with the following content:

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

# we want to resemble the code from the Vagrantfile file
# users adjust the settings here
plugins = [{
  '_id' => '0',
  'name' => 'vagrant-hostmanager',
  'config_key' => 'hostmanager',
  'required' => true,
  'enabled' => true,
  'manage_host' => true,
  'manage_guest' => true,
  'ignore_private_ip' => true
}]

# this is not supposed to be edited by users
Vagrant.configure('2') do |config|

  plugins.each do |plugin|
    plugin_name = plugin['name']
    if plugin['required'] == true
      unless Vagrant.has_plugin?(plugin_name)
        puts red("required: '$ vagrant plugin install #{plugin_name}'")
        exit!
      end
    end

    if Vagrant.has_plugin?(plugin_name) and plugin.key?('config_key')
      config_key = plugin['config_key']
      if 'hostmanager' == config_key

        unless plugin['enabled'].nil?
          config.hostmanager.enabled = plugin['enabled']
        end

        unless plugin['manage_host'].nil?
          config.hostmanager.manage_host = plugin['manage_host']
        end

        unless plugin['manage_guest'].nil?
          config.hostmanager.manage_guest = plugin['manage_guest']
        end

        unless plugin['ignore_private_ip'].nil?
          config.hostmanager.ignore_private_ip = plugin['ignore_private_ip']
        end

        unless plugin['include_offline'].nil?
          config.hostmanager.include_offline = plugin['include_offline']
        end

        unless plugin['aliases'].nil?
          config.hostmanager.aliases = plugin['aliases']
        end

        # workaround for :public_network
        config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
          read_ip_address(vm)
        end
      end
    end
  end
end

# thanks to https://github.com/devopsgroup-io/vagrant-hostmanager/issues/121#issuecomment-69050265
$logger = Log4r::Logger.new('Vagrantfile-ext')

def read_ip_address(machine)
  command = "LANG=en ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }'"
  result  = ""

  $logger.info "Processing #{ machine.name } ... "

  begin
    # sudo is needed for ifconfig
    machine.communicate.sudo(command) do |type, data|
      result << data if type == :stdout
    end
    $logger.info "Processing #{ machine.name } ... success"
  rescue
    result = "# NOT-UP"
    $logger.info "Processing #{ machine.name } ... not running"
  end

  # the second inet is more accurate
  result.chomp.split("\n").last
end

and it should work.

hoatle added a commit to hoatle/teracy-dev that referenced this issue Feb 9, 2017
hoatle added a commit to hoatle/teracy-dev that referenced this issue Feb 9, 2017
hoatle added a commit to hoatle/teracy-dev that referenced this issue Feb 9, 2017
….rb: support to load more extension files
hoatle added a commit that referenced this issue Feb 9, 2017
@ #246 | should allow extend Vagrantfile with Vagrantfile-ext.rb
hoatle added a commit that referenced this issue Feb 9, 2017
…tead-of-hostsupdater-plugin_deps_#246

Features/#248 use hostmanager instead of hostsupdater plugin deps #246
@hoatle hoatle closed this as completed Feb 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant