vagrant-reverse-proxy 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b88dae7487696d9581390ffc45c65fe8abb635ed
4
- data.tar.gz: faa31f9eeb9dd4b915c8b1c23894550a3ebdee56
3
+ metadata.gz: ad23d444baedd4df858c5720a94e983e31401b9f
4
+ data.tar.gz: 1049c3a3ab95b7b4fcd517f76182fc0a717e4e5b
5
5
  SHA512:
6
- metadata.gz: 4b8c1562f636c89252d5aedaf2b9bbb1727131ab07c5ccd528b30ebb0aafb9d790557c018b40bdf134f85b8e7af5e368f17b981b9414691ba3b022ec470a15f4
7
- data.tar.gz: aea5ffc4ec2b606dde191179361ca3e17fb3f96c5f8855036fc3fecc6f68c44cf5eca93eb18db9d0f60f4cbb9cb72717b40d112a1b386398bee4d99e590ada97
6
+ metadata.gz: 2e0fe83f8e191a2193e56a908c7faf00cf0d0c4e957706558d8bf39b3166dcbba3fea5ab3a9e9884a68f995abb7e5841dabb2df5aea7340707b35127bd096e83
7
+ data.tar.gz: e860d8501191d327a1200e36e2e920775039ea2685a96ef9b48946a923bc34e55d228e5145fd2fb93152a704db11bae889da3a76051fa3cfecb69c43c6b86eac
data/README.md CHANGED
@@ -72,6 +72,11 @@ an array:
72
72
  As you can see, this allows you to define which port to connect to
73
73
  instead of the default port (which is port 80).
74
74
 
75
+ ### Specifying the NGINX configuration file path
76
+
77
+ If you want to change the location of the managed nginx configuration file, set the `config.reverse_proxy.nginx_config_file` value to a path on your host machine in the Vagrantfile configuration:
78
+
79
+ config.reverse_proxy.nginx_config_file = '/usr/local/etc/nginx/vagrant-proxy-config'
75
80
 
76
81
  ## Adding proxy support to your application
77
82
 
@@ -98,3 +103,12 @@ If you're using Symfony, just use `setTrustedProxies()` on your
98
103
  `Request` object, and Symfony takes care of the rest. Note that
99
104
  `X-Base-Url` is not supported by either framework, so you'll need to
100
105
  add a bit of custom code there if you need to override the base URL.
106
+
107
+
108
+ ## Changelog
109
+
110
+ - 0.3 Allow overriding the location of the NGINX configuration file
111
+ (thanks to Sam Stevens). Support multiple VMs in a single Vagrant
112
+ config (suggested by Nicholas Alipaz).
113
+ - 0.2 Support for proxying of multiple ports in `vhosts` config.
114
+ - 0.1 First version
@@ -4,42 +4,51 @@ module VagrantPlugins
4
4
  class WriteNginxConfig
5
5
  def initialize(app, env, action)
6
6
  @app = app
7
- @global_env = env[:machine].env
8
- @provider = env[:machine].provider_name
9
- @config = @global_env.vagrantfile.config
10
7
  @action = action
11
8
  end
12
9
 
13
10
  def call(env)
14
11
  @app.call(env)
15
12
 
16
- return unless @config.reverse_proxy.enabled?
13
+ # Does this make much sense? What if we disable it later
14
+ # for one specific machine? Then, the config should still
15
+ # be removed.
16
+ return unless env[:machine].config.reverse_proxy.enabled?
17
17
 
18
18
  # Determine temp file and target file
19
- nginx_dir = '/etc/nginx'
20
- unless File.directory?(nginx_dir)
21
- env[:ui].error("Could not update nginx configuration: directory '#{nginx_dir}' does not exist. Continuing without proxy...")
19
+ nginx_config_file = env[:machine].config.reverse_proxy.nginx_config_file || '/etc/nginx/vagrant-proxy-config'
20
+
21
+ # Get the directory of the config file
22
+ nginx_config_dir = File.dirname(nginx_config_file)
23
+
24
+ unless File.directory?(nginx_config_dir)
25
+ env[:ui].error("Could not update nginx configuration: directory '#{nginx_config_dir}' does not exist. Continuing without proxy...")
22
26
  return
23
27
  end
24
- nginx_site = "#{nginx_dir}/vagrant-proxy-config"
25
- tmp_file = @global_env.tmp_path.join('nginx.vagrant-proxies')
28
+
29
+ tmp_file = env[:machine].env.tmp_path.join('nginx.vagrant-proxies')
26
30
 
27
31
  env[:ui].info('Updating nginx configuration. Administrator privileges will be required...')
28
32
 
