|
|
@ -1,19 +1,19 @@ |
|
|
|
#!/usr/bin/env python |
|
|
|
#!/usr/bin/env python |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Copyright (C) 2017, Maxim Lihachev, <envrm@yandex.ru> |
|
|
|
# |
|
|
|
# |
|
|
|
# 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 |
|
|
|
# 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 |
|
|
|
#Requirements |
|
|
|
# more details. |
|
|
|
# |
|
|
|
|
|
|
|
# CentOS: yum -y install python-pip |
|
|
|
|
|
|
|
# Debian: apt-get install python-pip |
|
|
|
|
|
|
|
# |
|
|
|
|
|
|
|
# pip install configparser |
|
|
|
|
|
|
|
# |
|
|
|
# |
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License along with |
|
|
|
|
|
|
|
# this program. If not, see <https://www.gnu.org/licenses/>. |
|
|
|
|
|
|
|
|
|
|
|
import re |
|
|
|
import re |
|
|
|
import os |
|
|
|
import os |
|
|
@ -28,6 +28,7 @@ import configparser |
|
|
|
from string import Template |
|
|
|
from string import Template |
|
|
|
from contextlib import contextmanager |
|
|
|
from contextlib import contextmanager |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager |
|
|
|
@contextmanager |
|
|
|
def if_needed(): |
|
|
|
def if_needed(): |
|
|
|
'''Execute with error ignoring''' |
|
|
|
'''Execute with error ignoring''' |
|
|
@ -36,6 +37,7 @@ def if_needed(): |
|
|
|
except: |
|
|
|
except: |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Dir: |
|
|
|
class Dir: |
|
|
|
'''Execute procedures in directory''' |
|
|
|
'''Execute procedures in directory''' |
|
|
|
def __init__(self, directory): |
|
|
|
def __init__(self, directory): |
|
|
@ -48,7 +50,8 @@ class Dir: |
|
|
|
def __exit__(self, type, value, traceback): |
|
|
|
def __exit__(self, type, value, traceback): |
|
|
|
os.chdir(self.previous_dir) |
|
|
|
os.chdir(self.previous_dir) |
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
def readConfig(config_file): |
|
|
|
def readConfig(config_file): |
|
|
|
'''Loading configuration file''' |
|
|
|
'''Loading configuration file''' |
|
|
@ -58,6 +61,7 @@ def readConfig(config_file): |
|
|
|
settings.read(config_file) |
|
|
|
settings.read(config_file) |
|
|
|
return settings |
|
|
|
return settings |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def opt(parameter): |
|
|
|
def opt(parameter): |
|
|
|
'''Getting parameters from file''' |
|
|
|
'''Getting parameters from file''' |
|
|
|
setting = parameter.split(":") |
|
|
|
setting = parameter.split(":") |
|
|
@ -69,13 +73,15 @@ def opt(parameter): |
|
|
|
|
|
|
|
|
|
|
|
return Template(value).safe_substitute(**substr) |
|
|
|
return Template(value).safe_substitute(**substr) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def saveTimestamp(outfile, mode="a"): |
|
|
|
def saveTimestamp(outfile, mode="a"): |
|
|
|
'''Saving timestamt to file''' |
|
|
|
'''Saving timestamt to file''' |
|
|
|
with open(outfile, mode) as log: |
|
|
|
with open(outfile, mode) as log: |
|
|
|
now = datetime.datetime.now() |
|
|
|
now = datetime.datetime.now() |
|
|
|
log.write(now.strftime("%Y-%m-%d %H:%M:%S\n")) |
|
|
|
log.write(now.strftime("%Y-%m-%d %H:%M:%S\n")) |
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
def git(command, stand="develop"): |
|
|
|
def git(command, stand="develop"): |
|
|
|
'''Executing git command''' |
|
|
|
'''Executing git command''' |
|
|
@ -84,6 +90,7 @@ def git(command, stand="develop"): |
|
|
|
|
|
|
|
|
|
|
|
return git_output |
|
|
|
return git_output |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop(stand="develop"): |
|
|
|
def stop(stand="develop"): |
|
|
|
'''Stop service''' |
|
|
|
'''Stop service''' |
|
|
|
if os.path.isfile(opt(stand + ':pid')): |
|
|
|
if os.path.isfile(opt(stand + ':pid')): |
|
|
@ -98,6 +105,7 @@ def stop(stand="develop"): |
|
|
|
os.remove(pid) |
|
|
|
os.remove(pid) |
|
|
|
print "#### Application stopped" |
|
|
|
print "#### Application stopped" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(stand): |
|
|
|
def start(stand): |
|
|
|
'''Start service''' |
|
|
|
'''Start service''' |
|
|
|
service_dir = opt(stand + ':path') |
|
|
|
service_dir = opt(stand + ':path') |
|
|
@ -118,16 +126,19 @@ def start(stand): |
|
|
|
print return_code |
|
|
|
print return_code |
|
|
|
exit(1) |
|
|
|
exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def restart(stand="develop"): |
|
|
|
def restart(stand="develop"): |
|
|
|
'''Restart service''' |
|
|
|
'''Restart service''' |
|
|
|
stop(stand) |
|
|
|
stop(stand) |
|
|
|
start(stand) |
|
|
|
start(stand) |
|
|
|
saveTimestamp(opt(stand + ':restarts_log')) |
|
|
|
saveTimestamp(opt(stand + ':restarts_log')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def clean_all(): |
|
|
|
def clean_all(): |
|
|
|
'''Remove artefacts''' |
|
|
|
'''Remove artefacts''' |
|
|
|
os.system("rm -rfv " + opt('develop:targets')) |
|
|
|
os.system("rm -rfv " + opt('develop:targets')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build(target="stand", modules=None): |
|
|
|
def build(target="stand", modules=None): |
|
|
|
'''Build service. [stand|modules|all]''' |
|
|
|
'''Build service. [stand|modules|all]''' |
|
|
|
play_cmd = opt(SERVICE + ':play') + " " + opt(SERVICE + ':sbt_config') |
|
|
|
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": |
|
|
|
if target == "modules" or target == "all": |
|
|
|
with Dir(opt('develop:modules_dir')): |
|
|
|
with Dir(opt('develop:modules_dir')): |
|
|
|
if modules == None: |
|
|
|
if modules is None: |
|
|
|
modules = os.walk(".").next()[1] |
|
|
|
modules = os.walk(".").next()[1] |
|
|
|
|
|
|
|
|
|
|
|
modules = filter(None, set(modules)) |
|
|
|
modules = filter(None, set(modules)) |
|
|
@ -164,6 +175,7 @@ def build(target="stand", modules=None): |
|
|
|
if return_code != 0: |
|
|
|
if return_code != 0: |
|
|
|
exit(1) |
|
|
|
exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_from_git(): |
|
|
|
def update_from_git(): |
|
|
|
'''Update app with modules''' |
|
|
|
'''Update app with modules''' |
|
|
|
git('checkout ' + opt('build:git_branch')) |
|
|
|
git('checkout ' + opt('build:git_branch')) |
|
|
@ -180,20 +192,23 @@ def update_from_git(): |
|
|
|
|
|
|
|
|
|
|
|
build("all", modules) |
|
|
|
build("all", modules) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def copy_libs(): |
|
|
|
def copy_libs(): |
|
|
|
'''Copy libraries''' |
|
|
|
'''Copy libraries''' |
|
|
|
with if_needed(): |
|
|
|
with if_needed(): |
|
|
|
shutil.rmtree(opt(SERVICE + ':class_path')) |
|
|
|
shutil.rmtree(opt(SERVICE + ':class_path')) |
|
|
|
shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path')) |
|
|
|
shutil.copytree(opt('develop:class_path'), opt(SERVICE + ':class_path')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update(): |
|
|
|
def update(): |
|
|
|
'''Update current service from repository''' |
|
|
|
'''Update current service from repository''' |
|
|
|
saveTimestamp(opt('develop:restarts_log')) |
|
|
|
saveTimestamp(opt('develop:restarts_log')) |
|
|
|
stop() |
|
|
|
stop() |
|
|
|
update_from_git() |
|
|
|
update_from_git() |
|
|
|
start() |
|
|
|
start(SERVICE) |
|
|
|
saveTimestamp(opt('develop:update_log'), 'w') |
|
|
|
saveTimestamp(opt('develop:update_log'), 'w') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upgrade(): |
|
|
|
def upgrade(): |
|
|
|
'''Update developing service from repository''' |
|
|
|
'''Update developing service from repository''' |
|
|
|
update_from_git() |
|
|
|
update_from_git() |
|
|
@ -204,7 +219,8 @@ def upgrade(): |
|
|
|
start(SERVICE) |
|
|
|
start(SERVICE) |
|
|
|
saveTimestamp(opt(SERVICE + ':update_log'), 'w') |
|
|
|
saveTimestamp(opt(SERVICE + ':update_log'), 'w') |
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------- |
|
|
|
|
|
|
|
|
|
|
|
def show_help(): |
|
|
|
def show_help(): |
|
|
|
exe = os.path.basename(sys.argv[0]) |
|
|
|
exe = os.path.basename(sys.argv[0]) |
|
|
@ -220,19 +236,20 @@ def show_help(): |
|
|
|
print "\n[service] can be one of «" + "», «".join(ast.literal_eval(opt('service:projects')).keys()) + "»." |
|
|
|
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" |
|
|
|
print "\nDefault value of [service] is", opt('service:default'), "\n" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
settings = [] |
|
|
|
settings = [] |
|
|
|
|
|
|
|
|
|
|
|
readConfig('config.ini') |
|
|
|
readConfig('config.ini') |
|
|
|
|
|
|
|
|
|
|
|
allowed_tasks = ['start', 'stop', 'restart', 'build', 'update', 'upgrade'] |
|
|
|
allowed_tasks = ['start', 'stop', 'restart', 'build', 'update', 'upgrade'] |
|
|
|
|
|
|
|
|
|
|
|
#Services allowed for building |
|
|
|
# Services allowed for building |
|
|
|
services = ast.literal_eval(opt('service:projects')) |
|
|
|
services = ast.literal_eval(opt('service:projects')) |
|
|
|
|
|
|
|
|
|
|
|
#Default service |
|
|
|
# Default service |
|
|
|
SERVICE = ast.literal_eval(opt('service:projects'))[opt('service:default')] |
|
|
|
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: |
|
|
|
if len(sys.argv) > 2 and sys.argv[2] in allowed_tasks: |
|
|
|
arg_index = 2 |
|
|
|
arg_index = 2 |
|
|
|
if sys.argv[1] in services: |
|
|
|
if sys.argv[1] in services: |
|
|
@ -243,16 +260,16 @@ else: |
|
|
|
show_help() |
|
|
|
show_help() |
|
|
|
exit(1) |
|
|
|
exit(1) |
|
|
|
|
|
|
|
|
|
|
|
#Procedure for execution |
|
|
|
# Procedure for execution |
|
|
|
task = globals()[sys.argv[arg_index]] |
|
|
|
task = globals()[sys.argv[arg_index]] |
|
|
|
|
|
|
|
|
|
|
|
if task: |
|
|
|
if task: |
|
|
|
target = { |
|
|
|
target = { |
|
|
|
'update' : 'develop', |
|
|
|
'update': 'develop', |
|
|
|
'upgrade': 'mroot' |
|
|
|
'upgrade': 'mroot' |
|
|
|
}.get(sys.argv[1], "develop") |
|
|
|
}.get(sys.argv[1], "develop") |
|
|
|
|
|
|
|
|
|
|
|
if len(sys.argv) > 2 and sys.argv[2] in ['start','stop','restart']: |
|
|
|
if len(sys.argv) > 2 and sys.argv[2] in ['start', 'stop', 'restart']: |
|
|
|
SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] |
|
|
|
SERVICE = ast.literal_eval(opt('service:projects'))[sys.argv[1]] |
|
|
|
print "CMD: " + sys.argv[1] + " " + sys.argv[2], "| SERVICE: ", SERVICE |
|
|
|
print "CMD: " + sys.argv[1] + " " + sys.argv[2], "| SERVICE: ", SERVICE |
|
|
|
task(SERVICE) |
|
|
|
task(SERVICE) |
|
|
@ -262,4 +279,3 @@ if task: |
|
|
|
else: |
|
|
|
else: |
|
|
|
print "CMD: " + sys.argv[arg_index] |
|
|
|
print "CMD: " + sys.argv[arg_index] |
|
|
|
task() |
|
|
|
task() |
|
|
|
|
|
|
|
|