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

72 lines
1.5 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
2019-09-19 13:20:27 +00:00
def home_dir
site.path
end
def gems_dir
2020-10-04 00:44:54 +00:00
ENV.fetch('GEMS_DIR', Pathname.new(File.join(home_dir, '..', 'gems')).realpath.to_s)
2019-09-19 13:20:27 +00:00
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.
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
2020-10-04 00:32:32 +00:00
r&.success?
end
# rubocop:enable Metrics/MethodLength
end