Skip to content

Instantly share code, notes, and snippets.

@attakei
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attakei/fd6650be46418ccf2e0e to your computer and use it in GitHub Desktop.
Save attakei/fd6650be46418ccf2e0e to your computer and use it in GitHub Desktop.
Ansible module to run pserve command.

Andible simple module to pserve command controll

How to setup

Save pserve.py to library/pserve.py

The library folder needs to be on the same level as your playbook.

Usage

tasks:
- name: start pserve
  pserve: state=started app_path=/path/to/pyramidapp config_uri=development.ini
tasks:
- copy: src=development.ini dest=/path/to/pyramidapp
  notify:
  - restart pyramid
handlers:
- name: restart pyramid
  pserve: state=restarted app_path=/path/to/pyramidapp config_uri=development.ini
#!/usr/bin/env python
"""ansible module to controll pyramid 'pserve'
"""
import os
import time
def check_pid_file_exists(module, pid_path):
cmd = 'ls ' + pid_path
rc, out, err = module.run_command(cmd)
if rc == 0:
return True
else:
return False
def _ensure_virtualenv(module):
venv_param = module.params['virtualenv']
if venv_param is None:
return
vbin = os.path.join(os.path.expanduser(venv_param), 'bin')
activate = os.path.join(vbin, 'activate')
if not os.path.exists(activate):
virtualenv = module.get_bin_path('virtualenv', True)
vcmd = '%s %s' % (virtualenv, venv_param)
vcmd = [virtualenv, venv_param]
rc, out_venv, err_venv = module.run_command(vcmd)
if rc != 0:
_fail(module, vcmd, out_venv, err_venv)
os.environ["PATH"] = "%s:%s" % (vbin, os.environ["PATH"])
os.environ["VIRTUAL_ENV"] = venv_param
def main():
valid_states = ('started', 'stopped', 'restarted', 'reloaded')
module = AnsibleModule(
argument_spec=dict(
state = dict(default=None, required=True),
app_path = dict(default=None, required=True),
config_uri = dict(default=None, required=True),
pythonpath = dict(default=None, required=False, aliases=['python_path']),
virtualenv = dict(default=None, required=False, aliases=['virtual_env']),
name = dict(default=None, required=False),
app_name = dict(default=None, required=False),
server_name = dict(default=None, required=False),
pid_file = dict(default=None, required=False),
),
)
app_path = module.params['app_path']
cmd = 'pserve {config_uri} {command} --daemon {options}'
params = {}
options = {}
config_uri = module.params['config_uri']
params['config_uri'] = config_uri
# Build extra vars
app_name = module.params.get('app_name') or module.params.get('name') or None
if app_name is not None:
options['--app-name'] = app_name
server_name = module.params.get('server_name') or module.params.get('name') or None
if server_name is not None:
options['--server-name'] = server_name
pid_file = module.params.get('pid_file')
if pid_file is not None:
pid_file = os.path.abspath(os.path.join(app_path, pid_file))
options['--pid-file'] = pid_file
else:
pid_file = os.path.abspath(os.path.join(app_path, 'pyramid.pid'))
params['options'] = ' '.join(['%s=%s' % (key, value) for (key, value) in options.items()])
# Controll by state
state = module.params['state']
pid_exists = check_pid_file_exists(module, pid_file)
if state == 'started':
if pid_exists:
module.exit_json(msg='service is already running.')
params['command'] = 'start'
elif state == 'stopped':
if not pid_exists:
module.exit_json(msg='service is already stopped.')
params['command'] = 'stop'
elif state in 'restarted':
if not pid_exists:
module.exit_json(msg='service is not running.')
params['command'] = 'restart'
else:
module.fail_json(msg='"%s" is invalid state. %s' % (state, str(valid_states)))
_ensure_virtualenv(module)
command = cmd.format(**params)
rc, out, err = module.run_command(command, cwd=os.path.expanduser(app_path))
if rc != 0:
module.fail_json(msg=err)
time.sleep(1)
pid_exists_after = check_pid_file_exists(module, pid_file)
if pid_exists_after:
module.exit_json(changed=True, msg=out)
else:
module.fail_json(msg='process failed, ')
# import module snippets
from ansible.module_utils.basic import *
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment