2022-04-09 21:55:50 +03:00
import os
import signal
import subprocess
2022-04-25 19:36:11 +03:00
import time
2021-12-22 00:13:27 +03:00
2022-04-09 21:55:50 +03:00
from setproctitle import setproctitle
from ipaddress import ip_address
from doreah . control import mainfunction
from doreah . io import col
2022-04-09 22:11:06 +03:00
from doreah . logging import log
2022-04-09 21:55:50 +03:00
from . import __pkginfo__ as pkginfo
2022-04-09 22:39:04 +03:00
from . pkg_global import conf
2022-04-09 21:55:50 +03:00
from . proccontrol import tasks
2022-04-09 22:02:17 +03:00
from . setup import setup
2022-05-01 23:39:16 +03:00
from . dev import generate , apidebug
2022-04-09 21:55:50 +03:00
def print_header_info ( ) :
print ( )
#print("#####")
print ( col [ ' yellow ' ] ( " Maloja " ) , f " v { pkginfo . VERSION } " )
print ( pkginfo . HOMEPAGE )
#print("#####")
print ( )
def get_instance ( ) :
try :
return int ( subprocess . check_output ( [ " pidof " , " maloja " ] ) )
2022-04-24 20:41:55 +03:00
except Exception :
2022-04-09 21:55:50 +03:00
return None
def get_instance_supervisor ( ) :
try :
return int ( subprocess . check_output ( [ " pidof " , " maloja_supervisor " ] ) )
2022-04-24 20:41:55 +03:00
except Exception :
2022-04-09 21:55:50 +03:00
return None
def restart ( ) :
2022-04-25 19:36:11 +03:00
if stop ( ) :
start ( )
else :
print ( col [ " red " ] ( " Could not stop Maloja! " ) )
2022-04-09 21:55:50 +03:00
def start ( ) :
if get_instance_supervisor ( ) is not None :
print ( " Maloja is already running. " )
else :
print_header_info ( )
setup ( )
try :
#p = subprocess.Popen(["python3","-m","maloja.server"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
2022-04-09 22:11:06 +03:00
sp = subprocess . Popen ( [ " python3 " , " -m " , " maloja " , " supervisor " ] , stdout = subprocess . DEVNULL , stderr = subprocess . DEVNULL )
2022-04-09 21:55:50 +03:00
print ( col [ " green " ] ( " Maloja started! " ) )
2022-04-09 22:39:04 +03:00
port = conf . malojaconfig [ " PORT " ]
2022-04-09 21:55:50 +03:00
print ( " Visit your server address (Port " + str ( port ) + " ) to see your web interface. Visit /admin_setup to get started. " )
print ( " If you ' re installing this on your local machine, these links should get you there: " )
print ( " \t " + col [ " blue " ] ( " http://localhost: " + str ( port ) ) )
print ( " \t " + col [ " blue " ] ( " http://localhost: " + str ( port ) + " /admin_setup " ) )
return True
2022-04-24 20:41:55 +03:00
except Exception :
2022-04-09 21:55:50 +03:00
print ( " Error while starting Maloja. " )
return False
def stop ( ) :
2022-04-25 19:36:11 +03:00
for attempt in [ ( signal . SIGTERM , 2 ) , ( signal . SIGTERM , 5 ) , ( signal . SIGKILL , 3 ) , ( signal . SIGKILL , 5 ) ] :
pid_sv = get_instance_supervisor ( )
pid = get_instance ( )
if pid is None and pid_sv is None :
print ( " Maloja stopped! " )
return True
if pid_sv is not None :
os . kill ( pid_sv , attempt [ 0 ] )
if pid is not None :
os . kill ( pid , attempt [ 0 ] )
time . sleep ( attempt [ 1 ] )
return False
2022-04-09 21:55:50 +03:00
print ( " Maloja stopped! " )
return True
def onlysetup ( ) :
print_header_info ( )
setup ( )
print ( " Setup complete! " )
def run_server ( ) :
print_header_info ( )
setup ( )
setproctitle ( " maloja " )
from . import server
server . run_server ( )
2022-04-09 22:11:06 +03:00
def run_supervisor ( ) :
setproctitle ( " maloja_supervisor " )
while True :
log ( " Maloja is not running, starting... " , module = " supervisor " )
try :
process = subprocess . Popen (
[ " python3 " , " -m " , " maloja " , " run " ] ,
stdout = subprocess . DEVNULL ,
stderr = subprocess . DEVNULL ,
)
except Exception as e :
log ( " Error starting Maloja: " + str ( e ) , module = " supervisor " )
else :
try :
process . wait ( )
except Exception as e :
log ( " Maloja crashed: " + str ( e ) , module = " supervisor " )
2022-04-09 21:55:50 +03:00
def debug ( ) :
os . environ [ " MALOJA_DEV_MODE " ] = ' true '
2022-04-09 22:39:04 +03:00
conf . malojaconfig . load_environment ( )
2022-04-09 21:55:50 +03:00
direct ( )
def print_info ( ) :
print_header_info ( )
2022-04-09 22:39:04 +03:00
print ( col [ ' lightblue ' ] ( " Configuration Directory: " ) , conf . dir_settings [ ' config ' ] )
print ( col [ ' lightblue ' ] ( " Data Directory: " ) , conf . dir_settings [ ' state ' ] )
print ( col [ ' lightblue ' ] ( " Log Directory: " ) , conf . dir_settings [ ' logs ' ] )
2022-12-05 02:05:19 +03:00
print ( col [ ' lightblue ' ] ( " Network: " ) , f " Dual Stack, Port { conf . malojaconfig [ ' port ' ] } " if conf . malojaconfig [ ' host ' ] == " * " else f " IPv { ip_address ( conf . malojaconfig [ ' host ' ] ) . version } , Port { conf . malojaconfig [ ' port ' ] } " )
2022-04-09 22:39:04 +03:00
print ( col [ ' lightblue ' ] ( " Timezone: " ) , f " UTC { conf . malojaconfig [ ' timezone ' ] : +d } " )
2022-04-09 21:55:50 +03:00
print ( )
2023-02-26 17:48:50 +03:00
try :
import pkg_resources
for pkg in ( " sqlalchemy " , " waitress " , " bottle " , " doreah " , " jinja2 " ) :
print ( col [ ' cyan ' ] ( f " { pkg } : " . ljust ( 13 ) ) , pkg_resources . get_distribution ( pkg ) . version )
except ImportError :
print ( " Could not determine dependency versions. " )
2022-04-09 21:55:50 +03:00
print ( )
@mainfunction ( { " l " : " level " , " v " : " version " , " V " : " version " } , flags = [ ' version ' , ' include_images ' ] , shield = True )
def main ( * args , * * kwargs ) :
actions = {
# server
" start " : start ,
" restart " : restart ,
" stop " : stop ,
" run " : run_server ,
2022-04-09 22:11:06 +03:00
" supervisor " : run_supervisor ,
2022-04-09 21:55:50 +03:00
" debug " : debug ,
" setup " : onlysetup ,
# admin scripts
" import " : tasks . import_scrobbles , # maloja import /x/y.csv
" backup " : tasks . backup , # maloja backup --targetfolder /x/y --include_images
2022-04-09 22:24:48 +03:00
" generate " : generate . generate_scrobbles , # maloja generate 400
2022-04-09 21:55:50 +03:00
" export " : tasks . export , # maloja export
2022-05-01 23:39:16 +03:00
" apidebug " : apidebug . run , # maloja apidebug
2022-04-09 21:55:50 +03:00
# aux
" info " : print_info
}
if " version " in kwargs :
print ( info . VERSION )
2022-04-14 16:19:38 +03:00
return True
2022-04-09 21:55:50 +03:00
else :
try :
action , * args = args
action = actions [ action ]
except ( ValueError , KeyError ) :
print ( " Valid commands: " + " " . join ( a for a in actions ) )
2022-04-14 16:19:38 +03:00
return False
2022-04-09 21:55:50 +03:00
return action ( * args , * * kwargs )