sutty/app/models/deploy.rb

86 lines
1.7 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
def deploy
raise NotImplementedError
end
def limit
raise NotImplementedError
end
def size
raise NotImplementedError
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
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)
r = nil
lines = []
time_start
Dir.chdir(site.path) do
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, o, t|
r = t.value
# XXX: Tenemos que leer línea por línea porque en salidas largas
# se cuelga la IO
# TODO: Enviar a un websocket para ver el proceso en vivo?
o.each do |line|
lines << line
end
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
private
# @param [String]
# @return [String]
def readable_cmd(cmd)
cmd.split(' -', 2).first.tr(' ', '_')
end
end