mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-23 08:36:22 +00:00
Merge branch 'deploy-rsync' into panel.sutty.nl
This commit is contained in:
commit
7e88e9b7e9
4 changed files with 38 additions and 11 deletions
|
@ -3,7 +3,7 @@
|
|||
# Sincroniza sitios a servidores remotos usando Rsync. El servidor
|
||||
# remoto tiene que tener rsync instalado.
|
||||
class DeployRsync < Deploy
|
||||
store :values, accessors: %i[flags destination host_keys], coder: JSON
|
||||
store :values, accessors: %i[destination host_keys], coder: JSON
|
||||
|
||||
def deploy(output: false)
|
||||
ssh? && rsync(output: output)
|
||||
|
@ -16,6 +16,13 @@ class DeployRsync < Deploy
|
|||
0
|
||||
end
|
||||
|
||||
# Devolver el destino o lanzar un error si no está configurado
|
||||
def destination
|
||||
values[:destination].tap do |d|
|
||||
raise(ArgumentError, 'destination no está configurado') if d.blank?
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Verificar la conexión SSH implementando Trust On First Use
|
||||
|
@ -73,12 +80,11 @@ class DeployRsync < Deploy
|
|||
end
|
||||
end
|
||||
|
||||
# Sincroniza hacia el directorio remoto, usando las flags opcionales.
|
||||
# Sincroniza hacia el directorio remoto
|
||||
#
|
||||
# @return [Boolean]
|
||||
def rsync(output: false)
|
||||
run %(rsync -av #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/),
|
||||
output: output
|
||||
run %(rsync -avi --timeout=5 #{Shellwords.escape source}/ #{Shellwords.escape destination}/), output: output
|
||||
end
|
||||
|
||||
# El origen es el destino de la compilación
|
||||
|
@ -87,11 +93,4 @@ class DeployRsync < Deploy
|
|||
def source
|
||||
site.deploys.find_by(type: 'DeployLocal').destination
|
||||
end
|
||||
|
||||
# Devolver el destino o lanzar un error si no está configurado
|
||||
def destination
|
||||
values[:destination].tap do |d|
|
||||
raise(ArgumentError, 'destination no está configurado') if d.blank?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,6 +9,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
|
|||
self.site = Site.new params
|
||||
|
||||
add_role temporal: false, rol: 'usuarie'
|
||||
sync_nodes
|
||||
|
||||
I18n.with_locale(usuarie&.lang&.to_sym || I18n.default_locale) do
|
||||
site.save &&
|
||||
|
@ -144,4 +145,11 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
|
|||
PostService.new(site: site, usuarie: usuarie, post: post,
|
||||
params: params).update
|
||||
end
|
||||
|
||||
# Crea los deploys necesarios para sincronizar a otros nodos de Sutty
|
||||
def sync_nodes
|
||||
Rails.application.nodes.each do |node|
|
||||
site.deploys.build(type: 'DeployRsync', destination: "sutty@#{node}:#{site.hostname}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -55,5 +55,9 @@ module Sutty
|
|||
EmailAddress::Config.error_messages translations.transform_keys(&:to_s), locale.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def nodes
|
||||
@nodes ||= ENV.fetch('SUTTY_NODES', '').split(',')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
16
db/migrate/20220406211042_add_deploy_rsync_to_sites.rb
Normal file
16
db/migrate/20220406211042_add_deploy_rsync_to_sites.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Agrega un DeployRsync hacia los servidores alternativos para cada
|
||||
# sitio
|
||||
class AddDeployRsyncToSites < ActiveRecord::Migration[6.1]
|
||||
def up
|
||||
Site.find_each do |site|
|
||||
SiteService.new(site: site).send :sync_nodes
|
||||
site.save
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
DeployRsync.destroy_all
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue