mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
901bca58bb
* fixed validation-modules for apache2_mod_proxy.py
* fixed validation-modules for apache2_module.py
* fixed validation-modules for deploy_helper.py
The ignore lines were put back in place because
add_file_common_args=True is used and the module inherits a number of
options that do not show up in the documentation (nor should they).
* fixed validation-modules for ejabberd_user.py
* fixed validation-modules for gunicorn.py
* fixed validation-modules for htpasswd.py
* fixed validation-modules for jenkins_job.py
* fixed validation-modules for jenkins_job_info.py
* fixed validation-modules for jenkins_plugin.py
* fixed validation-modules for jenkins_script.py
* fixed validation-modules for jira.py
* fixed validation-modules for nginx_status_facts.py
* fixed validation-modules for rundeck_acl_policy.py
* fixed validation-modules for rundeck_project.py
* fixed validation-modules for supervisorctl.py
* fixed validation-modules for taiga_issue.py
* fixed pylint mistake in plugins/modules/web_infrastructure/jenkins_job_info.py
* removed ignore lines for almost-all web_infrastructure modules
* rolled back ignore lines for nested sophos_utm modules that were not adjusted
* Removed doc-missing-type from ignore-2.11.txt for deploy_helper and jenkins_plugin
* When adding lines back to ignore files, we added more than it was before. Removing.
* Rolled back deprecation line in ignore-2.9.txt for nginx_status_facts
* Rolled back yet another line in ignore-2.9.txt for nginx_status_facts
* Fixed argument_spec and docs for crypt_scheme parameter in htpasswd, per PR
* Added extends_documentation_fragment:files to deploy_helper and jenkins_plugin
* Removed long-deprecated option params from jenkins_plugin, removed validate-modules lines from ignore files for that module
* Update plugins/modules/web_infrastructure/htpasswd.py
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit 85371e7b6d
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
230 lines
6.8 KiB
Python
230 lines
6.8 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# (c) 2017, Alejandro Gomez <alexgomez2202@gmail.com>
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
__metaclass__ = type
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: gunicorn
|
|
short_description: Run gunicorn with various settings.
|
|
description:
|
|
- Starts gunicorn with the parameters specified. Common settings for gunicorn
|
|
configuration are supported. For additional configuration use a config file
|
|
See U(https://gunicorn-docs.readthedocs.io/en/latest/settings.html) for more
|
|
options. It's recommended to always use the chdir option to avoid problems
|
|
with the location of the app.
|
|
requirements: [gunicorn]
|
|
author:
|
|
- "Alejandro Gomez (@agmezr)"
|
|
options:
|
|
app:
|
|
type: str
|
|
required: true
|
|
aliases: ['name']
|
|
description:
|
|
- The app module. A name refers to a WSGI callable that should be found in the specified module.
|
|
venv:
|
|
type: path
|
|
aliases: ['virtualenv']
|
|
description:
|
|
- 'Path to the virtualenv directory.'
|
|
config:
|
|
type: path
|
|
description:
|
|
- 'Path to the gunicorn configuration file.'
|
|
aliases: ['conf']
|
|
chdir:
|
|
type: path
|
|
description:
|
|
- 'Chdir to specified directory before apps loading.'
|
|
pid:
|
|
type: path
|
|
description:
|
|
- 'A filename to use for the PID file. If not set and not found on the configuration file a tmp
|
|
pid file will be created to check a successful run of gunicorn.'
|
|
worker:
|
|
type: str
|
|
choices: ['sync', 'eventlet', 'gevent', 'tornado ', 'gthread', 'gaiohttp']
|
|
description:
|
|
- 'The type of workers to use. The default class (sync) should handle most "normal" types of workloads.'
|
|
user:
|
|
type: str
|
|
description:
|
|
- 'Switch worker processes to run as this user.'
|
|
notes:
|
|
- If not specified on config file, a temporary error log will be created on /tmp dir.
|
|
Please make sure you have write access in /tmp dir. Not needed but will help you to
|
|
identify any problem with configuration.
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
- name: Simple gunicorn run example
|
|
community.general.gunicorn:
|
|
app: 'wsgi'
|
|
chdir: '/workspace/example'
|
|
|
|
- name: Run gunicorn on a virtualenv
|
|
community.general.gunicorn:
|
|
app: 'wsgi'
|
|
chdir: '/workspace/example'
|
|
venv: '/workspace/example/venv'
|
|
|
|
- name: Run gunicorn with a config file
|
|
community.general.gunicorn:
|
|
app: 'wsgi'
|
|
chdir: '/workspace/example'
|
|
conf: '/workspace/example/gunicorn.cfg'
|
|
|
|
- name: Run gunicorn as ansible user with specified pid and config file
|
|
community.general.gunicorn:
|
|
app: 'wsgi'
|
|
chdir: '/workspace/example'
|
|
conf: '/workspace/example/gunicorn.cfg'
|
|
venv: '/workspace/example/venv'
|
|
pid: '/workspace/example/gunicorn.pid'
|
|
user: 'ansible'
|
|
'''
|
|
|
|
RETURN = '''
|
|
gunicorn:
|
|
description: process id of gunicorn
|
|
returned: changed
|
|
type: str
|
|
sample: "1234"
|
|
'''
|
|
|
|
import os
|
|
import time
|
|
|
|
# import ansible utils
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
def search_existing_config(config, option):
|
|
''' search in config file for specified option '''
|
|
if config and os.path.isfile(config):
|
|
data_config = None
|
|
with open(config, 'r') as f:
|
|
for line in f:
|
|
if option in line:
|
|
return line
|
|
return None
|
|
|
|
|
|
def remove_tmp_file(file_path):
|
|
''' remove temporary files '''
|
|
if os.path.isfile(file_path):
|
|
os.remove(file_path)
|
|
|
|
|
|
def main():
|
|
|
|
# available gunicorn options on module
|
|
gunicorn_options = {
|
|
'config': '-c',
|
|
'chdir': '--chdir',
|
|
'worker': '-k',
|
|
'user': '-u',
|
|
}
|
|
|
|
module = AnsibleModule(
|
|
argument_spec=dict(
|
|
app=dict(required=True, type='str', aliases=['name']),
|
|
venv=dict(required=False, type='path', default=None, aliases=['virtualenv']),
|
|
config=dict(required=False, default=None, type='path', aliases=['conf']),
|
|
chdir=dict(required=False, type='path', default=None),
|
|
pid=dict(required=False, type='path', default=None),
|
|
user=dict(required=False, type='str'),
|
|
worker=dict(required=False,
|
|
type='str',
|
|
choices=['sync', 'eventlet', 'gevent', 'tornado ', 'gthread', 'gaiohttp']
|
|
),
|
|
)
|
|
)
|
|
|
|
# temporary files in case no option provided
|
|
tmp_error_log = os.path.join(module.tmpdir, 'gunicorn.temp.error.log')
|
|
tmp_pid_file = os.path.join(module.tmpdir, 'gunicorn.temp.pid')
|
|
|
|
# remove temp file if exists
|
|
remove_tmp_file(tmp_pid_file)
|
|
remove_tmp_file(tmp_error_log)
|
|
|
|
# obtain app name and venv
|
|
params = module.params
|
|
app = params['app']
|
|
venv = params['venv']
|
|
pid = params['pid']
|
|
|
|
# use venv path if exists
|
|
if venv:
|
|
gunicorn_command = "/".join((venv, 'bin', 'gunicorn'))
|
|
else:
|
|
gunicorn_command = 'gunicorn'
|
|
|
|
# to daemonize the process
|
|
options = ["-D"]
|
|
|
|
# fill options
|
|
for option in gunicorn_options:
|
|
param = params[option]
|
|
if param:
|
|
options.append(gunicorn_options[option])
|
|
options.append(param)
|
|
|
|
error_log = search_existing_config(params['config'], 'errorlog')
|
|
if not error_log:
|
|
# place error log somewhere in case of fail
|
|
options.append("--error-logfile")
|
|
options.append(tmp_error_log)
|
|
|
|
pid_file = search_existing_config(params['config'], 'pid')
|
|
if not params['pid'] and not pid_file:
|
|
pid = tmp_pid_file
|
|
|
|
# add option for pid file if not found on config file
|
|
if not pid_file:
|
|
options.append('--pid')
|
|
options.append(pid)
|
|
|
|
# put args together
|
|
args = [gunicorn_command] + options + [app]
|
|
rc, out, err = module.run_command(args, use_unsafe_shell=False, encoding=None)
|
|
|
|
if not err:
|
|
# wait for gunicorn to dump to log
|
|
time.sleep(0.5)
|
|
if os.path.isfile(pid):
|
|
with open(pid, 'r') as f:
|
|
result = f.readline().strip()
|
|
|
|
if not params['pid']:
|
|
os.remove(pid)
|
|
|
|
module.exit_json(changed=True, pid=result, debug=" ".join(args))
|
|
else:
|
|
# if user defined own error log, check that
|
|
if error_log:
|
|
error = 'Please check your {0}'.format(error_log.strip())
|
|
else:
|
|
if os.path.isfile(tmp_error_log):
|
|
with open(tmp_error_log, 'r') as f:
|
|
error = f.read()
|
|
# delete tmp log
|
|
os.remove(tmp_error_log)
|
|
else:
|
|
error = "Log not found"
|
|
|
|
module.fail_json(msg='Failed to start gunicorn. {0}'.format(error), error=err)
|
|
|
|
else:
|
|
module.fail_json(msg='Failed to start gunicorn {0}'.format(err), error=err)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|