29
- machines = get_machines()
30
- # This code is so stupid
33
+ sm = start_marker(env[:machine])
34
+ em = end_marker(env[:machine])
35
+
36
+ # This code is so stupid: We write a tmp file with the
37
+ # current config, filtered to exclude this machine. Later,
38
+ # we put this machine's config back in. It might have
39
+ # changed, and might not have been present originally.
31
40
  File.open(tmp_file, 'w') do |new|
32
41
  begin
33
- File.open(nginx_site, 'r') do |old|
34
- sm = machines.map {|m| start_marker(m) }
35
- em = machines.map {|m| end_marker(m) }
36
-
42
+ File.open(nginx_config_file, 'r') do |old|
43
+ # First, remove old entries for this machine.
37
44
  while ln = old.gets() do
38
- if sm.member?(ln.chomp)
39
- until !ln || em.member?(ln.chomp) do
45
+ if sm == ln.chomp
46
+ # Skip lines until we find EOF or end marker
47
+ until !ln || em == ln.chomp do
40
48
  ln = old.gets()
41
49
  end
42
50
  else
51
+ # Keep lines for other machines.
43
52
  new.puts(ln)
44
53
  end
45
54
  end
@@ -49,20 +58,22 @@ module VagrantPlugins
49
58
  # we'll create it soon enough.
50
59
  end
51
60
 
52
- if @action == :add
53
- machines.each do |m|
54
- new.write(start_marker(m)+"\n"+server_block(m)+end_marker(m)+"\n")
61
+ if @action == :add # Removal is already (always) done above
62
+ # Write the config for this machine
63
+ if env[:machine].config.reverse_proxy.enabled?
64
+ new.write(sm+"\n"+server_block(env[:machine])+em+"\n")
55
65
  end
56
66
  end
57
67
  end
58
68
 
59
- Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_site)
69
+ # Finally, copy tmp config to actual config and reload nginx
70
+ Kernel.system('sudo', 'cp', tmp_file.to_s, nginx_config_file)
60
71
  Kernel.system('sudo', 'service', 'nginx', 'reload')
61
72
  end
62
73
 
63
74
  def server_block(machine)
64
- if @config.reverse_proxy.vhosts
65
- vhosts = @config.reverse_proxy.vhosts
75
+ if machine.config.reverse_proxy.vhosts
76
+ vhosts = machine.config.reverse_proxy.vhosts
66
77
  else
67
78
  host = machine.config.vm.hostname || machine.name
68
79
  vhosts = {host => host}
@@ -95,20 +106,6 @@ EOF
95
106
  "# END #{m.id} #"
96
107
  end
97
108
 
98
- # Machine-finding code stolen from vagrant-hostmanager :)
99
- def get_machines()
100
- # Collect only machines that exist for the current provider
101
- @global_env.active_machines.collect do |name, provider|
102
- if provider == @provider
103
- begin
104
- @global_env.machine(name, @provider)
105
- rescue Vagrant::Errors::MachineNotFound
106
- nil #ignore
107
- end
108
- end
109
- end.compact
110
- end
111
-
112
109
  # Also from vagrant-hostmanager
113
110
  def get_ip_address(machine)
114
111
  ip = nil
@@ -2,12 +2,13 @@ module VagrantPlugins
2
2
  module ReverseProxy
3
3
  class Plugin
4
4
  class Config < Vagrant.plugin(2, :config)
5
- attr_accessor :enabled, :vhosts
5
+ attr_accessor :enabled, :vhosts, :nginx_config_file
6
6
  alias_method :enabled?, :enabled
7
7
 
8
8
  def initialize
9
9
  @enabled = UNSET_VALUE
10
10
  @vhosts = UNSET_VALUE
11
+ @nginx_config_file = UNSET_VALUE
11
12
  end
12
13
 
13
14
  def finalize!
@@ -29,6 +30,9 @@ module VagrantPlugins
29
30
  end
30
31
  end
31
32
  end
33
+ if @nginx_config_file == UNSET_VALUE
34
+ @nginx_config_file = nil
35
+ end
32
36
  end
33
37
 
34
38
  def validate(machine)
@@ -61,6 +65,10 @@ module VagrantPlugins
61
65
  errors << 'vhosts must be an array of hostnames, a string=>string hash or nil'
62
66
  end
63
67
 
68
+ unless @nginx_config_file.instance_of?(String) || @nginx_config_file == nil || @nginx_config_file == UNSET_VALUE
69
+ errors << 'nginx_config_file must be a string'
70
+ end
71
+
64
72
  { 'Reverse proxy configuration' => errors.compact }
65
73
  end
66
74
  end
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module ReverseProxy
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-reverse-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bex
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-11 00:00:00.000000000 Z
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler