2019-07-24 23:51:29 +00:00
|
|
|
# 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
|
2019-07-26 00:07:53 +00:00
|
|
|
has_many :build_stats
|
2019-07-24 23:51:29 +00:00
|
|
|
|
|
|
|
def deploy
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
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
|
|
|
|
Pathname.new(File.join(home_dir, '..', 'gems')).realpath.to_s
|
|
|
|
end
|
|
|
|
|
2019-07-24 23:51:29 +00:00
|
|
|
# Corre un comando y devuelve true si terminó correctamente
|
2019-07-26 00:07:53 +00:00
|
|
|
# rubocop:disable Metrics/MethodLength
|
2019-07-24 23:51:29 +00:00
|
|
|
def run(cmd)
|
2019-07-26 00:07:53 +00:00
|
|
|
# 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(' ', '_')
|
2019-07-26 00:07:53 +00:00
|
|
|
r = nil
|
2019-07-24 23:51:29 +00:00
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
time_start
|
2019-07-24 23:51:29 +00:00
|
|
|
Dir.chdir(site.path) do
|
2019-07-26 00:07:53 +00:00
|
|
|
Open3.popen2e(env, cmd, unsetenv_others: true) do |_, o, t|
|
2019-07-24 23:51:29 +00:00
|
|
|
r = t.value
|
2019-07-26 00:07:53 +00:00
|
|
|
stat.log = o.read
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-26 00:07:53 +00:00
|
|
|
time_stop
|
|
|
|
|
|
|
|
stat.seconds = time_spent_in_seconds
|
|
|
|
stat.bytes = size
|
|
|
|
stat.save
|
2019-07-24 23:51:29 +00:00
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
r.try :exited?
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
2019-07-26 00:07:53 +00:00
|
|
|
# rubocop:enable Metrics/MethodLength
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|