33require 'yaml'
44require 'fileutils'
55
6- if ARGV . size != 1
7- puts "usage: setup-app <config.yml>"
6+ if ARGV . size != 1 and ARGV . size != 2
7+ puts "usage: setup-app [--force] <config.yml>"
88 exit ( 1 )
99end
1010
1111HERE = File . dirname ( __FILE__ )
1212DEFAULT_DOMAIN_ROOT = "local.dev-gutools.co.uk"
1313NGINX_DIR = `#{ HERE } /locate-nginx` . chomp
1414
15- config_file = ARGV [ 0 ]
15+ config_file = ARGV [ 0 ] != "--force" ? ARGV [ 0 ] : ARGV [ 1 ]
16+
17+ force = ARGV [ 0 ] == "--force" or ARGV [ 1 ] == "--force"
1618
1719config = YAML . load_file ( config_file )
1820name = config [ 'name' ]
@@ -24,32 +26,32 @@ FileUtils.mkdir_p(dest_dir)
2426
2527dest = File . join ( dest_dir , "#{ name } .conf" )
2628
27- file = File . open ( dest , 'w' ) do | file |
29+ server_config = ""
2830
29- config [ 'mappings' ] . each do |mapping |
31+ config [ 'mappings' ] . each do |mapping |
3032
31- domain_root = mapping [ 'domain-root' ] || global_domain_root
32- path = mapping [ 'path' ] || ''
33- websocket = mapping [ 'websocket' ]
33+ domain_root = mapping [ 'domain-root' ] || global_domain_root
34+ path = mapping [ 'path' ] || ''
35+ websocket = mapping [ 'websocket' ]
3436
35- domain = if mapping [ 'prefix' ] then "#{ mapping [ 'prefix' ] } .#{ domain_root } " else "#{ domain_root } " end
37+ domain = if mapping [ 'prefix' ] then "#{ mapping [ 'prefix' ] } .#{ domain_root } " else "#{ domain_root } " end
3638
37- client_max_body_size = mapping [ 'client_max_body_size' ]
39+ client_max_body_size = mapping [ 'client_max_body_size' ]
3840
39- file . write <<-EOS
41+ server_config << <<-EOS
4042server {
4143 listen #{ port } ;
4244 server_name #{ domain } ;
4345EOS
44- if client_max_body_size
45- file . write <<-EOS
46+ if client_max_body_size
47+ server_config << <<-EOS
4648 client_max_body_size #{ client_max_body_size } ;
4749
48- EOS
49- end
50+ EOS
51+ end
5052
51- if websocket
52- file . write <<-EOS
53+ if websocket
54+ server_config << <<-EOS
5355
5456 location #{ websocket } {
5557 proxy_pass http://localhost:#{ mapping [ 'port' ] } #{ websocket } ;
6062 proxy_buffering off;
6163 }
6264EOS
63- end
65+ end
6466
65- file . write <<-EOS
67+ server_config << <<-EOS
6668
6769 location / {
6870 proxy_http_version 1.1;
7779
7880EOS
7981
80- if ssl
81- file . write <<-EOS
82+ if ssl
83+ server_config << <<-EOS
8284 ssl_certificate #{ domain } .crt;
8385 ssl_certificate_key #{ domain } .key;
8486
8890 ssl_ciphers HIGH:!aNULL:!MD5;
8991 ssl_prefer_server_ciphers on;
9092EOS
91- end
93+ end
9294
93- file . write <<-EOS
95+ server_config << <<-EOS
9496}
9597
9698EOS
9799
98- if ssl
99- file . write <<-EOS
100+ if ssl
101+ server_config << <<-EOS
100102server {
101103 listen 80;
102104 server_name #{ domain } ;
@@ -106,13 +108,27 @@ server {
106108}
107109
108110EOS
109- end
110- if ssl
111- ` #{ HERE } /setup-cert #{ domain } `
112- end
113- end
111+ end
112+ if ssl
113+ opts = force ? "--force" : ""
114+ ` #{ HERE } /setup-cert #{ domain } #{ opts } `
115+ end
114116end
115117
116- puts "Restarting nginx. This needs sudo permission, please enter password when prompted."
117- `#{ HERE } /restart-nginx`
118- puts "Done."
118+ begin
119+ old_server_config = File . read ( dest )
120+ rescue
121+ old_server_config = ""
122+ end
123+
124+ if force or old_server_config != server_config
125+ file = File . open ( dest , 'w' ) do |file |
126+ file . write server_config
127+ puts "Restarting nginx. This needs sudo permission, please enter password when prompted."
128+ `#{ HERE } /restart-nginx`
129+ puts "Done."
130+ end
131+ else
132+ puts "Found existing nginx configuration for this app, so doing nothing."
133+ puts "Rerun with --force to force installation of new configuration if required."
134+ end
0 commit comments