changeset 1628:520707a70379

add host
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 07 Dec 2021 23:29:58 -0700
parents 07be5015159d
children 124887e02dab
files .hgignore host/Config.luan host/check_app.sh host/init.luan host/restart.sh host/run.luan host/serve.sh host/start.sh host/stop.sh host/update.sh
diffstat 10 files changed, 257 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Tue Dec 07 22:15:16 2021 -0700
+++ b/.hgignore	Tue Dec 07 23:29:58 2021 -0700
@@ -10,3 +10,5 @@
 local/
 luan-*.tar
 .DS_Store
+host/sites/
+host/logs/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/Config.luan	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,60 @@
+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 Package = require "luan:Package.luan"
+local load = Package.load or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Config"
+
+
+uri("file:local").mkdir()
+local password_file = uri("file:local/password.txt")
+local password = password_file.exists() and password_file.read_text() or "password"
+local site_config = nil
+local site_name
+for _, site_dir in ipairs( uri("file:sites").children() or {} ) do
+	local name = site_dir.name()
+	local config = load("file:sites/"..name.."/site/private/host/Config.luan")
+	if config == false then
+		continue
+	end
+	if config.password ~= password and config.old_password ~= password then
+		logger.warn("wrong password for "..name)
+		continue
+	end
+	if site_config == nil then
+		site_config = config
+		site_name = name
+	else
+		logger.error("conflicting Config in "..name.." and "..site_name)
+	end
+end
+if site_config ~= nil then
+	logger.info("using Config from "..site_name)
+	local site_password = site_config.password or error()
+	if site_password ~= password then
+		password_file.write_text(password)
+	end
+	return site_config
+end
+if password ~= "password" then
+	password_file.write_text("password")
+end
+
+
+local Config = {}
+
+Config.password = "password"
+
+--[[  an example
+Config.postgres = {
+	class = "org.postgresql.Driver"
+	url = "jdbc:postgresql://localhost:5432/postgres"
+	user = "someone"
+	password = "password"
+}
+]]
+
+return Config
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/check_app.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,23 @@
+PIDS="";
+
+function getPids() {
+  PIDS=$(ps ax | awk '{if($0~"[r]un.luan"){print $1}}');
+}
+
+function checkStopped() {
+  getPids;
+  if [ "$PIDS" != "" ]; then
+    echo "App already running";
+    exit 0;
+  fi;
+}
+
+function checkRunning() {
+  getPids;
+  if [ "$PIDS" != "" ]; then
+    echo "App running";
+  else
+    echo "App not running";
+    exit 0;
+  fi;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/init.luan	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,4 @@
+-- run for each site
+
+local Luan = require "luan:Luan.luan"
+--Luan.do_file "file:https.luan"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/restart.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+set -e
+
+cd `dirname $0`;
+
+if [ "$1" == "monitoring" ]; then
+	if [ ! -f "started.lock" ]; then
+		echo "stopped with stop script";
+		exit 0;
+    fi;
+fi;
+./stop.sh;
+./start.sh;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/run.luan	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,50 @@
+require "java"
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local ipairs = Luan.ipairs or error()
+local Number = require "luan:Number.luan"
+local long = Number.long or error()
+local Logging = require "luan:logging/Logging.luan"
+
+local log_to_console = false
+for _, arg in ipairs{...} do
+	if arg == "console" then
+		log_to_console = true
+	end
+end
+if not log_to_console then
+	local LuanLogger = require "java:luan.modules.logging.LuanLogger"
+	local Layouts = require "java:goodjava.logger.Layouts"
+	local DateLayout = require "java:goodjava.logger.DateLayout"
+	local ListLayout = require "java:goodjava.logger.ListLayout"
+	local Level = require "java:goodjava.logger.Level"
+	local RollingFileAppender = require "java:goodjava.logger.RollingFileAppender"
+	local LevelAppender = require "java:goodjava.logger.LevelAppender"
+	local ListAppender = require "java:goodjava.logger.ListAppender"
+
+	local one_mb = long(1024*1024)
+	local layout = ListLayout.new(DateLayout.new("yyyy-MM-dd HH:mm:ss,SSS")," ",Layouts.LEVEL_PADDED," ",Layouts.LOGGER," - ",Layouts.MESSAGE,"\n",Layouts.THROWABLE)
+
+	local function new_appender(file,level)
+		local appender = RollingFileAppender.new(layout, file)
+		appender.maxFileSize = one_mb
+		appender = LevelAppender.new(appender,level)
+		return appender
+	end
+
+	local err = new_appender("logs/luan_error.log",Level.ERROR)
+	local warn = new_appender("logs/luan_warn.log",Level.WARN)
+	local info = new_appender("logs/luan_info.log",Level.INFO)
+	local appender = ListAppender.new(err,warn,info)
+	LuanLogger.configure(appender)
+end
+
+local logger = Logging.logger "luanhost-run"
+
+
+local do_file = Luan.do_file or error()
+local WebHandler = require "java:luan.host.WebHandler"
+
+WebHandler.config = require "file:Config.luan"
+
+do_file "classpath:luan/host/run.luan"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/serve.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -e
+
+. check_app.sh
+
+checkStopped;
+
+luan run.luan console 2>&1 | tee err
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/start.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+set -e
+
+#seconds to wait
+toWait=10;
+i=0;
+
+. check_app.sh;
+checkStopped;
+
+. luan string:;
+
+mkdir -p logs 2>/dev/null;
+
+touch started.lock;
+
+if [ "$1" == "launchd" ]; then
+  java -Xms1024M -classpath $CLASSPATH luan.Luan run.luan logs 1>logs/stdout.log 2>logs/stderr.log
+else
+  java -Xms1024M -classpath $CLASSPATH luan.Luan run.luan logs 1>logs/stdout.log 2>logs/stderr.log &
+
+  getPids;
+  if [ "$PIDS" == "" ]; then
+    while [ $i -lt $toWait ]; do
+      sleep 1;
+      getPids;
+      if [ "$PIDS" != "" ]; then
+        break
+      fi;
+      let i=$i+1;
+    done;
+  fi;
+
+  checkRunning;
+fi;
+
+#sudo /usr/local/bin/nginx -c $(pwd)/local/nginx.conf;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/stop.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+. check_app.sh;
+
+#seconds to wait
+toWait=30;
+i=0;
+
+getPids;
+
+if [ "$PIDS" != "" ]; then
+  while [ $i -lt $toWait ]; do
+    kill -TERM $PIDS;
+    sleep 1;
+    getPids;
+    if [ "$PIDS" == "" ]; then
+      break
+    fi;
+    let i=$i+1;
+  done;
+
+  if [ "$PIDS" != "" ]; then
+    echo "Can't stop app in ${i} seconds";
+    exit 0;
+  else
+    rm started.lock;
+    echo "App stopped";
+  fi;
+else
+    echo "no running app found";
+fi;
+
+#sudo /usr/local/bin/nginx -s stop;
+exit 0;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/host/update.sh	Tue Dec 07 23:29:58 2021 -0700
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+set -e
+
+./stop.sh
+
+echo Updating hg
+#hg pull -u https://luanhost_update:njer2ndsBVG@hg.luan.software/luanhost
+hg pull
+
+../scripts/build-luan.sh
+
+mkdir -p local
+mkdir -p logs
+rm -f logs/*
+hg identify >logs/changeset.txt
+
+#cp startup/mime.types local/mime.types;
+#./luan.sh startup/nginx.conf.luan $(pwd) $(whoami) $(id -gn) > local/nginx.conf
+#./luan.sh startup/nginx.default.conf.luan $(pwd) $(whoami) $(id -gn) > local/nginx.default.conf
+
+echo Starting...
+./start.sh