5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 22:06:08 +00:00
panel/app/models/deploy.rb

66 lines
1.4 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
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
# Corre un comando y devuelve true si terminó correctamente
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
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.
2019-08-02 00:20:42 +00:00
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 :exited?
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
end