From 515523ed572c146db2acd879a9abe7be8b604041 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Mon, 20 Mar 2023 08:35:39 -0300 Subject: [PATCH] Add support for building configuration from local.yml --- Rakefile.rb | 1 + .../al-quran.reflectslight.io.conf.erb | 16 +++-- config/local.yml.sample | 4 +- tasks/config.rake | 66 ++++++++++++------- 4 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Rakefile.rb b/Rakefile.rb index 0303cfd..2f895df 100644 --- a/Rakefile.rb +++ b/Rakefile.rb @@ -4,6 +4,7 @@ require "bundler/setup" require "ryo" require "listen" require_relative "lib/tasks" +load "tasks/config.rake" namespace :nanoc do desc "Compile the website" diff --git a/config/generic/usr.local.etc/nginx/enabled_sites/al-quran.reflectslight.io.conf.erb b/config/generic/usr.local.etc/nginx/enabled_sites/al-quran.reflectslight.io.conf.erb index c5de669..a752af0 100644 --- a/config/generic/usr.local.etc/nginx/enabled_sites/al-quran.reflectslight.io.conf.erb +++ b/config/generic/usr.local.etc/nginx/enabled_sites/al-quran.reflectslight.io.conf.erb @@ -1,19 +1,27 @@ +<% ssl_on = nginx.ssl.cert && nginx.ssl.cert_key %> + +<% if ssl_on %> server { server_name <%= rc.hostname %>; listen 80; return 301 https://$host$request_uri; } +<% end %> server { gzip_static on; server_name <%= rc.hostname %>; - listen 443 ssl; - ssl_certificate <%= nginx.ssl.cert %>; - ssl_certificate_key <%= nginx.ssl.cert_key %>; error_log <%= nginx.logs.errors %> info; access_log <%= nginx.logs.access %> combined; - add_header Strict-Transport-Security "max-age=31536000" always; location / { root <%= nginx.root %>; } + <% if ssl_on %> + listen 443 ssl; + add_header Strict-Transport-Security "max-age=31536000" always; + ssl_certificate <%= nginx.ssl.cert %>; + ssl_certificate_key <%= nginx.ssl.cert_key %>; + <% else %> + listen 80; + <% end %> } diff --git a/config/local.yml.sample b/config/local.yml.sample index 67de285..36591c6 100644 --- a/config/local.yml.sample +++ b/config/local.yml.sample @@ -17,5 +17,5 @@ nginx: ## # optional ssl: - cert: - cert_key: + cert: + cert_key: diff --git a/tasks/config.rake b/tasks/config.rake index 2bf6a08..c1b60c3 100644 --- a/tasks/config.rake +++ b/tasks/config.rake @@ -12,34 +12,54 @@ read_options = ->(env:) do Ryo.from(YAML.load_file(path)) end +get_build_dir = -> (env:) do + path = File.join("./build", env) + mkdir_p(path) unless Dir.exist?(path) + path +end + +build_files = -> (env:, base:, glob:) do + options = read_options.call(env:) + build_dir = get_build_dir.call(env:) + context = ERBContext.with_locals(options) + Dir.glob(glob, base:).each do |file| + erbf = File.join(base, file) + path = File.join(build_dir, File.dirname(file)) + dest = File.join(path, File.basename(file, ".erb")) + mkdir_p(path) + File.binwrite dest, + ERB.new(File.binread(erbf), trim_mode: "-").result(context) + print "View #{dest} [y/n]:" + system("cat #{dest} | less") if $stdin.gets.chomp == "y" + end +end + task "config:build", :env do |task, args| - Rake::Task["config:build:etc"].invoke(args[:env]) - Rake::Task["config:build:nginx"].invoke(args[:env]) + env = args[:env] + case env + when "remote" + Rake::Task["config:build:etc"].invoke(env) + when "local" + else + warn "env should be 'remote', or 'local', got: #{env}" + end + Rake::Task["config:build:nginx"].invoke(env) end task "config:build:etc", :env do |task, args| - options = read_options.call(**args) - context = ERBContext.with_locals(options) - glob = File.join("config", args[:env], "etc", "*.conf.erb") - etc_files = Dir.glob(glob) - etc_files.each do |file| - dest = File.join(File.dirname(file), File.basename(file, ".erb")) - File.binwrite dest, - ERB.new(File.binread(file), trim_mode: "-").result(context) - print "View #{dest} [y/n]:" - system("cat #{dest} | less") if $stdin.gets.chomp == "y" - end + env = args[:env] + build_files.call( + env:, + base: "config/#{env}", + glob: "etc/*.conf.erb" + ) end task "config:build:nginx", :env do |task, args| - options = read_options.call(**args) - context = ERBContext.with_locals(options) - glob = File.join("config", "generic", "usr.local.etc", "**", "*.conf.erb") - Dir.glob(glob).each do |file| - dest = File.join(File.dirname(file), File.basename(file, ".erb")) - File.binwrite dest, - ERB.new(File.binread(file), trim_mode: "-").result(context) - print "View #{dest} [y/n]:" - system("cat #{dest} | less") if $stdin.gets.chomp == "y" - end + env = args[:env] + build_files.call( + env:, + base: "config/generic", + glob: "usr.local.etc/**/*.conf.erb" + ) end