mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-17 09:36:23 +00:00
Merge branch 'cleanup' into 'rails'
hacer tareas de limpieza See merge request sutty/sutty!91
This commit is contained in:
commit
4aba6379d8
9 changed files with 97 additions and 33 deletions
|
@ -2,3 +2,4 @@
|
||||||
*
|
*
|
||||||
# Solo agregar lo que usamos en COPY
|
# Solo agregar lo que usamos en COPY
|
||||||
# !./archivo
|
# !./archivo
|
||||||
|
!./monit.conf
|
||||||
|
|
|
@ -15,6 +15,8 @@ RUN apk add --no-cache libxslt libxml2 postgresql-libs libssh2 \
|
||||||
RUN gem install --no-document --no-user-install foreman
|
RUN gem install --no-document --no-user-install foreman
|
||||||
RUN wget https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-amd64.tar.gz -O - | tar --strip-components 1 -xvzf - pandoc-${PANDOC_VERSION}/bin/pandoc && mv /bin/pandoc /usr/bin/pandoc
|
RUN wget https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-amd64.tar.gz -O - | tar --strip-components 1 -xvzf - pandoc-${PANDOC_VERSION}/bin/pandoc && mv /bin/pandoc /usr/bin/pandoc
|
||||||
|
|
||||||
|
COPY ./monit.conf /etc/monit.d/sutty.conf
|
||||||
|
|
||||||
VOLUME "/srv"
|
VOLUME "/srv"
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
|
|
8
Procfile
8
Procfile
|
@ -1,8 +1,2 @@
|
||||||
migrate: bundle exec rake db:prepare db:seed
|
cleanup: bundle exec rake cleanup:everything
|
||||||
sutty: bundle exec puma config.ru
|
|
||||||
blazer_5m: bundle exec rake blazer:run_checks SCHEDULE="5 minutes"
|
|
||||||
blazer_1h: bundle exec rake blazer:run_checks SCHEDULE="1 hour"
|
|
||||||
blazer_1d: bundle exec rake blazer:run_checks SCHEDULE="1 day"
|
|
||||||
blazer: bundle exec rake blazer:send_failing_checks
|
|
||||||
prometheus: bundle exec prometheus_exporter -b 0.0.0.0 --prefix "sutty_"
|
|
||||||
stats: bundle exec rake stats:process_all
|
stats: bundle exec rake stats:process_all
|
||||||
|
|
|
@ -23,6 +23,9 @@ class Deploy < ApplicationRecord
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Realizar tareas de limpieza.
|
||||||
|
def cleanup!; end
|
||||||
|
|
||||||
def time_start
|
def time_start
|
||||||
@start = Time.now
|
@start = Time.now
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,6 +45,17 @@ class DeployLocal < Deploy
|
||||||
File.join(Rails.root, '_deploy', site.hostname)
|
File.join(Rails.root, '_deploy', site.hostname)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Libera espacio eliminando archivos temporales
|
||||||
|
#
|
||||||
|
# @return [nil]
|
||||||
|
def cleanup!
|
||||||
|
FileUtils.rm_rf(gems_dir)
|
||||||
|
FileUtils.rm_rf(yarn_cache_dir)
|
||||||
|
FileUtils.rm_rf(File.join(site.path, 'node_modules'))
|
||||||
|
FileUtils.rm_rf(File.join(site.path, '.sass-cache'))
|
||||||
|
FileUtils.rm_rf(File.join(site.path, '.jekyll-cache'))
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def mkdir
|
def mkdir
|
||||||
|
|
|
@ -147,6 +147,23 @@ class Site
|
||||||
rugged.index.remove(relativize(file))
|
rugged.index.remove(relativize(file))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Garbage collection
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
def gc
|
||||||
|
env = { 'PATH' => '/usr/bin', 'LANG' => ENV['LANG'], 'HOME' => path }
|
||||||
|
cmd = 'git gc'
|
||||||
|
|
||||||
|
r = nil
|
||||||
|
Dir.chdir(path) do
|
||||||
|
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, _, t|
|
||||||
|
r = t.value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
r&.success?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Si Sutty tiene una llave privada de tipo ED25519, devuelve las
|
# Si Sutty tiene una llave privada de tipo ED25519, devuelve las
|
||||||
|
|
47
app/services/cleanup_service.rb
Normal file
47
app/services/cleanup_service.rb
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Realiza tareas de limpieza en todos los sitios, para optimizar y
|
||||||
|
# liberar espacio.
|
||||||
|
class CleanupService
|
||||||
|
# Días de antigüedad de los sitios
|
||||||
|
attr_reader :before
|
||||||
|
|
||||||
|
# @param :before [ActiveSupport::TimeWithZone] Cuánto tiempo lleva sin usarse un sitio.
|
||||||
|
def initialize(before: 30.days.ago)
|
||||||
|
@before = before
|
||||||
|
end
|
||||||
|
|
||||||
|
# Limpieza general
|
||||||
|
#
|
||||||
|
# @return [nil]
|
||||||
|
def cleanup_everything!
|
||||||
|
cleanup_older_sites!
|
||||||
|
cleanup_newer_sites!
|
||||||
|
end
|
||||||
|
|
||||||
|
# Encuentra todos los sitios sin actualizar y realiza limpieza.
|
||||||
|
#
|
||||||
|
# @return [nil]
|
||||||
|
def cleanup_older_sites!
|
||||||
|
Site.where('updated_at < ?', before).find_each do |site|
|
||||||
|
next unless File.directory? site.path
|
||||||
|
|
||||||
|
site.deploys.find_each(&:cleanup!)
|
||||||
|
|
||||||
|
site.repository.gc
|
||||||
|
site.touch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Tareas para los sitios en uso
|
||||||
|
#
|
||||||
|
# @return [nil]
|
||||||
|
def cleanup_newer_sites!
|
||||||
|
Site.where('updated_at >= ?', before).find_each do |site|
|
||||||
|
next unless File.directory? site.path
|
||||||
|
|
||||||
|
site.repository.gc
|
||||||
|
site.touch
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
11
lib/tasks/cleanup.rake
Normal file
11
lib/tasks/cleanup.rake
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
namespace :cleanup do
|
||||||
|
desc 'Cleanup sites'
|
||||||
|
task everything: :environment do
|
||||||
|
before = ENV.fetch('BEFORE', '30').to_i.days.ago
|
||||||
|
service = CleanupService.new(before: before)
|
||||||
|
|
||||||
|
service.cleanup_everything!
|
||||||
|
end
|
||||||
|
end
|
30
monit.conf
30
monit.conf
|
@ -1,29 +1,7 @@
|
||||||
check process sutty with pidfile /srv/tmp/puma.pid
|
# Limpiar mensualmente
|
||||||
start program = "/usr/local/bin/sutty start"
|
check program cleanup
|
||||||
stop program = "/usr/local/bin/sutty stop"
|
with path "/usr/bin/foreman run -f /srv/Procfile -d /srv cleanup" as uid "rails" gid "www-data"
|
||||||
|
every "0 3 1 * *"
|
||||||
check process prometheus with pidfile /tmp/prometheus.pid
|
|
||||||
start program = "/usr/local/bin/sutty prometheus start"
|
|
||||||
stop program = "/usr/local/bin/sutty prometheus start"
|
|
||||||
|
|
||||||
check program blazer_5m
|
|
||||||
with path "/usr/local/bin/sutty blazer 5m"
|
|
||||||
every 5 cycles
|
|
||||||
if status != 0 then alert
|
|
||||||
|
|
||||||
check program blazer_1h
|
|
||||||
with path "/usr/local/bin/sutty blazer 1h"
|
|
||||||
every 60 cycles
|
|
||||||
if status != 0 then alert
|
|
||||||
|
|
||||||
check program blazer_1d
|
|
||||||
with path "/usr/local/bin/sutty blazer 1d"
|
|
||||||
every 1440 cycles
|
|
||||||
if status != 0 then alert
|
|
||||||
|
|
||||||
check program blazer
|
|
||||||
with path "/usr/local/bin/sutty blazer"
|
|
||||||
every 61 cycles
|
|
||||||
if status != 0 then alert
|
if status != 0 then alert
|
||||||
|
|
||||||
check program access_logs
|
check program access_logs
|
||||||
|
|
Loading…
Reference in a new issue