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

102 lines
1.9 KiB
Ruby
Raw 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 = []
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-04-18 20:27:43 +00:00
ExceptionNotifier.notify(e, data: { site: site.name })
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
private
# @param [String]
# @return [String]
def readable_cmd(cmd)
cmd.split(' -', 2).first.tr(' ', '_')
end
end