# 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 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 ENV.fetch('GEMS_DIR', Pathname.new(File.join(home_dir, '..', 'gems')).realpath.to_s) end # Corre un comando y devuelve true si terminó correctamente # rubocop:disable Metrics/MethodLength def run(cmd) # XXX: prestar atención a la concurrencia de sqlite3, se podría # enviar los datos directamente a una API para que se manejen desde # el proceso principal de rails y evitar problemas. stat = build_stats.build action: cmd.split(' -', 2).first.tr(' ', '_') r = nil time_start Dir.chdir(site.path) do Open3.popen2e(env, cmd, unsetenv_others: true) do |_, o, t| r = t.value stat.log = o.read end end time_stop stat.seconds = time_spent_in_seconds stat.bytes = size stat.save r.try :success? end # rubocop:enable Metrics/MethodLength end