2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-08 15:19:46 +02:00
|
|
|
# Copyright (c) 2017, Alejandro Gomez <alexgomez2202@gmail.com>
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: gunicorn
|
2022-11-09 07:33:03 +01:00
|
|
|
short_description: Run gunicorn with various settings
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
aliases: ['name']
|
|
|
|
description:
|
|
|
|
- The app module. A name refers to a WSGI callable that should be found in the specified module.
|
|
|
|
venv:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases: ['virtualenv']
|
|
|
|
description:
|
|
|
|
- 'Path to the virtualenv directory.'
|
|
|
|
config:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- 'Path to the gunicorn configuration file.'
|
2020-10-31 13:53:57 +01:00
|
|
|
aliases: ['conf']
|
2020-03-09 10:11:07 +01:00
|
|
|
chdir:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- 'Chdir to specified directory before apps loading.'
|
|
|
|
pid:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: path
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
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:
|
2020-10-31 13:53:57 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
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 = '''
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Simple gunicorn run example
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gunicorn:
|
2020-03-09 10:11:07 +01:00
|
|
|
app: 'wsgi'
|
|
|
|
chdir: '/workspace/example'
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Run gunicorn on a virtualenv
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gunicorn:
|
2020-03-09 10:11:07 +01:00
|
|
|
app: 'wsgi'
|
|
|
|
chdir: '/workspace/example'
|
|
|
|
venv: '/workspace/example/venv'
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Run gunicorn with a config file
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gunicorn:
|
2020-03-09 10:11:07 +01:00
|
|
|
app: 'wsgi'
|
|
|
|
chdir: '/workspace/example'
|
|
|
|
conf: '/workspace/example/gunicorn.cfg'
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Run gunicorn as ansible user with specified pid and config file
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gunicorn:
|
2020-03-09 10:11:07 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
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):
|
|
|
|
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']),
|
2021-07-28 08:43:09 +02:00
|
|
|
venv=dict(type='path', aliases=['virtualenv']),
|
|
|
|
config=dict(type='path', aliases=['conf']),
|
|
|
|
chdir=dict(type='path'),
|
|
|
|
pid=dict(type='path'),
|
|
|
|
user=dict(type='str'),
|
|
|
|
worker=dict(type='str', choices=['sync', 'eventlet', 'gevent', 'tornado ', 'gthread', 'gaiohttp']),
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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:
|
2021-07-28 08:43:09 +02:00
|
|
|
gunicorn_command = module.get_bin_path('gunicorn')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
# 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()
|