diff --git a/.gitignore b/.gitignore index 19f98d7..fff347a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ __pycache__ cache/* logs/* +env/* diff --git a/botan-start.py b/botan-start.py index ad7b45e..bd0c3e7 100755 --- a/botan-start.py +++ b/botan-start.py @@ -1,43 +1,18 @@ #!/usr/bin/env python3 import os, pwd, grp -from botanjs.config import Config as config - -def drop_privileges( uid_name='nobody', gid_name='nogroup' ): - # Get the uid/gid from the name - running_uid = pwd.getpwnam( uid_name ).pw_uid - running_gid = grp.getgrnam( gid_name ).gr_gid - - current_uid = os.getuid() - - if current_uid != 0: - if running_uid == current_uid: - return True - print( "Service must be started by root" ) - return False - - # Remove group privileges - os.setgroups([]) - - # Try setting the new uid/gid - os.setgid( running_gid ) - os.setuid( running_uid ) - - # Ensure a very conservative umask - old_umask = os.umask( 0o022 ) - - return True +from botanjs.config import Config as config, DEBUG import shutil -# Create the lock folder for celery -lockDir = "/var/run/celery" -os.makedirs( lockDir, exist_ok=True ) -shutil.chown( lockDir, config["Service"]["user"] ) +SiteRoot = os.path.abspath( "." ) -# Imediately drop the root privilege -if drop_privileges( config["Service"]["user"], config["Service"]["group"] ) != True: - exit() +config["Paths"]["SiteRoot"] = SiteRoot; + +# Create the lock folder for celery +lockDir = SiteRoot + "env/var/run/celery" + +os.makedirs( lockDir, exist_ok=True ) import sys from subprocess import Popen @@ -51,31 +26,32 @@ if __name__ == "__main__": jwork = "botanjs.service.jwork" nodeName = "botanNode1" - Popen([ - "celery" - , "multi" - , "restart" - , nodeName - , "-A" - , jwork - , "worker" - , "--pidfile=/var/run/celery/" + jwork + ".pid" - , "--logfile=" + os.path.join( config["Paths"]["Log"], jwork + ".log" ) - , "--workdir=" + config["Paths"]["Runtime"] - , "beat" - , "-l" - , "info" - ]) - - import logging - logging.basicConfig( - filename = os.path.join( config["Paths"]["Log"], "access.log" ) - , level = logging.DEBUG + celOut = open( os.path.join( config["Paths"]["Log"], jwork + "-err.log" ), "a+" ) + cel = Popen( + [ + "celery", "multi", "restart", nodeName + , "-A", jwork, "worker" + , "--pidfile=" + lockDir + jwork + ".pid" + , "--logfile=" + os.path.join( config["Paths"]["Log"], jwork + ".log" ) + , "--workdir=" + config["Paths"]["Runtime"] + , "beat", "-l", "info" + ] + , stdout = celOut + , stderr = celOut ) + celOut.close() + + if not DEBUG and os.fork(): + import logging + logging.basicConfig( + filename = os.path.join( config["Paths"]["Log"], "access.log" ) + , level = logging.DEBUG + ) + sys.exit() + WebAPI( jsCache = config["Paths"]["Cache"] , jsRoot = config["BotanJS"]["SrcDir"] , brokerURL = config["BotanJS"]["CeleryBroker"] ) - diff --git a/botanjs/config.py b/botanjs/config.py index 31eaaa7..2117351 100755 --- a/botanjs/config.py +++ b/botanjs/config.py @@ -3,3 +3,5 @@ import configparser Config = configparser.ConfigParser( interpolation = configparser.ExtendedInterpolation() ) Config.read( "settings.ini" ) + +DEBUG = Config[ "Env" ][ "Debug" ] diff --git a/botanjs/service/templates/test/Dandelian.CSSAnimations.MovieClip.html b/botanjs/service/templates/test/Dandelian.CSSAnimations.MovieClip.html deleted file mode 100644 index 12bc6be..0000000 --- a/botanjs/service/templates/test/Dandelian.CSSAnimations.MovieClip.html +++ /dev/null @@ -1,40 +0,0 @@ - - -Botan JS - Test - - - - - - - - - diff --git a/botanjs/service/templates/test/System.Net.ClassLoader.html b/botanjs/service/templates/test/System.Net.ClassLoader.html deleted file mode 100644 index 398e9b1..0000000 --- a/botanjs/service/templates/test/System.Net.ClassLoader.html +++ /dev/null @@ -1,25 +0,0 @@ - - -Botan JS - Test - - - - - - - - diff --git a/botanjs/service/templates/test/System.utils.IKey.html b/botanjs/service/templates/test/System.utils.IKey.html deleted file mode 100644 index bedef20..0000000 --- a/botanjs/service/templates/test/System.utils.IKey.html +++ /dev/null @@ -1,13 +0,0 @@ - - -Botan JS - Test - - - - - - - diff --git a/botanjs/service/templates/test_list.html b/botanjs/service/templates/test_list.html deleted file mode 100644 index 4732e8b..0000000 --- a/botanjs/service/templates/test_list.html +++ /dev/null @@ -1,19 +0,0 @@ - - -Botan JS - List of Tests - - -
-{% for d in data %}
-    {{ d }}
-    
-{% endfor %} -
- - diff --git a/botanjs/service/webapi.py b/botanjs/service/webapi.py index db3ef88..4626a0c 100755 --- a/botanjs/service/webapi.py +++ b/botanjs/service/webapi.py @@ -4,6 +4,8 @@ from flask import Response from flask import render_template from botanjs.service.jclassresv import BotanClassResolver as JCResv from botanjs.service.jwork import app as CeleryApp, JWork +from botanjs.config import DEBUG + import os class WebAPI: @@ -16,12 +18,6 @@ class WebAPI: def __init__( self, jsRoot = "../src", jsCache = "/tmp", brokerURL = None ): - self.test_templates = os.path.join( - os.path.dirname( os.path.realpath( __file__ ) ) - , "templates" - , "test" - ) - self.BRoot = os.path.abspath( jsRoot ) self.BCache = os.path.join( jsCache, "botanjs" ) self.BMap = os.path.join( self.BCache, "bmap.xml" ) @@ -29,27 +25,17 @@ class WebAPI: if brokerURL != None: CeleryApp.conf.update( BROKER_URL = brokerURL ) - self.app = Flask( __name__, static_url_path = '', static_folder = self.BCache ) + print( __name__ ) + self.app = Flask( __name__, static_url_path = self.BCache, static_folder = self.BCache ) self.app.jinja_env.add_extension( "compressinja.html.HtmlCompressor" ) - self.app.add_url_rule( "/test" , view_func = self.r_test_list ) - self.app.add_url_rule( "/test/" , view_func = self.r_test ) - self.app.add_url_rule( "//" , view_func = self.api_request ) + self.app.add_url_rule( "/" , view_func = self.index ) + self.app.add_url_rule( "//" , view_func = self.api_request ) - self.app.run( host="0.0.0.0" ) - - def r_test_list( self ): - for root, dirs, files in os.walk( self.test_templates ): - break - - files.sort() - files = ( os.path.splitext( x )[0] for x in files ) - - return render_template( "test_list.html", data = files ) - - def r_test( self, test_file ): - return render_template( os.path.join( "test", test_file + ".html" ) ) + self.app.run( host = "0.0.0.0", debug = DEBUG ) + def index( self ): + return "Hello, this is the BotanJS Service API.", 200 def api_request( self, mode, code ): try: @@ -61,10 +47,9 @@ class WebAPI: srvHandler = JCResv( JWork, self.BRoot, self.BMap, self.BCache ) return Response( srvHandler.getAPI( code, mode = mode ), mimetype = t ) - except LookupError as e: + except Exception as e: return str(e), 404 - # except Exception as e: - # return str(e), 404 + return "Invalid request", 404 diff --git a/env/README.md b/env/README.md new file mode 100644 index 0000000..1834eef --- /dev/null +++ b/env/README.md @@ -0,0 +1 @@ +virtualenv here diff --git a/settings.ini b/settings.ini index 65c8da3..559e1da 100644 --- a/settings.ini +++ b/settings.ini @@ -1,17 +1,13 @@ +[Env] +Debug = True + [Paths] -SiteRoot = /var/www/botanjs Runtime = ${SiteRoot} Log = ${SiteRoot}/logs Cache = ${SiteRoot}/cache [BotanJS] SrcDir = ${Paths:Runtime}/botanjs/src - -REDIS_PASS = PASSWORD_FOR_REDIS_DB - CeleryBroker = redis://:${REDIS_PASS}@123.123.123.123:1234/9 - -[Service] -user = www-data -group = www-data +REDIS_PASS = PASSWORD_FOR_REDIS_DB