Replace Tasks::Deploy::Local

This commit is contained in:
0x1eef 2023-03-25 08:02:55 -03:00
parent 605b7c983d
commit 3e5435dca1
6 changed files with 43 additions and 68 deletions

View file

@ -32,5 +32,7 @@ Layout/ArgumentAlignment:
Enabled: false
Layout/ArrayAlignment:
Enabled: false
Layout/ExtraSpacing:
Enabled: false
Style/TrivialAccessors:
Enabled: false

View file

@ -3,14 +3,16 @@
require "ryo"
require "listen"
require_relative "lib/tasks"
ENV["SASS_PATH"] = "./src/css/"
load "tasks/config/build.rake"
load "tasks/config/install.rake"
load "tasks/deploy.rake"
namespace :nanoc do
desc "Compile the website"
task :compile do
sh "ruby -S bundle exec nanoc co"
sh "bundle exec nanoc co"
end
desc "Delete the build directory"
@ -24,7 +26,7 @@ task build: "nanoc:compile"
desc "Start a Ruby web server on localhost"
task server: ["nanoc:compile"] do
Dir.chdir(File.join(Dir.getwd, "build", "al-quran")) do
sh "ruby -S bundle exec adsf"
sh "bundle exec adsf"
end
end
@ -37,25 +39,9 @@ namespace :watch do
end.start
Rake::Task["server"].invoke
end
namespace :deploy do
desc "Watch for changes (deploy:local task)"
task :local do
Bundler.with_unbundled_env { Process.wait spawn("rake deploy:local") }
Listen.to(File.join(Dir.getwd, "src")) do
Bundler.with_unbundled_env { sh "rake deploy:local" }
end.start
sleep
end
end
end
namespace :deploy do
desc "Deploy to a local web server (eg nginx)"
task local: ["env:set-development-vars", "nanoc:compile"] do
Tasks::Deploy::Local.call
end
desc "Deploy to production"
task remote: ["env:verify-production-branch",
"env:set-production-vars",

View file

@ -1,6 +1,5 @@
# frozen_string_literal: true
module Tasks::Deploy
require_relative "deploy/local"
require_relative "deploy/remote"
end

View file

@ -1,49 +0,0 @@
# frozen_string_literal: true
class Tasks::Deploy::Local
require "ryo"
require "yaml"
require "paint"
def self.call(...)
new(...).call
end
def call
src, dest = settings.src, settings.dest
user, group = settings.user, settings.group
logs_dir = settings.logs_dir
doas "root", "/bin/mkdir", "-p", logs_dir
doas "root", "/bin/mkdir", "-p", dest
doas "root", "/bin/rm", "-rf", File.join(dest, "*")
doas "root", "/bin/cp", "-r", src, dest
doas "root", "/usr/sbin/chown", "-R", [user, group].join(":"), dest
doas "root", "/bin/chmod", "-R", "og-rwx", dest
doas "root", "/bin/chmod", "-R", "u+rwX", dest
end
def to_proc
proc { call }
end
private
def nanoc
@nanoc ||= Ryo.from(YAML.load_file("./nanoc.yaml"))
end
def settings
nanoc.deploy.local
end
def doas(user, *cmd)
print " " * 2, "-> ", File.basename(cmd[0]), ": "
_, status = Process.wait2 spawn("doas", "-u", user, *cmd)
if status.success?
print Paint["OK", :green, :bold], "\n"
else
print Paint["ERROR", :red, :bold], "\n"
exit!
end
end
end

View file

@ -41,6 +41,7 @@ task "config:build", :env do |task, args|
when "remote"
Rake::Task["config:build:etc"].invoke(env)
when "local"
# no-op
else
warn "env should be 'remote', or 'local', got: #{env}"
end

36
tasks/deploy.rake Normal file
View file

@ -0,0 +1,36 @@
##
# frozen_string_literal: true
read_options = ->(env:) do
path = File.join(Dir.getwd, "config", "#{env}.yml")
Ryo.from(YAML.load_file(path))
end
desc "Deploy the website"
task "deploy", [:env] => %i[nanoc:compile] do |task, args|
env = args[:env]
case env
when "remote"
# TODO
when "local"
if Process.euid != 0
sh "doas -u root bundle exec rake deploy:local"
exit $?.exitstatus
end
Rake::Task["deploy:local"].invoke
end
end
task "deploy:local" do
options = read_options.call(env: "local")
logs_dir = File.dirname(options.nginx.logs.access)
dest_dir = options.nginx.root
chown = Array.new(2) { options.nginx.user }.join(":")
sh "mkdir -p #{logs_dir}"
sh "mkdir -p #{dest_dir}"
sh "rm -rf #{File.join(dest_dir, "*")}"
sh "cp -R build/al-quran/ #{dest_dir}"
sh "chown -R #{chown} #{dest_dir}"
sh "chmod -R og-rwx #{dest_dir}"
sh "chmod -R u+rwX #{dest_dir}"
end