module VagrantPlugins module ReverseProxy module Action class WriteNginxConfig def initialize(app, env, action) @app = app @global_env = env[:machine].env @provider = env[:machine].provider_name @config = @global_env.vagrantfile.config @action = action 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...') machines = get_machines() # This code is so stupid File.open(nginx_site, 'r') do |old| File.open(tmp_file, 'w') do |new| sm = machines.map {|m| start_marker(m) } em = machines.map {|m| end_marker(m) } while ln = old.gets() do if sm.member?(ln.chomp) until !ln || em.member?(ln.chomp) do ln = old.gets() end else new.puts(ln) end end if @action == :add machines.each do |m| new.write(start_marker(m)+"\n"+server_block(m)+end_marker(m)+"\n") end end end end Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site) Kernel.system('sudo', 'service', 'nginx', 'reload') 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| <