Skip to content

Commit 205b51d

Browse files
committed
Only make changes in setup-app and -cert if previous config does not already exist
1 parent 9ad4533 commit 205b51d

File tree

3 files changed

+73
-43
lines changed

3 files changed

+73
-43
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,22 @@ Stops, if running, and starts nginx.
122122
#### `setup-cert`
123123

124124
```bash
125-
dev-nginx setup-cert demo-frontend.foobar.co.uk
125+
dev-nginx setup-cert demo-frontend.foobar.co.uk [--force]
126126
```
127127

128128
Uses `mkcert` to issue a certificate for a domain, writing it to `~/.gu/mkcert` and symlinking it into the directory nginx is installed.
129+
By default will do nothing if a certificate has been issued, installed and remains valid for the next 14 days. Include the `--force` flag
130+
to issue a new certificate if required.
129131

130132
#### `setup-app`
131133

132134
```bash
133-
dev-nginx setup-app /path/to/nginx-mapping.yml
135+
dev-nginx setup-app [--force] /path/to/nginx-mapping.yml
134136
```
135137

136138
Generates config for nginx proxy site(s) from a config file, issues the certificate(s) and restarts nginx.
139+
By default will do nothing if config already exists and matches what would be generated. Include the `--force` flag to
140+
install config and issue new certificates if required.
137141

138142
##### Config format
139143

script/setup-app

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
require 'yaml'
44
require '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)
99
end
1010

1111
HERE=File.dirname(__FILE__)
1212
DEFAULT_DOMAIN_ROOT = "local.dev-gutools.co.uk"
1313
NGINX_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

1719
config = YAML.load_file(config_file)
1820
name = config['name']
@@ -24,32 +26,32 @@ FileUtils.mkdir_p(dest_dir)
2426

2527
dest = 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
4042
server {
4143
listen #{port};
4244
server_name #{domain};
4345
EOS
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};
@@ -60,9 +62,9 @@ EOS
6062
proxy_buffering off;
6163
}
6264
EOS
63-
end
65+
end
6466

65-
file.write <<-EOS
67+
server_config << <<-EOS
6668
6769
location / {
6870
proxy_http_version 1.1;
@@ -77,8 +79,8 @@ EOS
7779
7880
EOS
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
@@ -88,15 +90,15 @@ EOS
8890
ssl_ciphers HIGH:!aNULL:!MD5;
8991
ssl_prefer_server_ciphers on;
9092
EOS
91-
end
93+
end
9294

93-
file.write <<-EOS
95+
server_config << <<-EOS
9496
}
9597
9698
EOS
9799

98-
if ssl
99-
file.write <<-EOS
100+
if ssl
101+
server_config << <<-EOS
100102
server {
101103
listen 80;
102104
server_name #{domain};
@@ -106,13 +108,27 @@ server {
106108
}
107109
108110
EOS
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
114116
end
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

script/setup-cert

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,24 @@ DOMAIN=$1
4545
KEY_FILE=${CERT_DIRECTORY}/${DOMAIN}.key
4646
CERT_FILE=${CERT_DIRECTORY}/${DOMAIN}.crt
4747

48-
mkcert -install
48+
# test certificate has been created, and installed, and does not expire in next 14 days
49+
if [[ "$2" != "--force" ]] && \
50+
[[ -r "$CERT_FILE" ]] && \
51+
[[ -r "${NGINX_HOME}/${DOMAIN}.crt" ]] && \
52+
>/dev/null 2>&1 openssl x509 -in "$CERT_FILE" -noout -checkend 1209600
53+
then
54+
echo -e "🔐 Found existing certificate for: ${YELLOW}${DOMAIN}${NC}"
55+
echo -e "Rerun with --force to recreate if needed."
56+
else
57+
mkcert -install
4958

50-
echo -e "🔐 Creating certificate for: ${YELLOW}${DOMAIN}${NC}"
51-
mkdir -p ${CERT_DIRECTORY}
52-
mkcert -key-file=${KEY_FILE} -cert-file=${CERT_FILE} ${DOMAIN}
59+
echo -e "🔐 Creating certificate for: ${YELLOW}${DOMAIN}${NC}"
60+
mkdir -p ${CERT_DIRECTORY}
61+
mkcert -key-file=${KEY_FILE} -cert-file=${CERT_FILE} ${DOMAIN}
5362

54-
echo -e "Symlinking the certificate for nginx at ${NGINX_HOME}"
55-
ln -sf ${KEY_FILE} ${NGINX_HOME}/${DOMAIN}.key
56-
ln -sf ${CERT_FILE} ${NGINX_HOME}/${DOMAIN}.crt
63+
echo -e "Symlinking the certificate for nginx at ${NGINX_HOME}"
64+
ln -sf ${KEY_FILE} ${NGINX_HOME}/${DOMAIN}.key
65+
ln -sf ${CERT_FILE} ${NGINX_HOME}/${DOMAIN}.crt
5766

58-
echo -e "🚀 ${YELLOW}Done. Please restart nginx.${NC}"
67+
echo -e "🚀 ${YELLOW}Done. Please restart nginx.${NC}"
68+
fi

0 commit comments

Comments
 (0)