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
|
2020-11-26 17:40:03 +00:00
|
|
|
has_many :build_stats, dependent: :destroy
|
2019-07-24 23:51:29 +00:00
|
|
|
|
2022-03-16 21:19:04 +00:00
|
|
|
def deploy(**)
|
2019-07-24 23:51:29 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-04-06 23:07:14 +00:00
|
|
|
def url
|
2019-07-24 23:51:29 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
def size
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2022-04-15 13:39:44 +00:00
|
|
|
# Realizar tareas de limpieza.
|
|
|
|
def cleanup!; end
|
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
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
|
|
|
|
|
2021-12-24 13:08:09 +00:00
|
|
|
# XXX: Ver DeployLocal#bundle
|
2019-09-19 13:20:27 +00:00
|
|
|
def gems_dir
|
2021-04-10 16:17:24 +00:00
|
|
|
@gems_dir ||= Rails.root.join('_storage', 'gems', site.name)
|
2019-09-19 13:20:27 +00:00
|
|
|
end
|
|
|
|
|
2021-04-14 16:09:03 +00:00
|
|
|
# Corre un comando, lo registra en la base de datos y devuelve el
|
|
|
|
# estado.
|
|
|
|
#
|
|
|
|
# @param [String]
|
|
|
|
# @return [Boolean]
|
2022-03-14 16:39:54 +00:00
|
|
|
def run(cmd, output: false)
|
2019-07-26 00:07:53 +00:00
|
|
|
r = nil
|
2021-04-14 16:09:03 +00:00
|
|
|
lines = []
|
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|
|
2021-04-14 16:09:03 +00:00
|
|
|
# TODO: Enviar a un websocket para ver el proceso en vivo?
|
2022-03-14 16:40:17 +00:00
|
|
|
Thread.new do
|
|
|
|
o.each do |line|
|
|
|
|
lines << line
|
|
|
|
|
|
|
|
puts line if output
|
|
|
|
end
|
2022-04-18 20:27:43 +00:00
|
|
|
rescue IOError => e
|
2022-06-29 21:41:51 +00:00
|
|
|
lines << e.message
|
|
|
|
puts e.message if output
|
2021-04-14 16:09:03 +00:00
|
|
|
end
|
2022-04-18 20:27:43 +00:00
|
|
|
|
2022-03-14 16:40:17 +00:00
|
|
|
r = t.value
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
end
|
2019-07-26 00:07:53 +00:00
|
|
|
time_stop
|
|
|
|
|
2021-04-14 16:09:03 +00:00
|
|
|
build_stats.create action: readable_cmd(cmd),
|
|
|
|
log: lines.join,
|
|
|
|
seconds: time_spent_in_seconds,
|
|
|
|
bytes: size,
|
|
|
|
status: r&.success?
|
2019-07-24 23:51:29 +00:00
|
|
|
|
2020-10-04 00:32:32 +00:00
|
|
|
r&.success?
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
2021-04-14 16:09:03 +00:00
|
|
|
|
2022-10-05 21:44:23 +00:00
|
|
|
# Variables de entorno
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def local_env
|
|
|
|
@local_env ||= {}
|
|
|
|
end
|
|
|
|
|
2021-04-14 16:09:03 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# @param [String]
|
|
|
|
# @return [String]
|
|
|
|
def readable_cmd(cmd)
|
|
|
|
cmd.split(' -', 2).first.tr(' ', '_')
|
|
|
|
end
|
2022-10-05 21:44:23 +00:00
|
|
|
|
|
|
|
def deploy_local
|
|
|
|
@deploy_local ||= site.deploys.find_by(type: 'DeployLocal')
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_local_deploys
|
|
|
|
@non_local_deploys ||= site.deploys.where.not(type: 'DeployLocal')
|
|
|
|
end
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|