Skip to content

Commit

Permalink
feat: dokku_builder module (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
fr3fou authored Nov 18, 2021
1 parent f5c8d1a commit 8f223a6
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,43 @@ Create or destroy dokku apps
state: absent
```

### dokku_builder

Manage the builder configuration for a given dokku application

#### Parameters

|Parameter|Choices/Defaults|Comments|
|---------|----------------|--------|
|app<br /><sup>*required*</sup>||The name of the app. This is required only if global is set to False.|
|global|*Default:* False|If the property being set is global|
|property<br /><sup>*required*</sup>||The property to be changed (e.g., `build-dir`, `selected`)|
|value||The value of the builder property (leave empty to unset)|

#### Example

```yaml
- name: Overriding the auto-selected builder
dokku_builder:
app: node-js-app
property: selected
value: dockerfile
- name: Setting the builder to the default value
dokku_builder:
app: node-js-app
property: selected
- name: Changing the build build directory
dokku_builder:
app: monorepo
property: build-dir
value: backend
- name: Overriding the auto-selected builder globally
dokku_builder:
global: true
property: selected
value: herokuish
```

### dokku_certs

Manages ssl configuration for an app.
Expand Down
109 changes: 109 additions & 0 deletions library/dokku_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess

from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = """
---
module: dokku_builder
short_description: Manage the builder configuration for a given dokku application
options:
app:
description:
- The name of the app. This is required only if global is set to False.
required: True
default: null
aliases: []
property:
description:
- The property to be changed (e.g., `build-dir`, `selected`)
required: True
aliases: []
value:
description:
- The value of the builder property (leave empty to unset)
required: False
aliases: []
global:
description:
- If the property being set is global
required: False
default: False
aliases: []
author: Simo Aleksandrov
"""

EXAMPLES = """
- name: Overriding the auto-selected builder
dokku_builder:
app: node-js-app
property: selected
value: dockerfile
- name: Setting the builder to the default value
dokku_builder:
app: node-js-app
property: selected
- name: Changing the build build directory
dokku_builder:
app: monorepo
property: build-dir
value: backend
- name: Overriding the auto-selected builder globally
dokku_builder:
global: true
property: selected
value: herokuish
"""


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

if data["global"] and data["app"]:
is_error = True
meta["error"] = 'When "global" is set to true, "app" must not be provided.'
return (is_error, has_changed, meta)

# Check if "value" is set and evaluates to a non-empty string, otherwise use an empty string
value = data["value"] if "value" in data else None
if not value:
value = ""

command = "dokku builder:set {0} {1} {2}".format(
"--global" if data["global"] else data["app"],
data["property"],
value,
)

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 main():
fields = {
"app": {"required": False, "type": "str"},
"property": {"required": True, "type": "str"},
"value": {"required": False, "type": "str", "no_log": True},
"global": {"required": False, "type": "bool"},
}

module = AnsibleModule(argument_spec=fields, supports_check_mode=False)
is_error, has_changed, result = dokku_builder(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()
18 changes: 18 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,21 @@
msg: |
'ms' not found in output of 'dokku apps:list':
{{ dokku_apps.stdout }}
# Testing dokku_builder
- name: Configuring the builder for an app
dokku_builder:
app: example-app
property: build-dir
value: /app

- name: Get builder output # noqa 301
command: dokku builder:report example-app
register: dokku_builder

- name: Check that the build dir was set correctly
assert:
that:
- "'/app' in dokku_builder.stdout"
msg: |-
build-dir '/app' not found in output of 'dokku builder':
{{ dokku_builder.stdout }}

0 comments on commit 8f223a6

Please sign in to comment.