File hugo-deployer of Package hugo-compress-site
#!/usr/bin/ruby
#
# License: GNU Affero General Public License v3.0 only
#
require 'fileutils'
require 'logger'
class DeployHugo
def initialize
@loglog = Logger.new(STDERR)
@loglog.level = Logger::INFO
# @loglog.datetime_format = ''
ssh_dir_path = File.expand_path("~/.ssh")
@known_hosts_path = "#{ssh_dir_path}/deployer_known_hosts"
@key_file_path = "#{ssh_dir_path}/deployer_id_ed25519"
old_umask = File.umask(0077)
if FileUtils.mkdir_p(ssh_dir_path)
@known_hosts_path = write_env_var_to_file('DEPLOY_KNOWN_HOSTS', @known_hosts_path, 1)
@key_file_path = write_env_var_to_file('DEPLOY_KEY', @key_file_path, 2)
deploy_target = envvar_or_error_out('DEPLOY_TARGET', 3)
unless(system("hugo-compress-site public"))
fail_with_message("Compressing the site failed", 4)
end
permissions='u=rwX,go=rX'
@loglog.info("Fixing permissions do #{permissions} ...")
FileUtils.chmod_R(permissions, "public", verbose: true)
@loglog.info("Running rsync ...")
exit(system("rsync --archive --update --human-readable --delete-after -e 'ssh -o UserKnownHostsFile=#{@known_hosts_path} -i #{@key_file_path} -o LogLevel=error' public/ #{deploy_target}/"))
end
end
def write_env_var_to_file(envvar, path, code)
@loglog.info("Writing #{path} ...")
value=envvar_or_error_out(envvar, code)
if File.exist?(value)
@loglog.info("The path #{path} is already a file switching from #{path} to #{value}")
return value
end
value_length = value.length
written_bytes_count = File.write(path, value)
if value_length != written_bytes_count
fail_with_message("Only wrote #{written_bytes_count} instead of expected #{value_length} to #{path}. Exiting ...")
end
path
end
def envvar_or_error_out(envvar, code=1)
return ENV[envvar] if ENV.include?(envvar)
fail_with_message("env var #{envvar} missing. Exiting ...")
end
def fail_with_message(message, code)
@loglog.error(message)
exit(code)
end
end
DeployHugo.new