5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-05 21:55:46 +00:00

Merge branch 'distributed-press' into panel.sutty.nl

This commit is contained in:
f 2023-01-23 18:40:15 -03:00
commit bb8acfa917
2 changed files with 96 additions and 39 deletions

View file

@ -1,13 +1,43 @@
# frozen_string_literal: true
# Soportar Distributed Press APIv0
#
# No se realiza ninguna acción porque el deploy se hace desde el plugin
# local.
class DeployDistributedPress < Deploy
store :values, accessors: %i[api_url api_key hostname], coder: JSON
require 'distributed_press/v1/client/auth'
require 'distributed_press/v1/client/site'
def deploy(**); end
# 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.
#
# Al ser publicado, envía los archivos en un tarball y actualiza la
# información.
class DeployDistributedPress < Deploy
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(output: true)
time_start
status = false
site_client.tap do |c|
update remote_info: c.show(publishing_site).to_h
status = c.publish(publishing_site, deploy_local.destination)
end
time_stop
create_stat! status
status
end
def limit; end
@ -15,21 +45,59 @@ class DeployDistributedPress < Deploy
deploy_local.size
end
# TODO: Devolver hyper:// y otras
def url
"ipfs://#{hostname}/"
end
# Devuelve variables de entorno para enviarle a DeployLocal
#
# @return [Hash]
def local_env
{
'DISTRIBUTED_PRESS_PROJECT_DOMAIN' => hostname,
'DISTRIBUTED_PRESS_API_KEY' => api_key,
'DISTRIBUTED_PRESS_API_URL' => api_url
}
end
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
end
# El cliente para actualizar el sitio
#
# @return [DistributedPress::V1::Client::Site]
def site_client
DistributedPress::V1::Client::Site.new(publisher.client)
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]
# @return [nil]
def create_stat!(status)
build_stats.create action: publisher.to_s, seconds: time_spent_in_seconds, bytes: size, status: status
nil
end
end

View file

@ -22,22 +22,6 @@ class DistributedPressPublisher < ApplicationRecord
where('expires_at > ? and expires_at < ?', Time.now, Time.now + 1.hour)
}
# Al cambiar el token genera un cliente nuevo
#
# @return [String]
def token=(new_token)
@client = nil
super
end
# Al cambiar la instancia genera un cliente nuevo
#
# @return [String]
def instance=(new_instance)
@client = nil
super
end
# Instancia un cliente de Distributed Press a partir del token. Al
# cargar un token a punto de vencer se renueva automáticamente.
#
@ -46,6 +30,11 @@ class DistributedPressPublisher < ApplicationRecord
@client ||= DistributedPress::V1::Client.new(url: instance, token: token)
end
# @return [String]
def to_s
"Distributed Press <#{instance}>"
end
private
# Actualiza o desactiva la fecha de vencimiento a partir de la