module VagrantPlugins module ReverseProxy module Action class WriteNginxConfig def initialize(app, env) @app = app @global_env = env[:machine].env @provider = env[:machine].provider_name @config = @global_env.vagrantfile.config end def call(env) @app.call(env) return unless @config.reverse_proxy.enabled? # Determine temp file and target file nginx_dir = '/etc/nginx' unless File.directory?(nginx_dir) env[:ui].error("Could not update nginx configuration: directory '#{nginx_dir}' does not exist. Continuing without proxy...") return end nginx_site = "#{nginx_dir}/vagrant-proxy-config" tmp_file = @global_env.tmp_path.join('nginx.vagrant-proxies') env[:ui].info('Updating nginx configuration. Administrator privileges will be required...') File.open(tmp_file, 'w') do |f| get_machines().each do |m| f.write(server_block(m)) end end Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site) Kernel.system('sudo', 'service', 'reload', 'nginx') end def server_block(machine) if @config.reverse_proxy.vhosts vhosts = @config.reverse_proxy.vhosts else vhosts = [machine.config.vm.hostname || machine.name] end ip = get_ip_address(machine) vhosts.collect do |vhost| <