2022-10-05 21:44:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-20 23:34:27 +00:00
|
|
|
require 'distributed_press/v1/client/auth'
|
|
|
|
require 'distributed_press/v1/client/site'
|
|
|
|
|
|
|
|
# Soportar Distributed Press APIv1
|
|
|
|
#
|
|
|
|
# Usa tokens de publicación efímeros para todas las acciones.
|
|
|
|
#
|
|
|
|
# Al ser creado, genera el sitio en la instancia de Distributed Press
|
|
|
|
# configurada y almacena el ID.
|
2022-10-05 21:44:23 +00:00
|
|
|
#
|
2023-01-20 23:34:27 +00:00
|
|
|
# Al ser publicado, envía los archivos en un tarball y actualiza la
|
|
|
|
# información.
|
2022-10-05 21:44:23 +00:00
|
|
|
class DeployDistributedPress < Deploy
|
2023-01-20 23:34:27 +00:00
|
|
|
store :values, accessors: %i[hostname remote_site_id remote_info], coder: JSON
|
|
|
|
|
|
|
|
before_create :create_remote_site!
|
|
|
|
|
|
|
|
# Actualiza la información y luego envía los cambios
|
|
|
|
#
|
|
|
|
# @param :output [Bool]
|
|
|
|
# @return [Bool]
|
|
|
|
def deploy
|
|
|
|
status = false
|
2023-02-08 20:08:02 +00:00
|
|
|
log = []
|
|
|
|
|
|
|
|
time_start
|
2023-01-20 23:34:27 +00:00
|
|
|
|
|
|
|
site_client.tap do |c|
|
2023-02-08 20:08:02 +00:00
|
|
|
stdout = Thread.new(publisher.logger_out) do |io|
|
|
|
|
until io.eof?
|
|
|
|
line = io.gets
|
|
|
|
|
|
|
|
puts line if output
|
|
|
|
log << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-20 23:34:27 +00:00
|
|
|
update remote_info: c.show(publishing_site).to_h
|
|
|
|
|
|
|
|
status = c.publish(publishing_site, deploy_local.destination)
|
2023-02-08 20:08:02 +00:00
|
|
|
|
|
|
|
publisher.logger.close
|
|
|
|
stdout.join
|
2023-01-20 23:34:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
time_stop
|
|
|
|
|
2023-02-08 20:08:02 +00:00
|
|
|
create_stat! status, log.join
|
2022-10-05 21:44:23 +00:00
|
|
|
|
2023-01-20 23:34:27 +00:00
|
|
|
status
|
|
|
|
end
|
2022-10-05 21:44:23 +00:00
|
|
|
|
|
|
|
def limit; end
|
|
|
|
|
|
|
|
def size
|
|
|
|
deploy_local.size
|
|
|
|
end
|
|
|
|
|
2023-01-20 23:34:27 +00:00
|
|
|
def destination; end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# El cliente de la API
|
|
|
|
#
|
|
|
|
# TODO: cuando soportemos más, tiene que haber una relación entre
|
|
|
|
# DeployDistributedPress y DistributedPressPublisher.
|
|
|
|
#
|
|
|
|
# @return [DistributedPressPublisher]
|
|
|
|
def publisher
|
|
|
|
@publisher ||= DistributedPressPublisher.first
|
2022-10-05 21:44:23 +00:00
|
|
|
end
|
|
|
|
|
2023-01-20 23:34:27 +00:00
|
|
|
# El cliente para actualizar el sitio
|
|
|
|
#
|
|
|
|
# @return [DistributedPress::V1::Client::Site]
|
|
|
|
def site_client
|
2023-01-23 21:39:10 +00:00
|
|
|
DistributedPress::V1::Client::Site.new(publisher.client)
|
2023-01-20 23:34:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Genera el esquema de datos para poder publicar el sitio
|
|
|
|
#
|
|
|
|
# @return [DistributedPress::V1::Schemas::PublishingSite]
|
|
|
|
def publishing_site
|
|
|
|
DistributedPress::V1::Schemas::PublishingSite.new.call(id: remote_site_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Genera el esquema de datos para crear el sitio
|
|
|
|
#
|
|
|
|
# @return [DistributedPressPublisher::V1::Schemas::NewSite]
|
|
|
|
def create_site
|
|
|
|
DistributedPress::V1::Schemas::NewSite.new.call(domain: hostname, protocols: { http: true, ipfs: true, hyper: true })
|
|
|
|
end
|
|
|
|
|
|
|
|
# Crea el sitio en la instancia con el hostname especificado
|
|
|
|
#
|
|
|
|
# @return [nil]
|
|
|
|
def create_remote_site!
|
|
|
|
created_site = site_client.create(create_site)
|
|
|
|
|
|
|
|
self.remote_site_id = created_site[:id]
|
|
|
|
self.remote_info = created_site.to_h
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Registra lo que sucedió
|
|
|
|
#
|
|
|
|
# @param status [Bool]
|
2023-02-08 20:08:02 +00:00
|
|
|
# @param log [String]
|
2023-01-20 23:34:27 +00:00
|
|
|
# @return [nil]
|
2023-02-08 20:08:02 +00:00
|
|
|
def create_stat!(status, log)
|
|
|
|
build_stats.create action: publisher.to_s,log: log, seconds: time_spent_in_seconds, bytes: size, status: status
|
2023-01-20 23:34:27 +00:00
|
|
|
nil
|
|
|
|
end
|
2022-10-05 21:44:23 +00:00
|
|
|
end
|