Class: Stable::Services::CaddyManager
- Inherits:
-
Object
- Object
- Stable::Services::CaddyManager
- Defined in:
- lib/stable/services/caddy_manager.rb
Overview
Service for managing Caddy web server configuration
Class Method Summary collapse
- .add_app(name, skip_ssl: false) ⇒ Object
- .caddyfile ⇒ Object
- .ensure_running! ⇒ Object
- .reload ⇒ Object
- .reload_if_running ⇒ Object
- .remove(domain) ⇒ Object
- .running? ⇒ Boolean
Class Method Details
.add_app(name, skip_ssl: false) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/stable/services/caddy_manager.rb', line 24 def add_app(name, skip_ssl: false) app = Services::AppRegistry.all.find { |a| a[:name] == name } return unless app domain = app[:domain] port = app[:port] ensure_certs_dir! unless skip_ssl ensure_cert_for!(domain) unless skip_ssl FileUtils.touch(caddyfile) content = File.read(caddyfile) content = remove_domain_block(content, domain) content << build_block(domain, port, skip_ssl: skip_ssl) atomic_write(caddyfile, content) system("caddy fmt --overwrite #{caddyfile}") ensure_running! end |
.caddyfile ⇒ Object
8 9 10 |
# File 'lib/stable/services/caddy_manager.rb', line 8 def caddyfile Stable::Paths.caddyfile end |
.ensure_running! ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/stable/services/caddy_manager.rb', line 55 def ensure_running! if running? reload else system("caddy run --config #{caddyfile} --adapter caddyfile &") sleep 2 end end |
.reload ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/stable/services/caddy_manager.rb', line 46 def reload if system('which caddy > /dev/null') pid = Process.spawn("caddy reload --config #{caddyfile}") Process.detach(pid.to_i) else puts 'Caddy not found. Install Caddy first.' end end |
.reload_if_running ⇒ Object
64 65 66 |
# File 'lib/stable/services/caddy_manager.rb', line 64 def reload_if_running reload if running? end |
.remove(domain) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/stable/services/caddy_manager.rb', line 12 def remove(domain) return unless File.exist?(caddyfile) content = File.read(caddyfile) content = remove_domain_block(content, domain) atomic_write(caddyfile, content) system("caddy fmt --overwrite #{caddyfile}") reload_if_running end |
.running? ⇒ Boolean
68 69 70 71 72 73 74 |
# File 'lib/stable/services/caddy_manager.rb', line 68 def running? require 'socket' TCPSocket.new('127.0.0.1', 2019).close true rescue Errno::ECONNREFUSED false end |