diff --git a/.bumpversion.cfg b/.bumpversion.cfg new file mode 100644 index 0000000..62b2d16 --- /dev/null +++ b/.bumpversion.cfg @@ -0,0 +1,10 @@ +[bumpversion] +commit = True +tag = True +current_version = 0.2.0 +parse = (?P\d+)\.(?P\d+)\.(?P\d+)(\-(?P[a-z]+))? +serialize = + {major}.{minor}.{patch}-{release} + {major}.{minor}.{patch} + +[bumpversion:file:threebot_worker/__init__.py] diff --git a/setup.py b/setup.py index ff24a5f..29fb380 100644 --- a/setup.py +++ b/setup.py @@ -15,11 +15,11 @@ def long_description(): setup( name="threebot-worker", version=app.__version__, - description="Worker scripts for the 3bot plattform", + description="Worker scripts for the 3bot plattform.", long_description=long_description(), author='arteria GmbH', author_email="admin@arteria.ch", - maintainer_email="renner@arteria.ch", + maintainer_email="admin@arteria.ch", url="https://github.com/3bot/3bot-worker", packages=find_packages(), include_package_data=True, diff --git a/threebot_worker/__init__.py b/threebot_worker/__init__.py index 4a7d320..a55e329 100644 --- a/threebot_worker/__init__.py +++ b/threebot_worker/__init__.py @@ -1,4 +1,4 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -__version__ = '0.1.6' +__version__ = '0.2.0' diff --git a/threebot_worker/daemon.py b/threebot_worker/daemon.py index 342cad1..a3895e0 100644 --- a/threebot_worker/daemon.py +++ b/threebot_worker/daemon.py @@ -58,9 +58,12 @@ def daemonize(self): # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() - si = file(self.stdin, 'r') - so = file(self.stdout, 'a+') - se = file(self.stderr, 'a+', 0) + #si = file(self.stdin, 'r') + #so = file(self.stdout, 'a+') + #se = file(self.stderr, 'a+', 0) + si = open(self.stdin, 'r') + so = open(self.stdout, 'a+') + se = open(self.stderr, 'a+') os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) @@ -68,7 +71,8 @@ def daemonize(self): # write pidfile atexit.register(self.delpid) pid = str(os.getpid()) - file(self.pidfile, 'w+').write("%s\n" % pid) + #file(self.pidfile, 'w+').write("%s\n" % pid) + open(self.pidfile, 'w+').write("%s\n" % pid) def delpid(self): os.remove(self.pidfile) @@ -79,7 +83,8 @@ def start(self): """ # Check for a pidfile to see if the daemon already runs try: - pf = file(self.pidfile, 'r') + #pf = file(self.pidfile, 'r') + pf = open(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: @@ -101,7 +106,8 @@ def stop(self): """ # Get the pid from the pidfile try: - pf = file(self.pidfile, 'r') + #pf = file(self.pidfile, 'r') + pf = open(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: @@ -135,7 +141,8 @@ def restart(self): def status(self): try: - pf = file(self.pidfile, 'r') + #pf = file(self.pidfile, 'r') + pf = open(self.pidfile, 'r') pid = int(pf.read().strip()) pf.close() except IOError: diff --git a/threebot_worker/threebot-worker b/threebot_worker/threebot-worker index 8edd415..66d441b 100755 --- a/threebot_worker/threebot-worker +++ b/threebot_worker/threebot-worker @@ -5,7 +5,7 @@ import os import sys import subprocess import zmq -import ConfigParser +import configparser import logging import threebot_crypto @@ -27,15 +27,15 @@ LOGFILE = os.path.join(BASEDIR, '3bot.log') LOGLEVEL = 'ERROR' -Config = ConfigParser.ConfigParser() +Config = configparser.ConfigParser() if os.path.isfile(CONFIGFILE): Config.read(CONFIGFILE) else: - print "No configfile found in: '%s'" % CONFIGFILE - print "----" - print "Creating basic configfile in '%s'" % CONFIGFILE - print "----" + print("No configfile found in: '%s'" % CONFIGFILE) + print("----") + print("Creating basic configfile in '%s'" % CONFIGFILE) + print("----") os.makedirs(BASEDIR) cfgfile = open(CONFIGFILE, 'w') @@ -44,10 +44,10 @@ else: Config.add_section('3bot-settings') Config.set('3bot-settings', 'BOT_ENDPOINT', '*') - port = raw_input('Enter PORT: ') + port = input('Enter PORT: ') Config.set('3bot-settings', 'PORT', port) - sec_key = raw_input('Enter SECRET_KEY: ') + sec_key = input('Enter SECRET_KEY: ') Config.set('3bot-settings', 'SECRET_KEY', sec_key) Config.write(cfgfile) @@ -59,31 +59,31 @@ try: BOT = Config.get('3bot-settings', 'BOT_ENDPOINT') PORT = Config.get('3bot-settings', 'PORT') except: - print "Invalid config file in: '%s'. Could not find BOT or PORT declaration" % CONFIGFILE - print "You can find a basic config file in the documentation." + print("Invalid config file in: '%s'. Could not find BOT or PORT declaration" % CONFIGFILE) + print("You can find a basic config file in the documentation.") sys.exit(2) try: SECRET_KEY = Config.get('3bot-settings', 'SECRET_KEY') except: - print "Invalid config file in: '%s'. Could not find SECRET_KEY declaration" % CONFIGFILE - print "You can find a basic config file in the documentation." + print("Invalid config file in: '%s'. Could not find SECRET_KEY declaration" % CONFIGFILE) + print("You can find a basic config file in the documentation.") sys.exit(2) try: DRY_RUN = Config.getboolean('3bot-settings', 'DRY_RUN') -except ConfigParser.NoOptionError: +except configparser.NoOptionError: DRY_RUN = False try: LOGFILE = Config.get('3bot-settings', 'LOGFILE') -except ConfigParser.NoOptionError: +except configparser.NoOptionError: pass try: LOGLEVEL = Config.get('3bot-settings', 'LOGLEVEL') -except ConfigParser.NoOptionError: +except configparser.NoOptionError: pass if LOGLEVEL == 'DEBUG': @@ -112,10 +112,8 @@ def write_script(directory, script, body): with open(task_path, 'w+') as task_file: task_file.write(body.encode('utf8').replace('\r\n', '\n')) logging.info("Saving new Script file at: %s" % task_path) - # change permission - os.chmod(task_path, 0755) - + os.chmod(task_path, 0o755) return task_path @@ -162,7 +160,7 @@ def run_command(request): callable = " && ".join(script_bits) else: callable = script_bits[0] - + if DRY_RUN: # dumping path to task script to stdout instead of executing it, allows to 'cat' later on the target node response = {'stdout': callable, 'stderr': '', 'exit_code': 0} @@ -172,35 +170,34 @@ def run_command(request): p = subprocess.Popen(callable, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) logging.info("Executing Script file at: %s" % task_path) t_stdout = "" - t_stderr = "" - + t_stderr = "" try: t_stdout = p.stdout.read() - except AttributeError, e: + except AttributeError as e: logging.info(e) try: t_stderr = p.stderr.read() - except AttributeError, e: + except AttributeError as e: logging.info(e) response = {'stdout': t_stdout, 'stderr': t_stderr, 'exit_code': p.wait()} del p - + return response if sys.argv[1] in ['restart', 'start', 'status', ]: - print '---' - print 'BASEDIR: %s' % str(BASEDIR) - print 'CONFIGFILE: %s' % str(CONFIGFILE) - print 'SCRIPTDIR: %s' % str(SCRIPTDIR) - print 'ENDPOINT: %s' % str(BOT) - print 'PORT: %s' % str(PORT) - print 'LOGFILE: %s' % str(LOGFILE) - print 'LOGLEVEL: %s' % str(LOGLEVEL) - print 'PIDFILE: %s' % str(PIDFILE) - print 'DRY_RUN: %s' % str(DRY_RUN) - print '---' + print('---') + print('BASEDIR: %s' % str(BASEDIR)) + print('CONFIGFILE: %s' % str(CONFIGFILE)) + print('SCRIPTDIR: %s' % str(SCRIPTDIR)) + print('ENDPOINT: %s' % str(BOT)) + print('PORT: %s' % str(PORT)) + print('LOGFILE: %s' % str(LOGFILE)) + print('LOGLEVEL: %s' % str(LOGLEVEL)) + print('PIDFILE: %s' % str(PIDFILE)) + print('DRY_RUN: %s' % str(DRY_RUN)) + print('---') class WorkerDeamon(Daemon): @@ -209,13 +206,13 @@ class WorkerDeamon(Daemon): def run(self): logging.info("Starting the 3bot worker listening on %s:%s" % (BOT, PORT)) - context = zmq.Context(1) server = context.socket(zmq.REP) server.bind("%s://%s:%s" % (PROTOCOL, BOT, PORT)) while True: request = server.recv(FLAGS) + print (request) request = threebot_crypto.decrypt(request, SECRET_KEY) logging.info("Received request") if request: @@ -252,9 +249,9 @@ if __name__ == "__main__": elif 'status' == sys.argv[1]: daemon.status() else: - print "Unknown command" + print("Unknown command") sys.exit(2) sys.exit(0) else: - print "usage: %s start|stop|restart|status" % sys.argv[0] + print("usage: %s start|stop|restart|status" % sys.argv[0]) sys.exit(2)