2019-07-24 23:51:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# Alojamiento local, genera el sitio como si corriéramos `jekyll build`.
|
2019-07-24 23:51:29 +00:00
|
|
|
class DeployLocal < Deploy
|
2021-08-02 00:51:44 +00:00
|
|
|
# Asegurarse que el hostname es el permitido.
|
2021-08-14 23:21:30 +00:00
|
|
|
before_validation :reset_hostname!, :default_hostname!
|
2021-08-14 23:27:08 +00:00
|
|
|
# Actualiza el hostname con www si cambiamos el hostname
|
|
|
|
before_update :update_deploy_www!, if: :hostname_changed?
|
2019-07-24 23:51:29 +00:00
|
|
|
|
|
|
|
# Realizamos la construcción del sitio usando Jekyll y un entorno
|
|
|
|
# limpio para no pasarle secretos
|
|
|
|
#
|
|
|
|
# Pasamos variables de entorno mínimas para no filtrar secretos de
|
|
|
|
# Sutty
|
|
|
|
def deploy
|
2020-01-24 15:12:49 +00:00
|
|
|
return false unless mkdir
|
|
|
|
return false unless yarn
|
|
|
|
return false unless bundle
|
|
|
|
|
|
|
|
jekyll_build
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
|
2019-07-26 00:07:53 +00:00
|
|
|
# Obtener el tamaño de todos los archivos y directorios (los
|
|
|
|
# directorios son archivos :)
|
2021-08-02 00:51:44 +00:00
|
|
|
#
|
|
|
|
# @return [Integer]
|
2019-07-26 00:07:53 +00:00
|
|
|
def size
|
|
|
|
paths = [destination, File.join(destination, '**', '**')]
|
|
|
|
|
|
|
|
Dir.glob(paths).map do |file|
|
2021-08-02 00:51:44 +00:00
|
|
|
File.symlink?(file) ? 0 : File.size(file)
|
2019-07-26 00:07:53 +00:00
|
|
|
end.inject(:+)
|
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# El hostname es el nombre del sitio más el dominio principal.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def default_hostname
|
|
|
|
"#{site.name}.#{Site.domain}"
|
2019-09-25 16:05:18 +00:00
|
|
|
end
|
|
|
|
|
2019-07-24 23:51:29 +00:00
|
|
|
private
|
|
|
|
|
2021-08-07 21:26:52 +00:00
|
|
|
def reset_hostname!
|
|
|
|
self.hostname = nil
|
|
|
|
end
|
|
|
|
|
2021-08-14 23:27:08 +00:00
|
|
|
# XXX: En realidad el DeployWww debería regenerar su propio hostname.
|
|
|
|
def update_deploy_www!
|
|
|
|
site.deploys.where(type: 'DeployWww').map do |www|
|
|
|
|
www.update hostname: www.default_hostname
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# Crea el directorio destino si no existe.
|
2019-09-16 16:15:20 +00:00
|
|
|
def mkdir
|
|
|
|
FileUtils.mkdir_p destination
|
|
|
|
end
|
|
|
|
|
2019-07-24 23:51:29 +00:00
|
|
|
# Un entorno que solo tiene lo que necesitamos
|
2021-08-02 00:51:44 +00:00
|
|
|
#
|
|
|
|
# @return [Hash]
|
2019-07-24 23:51:29 +00:00
|
|
|
def env
|
2019-07-26 00:07:53 +00:00
|
|
|
# XXX: This doesn't support Windows paths :B
|
2019-09-20 13:31:46 +00:00
|
|
|
paths = [File.dirname(`which bundle`), '/usr/bin', '/bin']
|
2019-07-24 23:51:29 +00:00
|
|
|
|
2019-09-19 13:20:27 +00:00
|
|
|
{
|
2021-08-02 00:51:44 +00:00
|
|
|
'HOME' => site.path,
|
2019-09-19 13:20:27 +00:00
|
|
|
'PATH' => paths.join(':'),
|
2021-04-14 21:22:17 +00:00
|
|
|
'SPREE_API_KEY' => site.tienda_api_key,
|
|
|
|
'SPREE_URL' => site.tienda_url,
|
2021-01-25 19:05:35 +00:00
|
|
|
'AIRBRAKE_PROJECT_ID' => site.id.to_s,
|
|
|
|
'AIRBRAKE_PROJECT_KEY' => site.airbrake_api_key,
|
2020-01-24 15:12:49 +00:00
|
|
|
'JEKYLL_ENV' => Rails.env,
|
2020-05-27 22:03:27 +00:00
|
|
|
'LANG' => ENV['LANG']
|
2019-09-19 13:20:27 +00:00
|
|
|
}
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# @return [String]
|
2019-07-24 23:51:29 +00:00
|
|
|
def yarn_lock
|
|
|
|
File.join(site.path, 'yarn.lock')
|
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# Determina si este proyecto se gestiona con Yarn, buscando si el
|
|
|
|
# archivo yarn.lock existe.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2019-07-24 23:51:29 +00:00
|
|
|
def yarn_lock?
|
|
|
|
File.exist? yarn_lock
|
|
|
|
end
|
|
|
|
|
2020-01-24 15:12:49 +00:00
|
|
|
def gem
|
|
|
|
run %(gem install bundler --no-document)
|
|
|
|
end
|
|
|
|
|
2019-07-24 23:51:29 +00:00
|
|
|
# Corre yarn dentro del repositorio
|
2021-08-02 00:51:44 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean,Nil]
|
2019-07-24 23:51:29 +00:00
|
|
|
def yarn
|
2021-08-07 18:55:00 +00:00
|
|
|
return true unless yarn_lock?
|
2019-07-24 23:51:29 +00:00
|
|
|
|
|
|
|
run 'yarn'
|
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# Instala las dependencias.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2019-07-24 23:51:29 +00:00
|
|
|
def bundle
|
2020-10-04 00:44:54 +00:00
|
|
|
if Rails.env.production?
|
|
|
|
run %(bundle install --no-cache --path="#{gems_dir}")
|
|
|
|
else
|
|
|
|
run %(bundle install)
|
|
|
|
end
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
|
2021-08-02 00:51:44 +00:00
|
|
|
# Genera el sitio.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2019-07-24 23:51:29 +00:00
|
|
|
def jekyll_build
|
2021-04-14 16:09:03 +00:00
|
|
|
run %(bundle exec jekyll build --trace --profile --destination "#{escaped_destination}")
|
2019-07-24 23:51:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# no debería haber espacios ni caracteres especiales, pero por si
|
|
|
|
# acaso...
|
|
|
|
def escaped_destination
|
|
|
|
Shellwords.escape destination
|
|
|
|
end
|
|
|
|
|
|
|
|
# Eliminar el destino si se elimina el deploy
|
|
|
|
def remove_destination!
|
|
|
|
FileUtils.rm_rf destination
|
|
|
|
end
|
|
|
|
end
|