Browse Source

pep8

master
Maxim Likhachev 6 years ago
parent
commit
4f1eb153dd
  1. 68
      fabfile.py

68
fabfile.py vendored

@ -1,16 +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 updating service/application on remote server using Fabric. # 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
# more details.
# #
# You should have received a copy of the GNU General Public License along with
#Installing fabric: # this program. If not, see <https://www.gnu.org/licenses/>.
# apt-get install python-dev libffi-dev
# pip install configparser
# pip install fabric
from __future__ import with_statement from __future__ import with_statement
from fabric.api import * from fabric.api import *
@ -24,19 +27,23 @@ import configparser
from contextlib import contextmanager from contextlib import contextmanager
#-----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# Class for errors catching # Class for errors catching
class FabricException(Exception): class FabricException(Exception):
pass pass
#----------------------------------------------------------------------------- # ----------------------------------------------------------------------------
#Color highlighting of errors
# Color highlighting of errors
env.colorize_errors = True env.colorize_errors = True
#Generating exceprions instead fails
env.abort_exception=FabricException
#----------------------------------------------------------------------------- # Generating exceprions instead fails
env.abort_exception = FabricException
# ----------------------------------------------------------------------------
@contextmanager @contextmanager
def if_needed(): def if_needed():
@ -46,6 +53,7 @@ def if_needed():
except: except:
pass pass
def readConfig(config_file): def readConfig(config_file):
'''Read config file''' '''Read config file'''
global settings global settings
@ -54,25 +62,30 @@ def readConfig(config_file):
settings.read(config_file) settings.read(config_file)
return settings return settings
@contextmanager @contextmanager
def timestamp(*args): def timestamp(*args):
print '[' + current_time() + ']', args print '[' + current_time() + ']', args
yield yield
def opt(parameter): def opt(parameter):
'''Get options''' '''Get options'''
setting = parameter.split(":") setting = parameter.split(":")
return settings.get(setting[0], setting[1]) return settings.get(setting[0], setting[1])
def background_run(command): def background_run(command):
'''Background execution of commands on remote servers''' '''Background execution of commands on remote servers'''
subprocess.Popen(["nohup", command]) subprocess.Popen(["nohup", command])
def current_time(): def current_time():
'''Current time in HH:MM:SS format''' '''Current time in HH:MM:SS format'''
return datetime.datetime.now().time().strftime('%X') return datetime.datetime.now().time().strftime('%X')
#-----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
@task @task
def make_dist(): def make_dist():
@ -99,6 +112,7 @@ def diff_config():
else: else:
return True return True
def copy_dirs(directories): def copy_dirs(directories):
'''Upload local directories to remote server''' '''Upload local directories to remote server'''
for src_dir, dst_dir in ast.literal_eval(directories): for src_dir, dst_dir in ast.literal_eval(directories):
@ -110,20 +124,22 @@ def copy_dirs(directories):
put(config_file, remote_file) put(config_file, remote_file)
@task @task
def upload_dist(): def upload_dist():
'''Upload distributive to remote server''' '''Upload distributive to remote server'''
dist_dir = os.path.join(opt('product:app_path_dist'), date) dist_dir = os.path.join(opt('product:app_path_dist'), date)
run('mkdir -p %s' % dist_dir) run('mkdir -p %s' % dist_dir)
#Uploading archive # Uploading archive
with lcd(opt('release:app_path_dist')): with lcd(opt('release:app_path_dist')):
put('*.zip', dist_dir) put('*.zip', dist_dir)
#Extracting files # Extracting files
with cd(dist_dir): with cd(dist_dir):
run('yes All | unzip *.zip') run('yes All | unzip *.zip')
@task @task
def copy_config(): def copy_config():
'''Copy configuration files''' '''Copy configuration files'''
@ -134,11 +150,13 @@ def copy_config():
copy_dirs(opt('release:config_dirs')) copy_dirs(opt('release:config_dirs'))
@task @task
def copy_custom_conf(): def copy_custom_conf():
'''Upload custom.conf to remote server''' '''Upload custom.conf to remote server'''
put(opt('release:custom_conf_template'), opt('product:custom_conf_template')) put(opt('release:custom_conf_template'), opt('product:custom_conf_template'))
@task @task
def backup_files(): def backup_files():
'''Back up files''' '''Back up files'''
@ -147,40 +165,47 @@ def backup_files():
for to_backup in ast.literal_eval(opt('product:backup_files')): for to_backup in ast.literal_eval(opt('product:backup_files')):
run('cp -rv %s ../backup/%s' % (os.path.join(opt('product:app_path_stand'), to_backup), date)) run('cp -rv %s ../backup/%s' % (os.path.join(opt('product:app_path_stand'), to_backup), date))
@task @task
def backup_db(): def backup_db():
'''Back up distributive''' '''Back up distributive'''
with cd(opt('product:app_path_stand')): with cd(opt('product:app_path_stand')):
run("%s" % opt('product:backup_db_script')) run("%s" % opt('product:backup_db_script'))
@task @task
def backup_db_copy(): def backup_db_copy():
'''Backround upload of backup to remote server''' '''Backround upload of backup to remote server'''
with cd(opt('product:app_path_stand')): with cd(opt('product:app_path_stand')):
background_run(opt('product:backup_db_copy_script')) background_run(opt('product:backup_db_copy_script'))
@task @task
def stop(): def stop():
'''Stop service''' '''Stop service'''
run('kill $(cat %s)' % opt('product:app_pid_file')) run('kill $(cat %s)' % opt('product:app_pid_file'))
def remove_libs(): def remove_libs():
'''Remove previous version''' '''Remove previous version'''
run('rm -rfv %s' % opt('product:app_path_libs')) run('rm -rfv %s' % opt('product:app_path_libs'))
@task @task
def copy_libs(): def copy_libs():
'''Copy libs''' '''Copy libs'''
with cd(opt('product:app_path_stand')): with cd(opt('product:app_path_stand')):
run('cp -rv %s %s' % (os.path.join(opt('product:app_path_dist'), date, '*/lib'), opt('product:app_path_libs'))) run('cp -rv %s %s' % (os.path.join(opt('product:app_path_dist'), date, '*/lib'), opt('product:app_path_libs')))
@task @task
def start(): def start():
'''Start service''' '''Start service'''
with cd(opt('product:app_path_stand')): with cd(opt('product:app_path_stand')):
run("sh %s >> nohup.out 2>> nohup.out < /dev/null &" % opt('product:app_start_script'), pty=True) run("sh %s >> nohup.out 2>> nohup.out < /dev/null &" % opt('product:app_start_script'), pty=True)
#-----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
@task @task
def update(): def update():
'''Update service''' '''Update service'''
@ -196,21 +221,22 @@ def update():
with timestamp("NEXT STEP"): with timestamp("NEXT STEP"):
pass pass
with if_needed(): stop() with if_needed():
stop()
remove_libs() remove_libs()
copy_libs() copy_libs()
start() start()
#-----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
settings = [] settings = []
readConfig('config.ini') readConfig('config.ini')
#Remote host # Remote host
env.host_string = opt('product:host') env.host_string = opt('product:host')
date = datetime.datetime.now().strftime("%Y-%m-%d") date = datetime.datetime.now().strftime("%Y-%m-%d")

Loading…
Cancel
Save