diff --git a/Mylar.py b/Mylar.py index d22006ca..28668c47 100644 --- a/Mylar.py +++ b/Mylar.py @@ -22,7 +22,7 @@ from lib.configobj import ConfigObj import mylar -from mylar import webstart, logger, filechecker +from mylar import webstart, logger, filechecker, versioncheck try: import argparse @@ -66,6 +66,7 @@ def main(): parser.add_argument('--nolaunch', action='store_true', help='Prevent browser from launching on startup') parser.add_argument('--pidfile', help='Create a pid file (only relevant when running as a daemon)') parser.add_argument('--safe', action='store_true', help='redirect the startup page to point to the Manage Comics screen on startup') + #parser.add_argument('-u', '--update', action='store_true', help='force mylar to perform an update as if in GUI') args = parser.parse_args() @@ -74,6 +75,14 @@ def main(): elif args.quiet: mylar.VERBOSE = 0 + #if args.update: + # print('Attempting to update Mylar so things can work again...') + # try: + # versioncheck.update() + # except Exception, e: + # sys.exit('Mylar failed to update.') + + if args.daemon: if sys.platform == 'win32': print "Daemonize not supported under Windows, starting normally" @@ -82,7 +91,7 @@ def main(): mylar.VERBOSE=0 if args.pidfile : - mylar.PIDFILE = args.pidfile + mylar.PIDFILE = str(args.pidfile) # If the pidfile already exists, mylar may still be running, so exit if os.path.exists(mylar.PIDFILE): @@ -90,6 +99,7 @@ def main(): # The pidfile is only useful in daemon mode, make sure we can write the file properly if mylar.DAEMON: + mylar.CREATEPID = True try: file(mylar.PIDFILE, 'w').write("pid\n") except IOError, e: @@ -166,7 +176,10 @@ def main(): while True: if not mylar.SIGNAL: - time.sleep(1) + try: + time.sleep(1) + except KeyboardInterrupt: + mylar.SIGNAL = 'shutdown' else: logger.info('Received signal: ' + mylar.SIGNAL) if mylar.SIGNAL == 'shutdown': diff --git a/centos-mylar.init.d b/init-scripts/centos.init.d old mode 100644 new mode 100755 similarity index 100% rename from centos-mylar.init.d rename to init-scripts/centos.init.d diff --git a/init-scripts/ubuntu.default.mylar b/init-scripts/ubuntu.default.mylar new file mode 100755 index 00000000..6e8baffb --- /dev/null +++ b/init-scripts/ubuntu.default.mylar @@ -0,0 +1,21 @@ +## This is an example config file for use with running as a daemon on ubuntu. +## ( you can just make a blank file with the required lines as well ) +## +## Edit & Copy this file to /etc/default/mylar +## Otherwise default mylar options are used (all default options are stated below) +## +### ADD THE REQUIRED OPTIONS ON INDIVIDUAL LINES BELOW THE 'END OF' ..(ie. MYLAR_USER=evil) +### +## .:[CONFIG OPTIONS]:. +## +### MYLAR_USER= #$RUN_AS, username to run mylar under, the default is mylar +### MYLAR_HOME= #$APP_PATH, the location of mylar.py, the default is /opt/mylar +### MYLAR_DATA= #$DATA_DIR, the location of mylar.db, cache, logs, the default is /opt/mylar +### MYLAR_PIDFILE #$PIDFILE, the location of the pidfile, the default is /var/run/mylar/mylar.pid +### PYTHON_BIN= #$DAEMON, the location of the python binary, the default is /usr/bin/python +### MYLAR_OPTS= #$EXTRA_DAEMON_OPTS, extra cli option for mylar, i.e. " --config=/home/mylar/config.ini" +### SSD_OPTS= #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users" +### MYLAR_PORT= #$PORT_OPTS, hardcoded port for the webserver, overrides value in config.ini +## +## END OF.. +##########################################################################################################3 diff --git a/init-scripts/ubuntu.init.d b/init-scripts/ubuntu.init.d new file mode 100755 index 00000000..4fe368be --- /dev/null +++ b/init-scripts/ubuntu.init.d @@ -0,0 +1,214 @@ +#!/bin/sh +## +## Taken entirety from the program that Mylar is based off of - headphones. +## +# +# DO NOT EDIT THIS FILE +# +## +## Edit user configuation in /etc/default/mylar to change +## +## Make sure init script is executable +## sudo chmod +x /opt/mylar/init.ubuntu +## +## Install the init script +## sudo ln -s /opt/mylar/init.ubuntu /etc/init.d/mylar +## +## Create the mylar daemon user: +## sudo adduser --system --no-create-home mylar +## +## Make sure /opt/mylar is owned by the mylar user +## sudo chown mylar:nogroup -R /opt/mylar +## +## Touch the default file to stop the warning message when starting +## sudo touch /etc/default/mylar +## +## To start mylar automatically +## sudo update-rc.d mylar defaults +## +## To start/stop/restart mylar +## sudo service mylar start +## sudo service mylar stop +## sudo service mylar restart +## +## MYLAR_USER= #$RUN_AS, username to run mylar under, the default is mylar +## MYLAR_HOME= #$APP_PATH, the location of mylar.py, the default is /opt/mylar +## MYLAR_DATA= #$DATA_DIR, the location of mylar.db, cache, logs, the default is /opt/mylar +## MYLAR_PIDFILE= #$PID_FILE, the location of mylar.pid, the default is /var/run/mylar/mylar.pid +## PYTHON_BIN= #$DAEMON, the location of the python binary, the default is /usr/bin/python +## MYLAR_OPTS= #$EXTRA_DAEMON_OPTS, extra cli option for mylar, i.e. " --config=/home/mylar/config.ini" +## SSD_OPTS= #$EXTRA_SSD_OPTS, extra start-stop-daemon option like " --group=users" +## MYLAR_PORT= #$PORT_OPTS, hardcoded port for the webserver, overrides value in config.ini +## +## EXAMPLE if want to run as different user +## add MYLAR_USER=username to /etc/default/mylar +## otherwise default mylar is used +# +### BEGIN INIT INFO +# Provides: mylar +# Required-Start: $local_fs $network $remote_fs +# Required-Stop: $local_fs $network $remote_fs +# Should-Start: $NetworkManager +# Should-Stop: $NetworkManager +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: starts instance of mylar +# Description: starts instance of mylar using start-stop-daemon +### END INIT INFO + +# Script name +NAME=mylar + +# App name +DESC=mylar + +SETTINGS_LOADED=FALSE + +. /lib/lsb/init-functions + +# Source mylar configuration +if [ -f /etc/default/mylar ]; then + SETTINGS=/etc/default/mylar +else + log_warning_msg "/etc/default/mylar not found using default settings."; +fi + +check_retval() { + if [ $? -eq 0 ]; then + log_end_msg 0 + return 0 + else + log_end_msg 1 + exit 1 + fi +} + +load_settings() { + if [ $SETTINGS_LOADED != "TRUE" ]; then + . $SETTINGS + + ## The defaults + # Run as username + RUN_AS=${MYLAR_USER-mylar} + + # Path to app MYLAR_HOME=path_to_app_mylar.py + APP_PATH=${MYLAR_HOME-/opt/mylar} + + # Data directory where mylar.db, cache and logs are stored + DATA_DIR=${MYLAR_DATA-/opt/mylar} + + # Path to store PID file + PID_FILE=${MYLAR_PIDFILE-/var/run/mylar/mylar.pid} + + # Path to python bin + DAEMON=${PYTHON_BIN-/usr/bin/python} + + # Extra daemon option like: MYLAR_OPTS=" --config=/home/mylar/config.ini" + EXTRA_DAEMON_OPTS=${MYLAR_OPTS-} + + # Extra start-stop-daemon option like START_OPTS=" --group=users" + EXTRA_SSD_OPTS=${SSD_OPTS-} + + # Hardcoded port to run on, overrides config.ini settings + [ -n "$MYLAR_PORT" ] && { + PORT_OPTS=" --port=${MYLAR_PORT} " + } + + DAEMON_OPTS=" Mylar.py --quiet --daemon --nolaunch --pidfile=${PID_FILE} --datadir=${DATA_DIR} ${PORT_OPTS}${EXTRA_DAEMON_OPTS}" + + SETTINGS_LOADED=TRUE + fi + + [ -x $DAEMON ] || { + log_warning_msg "$DESC: Can't execute daemon, aborting. See $DAEMON"; + return 1;} + + return 0 +} + +load_settings || exit 0 + +is_running () { + # returns 1 when running, else 0. + if [ -e $PID_FILE ]; then + PID=`cat $PID_FILE` + + RET=$? + [ $RET -gt 1 ] && exit 1 || return $RET + else + return 1 + fi +} + +handle_pid () { + PID_PATH=`dirname $PID_FILE` + [ -d $PID_PATH ] || mkdir -p $PID_PATH && chown -R $RUN_AS $PID_PATH > /dev/null || { + log_warning_msg "$DESC: Could not create $PID_FILE, See $SETTINGS, aborting."; + return 1;} + + if [ -e $PID_FILE ]; then + PID=`cat $PID_FILE` + if ! kill -0 $PID > /dev/null 2>&1; then + log_warning_msg "Removing stale $PID_FILE" + rm $PID_FILE + fi + fi +} + +handle_datadir () { + [ -d $DATA_DIR ] || mkdir -p $DATA_DIR && chown -R $RUN_AS $DATA_DIR > /dev/null || { + log_warning_msg "$DESC: Could not create $DATA_DIR, See $SETTINGS, aborting."; + return 1;} +} + +handle_updates () { + chown -R $RUN_AS $APP_PATH > /dev/null || { + log_warning_msg "$DESC: $APP_PATH not writable by $RUN_AS for web-updates"; + return 0; } +} + +start_mylar () { + handle_pid + handle_datadir + handle_updates + if ! is_running; then + log_daemon_msg "Starting $DESC" + start-stop-daemon -o -d $APP_PATH -c $RUN_AS --start $EXTRA_SSD_OPTS --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS + check_retval + else + log_success_msg "$DESC: already running (pid $PID)" + fi +} + +stop_mylar () { + if is_running; then + log_daemon_msg "Stopping $DESC" + start-stop-daemon -o --stop --pidfile $PID_FILE --retry 15 + check_retval + else + log_success_msg "$DESC: not running" + fi +} + +case "$1" in + start) + start_mylar + ;; + stop) + stop_mylar + ;; + restart|force-reload) + stop_mylar + start_mylar + ;; + status) + status_of_proc -p "$PID_FILE" "$DAEMON" "$DESC" + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|restart|force-reload|status}" >&2 + exit 1 + ;; +esac + +exit 0 diff --git a/mylar/__init__.py b/mylar/__init__.py index b8142399..9dc154b0 100755 --- a/mylar/__init__.py +++ b/mylar/__init__.py @@ -48,6 +48,7 @@ OS_LANG, OS_ENCODING = locale.getdefaultlocale() VERBOSE = 1 DAEMON = False PIDFILE= None +CREATEPID = False SCHED = Scheduler() @@ -67,6 +68,8 @@ FolderMonitorScheduler = None DATA_DIR = None DBLOCK = False +UMASK = None + CONFIG_FILE = None CFG = None CONFIG_VERSION = None @@ -345,7 +348,7 @@ def initialize(): PROWL_ENABLED, PROWL_PRIORITY, PROWL_KEYS, PROWL_ONSNATCH, NMA_ENABLED, NMA_APIKEY, NMA_PRIORITY, NMA_ONSNATCH, PUSHOVER_ENABLED, PUSHOVER_PRIORITY, PUSHOVER_APIKEY, PUSHOVER_USERKEY, PUSHOVER_ONSNATCH, BOXCAR_ENABLED, BOXCAR_ONSNATCH, BOXCAR_TOKEN, \ PUSHBULLET_ENABLED, PUSHBULLET_APIKEY, PUSHBULLET_DEVICEID, PUSHBULLET_ONSNATCH, LOCMOVE, NEWCOM_DIR, FFTONEWCOM_DIR, \ PREFERRED_QUALITY, MOVE_FILES, RENAME_FILES, LOWERCASE_FILENAMES, USE_MINSIZE, MINSIZE, USE_MAXSIZE, MAXSIZE, CORRECT_METADATA, FOLDER_FORMAT, FILE_FORMAT, REPLACE_CHAR, REPLACE_SPACES, ADD_TO_CSV, CVINFO, LOG_LEVEL, POST_PROCESSING, SEARCH_DELAY, GRABBAG_DIR, READ2FILENAME, STORYARCDIR, CVURL, CVAPIFIX, CHECK_FOLDER, \ - COMIC_LOCATION, QUAL_ALTVERS, QUAL_SCANNER, QUAL_TYPE, QUAL_QUALITY, ENABLE_EXTRA_SCRIPTS, EXTRA_SCRIPTS, ENABLE_PRE_SCRIPTS, PRE_SCRIPTS, PULLNEW, COUNT_ISSUES, COUNT_HAVES, COUNT_COMICS, SYNO_FIX, CHMOD_FILE, CHMOD_DIR, ANNUALS_ON, CV_ONLY, CV_ONETIMER, WEEKFOLDER + COMIC_LOCATION, QUAL_ALTVERS, QUAL_SCANNER, QUAL_TYPE, QUAL_QUALITY, ENABLE_EXTRA_SCRIPTS, EXTRA_SCRIPTS, ENABLE_PRE_SCRIPTS, PRE_SCRIPTS, PULLNEW, COUNT_ISSUES, COUNT_HAVES, COUNT_COMICS, SYNO_FIX, CHMOD_FILE, CHMOD_DIR, ANNUALS_ON, CV_ONLY, CV_ONETIMER, WEEKFOLDER, UMASK if __INITIALIZED__: return False @@ -916,6 +919,9 @@ def initialize(): # runImmediately=True, # delay=60) + # Store the original umask + UMASK = os.umask(0) + os.umask(UMASK) __INITIALIZED__ = True return True @@ -943,6 +949,10 @@ def daemonize(): os.setsid() + # Make sure I can read my own files and shut out others + prev = os.umask(0) # @UndefinedVariable - only available in UNIX + os.umask(prev and int('077', 8)) + # Do second fork try: pid = os.fork() @@ -952,9 +962,9 @@ def daemonize(): except OSError, e: sys.exit("2nd fork failed: %s [%d]" % (e.strerror, e.errno)) - os.chdir("/") - os.umask(0) - + dev_null = file('/dev/null', 'r') + os.dup2(dev_null.fileno(), sys.stdin.fileno()) + si = open('/dev/null', "r") so = open('/dev/null', "a+") se = open('/dev/null', "a+") @@ -965,9 +975,10 @@ def daemonize(): pid = os.getpid() logger.info('Daemonized to PID: %s' % pid) - if PIDFILE: - logger.info('Writing PID %s to %s' % (pid, PIDFILE)) - file(PIDFILE, 'w').write("%s\n" % pid) + if CREATEPID: + logger.info("Writing PID %d to %s", pid, PIDFILE) + with file(PIDFILE, 'w') as fp: + fp.write("%s\n" % pid) def launch_browser(host, port, root): @@ -1632,7 +1643,7 @@ def shutdown(restart=False, update=False): except Exception, e: logger.warn('Mylar failed to update: %s. Restarting.' % e) - if PIDFILE : + if CREATEPID: logger.info('Removing pidfile %s' % PIDFILE) os.remove(PIDFILE) diff --git a/ubuntu-mylar.init.d b/ubuntu-mylar.init.d deleted file mode 100755 index c86b51d4..00000000 --- a/ubuntu-mylar.init.d +++ /dev/null @@ -1,80 +0,0 @@ -#! /bin/sh - -### BEGIN INIT INFO -# Provides: mylar -# Required-Start: $local_fs $network $remote_fs -# Required-Stop: $local_fs $network $remote_fs -# Should-Start: $NetworkManager -# Should-Stop: $NetworkManager -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: starts instance of mylar -# Description: starts instance of mylar using start-stop-daemon -### END INIT INFO - -############### EDIT ME ################## -# path to app -APP_PATH= - -# path to python bin -DAEMON=/usr/bin/python - -# startup args -DAEMON_OPTS=" Mylar.py -q" - -# script name -NAME=mylar - -# app name -DESC=mylar - -# user -RUN_AS= - -PID_FILE=/var/run/mylar.pid -PID_PATH=`dirname $PID_FILE` - -############### END EDIT ME ################## - -test -x $DAEMON || exit 0 - -set -e - -# Create PID if missing and remove stale PID file -if [ ! -d $PID_PATH ]; then - mkdir -p $PID_PATH - chown $RUN_AS $PID_PATH -fi - -if [ -e $PID_FILE ]; then - PID=`cat $PID_FILE` - if ! kill -0 $PID > /dev/null 2>&1; then - echo "Removing stale $PID_FILE" - rm $PID_FILE - fi -fi - -case "$1" in - start) - echo "Starting $DESC" - start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS - ;; - stop) - echo "Stopping $DESC" - start-stop-daemon --stop --pidfile $PID_FILE - ;; - - restart|force-reload) - echo "Restarting $DESC" - start-stop-daemon --stop --pidfile $PID_FILE - sleep 15 - start-stop-daemon -d $APP_PATH -c $RUN_AS --start --background --pidfile $PID_FILE --make-pidfile --exec $DAEMON -- $DAEMON_OPTS - ;; - *) - N=/etc/init.d/$NAME - echo "Usage: $N {start|stop|restart|force-reload}" >&2 - exit 1 - ;; -esac - -exit 0