| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2017, Alejandro Gomez <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: gunicorn |
| 11 |
short_description: Run gunicorn with various settings |
| 12 |
description: |
| 13 |
- Starts gunicorn with the parameters specified. Common settings for gunicorn configuration are supported. For additional |
| 14 |
configuration use a config file See U(https://gunicorn-docs.readthedocs.io/en/latest/settings.html) for more options. |
| 15 |
It's recommended to always use the chdir option to avoid problems with the location of the app. |
| 16 |
requirements: [gunicorn] |
| 17 |
author: |
| 18 |
- "Alejandro Gomez (@agmezr)" |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
app: |
| 28 |
type: str |
| 29 |
required: true |
| 30 |
aliases: ['name'] |
| 31 |
description: |
| 32 |
- The app module. A name refers to a WSGI callable that should be found in the specified module. |
| 33 |
venv: |
| 34 |
type: path |
| 35 |
aliases: ['virtualenv'] |
| 36 |
description: |
| 37 |
- Path to the virtualenv directory. |
| 38 |
config: |
| 39 |
type: path |
| 40 |
description: |
| 41 |
- Path to the gunicorn configuration file. |
| 42 |
aliases: ['conf'] |
| 43 |
chdir: |
| 44 |
type: path |
| 45 |
description: |
| 46 |
- Chdir to specified directory before apps loading. |
| 47 |
pid: |
| 48 |
type: path |
| 49 |
description: |
| 50 |
- A filename to use for the PID file. If not set and not found on the configuration file a tmp pid file is created to |
| 51 |
check a successful run of gunicorn. |
| 52 |
worker: |
| 53 |
type: str |
| 54 |
choices: ['sync', 'eventlet', 'gevent', 'tornado ', 'gthread', 'gaiohttp'] |
| 55 |
description: |
| 56 |
- The type of workers to use. The default class (sync) should handle most "normal" types of workloads. |
| 57 |
user: |
| 58 |
type: str |
| 59 |
description: |
| 60 |
- Switch worker processes to run as this user. |
| 61 |
notes: |
| 62 |
- If not specified on config file, a temporary error log is created on C(/tmp) directory. Please make sure you have write |
| 63 |
access in C(/tmp) directory. Not needed but it is helpful to identify any problem with configuration. |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Simple gunicorn run example |
| 68 |
community.general.gunicorn: |
| 69 |
app: 'wsgi' |
| 70 |
chdir: '/workspace/example' |
| 71 |
|
| 72 |
- name: Run gunicorn on a virtualenv |
| 73 |
community.general.gunicorn: |
| 74 |
app: 'wsgi' |
| 75 |
chdir: '/workspace/example' |
| 76 |
venv: '/workspace/example/venv' |
| 77 |
|
| 78 |
- name: Run gunicorn with a config file |
| 79 |
community.general.gunicorn: |
| 80 |
app: 'wsgi' |
| 81 |
chdir: '/workspace/example' |
| 82 |
conf: '/workspace/example/gunicorn.cfg' |
| 83 |
|
| 84 |
- name: Run gunicorn as ansible user with specified pid and config file |
| 85 |
community.general.gunicorn: |
| 86 |
app: 'wsgi' |
| 87 |
chdir: '/workspace/example' |
| 88 |
conf: '/workspace/example/gunicorn.cfg' |
| 89 |
venv: '/workspace/example/venv' |
| 90 |
pid: '/workspace/example/gunicorn.pid' |
| 91 |
user: 'ansible' |
| 92 |
""" |
| 93 |
|
| 94 |
RETURN = r""" |
| 95 |
gunicorn: |
| 96 |
description: Process ID of gunicorn. |
| 97 |
returned: changed |
| 98 |
type: str |
| 99 |
sample: "1234" |
| 100 |
""" |
| 101 |
|
| 102 |
import os |
| 103 |
import time |
| 104 |
|
| 105 |
from ansible.module_utils.basic import AnsibleModule |
| 106 |
|
| 107 |
|
| 108 |
def search_existing_config(config, option): |
| 109 |
"""search in config file for specified option""" |
| 110 |
if config and os.path.isfile(config): |
| 111 |
with open(config) as f: |
| 112 |
for line in f: |
| 113 |
if option in line: |
| 114 |
return line |
| 115 |
return None |
| 116 |
|
| 117 |
|
| 118 |
def remove_tmp_file(file_path): |
| 119 |
"""remove temporary files""" |
| 120 |
if os.path.isfile(file_path): |
| 121 |
os.remove(file_path) |
| 122 |
|
| 123 |
|
| 124 |
def main(): |
| 125 |
# available gunicorn options on module |
| 126 |
gunicorn_options = { |
| 127 |
"config": "-c", |
| 128 |
"chdir": "--chdir", |
| 129 |
"worker": "-k", |
| 130 |
"user": "-u", |
| 131 |
} |
| 132 |
|
| 133 |
module = AnsibleModule( |
| 134 |
argument_spec=dict( |
| 135 |
app=dict(required=True, type="str", aliases=["name"]), |
| 136 |
venv=dict(type="path", aliases=["virtualenv"]), |
| 137 |
config=dict(type="path", aliases=["conf"]), |
| 138 |
chdir=dict(type="path"), |
| 139 |
pid=dict(type="path"), |
| 140 |
user=dict(type="str"), |
| 141 |
worker=dict(type="str", choices=["sync", "eventlet", "gevent", "tornado ", "gthread", "gaiohttp"]), |
| 142 |
) |
| 143 |
) |
| 144 |
|
| 145 |
# temporary files in case no option provided |
| 146 |
tmp_error_log = os.path.join(module.tmpdir, "gunicorn.temp.error.log") |
| 147 |
tmp_pid_file = os.path.join(module.tmpdir, "gunicorn.temp.pid") |
| 148 |
|
| 149 |
# remove temp file if exists |
| 150 |
remove_tmp_file(tmp_pid_file) |
| 151 |
remove_tmp_file(tmp_error_log) |
| 152 |
|
| 153 |
# obtain app name and venv |
| 154 |
params = module.params |
| 155 |
app = params["app"] |
| 156 |
venv = params["venv"] |
| 157 |
pid = params["pid"] |
| 158 |
|
| 159 |
# use venv path if exists |
| 160 |
if venv: |
| 161 |
gunicorn_command = f"{venv}/bin/gunicorn" |
| 162 |
else: |
| 163 |
gunicorn_command = module.get_bin_path("gunicorn") |
| 164 |
|
| 165 |
# to daemonize the process |
| 166 |
options = ["-D"] |
| 167 |
|
| 168 |
# fill options |
| 169 |
for option in gunicorn_options: |
| 170 |
param = params[option] |
| 171 |
if param: |
| 172 |
options.append(gunicorn_options[option]) |
| 173 |
options.append(param) |
| 174 |
|
| 175 |
error_log = search_existing_config(params["config"], "errorlog") |
| 176 |
if not error_log: |
| 177 |
# place error log somewhere in case of fail |
| 178 |
options.append("--error-logfile") |
| 179 |
options.append(tmp_error_log) |
| 180 |
|
| 181 |
pid_file = search_existing_config(params["config"], "pid") |
| 182 |
if not params["pid"] and not pid_file: |
| 183 |
pid = tmp_pid_file |
| 184 |
|
| 185 |
# add option for pid file if not found on config file |
| 186 |
if not pid_file: |
| 187 |
options.append("--pid") |
| 188 |
options.append(pid) |
| 189 |
|
| 190 |
# put args together |
| 191 |
args = [gunicorn_command] + options + [app] |
| 192 |
rc, out, err = module.run_command(args, use_unsafe_shell=False, encoding=None) |
| 193 |
|
| 194 |
if not err: |
| 195 |
# wait for gunicorn to dump to log |
| 196 |
time.sleep(0.5) |
| 197 |
if os.path.isfile(pid): |
| 198 |
with open(pid) as f: |
| 199 |
result = f.readline().strip() |
| 200 |
|
| 201 |
if not params["pid"]: |
| 202 |
os.remove(pid) |
| 203 |
|
| 204 |
module.exit_json(changed=True, pid=result, debug=" ".join(args)) |
| 205 |
else: |
| 206 |
# if user defined own error log, check that |
| 207 |
if error_log: |
| 208 |
error = f"Please check your {error_log.strip()}" |
| 209 |
else: |
| 210 |
if os.path.isfile(tmp_error_log): |
| 211 |
with open(tmp_error_log) as f: |
| 212 |
error = f.read() |
| 213 |
# delete tmp log |
| 214 |
os.remove(tmp_error_log) |
| 215 |
else: |
| 216 |
error = "Log not found" |
| 217 |
|
| 218 |
module.fail_json(msg=f"Failed to start gunicorn. {error}", error=err) |
| 219 |
|
| 220 |
else: |
| 221 |
module.fail_json(msg=f"Failed to start gunicorn {err}", error=err) |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
main() |