Add support for building configuration from local.yml

This commit is contained in:
0x1eef 2023-03-20 08:35:39 -03:00
parent ea11bacd69
commit 515523ed57
4 changed files with 58 additions and 29 deletions

View file

@ -4,6 +4,7 @@ require "bundler/setup"
require "ryo" require "ryo"
require "listen" require "listen"
require_relative "lib/tasks" require_relative "lib/tasks"
load "tasks/config.rake"
namespace :nanoc do namespace :nanoc do
desc "Compile the website" desc "Compile the website"

View file

@ -1,19 +1,27 @@
<% ssl_on = nginx.ssl.cert && nginx.ssl.cert_key %>
<% if ssl_on %>
server { server {
server_name <%= rc.hostname %>; server_name <%= rc.hostname %>;
listen 80; listen 80;
return 301 https://$host$request_uri; return 301 https://$host$request_uri;
} }
<% end %>
server { server {
gzip_static on; gzip_static on;
server_name <%= rc.hostname %>; 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; error_log <%= nginx.logs.errors %> info;
access_log <%= nginx.logs.access %> combined; access_log <%= nginx.logs.access %> combined;
add_header Strict-Transport-Security "max-age=31536000" always;
location / { location / {
root <%= nginx.root %>; 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 %>
} }

View file

@ -17,5 +17,5 @@ nginx:
## ##
# optional # optional
ssl: ssl:
cert: <path> cert:
cert_key: <path> cert_key:

View file

@ -12,34 +12,54 @@ read_options = ->(env:) do
Ryo.from(YAML.load_file(path)) Ryo.from(YAML.load_file(path))
end 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| task "config:build", :env do |task, args|
Rake::Task["config:build:etc"].invoke(args[:env]) env = args[:env]
Rake::Task["config:build:nginx"].invoke(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 end
task "config:build:etc", :env do |task, args| task "config:build:etc", :env do |task, args|
options = read_options.call(**args) env = args[:env]
context = ERBContext.with_locals(options) build_files.call(
glob = File.join("config", args[:env], "etc", "*.conf.erb") env:,
etc_files = Dir.glob(glob) base: "config/#{env}",
etc_files.each do |file| glob: "etc/*.conf.erb"
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
end end
task "config:build:nginx", :env do |task, args| task "config:build:nginx", :env do |task, args|
options = read_options.call(**args) env = args[:env]
context = ERBContext.with_locals(options) build_files.call(
glob = File.join("config", "generic", "usr.local.etc", "**", "*.conf.erb") env:,
Dir.glob(glob).each do |file| base: "config/generic",
dest = File.join(File.dirname(file), File.basename(file, ".erb")) glob: "usr.local.etc/**/*.conf.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
end end