from fabric.api import env, task from fabric.contrib.files import upload_template from fabric.operations import sudo, run # from fabric.api import local import os from .maintenance import command as maintenance_command from .maintenance import edit as maintenance_edit from .utils import loggify def _initialize(configuration): params = dict() conf_name = "{project_name}.{project_branch}".format( project_name=configuration.project.name, project_branch=configuration.project.branch) conf_file = "{conf_name}.conf".format( conf_name=conf_name) conf_path = os.path.join( configuration.paths.supervisor.conf, conf_file) params['conf_name'] = conf_name params['conf_file'] = conf_file params['conf_path'] = conf_path return params @task def command(cmd=None): """ wrapper for the maintenance.command function """ # NOTE # if you find yourself getting errors running the supervisor commands, try # checking to see if supervisord has been started. Sometimes that's all the # trouble right there configuration = env.config param = _initialize(configuration) if cmd == "update": print("in update") # we don't need to specify the supervisor configuration file name, because # supervisor can figure it out. ie whatever.conf is referred to by # whatever if you have an error with sueprvisor, go to the log files and # double check what's going on. conf_name = param['conf_name'] maintenance_command('supervisor', cmd, conf_name) @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 reload(): """ reload supervisor """ command('reload') @task def update(): """ tell supervisor to update itself with the new configuration scripts """ command('update') @task def restart(): stop() start() @task def edit(param='help'): """ calls up mvim on the Supervisor conf file """ configuration = env.config if env.debug: logger = loggify('supervisor', 'edit') logger.debug() configuration_path = _initialize(configuration)['conf_path'] locations = { 'configuration': { 'path': configuration_path, 'desc': "supervisor configuration path", }, 'log.out': { 'path': configuration.logging.supervisor.out, 'desc': "out logging file", }, 'log.err': { 'path': configuration.logging.supervisor.err, 'desc': "err logging file", }, } if param in locations.keys(): remote_path = locations[param]['path'] maintenance_edit(remote_path=remote_path) else: # if param == 'help': print(""" "fab django.edit" automates editing files important to django 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 edit_gunicorn(): """ calls up mvim on the gunicorn conf file """ configuration = env.config if env.debug: logger = loggify('supervisor', 'edit_gunicorn') gunicorn_conf = os.path.join( configuration.templates.gunicorn.path.dest, 'build', configuration.templates.gunicorn.conf.dst) if env.debug: logger.debug("gunicorn_conf : %s" % gunicorn_conf) else: maintenance_edit(remote_path=gunicorn_conf) @task def upload(): """ create the supervisor configuration script put it in the build folder and link it to the scripts directory """ configuration = env.config if env.debug: logger = loggify('supervisor', 'upload') context = dict() context['project_name'] = configuration.project.name context['project_branch'] = configuration.project.branch context['project_user'] = configuration.project.user context['project_group'] = configuration.project.group # # sometimes this name is going to be different than the name we use for the # rest of the project context['django_settings_folder'] = \ configuration.paths.django.settings.base context['server_path_virtualenv_bin'] = os.path.join( configuration.paths.server.virtual, 'bin') context['server_path_code'] = configuration.paths.server.code context['server_path_scripts'] = configuration.paths.server.scripts context['gunicorn_conf_file'] = configuration.templates.gunicorn.conf.dst context['supervisor_logs_out'] = configuration.logging.supervisor.out context['supervisor_logs_err'] = configuration.logging.supervisor.err build_path = os.path.join( configuration.templates.supervisor.path.remote, 'build', configuration.templates.supervisor.conf.dst) files_path = os.path.join( configuration.templates.supervisor.path.local, 'files') copy_name = "{project_name}.{project_branch}.conf".format( project_name=configuration.project.name, project_branch=configuration.project.branch) copy_path = os.path.join( configuration.paths.supervisor.conf, copy_name) copy_command = "cp {build_path} {copy_path}".format( build_path=build_path, copy_path=copy_path) if env.debug: for key in context.keys(): logger.debug("%s\t\t: %s" % (key, context[key])) logger.debug('templates.conf.src : %s' % configuration.templates.supervisor.conf.src) logger.debug('build_path : %s' % build_path) logger.debug('files_path : %s' % files_path) if configuration.project.sudo: logger.debug("sudo(%s)" % copy_command) else: logger.debug("run(%s)" % copy_command) else: from . import utils utils.printvar("files_path", files_path) upload_template( filename=configuration.templates.supervisor.conf.src, destination=build_path, context=context, use_jinja=True, backup=True, template_dir=files_path ) if configuration.project.sudo: sudo(copy_command) else: run(copy_command) update() # restart() @task def remove(): """ stop the supervisor process for this branch, then remove the supervisor conf file from supervisor/conf.d """ if env.debug: logger = loggify('supervisor', 'remove') configuration = env.config param = _initialize(configuration) conf_path = param['conf_path'] # # include the '-f' option so that if nothing is there # it won't return an error rm_command = "rm -f {conf_path}".format(conf_path=conf_path) if env.debug: logger.debug("conf path : %s" % conf_path) logger.debug("rm_command : %s" % rm_command) else: stop() sudo(rm_command) @task def test(param): status()