diff --git a/service.py b/java-app-updater.py similarity index 84% rename from service.py rename to java-app-updater.py index b4ceb65..a544435 100755 --- a/service.py +++ b/java-app-updater.py @@ -1,19 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# Copyright (C) 2017, Maxim Lihachev, # -# Script for upgrading java/play application from git +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the Free +# Software Foundation, version 3. # -# (c) envrm -# - -#Requirements -# -# CentOS: yum -y install python-pip -# Debian: apt-get install python-pip -# -# pip install configparser +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. # +# You should have received a copy of the GNU General Public License along with +# this program. If not, see . import re import os @@ -28,6 +28,7 @@ import configparser from string import Template from contextlib import contextmanager + @contextmanager def if_needed(): '''Execute with error ignoring''' @@ -36,6 +37,7 @@ def if_needed(): except: pass + class Dir: '''Execute procedures in directory''' def __init__(self, directory): @@ -48,7 +50,8 @@ class Dir: def __exit__(self, type, value, traceback): os.chdir(self.previous_dir) -#----------------------------------------------------------------------- + +# ---------------------------------------------------------------------- def readConfig(config_file): '''Loading configuration file''' @@ -58,6 +61,7 @@ def readConfig(config_file): settings.read(config_file) return settings + def opt(parameter): '''Getting parameters from file''' setting = parameter.split(":") @@ -69,13 +73,15 @@ def opt(parameter): return Template(value).safe_substitute(**substr) + def saveTimestamp(outfile, mode="a"): '''Saving timestamt to file''' with open(outfile, mode) as log: now = datetime.datetime.now() log.write(now.strftime("%Y-%m-%d %H:%M:%S\n")) -#----------------------------------------------------------------------- + +# ---------------------------------------------------------------------- def git(command, stand="develop"): '''Executing git command''' @@ -84,6 +90,7 @@ def git(command, stand="develop"): return git_output + def stop(stand="develop"): '''Stop service''' if os.path.isfile(opt(stand + ':pid')): @@ -98,6 +105,7 @@ def stop(stand="develop"): os.remove(pid) print "#### Application stopped" + def start(stand): '''Start service''' service_dir = opt(stand + ':path') @@ -118,16 +126,19 @@ def start(stand): print return_code exit(1) + def restart(stand="develop"): '''Restart service''' stop(stand) start(stand) saveTimestamp(opt(stand + ':restarts_log')) + def clean_all(): '''Remove artefacts''' os.system("rm -rfv " + opt('develop:targets')) + def build(target="stand", modules=None): '''Build service. [stand|modules|all]''' play_cmd = opt(SERVICE + ':play') + " " + opt(SERVICE + ':sbt_config') @@ -136,7 +147,7 @@ def build(target="stand", modules=None): if target == "modules" or target == "all": with Dir(opt('develop:modules_dir')): - if modules == None: + if modules is None: modules = os.walk(".").next()[1] modules = filter(None, set(modules)) @@ -164,6 +175,7 @@ def build(target="stand", modules=None): if return_code != 0: exit(1) + def update_from_git(): '''Update app with modules''' git('checkout ' + opt('build:git_branch')) @@ -180,20 +192,23 @@ def update_from_git(): build("all", modules) + def copy_libs(): '''Copy libraries''' with if_needed(): shutil.rmtree(opt(SERVICE + ':class_path')) shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path')) + def update(): '''Update current service from repository''' saveTimestamp(opt('develop:restarts_log')) stop() update_from_git() - start() + start(SERVICE) saveTimestamp(opt('develop:update_log'), 'w') + def upgrade(): '''Update developing service from repository''' update_from_git() @@ -204,7 +219,8 @@ def upgrade(): start(SERVICE) saveTimestamp(opt(SERVICE + ':update_log'), 'w') -#----------------------------------------------------------------------- + +# ---------------------------------------------------------------------- def show_help(): exe = os.path.basename(sys.argv[0]) @@ -220,40 +236,41 @@ def show_help(): print "\n[service] can be one of «" + "», «".join(ast.literal_eval(opt('service:projects')).keys()) + "»." print "\nDefault value of [service] is", opt('service:default'), "\n" + settings = [] readConfig('config.ini') allowed_tasks = ['start', 'stop', 'restart', 'build', 'update', 'upgrade'] -#Services allowed for building +# Services allowed for building services = ast.literal_eval(opt('service:projects')) -#Default service -SERVICE = ast.literal_eval(opt('service:projects'))[opt('service:default')] +# Default service +SERVICE = ast.literal_eval(opt('service:projects'))[opt('service:default')] -#Command line arguments parsing +# Command line arguments parsing if len(sys.argv) > 2 and sys.argv[2] in allowed_tasks: arg_index = 2 if sys.argv[1] in services: - SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] + SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] elif len(sys.argv) > 1 and sys.argv[1] in allowed_tasks: arg_index = 1 else: show_help() exit(1) -#Procedure for execution +# Procedure for execution task = globals()[sys.argv[arg_index]] if task: target = { - 'update' : 'develop', + 'update': 'develop', 'upgrade': 'mroot' }.get(sys.argv[1], "develop") - if len(sys.argv) > 2 and sys.argv[2] in ['start','stop','restart']: - SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] + if len(sys.argv) > 2 and sys.argv[2] in ['start', 'stop', 'restart']: + SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] print "CMD: " + sys.argv[1] + " " + sys.argv[2], "| SERVICE: ", SERVICE task(SERVICE) elif len(sys.argv) > (arg_index + 1): @@ -262,4 +279,3 @@ if task: else: print "CMD: " + sys.argv[arg_index] task() -