From c49c63f77661413a208a47bd7a80f7b4b8d49535 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:11:21 -0300 Subject: [PATCH 1/9] timeout de rsync --- app/models/deploy_rsync.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/deploy_rsync.rb b/app/models/deploy_rsync.rb index 2387366a..5d360c4c 100644 --- a/app/models/deploy_rsync.rb +++ b/app/models/deploy_rsync.rb @@ -77,7 +77,7 @@ class DeployRsync < Deploy # # @return [Boolean] def rsync - run %(rsync -av #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/) + run %(rsync -av --timeout=5 #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/) end # El origen es el destino de la compilación From 8edc9b460af993ad4c52b1192962cb548c39a82a Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:17:55 -0300 Subject: [PATCH 2/9] obtener listado de nodos de un lugar central --- app/models/sutty.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 app/models/sutty.rb diff --git a/app/models/sutty.rb b/app/models/sutty.rb new file mode 100644 index 00000000..7ec8432c --- /dev/null +++ b/app/models/sutty.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# Configuración general de Sutty +class Sutty + extend self + + # Los nodos son otros servidores de Sutty hacia los que se sincronizan + # sitios. + # + # @return [Array] + def nodes + @nodes ||= ENV.fetch('SUTTY_NODES', '').split(',') + end +end From dcbd1c02ac32c1b183d6c90255da693ad67bbc9e Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:26:41 -0300 Subject: [PATCH 3/9] =?UTF-8?q?el=20m=C3=A9todo=20es=20p=C3=BAblico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/deploy_rsync.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/deploy_rsync.rb b/app/models/deploy_rsync.rb index 5d360c4c..50719cd1 100644 --- a/app/models/deploy_rsync.rb +++ b/app/models/deploy_rsync.rb @@ -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 @@ -86,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 From 5fac827ca09fd46231490c59f845b6ec6a2e6121 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:27:15 -0300 Subject: [PATCH 4/9] registrar los cambios --- app/models/deploy_rsync.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/deploy_rsync.rb b/app/models/deploy_rsync.rb index 50719cd1..7733406e 100644 --- a/app/models/deploy_rsync.rb +++ b/app/models/deploy_rsync.rb @@ -84,7 +84,7 @@ class DeployRsync < Deploy # # @return [Boolean] def rsync - run %(rsync -av --timeout=5 #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/) + run %(rsync -avi --timeout=5 #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/) end # El origen es el destino de la compilación From 20915443d84d7ddb50ef8417d957d2a2dbb2dbcd Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:27:52 -0300 Subject: [PATCH 5/9] deprecar las flags por ahora --- app/models/deploy_rsync.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/deploy_rsync.rb b/app/models/deploy_rsync.rb index 7733406e..12f4ce0e 100644 --- a/app/models/deploy_rsync.rb +++ b/app/models/deploy_rsync.rb @@ -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 ssh? && rsync @@ -80,11 +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 - run %(rsync -avi --timeout=5 #{flags ? Shellwords.escape(flags) : ''} #{Shellwords.escape source}/ #{Shellwords.escape destination}/) + run %(rsync -avi --timeout=5 #{Shellwords.escape source}/ #{Shellwords.escape destination}/) end # El origen es el destino de la compilación From a5e90257a30566ecd00fc36a63bf8fcb5e789528 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:38:03 -0300 Subject: [PATCH 6/9] generar un deploy rsync para el sitio con todos los nodos --- app/services/site_service.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 5e2fc706..2ffb8cdd 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -144,4 +144,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 + Sutty.nodes.each do |node| + site.deploys.build(type: 'DeployRsync', destination: "sutty@#{node}:#{site.hostname}") + end + end end From 10d950689b614f6ca2a99ace3cf14dce44b3a037 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 18:39:52 -0300 Subject: [PATCH 7/9] crear rsyncs al crear el sitio --- app/services/site_service.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 2ffb8cdd..89c5796d 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -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 && From a0134cc052578e2ec66025b3d51b2879fc144217 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 19:05:50 -0300 Subject: [PATCH 8/9] =?UTF-8?q?ya=20existe=20un=20lugar=20donde=20guardar?= =?UTF-8?q?=20la=20configuraci=C3=B3n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/sutty.rb | 14 -------------- app/services/site_service.rb | 2 +- config/application.rb | 4 ++++ 3 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 app/models/sutty.rb diff --git a/app/models/sutty.rb b/app/models/sutty.rb deleted file mode 100644 index 7ec8432c..00000000 --- a/app/models/sutty.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -# Configuración general de Sutty -class Sutty - extend self - - # Los nodos son otros servidores de Sutty hacia los que se sincronizan - # sitios. - # - # @return [Array] - def nodes - @nodes ||= ENV.fetch('SUTTY_NODES', '').split(',') - end -end diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 89c5796d..22423bb8 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -148,7 +148,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do # Crea los deploys necesarios para sincronizar a otros nodos de Sutty def sync_nodes - Sutty.nodes.each do |node| + Rails.application.nodes.each do |node| site.deploys.build(type: 'DeployRsync', destination: "sutty@#{node}:#{site.hostname}") end end diff --git a/config/application.rb b/config/application.rb index 7326ae0f..bc948936 100644 --- a/config/application.rb +++ b/config/application.rb @@ -49,5 +49,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 From aa0c359554917ef93547d3446e55ac73394587a8 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 6 Apr 2022 19:06:11 -0300 Subject: [PATCH 9/9] crear un rsync por cada sitio existente --- .../20220406211042_add_deploy_rsync_to_sites.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 db/migrate/20220406211042_add_deploy_rsync_to_sites.rb diff --git a/db/migrate/20220406211042_add_deploy_rsync_to_sites.rb b/db/migrate/20220406211042_add_deploy_rsync_to_sites.rb new file mode 100644 index 00000000..92b6f17b --- /dev/null +++ b/db/migrate/20220406211042_add_deploy_rsync_to_sites.rb @@ -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