mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-28 16:06:21 +00:00
65 lines
1.4 KiB
Ruby
65 lines
1.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
|
|
|
|
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.
|
|
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
|