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 "listen"
require_relative "lib/tasks"
load "tasks/config.rake"
namespace :nanoc do
desc "Compile the website"

View file

@ -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 %>
}

View file

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

View file

@ -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