| 
1995
 | 
     1 local Config = require "site:/private/Config.luan"
 | 
| 
 | 
     2 local Hosted = require "luan:host/Hosted.luan"
 | 
| 
 | 
     3 Hosted.no_security and Hosted.no_security(Config.hosting_password)
 | 
| 
 | 
     4 require "java"
 | 
| 
 | 
     5 local Luan = require "luan:Luan.luan"
 | 
| 
 | 
     6 local error = Luan.error
 | 
| 
 | 
     7 local ipairs = Luan.ipairs or error()
 | 
| 
 | 
     8 local Io = require "luan:Io.luan"
 | 
| 
 | 
     9 local uri = Io.uri or error()
 | 
| 
 | 
    10 local String = require "luan:String.luan"
 | 
| 
 | 
    11 local regex = String.regex or error()
 | 
| 
 | 
    12 local sub_string = String.sub or error()
 | 
| 
 | 
    13 local Http = require "luan:http/Http.luan"
 | 
| 
 | 
    14 local Mail = require "luan:mail/Mail.luan"
 | 
| 
 | 
    15 local Logging = require "luan:logging/Logging.luan"
 | 
| 
 | 
    16 local logger = Logging.logger "Utils"
 | 
| 
 | 
    17 
 | 
| 
 | 
    18 
 | 
| 
 | 
    19 local Utils = {}
 | 
| 
 | 
    20 
 | 
| 
 | 
    21 Utils.domain = Http.domain or "admin.me.luan.software"
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 Utils.server_domain = regex([[^admin\.(\w+\Q.luan.software\E)$]]).matches(Utils.domain) or error "invalid domain"
 | 
| 
 | 
    24 
 | 
| 
 | 
    25 Utils.sites_dir = uri(Http.dir).parent().parent()
 | 
| 
 | 
    26 
 | 
| 
 | 
    27 local send = Mail.sender{
 | 
| 
 | 
    28 	host = "mail.smtp2go.com"
 | 
| 
 | 
    29 	port = 465
 | 
| 
 | 
    30 	username = "luan.admin2"
 | 
| 
 | 
    31 	password = Config.mail_password or error()
 | 
| 
 | 
    32 }.send
 | 
| 
 | 
    33 
 | 
| 
 | 
    34 function Utils.send_mail(mail)
 | 
| 
 | 
    35 	mail.From = mail.From or Utils.domain.."<monitor@luan.software>"
 | 
| 
 | 
    36 	mail.To = mail.To or Config.email_to or error()
 | 
| 
 | 
    37 	send(mail)
 | 
| 
 | 
    38 end
 | 
| 
 | 
    39 
 | 
| 
 | 
    40 function Utils.ssh(host,cmd)
 | 
| 
 | 
    41 	local cmd = "ssh -t -oConnectTimeout=10 -oServerAliveInterval=10 -oBatchMode=yes -oStrictHostKeyChecking=no -p 14299 administrator@"..host.." '"..cmd.."'"
 | 
| 
 | 
    42 	local con = uri("bash:"..cmd)
 | 
| 
 | 
    43 	return con.read_text()
 | 
| 
 | 
    44 end
 | 
| 
 | 
    45 
 | 
| 
 | 
    46 local function last_modified_in_dir(dir)
 | 
| 
 | 
    47 	local rtn = 0
 | 
| 
 | 
    48 	for _, child in ipairs(dir.children()) do
 | 
| 
 | 
    49 		local lm = nil
 | 
| 
 | 
    50 		if child.is_directory() then
 | 
| 
 | 
    51 			lm = last_modified_in_dir(child)
 | 
| 
 | 
    52 		elseif child.is_file() then
 | 
| 
 | 
    53 			lm = child.last_modified()
 | 
| 
 | 
    54 		end
 | 
| 
 | 
    55 		if lm ~= nil and lm > rtn then
 | 
| 
 | 
    56 			rtn = lm
 | 
| 
 | 
    57 		end
 | 
| 
 | 
    58 	end
 | 
| 
 | 
    59 	return rtn
 | 
| 
 | 
    60 end
 | 
| 
 | 
    61 Utils.last_modified_in_dir = last_modified_in_dir
 | 
| 
 | 
    62 
 | 
| 
 | 
    63 
 | 
| 
 | 
    64 local luanhost_logs = uri "site:/private/local/logs/luanhost"
 | 
| 
 | 
    65 if not luanhost_logs.exists() then
 | 
| 
 | 
    66 	local logs = uri("file:logs").canonical()
 | 
| 
 | 
    67 	--logs.mkdir()  -- must exist
 | 
| 
 | 
    68 	if logs.exists() then
 | 
| 
 | 
    69 		logs.symlink_from(luanhost_logs)
 | 
| 
 | 
    70 		logger.info "linked to luanhost logs"
 | 
| 
 | 
    71 	else
 | 
| 
 | 
    72 		logger.error("logs dir doesn't exist")
 | 
| 
 | 
    73 	end
 | 
| 
 | 
    74 end
 | 
| 
 | 
    75 
 | 
| 
 | 
    76 
 | 
| 
 | 
    77 return Utils
 |