view host/admin/src/private/lib/Utils.luan @ 1995:301a6561fb6b

add host/admin
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 04 Jul 2025 10:25:38 -0600
parents
children
line wrap: on
line source

local Config = require "site:/private/Config.luan"
local Hosted = require "luan:host/Hosted.luan"
Hosted.no_security and Hosted.no_security(Config.hosting_password)
require "java"
local Luan = require "luan:Luan.luan"
local error = Luan.error
local ipairs = Luan.ipairs or error()
local Io = require "luan:Io.luan"
local uri = Io.uri or error()
local String = require "luan:String.luan"
local regex = String.regex or error()
local sub_string = String.sub or error()
local Http = require "luan:http/Http.luan"
local Mail = require "luan:mail/Mail.luan"
local Logging = require "luan:logging/Logging.luan"
local logger = Logging.logger "Utils"


local Utils = {}

Utils.domain = Http.domain or "admin.me.luan.software"

Utils.server_domain = regex([[^admin\.(\w+\Q.luan.software\E)$]]).matches(Utils.domain) or error "invalid domain"

Utils.sites_dir = uri(Http.dir).parent().parent()

local send = Mail.sender{
	host = "mail.smtp2go.com"
	port = 465
	username = "luan.admin2"
	password = Config.mail_password or error()
}.send

function Utils.send_mail(mail)
	mail.From = mail.From or Utils.domain.."<monitor@luan.software>"
	mail.To = mail.To or Config.email_to or error()
	send(mail)
end

function Utils.ssh(host,cmd)
	local cmd = "ssh -t -oConnectTimeout=10 -oServerAliveInterval=10 -oBatchMode=yes -oStrictHostKeyChecking=no -p 14299 administrator@"..host.." '"..cmd.."'"
	local con = uri("bash:"..cmd)
	return con.read_text()
end

local function last_modified_in_dir(dir)
	local rtn = 0
	for _, child in ipairs(dir.children()) do
		local lm = nil
		if child.is_directory() then
			lm = last_modified_in_dir(child)
		elseif child.is_file() then
			lm = child.last_modified()
		end
		if lm ~= nil and lm > rtn then
			rtn = lm
		end
	end
	return rtn
end
Utils.last_modified_in_dir = last_modified_in_dir


local luanhost_logs = uri "site:/private/local/logs/luanhost"
if not luanhost_logs.exists() then
	local logs = uri("file:logs").canonical()
	--logs.mkdir()  -- must exist
	if logs.exists() then
		logs.symlink_from(luanhost_logs)
		logger.info "linked to luanhost logs"
	else
		logger.error("logs dir doesn't exist")
	end
end


return Utils