2016-09-06 14:43:49 +03:00
|
|
|
from fabric.api import env, task, lcd
|
|
|
|
|
from fabric.api import local
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
|
2018-10-17 23:08:51 +03:00
|
|
|
from .utils import executize, virtualenv, loggify
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def command(program=None, cmd=None, extra_param=None):
|
|
|
|
|
"""
|
|
|
|
|
takes an argument and executes that command for nginx
|
|
|
|
|
|
|
|
|
|
program - name of program to be run, eg. 'nginx', 'supervisor'
|
|
|
|
|
cmd - can be 'start', 'stop' or 'status'
|
|
|
|
|
|
|
|
|
|
will then run the appropriate series of commands to get the job done based
|
|
|
|
|
on what we have in the configuration file
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
configuration = env.config
|
|
|
|
|
# logger = loggify('maintenance', 'command')
|
|
|
|
|
|
|
|
|
|
if program is None:
|
2018-10-17 22:48:54 +03:00
|
|
|
print("Error: You have not given a legitimate program")
|
2016-09-06 14:43:49 +03:00
|
|
|
|
2018-10-17 22:48:54 +03:00
|
|
|
print("permissable programs : %s" %
|
|
|
|
|
configuration.maintenance.keys())
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
configuration_program = getattr(configuration.maintenance, program)
|
|
|
|
|
|
2022-04-29 14:41:06 +03:00
|
|
|
# debugging
|
|
|
|
|
# print("configuration_program: %s" %
|
|
|
|
|
# configuration_program.commands.keys())
|
|
|
|
|
|
2016-09-06 14:43:49 +03:00
|
|
|
if cmd is None:
|
2018-10-17 22:48:54 +03:00
|
|
|
print("Error: You have not given a legitimate command")
|
|
|
|
|
print("permissable commands : %s" %
|
|
|
|
|
configuration_program.commands.keys())
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
# find out whether we are using sudo, run or local to
|
|
|
|
|
# execute the nginx command
|
|
|
|
|
|
|
|
|
|
_execute = executize(configuration_program.execute)
|
|
|
|
|
|
|
|
|
|
_command = getattr(configuration_program.commands, cmd)
|
|
|
|
|
|
2022-04-29 14:41:06 +03:00
|
|
|
# debugging
|
|
|
|
|
# print("_command: %s" % _command)
|
|
|
|
|
|
2016-09-06 14:43:49 +03:00
|
|
|
if extra_param is not None:
|
|
|
|
|
_command = "{command} {param}".format(
|
|
|
|
|
command=_command, param=extra_param)
|
|
|
|
|
|
|
|
|
|
if env.debug:
|
|
|
|
|
# logger.debug(
|
|
|
|
|
# "execute type : %s" % configuration_program.execute)
|
|
|
|
|
|
|
|
|
|
# logger.debug(
|
|
|
|
|
# "%s command : %s" % (program, _command))
|
|
|
|
|
|
|
|
|
|
# logger.debug("extra_param : %s" % extra_param)
|
|
|
|
|
|
|
|
|
|
# logger.debug("%s modified command : %s" % (program, _command))
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
_execute(_command)
|
|
|
|
|
|
|
|
|
|
|
2018-10-10 13:06:02 +03:00
|
|
|
def edit(remote_path, host_string=None):
|
2016-09-06 14:43:49 +03:00
|
|
|
"""
|
|
|
|
|
calls up mvim or vim on the file
|
|
|
|
|
|
|
|
|
|
remote_path - path to file we want to edit
|
2018-10-10 13:06:02 +03:00
|
|
|
host_string - what machine the file is located on
|
|
|
|
|
|
|
|
|
|
host_string is necessary b/c in the case where I want to edit
|
|
|
|
|
configuration files, it is essential to do so only on the localhost
|
|
|
|
|
|
|
|
|
|
that is, whether I'm editing staging, development or production project
|
|
|
|
|
yml files, the place to do that editing is ALWAYS on localhost, this is
|
|
|
|
|
because all fabric commands are executed from localhost and will check for
|
|
|
|
|
the necessary configuration files on local (I'm not running fabric from an
|
|
|
|
|
ssh command line on the aws server, I'm running it on my laptop)
|
2016-09-06 14:43:49 +03:00
|
|
|
"""
|
|
|
|
|
|
2018-10-10 13:06:02 +03:00
|
|
|
logger = loggify('maintenance', 'edit')
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
# configuration = env.config
|
|
|
|
|
|
2017-06-12 20:47:35 +03:00
|
|
|
if sys.platform == "darwin":
|
|
|
|
|
editor = "mvim"
|
|
|
|
|
else:
|
2025-04-23 20:12:29 +03:00
|
|
|
editor = "mvim"
|
2017-06-12 20:47:35 +03:00
|
|
|
|
|
|
|
|
# if env.key_filename:
|
|
|
|
|
# cmd_edit = "{editor} sftp://{user}@{host_string}/{remote_path}" \
|
|
|
|
|
# " -i {keyfile}".format(
|
|
|
|
|
# editor=editor,
|
|
|
|
|
# user=env.user,
|
|
|
|
|
# host_string=env.host_string,
|
|
|
|
|
# remote_path=remote_path,
|
|
|
|
|
# keyfile=env.key_filename,
|
|
|
|
|
# )
|
|
|
|
|
# else:
|
|
|
|
|
# cmd_edit = "{editor} sftp://{user}@{host_string}/{remote_path}".format(
|
|
|
|
|
# editor=editor,
|
|
|
|
|
# user=env.user,
|
|
|
|
|
# host_string=env.host_string,
|
|
|
|
|
# remote_path=remote_path)
|
|
|
|
|
|
|
|
|
|
cmd_edit = "{editor} sftp://{user}@{host_string}/{remote_path}".format(
|
|
|
|
|
editor=editor,
|
|
|
|
|
user=env.user,
|
|
|
|
|
host_string=env.host_string,
|
|
|
|
|
remote_path=remote_path)
|
|
|
|
|
|
2016-09-06 14:43:49 +03:00
|
|
|
if env.debug:
|
2018-10-10 13:06:02 +03:00
|
|
|
logger.debug("remote_path : %s" % remote_path)
|
|
|
|
|
logger.debug("env.host_string : %s" % env.host_string)
|
|
|
|
|
logger.debug("sys.platform : %s" % sys.platform)
|
2016-09-06 14:43:49 +03:00
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
if sys.platform == "darwin":
|
|
|
|
|
editor = "mvim"
|
|
|
|
|
else:
|
|
|
|
|
editor = "vim"
|
|
|
|
|
|
2018-10-10 13:06:02 +03:00
|
|
|
if not host_string:
|
|
|
|
|
host_string = env.host_string
|
|
|
|
|
|
2016-09-06 14:43:49 +03:00
|
|
|
cmd_edit = "{editor} sftp://{user}@{host_string}/{remote_path}".format(
|
|
|
|
|
editor=editor,
|
|
|
|
|
user=env.user,
|
2018-10-10 13:06:02 +03:00
|
|
|
host_string=host_string,
|
2016-09-06 14:43:49 +03:00
|
|
|
remote_path=remote_path)
|
|
|
|
|
|
|
|
|
|
local(cmd_edit)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def pyc_delete():
|
|
|
|
|
"""
|
|
|
|
|
Deletes *.pyc files from project source dir
|
|
|
|
|
"""
|
|
|
|
|
configuration = env.config
|
|
|
|
|
|
|
|
|
|
with lcd(configuration.paths.project.root):
|
|
|
|
|
local("find . -name '*.pyc' -delete")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def pyc_compile(force=False):
|
|
|
|
|
"""
|
|
|
|
|
Compile Python source files in a project source dir
|
|
|
|
|
"""
|
|
|
|
|
params = ['']
|
|
|
|
|
|
|
|
|
|
configuration = env.config
|
|
|
|
|
|
|
|
|
|
if force:
|
|
|
|
|
params.append('-f')
|
|
|
|
|
|
|
|
|
|
with lcd(configuration.paths.project.root):
|
|
|
|
|
with virtualenv():
|
|
|
|
|
local("python -m compileall {0} .".format(" ".join(params)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def get_base_dir():
|
|
|
|
|
import os
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
return BASE_DIR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@task
|
|
|
|
|
def get_project_root():
|
|
|
|
|
#
|
|
|
|
|
# NOTE: the PROJECT_ROOT value is very important. Make sure that
|
|
|
|
|
# if you move around this file, you account for where it is and add
|
|
|
|
|
# or take away "os.pardir" from os.path.join
|
|
|
|
|
|
|
|
|
|
PROJECT_ROOT = os.path.abspath(
|
|
|
|
|
os.path.join(get_base_dir(), os.pardir, os.pardir, os.pardir))
|
|
|
|
|
|
|
|
|
|
return PROJECT_ROOT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_configuration_path(name, branch):
|
|
|
|
|
#
|
|
|
|
|
# the locations of the files we need relative to the PROJECT_ROOT
|
|
|
|
|
|
|
|
|
|
param_list = ['config', 'layout']
|
|
|
|
|
|
|
|
|
|
if name not in param_list:
|
2018-10-17 22:48:54 +03:00
|
|
|
print("value %s was not legit. _get_configuration_path requires"
|
|
|
|
|
"value such from %s" % (name, param_list))
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
META_DIR = os.path.join(
|
2016-09-18 16:57:17 +03:00
|
|
|
get_project_root(), 'usr', 'meta', 'project')
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
if name == "config":
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# the configuration we are working with will change
|
|
|
|
|
# depending on what branch is being used. This value
|
|
|
|
|
# is passed in when the function is called
|
|
|
|
|
|
|
|
|
|
configname = "{branch}.yml".format(branch=branch)
|
|
|
|
|
|
|
|
|
|
path_meta = os.path.join(META_DIR, configname)
|
|
|
|
|
|
|
|
|
|
elif name == "layout":
|
|
|
|
|
path_meta = os.path.join(META_DIR, 'layout.yml')
|
|
|
|
|
|
|
|
|
|
return path_meta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_configuration(name, branch):
|
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# the locations of the files we need relative to the PROJECT_ROOT
|
|
|
|
|
|
2023-07-07 15:23:22 +03:00
|
|
|
if name == "config":
|
2016-09-06 14:43:49 +03:00
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# the configuration we are working with will change
|
|
|
|
|
# depending on what branch is being used. This value
|
|
|
|
|
# is passed in when the function is called
|
|
|
|
|
|
|
|
|
|
file_path = _get_configuration_path('config', branch)
|
|
|
|
|
|
|
|
|
|
elif name == "layout":
|
|
|
|
|
file_path = _get_configuration_path('layout', branch)
|
|
|
|
|
|
2022-04-29 14:41:06 +03:00
|
|
|
configuration_file = yaml.load(
|
|
|
|
|
open(file_path, 'r'), Loader=yaml.FullLoader)
|
2016-09-06 14:43:49 +03:00
|
|
|
return configuration_file
|