Skip to content

Commit

Permalink
Merge pull request #56 from dokku/upgrade-molecule
Browse files Browse the repository at this point in the history
Begin upgrade to molecule 3
  • Loading branch information
josegonzalez authored Mar 11, 2020
2 parents 8541be5 + fab7792 commit 27435ac
Show file tree
Hide file tree
Showing 29 changed files with 982 additions and 1,105 deletions.
1 change: 0 additions & 1 deletion .ansible-lint
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
skip_list:
- '204' # Lines should be no longer than 160 chars.
- '306' # Shells that use pipes should set the pipefail option.
- '102' # No Jinja2 in when
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 160
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible-role-requirements.yml
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:
- MOLECULE_DISTRO: debian9

install:
- pip install molecule docker
- pip install -r requirements.txt

before_script:
# Use actual Ansible Galaxy role name for the project directory.
Expand Down
15 changes: 15 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
extends: default
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length:
max: 160
level: warning
truthy: disable
ignore: |
default/roles
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ release: generate
cd .. && tar -zcvf dokku-$(shell cat meta/main.yml | grep version | head -n 1 | cut -d':' -f2 | xargs).tar.gz dokku

.PHONY: generate
generate: clean README.md defaults/main.yml
generate: clean README.md defaults/main.yml ansible-role-requirements.yml

.PHONY: README.md
README.md:
Expand All @@ -16,3 +16,7 @@ README.md:
.PHONY: defaults/main.yml
defaults/main.yml:
bin/generate

.PHONY: ansible-role-requirements.yml
ansible-role-requirements.yml:
bin/generate
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Manages ssl configuration for an app.

### dokku_clone

Deploys a repository to an undeployed application. Once a deploy has been attempted this task will not attempt another deploy.
Deploys a repository to an undeployed application.

#### Requirements

Expand Down
13 changes: 13 additions & 0 deletions bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,22 @@ def generate_defaults():
f.write(yaml.dump(defaults, explicit_start=True))


def generate_requirements():
role_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

meta = {}
meta_file = os.path.join(role_path, 'meta', 'main.yml')
with open(meta_file) as f:
meta = yaml.load(f.read(), Loader=yaml.SafeLoader)

with open(os.path.join(role_path, 'ansible-role-requirements.yml'), 'w') as f:
documents = yaml.dump(meta['dependencies'], f, explicit_start=True)


def main():
generate_defaults()
generate_readme()
generate_requirements()


if __name__ == "__main__":
Expand Down
67 changes: 30 additions & 37 deletions library/dokku_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ansible.module_utils.basic import AnsibleModule
import subprocess

DOCUMENTATION = '''
DOCUMENTATION = """
---
module: dokku_app
short_description: Create or destroy dokku apps
Expand All @@ -24,9 +24,9 @@
aliases: []
author: Jose Diaz-Gonzalez
requirements: [ ]
'''
"""

EXAMPLES = '''
EXAMPLES = """
- name: Create a dokku app
dokku_app:
app: hello-world
Expand All @@ -35,12 +35,13 @@
dokku_app:
app: hello-world
state: absent
'''
"""


def dokku_apps_exists(app):
exists = False
error = None
command = 'dokku --quiet apps:exists {0}'.format(app)
command = "dokku --quiet apps:exists {0}".format(app)
try:
subprocess.check_call(command, shell=True)
exists = True
Expand All @@ -52,22 +53,22 @@ def dokku_apps_exists(app):
def dokku_app_present(data):
is_error = True
has_changed = False
meta = {'present': False}
meta = {"present": False}

exists, error = dokku_apps_exists(data['app'])
exists, error = dokku_apps_exists(data["app"])
if exists:
is_error = False
meta['present'] = True
meta["present"] = True
return (is_error, has_changed, meta)

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

return (is_error, has_changed, meta)

Expand All @@ -77,56 +78,48 @@ def dokku_app_absent(data=None):
has_changed = False
meta = {"present": True}

exists, error = dokku_apps_exists(data['app'])
exists, error = dokku_apps_exists(data["app"])
if not exists:
is_error = False
meta['present'] = False
meta["present"] = False
return (is_error, has_changed, meta)

command = 'dokku --force apps:destroy {0}'.format(data['app'])
command = "dokku --force apps:destroy {0}".format(data["app"])
try:
subprocess.check_call(command, shell=True)
is_error = False
has_changed = True
meta['present'] = False
meta["present"] = False
except subprocess.CalledProcessError as e:
meta['error'] = str(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',
"app": {"required": True, "type": "str"},
"state": {
"required": False,
"default": "present",
"choices": ["present", "absent"],
"type": "str",
},
}
choice_map = {
'present': dokku_app_present,
'absent': dokku_app_absent,
"present": dokku_app_present,
"absent": dokku_app_absent,
}

module = AnsibleModule(
argument_spec=fields,
supports_check_mode=False
module = AnsibleModule(argument_spec=fields, supports_check_mode=False)
is_error, has_changed, result = choice_map.get(module.params["state"])(
module.params
)
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.fail_json(msg=result["error"], meta=result)
module.exit_json(changed=has_changed, meta=result)


if __name__ == '__main__':
if __name__ == "__main__":
main()
Loading

0 comments on commit 27435ac

Please sign in to comment.