mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-26 01:56:22 +00:00
125 lines
2.4 KiB
Ruby
125 lines
2.4 KiB
Ruby
# 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 = []
|
|
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
|
|
|
|
def home_dir
|
|
site.path
|
|
end
|
|
|
|
# XXX: Ver DeployLocal#bundle
|
|
def gems_dir
|
|
@gems_dir ||= Rails.root.join('_storage', 'gems', site.name)
|
|
end
|
|
|
|
# Corre un comando, lo registra en la base de datos y devuelve el
|
|
# estado.
|
|
#
|
|
# @param [String]
|
|
# @return [Boolean]
|
|
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
|
|
rescue IOError => e
|
|
lines << e.message
|
|
puts e.message if output
|
|
end
|
|
|
|
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?
|
|
|
|
r&.success?
|
|
end
|
|
|
|
# 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
|
|
|
|
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
|