Skip to content

Commit

Permalink
feat: dokku_checks module (#136)
Browse files Browse the repository at this point in the history
Add ` dokku_checks` module that allows disabling the zero-downtime checks for dokku apps.

Note: This module currently only allows to disable or enable *all* checks of a given app.
It is not suitable for managing apps where checks should be disabled only for individual processes.

Co-authored-by: Leopold Talirz <[email protected]>
  • Loading branch information
fr3fou and ltalirz authored Nov 28, 2021
1 parent 9135838 commit a6706ad
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,31 @@ Manages ssl configuration for an app.
state: absent
```

### dokku_checks

Manage the Zero Downtime checks for a dokku app

#### Parameters

|Parameter|Choices/Defaults|Comments|
|---------|----------------|--------|
|app<br /><sup>*required*</sup>||The name of the app|
|state|*Choices:* <ul><li>**present** (default)</li><li>absent</li></ul>|The state of the checks functionality|

#### Example

```yaml
- name: Disable the zero downtime deployment
dokku_checks:
app: hello-world
state: absent
- name: Re-enable the zero downtime deployment (enabled by default)
dokku_checks:
app: hello-world
state: present
```

### dokku_clone

Clone a git repository and deploy app.
Expand Down
133 changes: 133 additions & 0 deletions library/dokku_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.dokku_utils import subprocess_check_output

DOCUMENTATION = """
---
module: dokku_checks
short_description: Manage the Zero Downtime checks for a dokku app
options:
app:
description:
- The name of the app
required: True
default: null
aliases: []
state:
description:
- The state of the checks functionality
required: False
default: present
choices: [ "present", "absent" ]
aliases: []
author: Simo Aleksandrov
"""

EXAMPLES = """
- name: Disable the zero downtime deployment
dokku_checks:
app: hello-world
state: absent
- name: Re-enable the zero downtime deployment (enabled by default)
dokku_checks:
app: hello-world
state: present
"""


def dokku_checks_enabled(data):
command = "dokku --quiet checks:report {0}"
response, error = subprocess_check_output(command.format(data["app"]))

if error:
return None, error

report = response[0].split(":")[1]
return report.strip() != "_all_", error


def dokku_checks_present(data):
is_error = True
has_changed = False
meta = {"present": False}

enabled, error = dokku_checks_enabled(data)
if error:
meta["error"] = error
return (is_error, has_changed, meta)

if enabled:
is_error = False
meta["present"] = True
return (is_error, has_changed, meta)

command = "dokku --quiet checks:enable {0}".format(data["app"])
try:
subprocess.check_call(command, shell=True)
is_error = False
has_changed = True
meta["present"] = True
except subprocess.CalledProcessError as e:
meta["error"] = str(e)

return (is_error, has_changed, meta)


def dokku_checks_absent(data=None):
is_error = True
has_changed = False
meta = {"present": True}

enabled, error = dokku_checks_enabled(data)
if error:
meta["error"] = error
return (is_error, has_changed, meta)

if enabled is False:
is_error = False
meta["present"] = False
return (is_error, has_changed, meta)

command = "dokku --quiet checks:disable {0}".format(data["app"])
try:
subprocess.check_call(command, shell=True)
is_error = False
has_changed = True
meta["present"] = False
except subprocess.CalledProcessError as e:
meta["error"] = str(e)

return (is_error, has_changed, meta)


def main():
fields = {
"app": {"required": True, "type": "str"},
"state": {
"required": False,
"default": "present",
"choices": ["present", "absent"],
"type": "str",
},
}
choice_map = {
"present": dokku_checks_present,
"absent": dokku_checks_absent,
}

module = AnsibleModule(argument_spec=fields, supports_check_mode=False)
is_error, has_changed, result = choice_map.get(module.params["state"])(
module.params
)

if is_error:
module.fail_json(msg=result["error"], meta=result)
module.exit_json(changed=has_changed, meta=result)


if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,38 @@
msg: |-
'false' not found in output of 'dokku http-auth:report':
{{ dokku_http_auth_off.stdout }}
# Testing dokku_checks
- name: Disabling the Zero Downtime deployment
dokku_checks:
app: example-app
state: absent

- name: Get checks output # noqa 301
command: dokku checks:report example-app
register: dokku_checks

- name: Check that the checks were disabled
assert:
that:
- "'_all_' in dokku_checks.stdout"
msg: |-
checks were not disabled in output of 'dokku checks':
{{ dokku_checks.stdout }}
- name: Re-enabling the Zero Downtime deployment
dokku_checks:
app: example-app
state: present

- name: Get checks output # noqa 301
command: dokku checks:report example-app
register: dokku_checks

- name: Check that the checks were re-enabled
assert:
that:
- "'none' in dokku_checks.stdout"
msg: |-
checks were not enabled in output of 'dokku checks':
{{ dokku_checks.stdout }}

0 comments on commit a6706ad

Please sign in to comment.