Removed tests, using virtualenv

This commit is contained in:
斟酌 鵬兄 2016-05-28 00:20:58 +08:00
parent 866bf6db28
commit 41f1ab4c90
10 changed files with 49 additions and 185 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
__pycache__ __pycache__
cache/* cache/*
logs/* logs/*
env/*

View File

@ -1,43 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os, pwd, grp import os, pwd, grp
from botanjs.config import Config as config from botanjs.config import Config as config, DEBUG
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
import shutil import shutil
# Create the lock folder for celery SiteRoot = os.path.abspath( "." )
lockDir = "/var/run/celery"
os.makedirs( lockDir, exist_ok=True )
shutil.chown( lockDir, config["Service"]["user"] )
# Imediately drop the root privilege config["Paths"]["SiteRoot"] = SiteRoot;
if drop_privileges( config["Service"]["user"], config["Service"]["group"] ) != True:
exit() # Create the lock folder for celery
lockDir = SiteRoot + "env/var/run/celery"
os.makedirs( lockDir, exist_ok=True )
import sys import sys
from subprocess import Popen from subprocess import Popen
@ -51,31 +26,32 @@ if __name__ == "__main__":
jwork = "botanjs.service.jwork" jwork = "botanjs.service.jwork"
nodeName = "botanNode1" nodeName = "botanNode1"
Popen([ celOut = open( os.path.join( config["Paths"]["Log"], jwork + "-err.log" ), "a+" )
"celery" cel = Popen(
, "multi" [
, "restart" "celery", "multi", "restart", nodeName
, nodeName , "-A", jwork, "worker"
, "-A" , "--pidfile=" + lockDir + jwork + ".pid"
, jwork , "--logfile=" + os.path.join( config["Paths"]["Log"], jwork + ".log" )
, "worker" , "--workdir=" + config["Paths"]["Runtime"]
, "--pidfile=/var/run/celery/" + jwork + ".pid" , "beat", "-l", "info"
, "--logfile=" + os.path.join( config["Paths"]["Log"], jwork + ".log" ) ]
, "--workdir=" + config["Paths"]["Runtime"] , stdout = celOut
, "beat" , stderr = celOut
, "-l"
, "info"
])
import logging
logging.basicConfig(
filename = os.path.join( config["Paths"]["Log"], "access.log" )
, level = logging.DEBUG
) )
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( WebAPI(
jsCache = config["Paths"]["Cache"] jsCache = config["Paths"]["Cache"]
, jsRoot = config["BotanJS"]["SrcDir"] , jsRoot = config["BotanJS"]["SrcDir"]
, brokerURL = config["BotanJS"]["CeleryBroker"] , brokerURL = config["BotanJS"]["CeleryBroker"]
) )

View File

@ -3,3 +3,5 @@ import configparser
Config = configparser.ConfigParser( interpolation = configparser.ExtendedInterpolation() ) Config = configparser.ConfigParser( interpolation = configparser.ExtendedInterpolation() )
Config.read( "settings.ini" ) Config.read( "settings.ini" )
DEBUG = Config[ "Env" ][ "Debug" ]

View File

@ -1,40 +0,0 @@
<html>
<head>
<title>Botan JS - Test</title>
<script>
var debugEnv = true;
</script>
<link href="/ocss/Dandelion.CSSAnimations.MovieClip" rel="stylesheet" type="text/css" />
<script src="/ojs/Dandelion.CSSAnimations.MovieClip"></script>
<script>
function load ()
{
var CSSAnimations = BotanJS.import( "Dandelion.CSSAnimations.*" );
var mc = new CSSAnimations.MovieClip(
"http://file.astropenguin.net/blog/Gothic/__A.png"
// Row, Col
, 6 , 7
// wh
, 125 , 125
// Frame, start
, 42 , 0
);
// CSSAnimations.MouseOverMovie( mc );
document.body.appendChild( mc.stage );
mc.nextFrame();
mc.nextFrame();
mc.nextFrame();
mc.nextFrame();
mc.nextFrame();
mc.prevFrame();
};
</script>
<body onload="load()">
</body>
</html>

View File

@ -1,25 +0,0 @@
<html>
<head>
<title>Botan JS - Test</title>
<script>
var debugEnv = true;
var _AstConf_ = { };
</script>
<link href="/ocss/System.Net.ClassLoader" rel="stylesheet" type="text/css" />
<script src="/ojs/System.Net.ClassLoader"></script>
<script>
var Loader = BotanJS.import( "System.Net.ClassLoader" );
var makeMessagebox = function()
{
var MessageBox = BotanJS.import( "Components.MessageBox" );
new MessageBox( "Title", "Message" ).show();
};
var ldr = new Loader( "/" );
ldr.load( "Components.MessageBox", makeMessagebox );
</script>
<body>
</body>
</html>

View File

@ -1,13 +0,0 @@
<html>
<head>
<title>Botan JS - Test</title>
<script>
var debugEnv = true;
var _AstConf_ = { };
</script>
<link href="/ocss/System.utils.IKey" rel="stylesheet" type="text/css" />
<script src="/ojs/System.utils.IKey"></script>
<body>
</body>
</html>

View File

@ -1,19 +0,0 @@
<html>
<head>
<title>Botan JS - List of Tests</title>
<style>
<!--
body {
font-size: 1.2em;
}
-->
</style>
<body>
<pre>
{% for d in data %}
<a href="/test/{{ d }}">{{ d }}</a>
<br>
{% endfor %}
</pre>
</body>
</html>

View File

@ -4,6 +4,8 @@ from flask import Response
from flask import render_template from flask import render_template
from botanjs.service.jclassresv import BotanClassResolver as JCResv from botanjs.service.jclassresv import BotanClassResolver as JCResv
from botanjs.service.jwork import app as CeleryApp, JWork from botanjs.service.jwork import app as CeleryApp, JWork
from botanjs.config import DEBUG
import os import os
class WebAPI: class WebAPI:
@ -16,12 +18,6 @@ class WebAPI:
def __init__( self, jsRoot = "../src", jsCache = "/tmp", brokerURL = None ): 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.BRoot = os.path.abspath( jsRoot )
self.BCache = os.path.join( jsCache, "botanjs" ) self.BCache = os.path.join( jsCache, "botanjs" )
self.BMap = os.path.join( self.BCache, "bmap.xml" ) self.BMap = os.path.join( self.BCache, "bmap.xml" )
@ -29,27 +25,17 @@ class WebAPI:
if brokerURL != None: if brokerURL != None:
CeleryApp.conf.update( BROKER_URL = brokerURL ) 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.jinja_env.add_extension( "compressinja.html.HtmlCompressor" )
self.app.add_url_rule( "/test" , view_func = self.r_test_list ) self.app.add_url_rule( "/" , view_func = self.index )
self.app.add_url_rule( "/test/<string:test_file>" , view_func = self.r_test ) self.app.add_url_rule( "/<path:mode>/<path:code>" , view_func = self.api_request )
self.app.add_url_rule( "/<path:mode>/<path:code>" , view_func = self.api_request )
self.app.run( host="0.0.0.0" ) self.app.run( host = "0.0.0.0", debug = DEBUG )
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" ) )
def index( self ):
return "Hello, this is the BotanJS Service API.", 200
def api_request( self, mode, code ): def api_request( self, mode, code ):
try: try:
@ -61,10 +47,9 @@ class WebAPI:
srvHandler = JCResv( JWork, self.BRoot, self.BMap, self.BCache ) srvHandler = JCResv( JWork, self.BRoot, self.BMap, self.BCache )
return Response( srvHandler.getAPI( code, mode = mode ), mimetype = t ) return Response( srvHandler.getAPI( code, mode = mode ), mimetype = t )
except LookupError as e: except Exception as e:
return str(e), 404 return str(e), 404
# except Exception as e:
# return str(e), 404
return "Invalid request", 404 return "Invalid request", 404

1
env/README.md vendored Normal file
View File

@ -0,0 +1 @@
virtualenv here

View File

@ -1,17 +1,13 @@
[Env]
Debug = True
[Paths] [Paths]
SiteRoot = /var/www/botanjs
Runtime = ${SiteRoot} Runtime = ${SiteRoot}
Log = ${SiteRoot}/logs Log = ${SiteRoot}/logs
Cache = ${SiteRoot}/cache Cache = ${SiteRoot}/cache
[BotanJS] [BotanJS]
SrcDir = ${Paths:Runtime}/botanjs/src SrcDir = ${Paths:Runtime}/botanjs/src
REDIS_PASS = PASSWORD_FOR_REDIS_DB
CeleryBroker = redis://:${REDIS_PASS}@123.123.123.123:1234/9 CeleryBroker = redis://:${REDIS_PASS}@123.123.123.123:1234/9
REDIS_PASS = PASSWORD_FOR_REDIS_DB
[Service]
user = www-data
group = www-data