320 lines
8.6 KiB
Python
320 lines
8.6 KiB
Python
from fabric.api import env, task
|
|
from fabric.contrib.files import upload_template, exists
|
|
from fabric.operations import sudo
|
|
# from fabric.api import local
|
|
# from fabric.operations import run
|
|
import os
|
|
# import sys
|
|
|
|
from .maintenance import command as maintenance_command
|
|
from .maintenance import edit as maintenance_edit
|
|
|
|
from .utils import loggify
|
|
|
|
|
|
@task
|
|
def command(cmd=None):
|
|
"""
|
|
wrapper for the maintenance.command function
|
|
"""
|
|
|
|
# configuration = env.config
|
|
# logger = loggify('nginx', 'command')
|
|
|
|
maintenance_command('nginx', cmd)
|
|
|
|
|
|
@task
|
|
def start():
|
|
"""
|
|
wrapper for using above command:cmd=start
|
|
"""
|
|
|
|
command("start")
|
|
|
|
|
|
@task
|
|
def stop():
|
|
"""
|
|
wrapper for using above command:cmd=stop
|
|
"""
|
|
|
|
command("stop")
|
|
|
|
|
|
@task
|
|
def status():
|
|
"""
|
|
wrapper for using above command:cmd=stop
|
|
"""
|
|
|
|
command("status")
|
|
|
|
|
|
@task
|
|
def restart():
|
|
stop()
|
|
start()
|
|
|
|
|
|
@task
|
|
def edit(param='help'):
|
|
"""
|
|
calls up mvim on the Nginx conf file
|
|
"""
|
|
|
|
configuration = env.config
|
|
|
|
config_file_path = os.path.join(
|
|
configuration.nginx.sites_available,
|
|
configuration.nginx.conf.name)
|
|
|
|
locations = {
|
|
'conf': {
|
|
'path': config_file_path,
|
|
'desc': "nginx configuration file",
|
|
},
|
|
|
|
'log.error': {
|
|
'path': configuration.logging.nginx.error,
|
|
'desc': "nginx error log file",
|
|
},
|
|
|
|
'log.access': {
|
|
'path': configuration.logging.nxing.access,
|
|
'desc': "nginx access log file",
|
|
},
|
|
}
|
|
|
|
if param in locations.keys():
|
|
remote_path = locations[param]['path']
|
|
maintenance_edit(remote_path=remote_path)
|
|
else:
|
|
# if param == 'help':
|
|
|
|
print("""
|
|
"fab nginx.edit" automates editing files important to nginx whether
|
|
locally or remotely
|
|
|
|
to use this you must pass one of the editable locations in as a
|
|
parameter
|
|
|
|
currently editable locations are:
|
|
""")
|
|
|
|
for k_loc in locations.keys():
|
|
print("\t{0: <20} - {1}".format(k_loc, locations[k_loc]['desc']))
|
|
|
|
return
|
|
|
|
|
|
@task
|
|
def upload():
|
|
"""
|
|
put the nginx conf file for this project into nginx sites-available
|
|
"""
|
|
|
|
if env.debug:
|
|
logger = loggify('nginx', 'upload')
|
|
|
|
configuration = env.config
|
|
|
|
context = dict()
|
|
|
|
context['server_name'] = configuration.server.nginx.host
|
|
|
|
context['access_log'] = configuration.logging.nginx.access
|
|
context['error_log'] = configuration.logging.nginx.error
|
|
|
|
context['port'] = configuration.server.nginx.port
|
|
context['socket'] = configuration.server.nginx.socket
|
|
|
|
context['django_host'] = configuration.server.django.host
|
|
context['django_port'] = configuration.server.django.port
|
|
|
|
context['extended_name'] = configuration.project.extendedname
|
|
|
|
context['virtualenv_sitepackages'] = \
|
|
configuration.virtualenv.paths.sitepackages
|
|
|
|
context['server_media_static'] = \
|
|
configuration.paths.server.media.static
|
|
|
|
context['server_media_dynamic'] = \
|
|
configuration.paths.server.media.dynamic
|
|
|
|
destination_available = os.path.join(
|
|
configuration.nginx.sites_available,
|
|
configuration.nginx.conf.name)
|
|
|
|
destination_enabled = os.path.join(
|
|
configuration.nginx.sites_enabled,
|
|
configuration.nginx.conf.name)
|
|
|
|
build_path = os.path.join(
|
|
configuration.templates.nginx.path.remote,
|
|
'build',
|
|
configuration.templates.nginx.conf.dst)
|
|
|
|
files_path = os.path.join(
|
|
configuration.templates.nginx.path.local,
|
|
'files')
|
|
|
|
#
|
|
# this only gets used if sites_availabe is NOT equal to sites_enabled
|
|
|
|
cmd_link_available_enabled = 'ln -sf {available} {enabled}'.format(
|
|
available=destination_available,
|
|
enabled=destination_enabled)
|
|
|
|
if env.debug:
|
|
logger.debug("filename : %s" %
|
|
configuration.templates.nginx.conf.src)
|
|
|
|
logger.debug("dest_avail : %s" % destination_available)
|
|
logger.debug("dest_enabl : %s" % destination_enabled)
|
|
logger.debug("build_path : %s" % build_path)
|
|
logger.debug("files_path: : %s" % files_path)
|
|
logger.debug("context : %s" % context)
|
|
|
|
upload_msg1 = "upload_template(" \
|
|
"\n\tfilename={filename}," \
|
|
"\n\tdestination={destination_available}," \
|
|
"\n\tcontext=context," \
|
|
"\n\tuse_jinja=True," \
|
|
"\n\tuse_sudo=True," \
|
|
"\n\tbackup=False," \
|
|
"\n\ttemplate_dir={template_dir})".format(
|
|
filename=configuration.templates.nginx.conf.src,
|
|
destination_available=build_path,
|
|
context=context,
|
|
template_dir=files_path)
|
|
|
|
upload_msg2 = "upload_template(" \
|
|
"\n\tfilename={filename}," \
|
|
"\n\tdestination={destination_available}," \
|
|
"\n\tcontext=context," \
|
|
"\n\tuse_jinja=True," \
|
|
"\n\tuse_sudo=True," \
|
|
"\n\tbackup=False," \
|
|
"\n\ttemplate_dir={template_dir})".format(
|
|
filename=configuration.templates.nginx.conf.src,
|
|
destination_available=destination_available,
|
|
context=context,
|
|
template_dir=files_path)
|
|
|
|
logger.debug("upload cmd 1: %s" % upload_msg1)
|
|
logger.debug("upload cmd 2: %s" % upload_msg2)
|
|
|
|
copy_msg = "cp -s {build_path} {dest_path}".format(
|
|
build_path=build_path,
|
|
dest_path=destination_available)
|
|
|
|
logger.debug("sudo('%s')" % copy_msg)
|
|
|
|
logger.debug("\nsites_available : %s"
|
|
% configuration.nginx.sites_available)
|
|
|
|
logger.debug("sites_enabled : %s"
|
|
% configuration.nginx.sites_enabled)
|
|
|
|
logger.debug("if sites_enabled != sites_available then ...")
|
|
logger.debug("sudo('%s')" % cmd_link_available_enabled)
|
|
|
|
else:
|
|
# put the nginx.conf in the build directory and the
|
|
# /etc/nginx/sites-avaialbe location
|
|
|
|
upload_template(
|
|
filename=configuration.templates.nginx.conf.src,
|
|
destination=build_path,
|
|
context=context,
|
|
use_jinja=True,
|
|
use_sudo=False,
|
|
backup=False,
|
|
template_dir=files_path)
|
|
|
|
upload_template(
|
|
filename=configuration.templates.nginx.conf.src,
|
|
destination=destination_available,
|
|
context=context,
|
|
use_jinja=True,
|
|
use_sudo=True,
|
|
backup=True,
|
|
template_dir=files_path)
|
|
|
|
if configuration.nginx.sites_available \
|
|
== configuration.nginx.sites_enabled:
|
|
|
|
# if the sites_available and sites_enabled directories are the
|
|
# same then do nothing
|
|
pass
|
|
|
|
else:
|
|
sudo(cmd_link_available_enabled)
|
|
|
|
# got to this point? then restart the nginx server
|
|
|
|
restart()
|
|
|
|
|
|
@task
|
|
def remove():
|
|
"""
|
|
remove the nginx conf file from sites-available, and if necessary, from
|
|
sites-enabled too.
|
|
|
|
then restart the server
|
|
"""
|
|
|
|
if env.debug:
|
|
logger = loggify('nginx', 'remove')
|
|
|
|
configuration = env.config
|
|
|
|
sites_available = os.path.join(
|
|
configuration.nginx.sites_available,
|
|
configuration.nginx.conf.name)
|
|
|
|
sites_available_bak = sites_available + ".bak"
|
|
|
|
sites_enabled = os.path.join(
|
|
configuration.nginx.sites_enabled,
|
|
configuration.nginx.conf.name)
|
|
|
|
#
|
|
# include the '-f' option so that if nothing is there
|
|
# it won't return an error
|
|
|
|
cmd_remove_enabled = 'rm -f {enabled}'.format(
|
|
enabled=sites_enabled)
|
|
|
|
cmd_remove_available = 'rm -f {available}'.format(
|
|
available=sites_available)
|
|
|
|
cmd_remove_available_bak = 'rm -f {available}'.format(
|
|
available=sites_available_bak)
|
|
|
|
if env.debug:
|
|
logger.debug("sites_available : %s" % sites_available)
|
|
logger.debug("sites_enabled : %s" % sites_enabled)
|
|
|
|
logger.debug("sites_available.bak : %s" % sites_available_bak)
|
|
|
|
logger.debug("remove enabled : %s" % cmd_remove_enabled)
|
|
logger.debug("remove available : %s" % cmd_remove_available)
|
|
logger.debug("remove available.bak : %s" % cmd_remove_available_bak)
|
|
|
|
logger.debug("rm_en == rm_avail? %s" % (cmd_remove_enabled !=
|
|
cmd_remove_available))
|
|
|
|
else:
|
|
if cmd_remove_enabled != cmd_remove_available:
|
|
sudo(cmd_remove_enabled)
|
|
|
|
sudo(cmd_remove_available)
|
|
|
|
if exists(sites_available_bak):
|
|
sudo(cmd_remove_available_bak)
|
|
|
|
restart()
|