5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 00:36:21 +00:00
panel/app/models/deploy.rb

126 lines
2.4 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require 'open3'
# Este modelo implementa los distintos tipos de alojamiento que provee
# Sutty.
#
# Los datos se guardan en la tabla `deploys`. Para guardar los
# atributos, cada modelo tiene que definir su propio `store
# :attributes`.
class Deploy < ApplicationRecord
belongs_to :site
has_many :build_stats, dependent: :destroy
DEPENDENCIES = []
2023-03-27 17:44:22 +00:00
SOFT_DEPENDENCIES = []
def deploy(**)
raise NotImplementedError
end
def url
raise NotImplementedError
end
def limit
raise NotImplementedError
end
def size
raise NotImplementedError
end
# Realizar tareas de limpieza.
def cleanup!; end
def time_start
@start = Time.now
end
def time_stop
@stop = Time.now
end
def time_spent_in_seconds
(@stop - @start).round(3)
end
2019-09-19 13:20:27 +00:00
def home_dir
site.path
end
2021-12-24 13:08:09 +00:00
# XXX: Ver DeployLocal#bundle
2019-09-19 13:20:27 +00:00
def gems_dir
2021-04-10 16:17:24 +00:00
@gems_dir ||= Rails.root.join('_storage', 'gems', site.name)
2019-09-19 13:20:27 +00:00
end
# Corre un comando, lo registra en la base de datos y devuelve el
# estado.
#
# @param [String]
# @return [Boolean]
2022-03-14 16:39:54 +00:00
def run(cmd, output: false)
r = nil
lines = []
time_start
Dir.chdir(site.path) do
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, o, t|
# TODO: Enviar a un websocket para ver el proceso en vivo?
Thread.new do
o.each do |line|
lines << line
puts line if output
end
2022-04-18 20:27:43 +00:00
rescue IOError => e
2022-06-29 21:41:51 +00:00
lines << e.message
puts e.message if output
end
2022-04-18 20:27:43 +00:00
r = t.value
end
end
time_stop
build_stats.create action: readable_cmd(cmd),
log: lines.join,
seconds: time_spent_in_seconds,
bytes: size,
status: r&.success?
2020-10-04 00:32:32 +00:00
r&.success?
end
2022-10-05 21:44:23 +00:00
# Variables de entorno
#
# @return [Hash]
def local_env
@local_env ||= {}
end
# Trae todas las dependencias
#
# @return [Array]
def self.all_dependencies
self::DEPENDENCIES | self::SOFT_DEPENDENCIES
end
private
# @param [String]
# @return [String]
def readable_cmd(cmd)
cmd.split(' -', 2).first.tr(' ', '_')
end
2022-10-05 21:44:23 +00:00
def deploy_local
@deploy_local ||= site.deploys.find_by(type: 'DeployLocal')
end
def non_local_deploys
@non_local_deploys ||= site.deploys.where.not(type: 'DeployLocal')
end
end