Skip to content

Commit

Permalink
refactor dokku_clone module
Browse files Browse the repository at this point in the history
reuse functionality from dokku_app
  • Loading branch information
ltalirz committed Feb 3, 2021
1 parent 4317765 commit fe7b1de
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ Manages ssl configuration for an app.

### dokku_clone

Deploys a repository to an undeployed application.
Clone repository and deploy app.

#### Parameters

|Parameter|Choices/Defaults|Comments|
|---------|----------------|--------|
|app<br /><sup>*required*</sup>||The name of the app|
|repository<br /><sup>*required*</sup>||Git repository url|
|version||Git tree (tag or branch name)|
|version||Git tree (tag or branch name). If not provided, default branch is used.|

#### Example

Expand Down
64 changes: 4 additions & 60 deletions library/dokku_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# from ansible.module_utils.basic import *
from ansible.module_utils.basic import AnsibleModule
import subprocess
from ansible.module_utils.dokku_app import (
dokku_app_present,
dokku_app_absent,
)

DOCUMENTATION = """
---
Expand Down Expand Up @@ -38,64 +40,6 @@
"""


def dokku_apps_exists(app):
exists = False
error = None
command = "dokku --quiet apps:exists {0}".format(app)
try:
subprocess.check_call(command, shell=True)
exists = True
except subprocess.CalledProcessError as e:
error = str(e)
return exists, error


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

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

command = "dokku apps:create {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_app_absent(data=None):
is_error = True
has_changed = False
meta = {"present": True}

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

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
except subprocess.CalledProcessError as e:
meta["error"] = str(e)

return (is_error, has_changed, meta)


def main():
fields = {
"app": {"required": True, "type": "str"},
Expand Down
88 changes: 46 additions & 42 deletions library/dokku_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# -*- coding: utf-8 -*-
# from ansible.module_utils.basic import *
from ansible.module_utils.basic import AnsibleModule
import os
from ansible.module_utils.dokku_app import (
dokku_app_present,
)
import subprocess


DOCUMENTATION = """
---
module: dokku_clone
short_description: Deploys a repository to an undeployed application.
short_description: Clone repository and deploy app.
options:
app:
description:
Expand All @@ -29,12 +31,6 @@
required: False
default: null
aliases: []
build:
description:
- Whether to build the app (only when repository changes)
required: False
default: True
aliases: []
author: Jose Diaz-Gonzalez
"""

Expand All @@ -50,54 +46,63 @@
"""


def get_state(b_path):
""" Find out current state """
def dokku_git_sha(data):
"""Get SHA of current app repository.
if os.path.lexists(b_path):
if os.path.islink(b_path):
return "link"
elif os.path.isdir(b_path):
return "directory"
elif os.stat(b_path).st_nlink > 1:
return "hard"
# could be many other things, but defaulting to file
return "file"
Returns `None` if app does not exist.
"""
command_git_report = "dokku git:report {app} --git-sha".format(app=data["app"])
try:
sha = subprocess.check_output(
command_git_report, stderr=subprocess.STDOUT, shell=True
)
except subprocess.CalledProcessError:
sha = None

return "absent"
return sha


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

index_state = get_state("/home/dokku/{0}/HEAD".format(data["app"]))
if index_state == "file":
is_error = False
meta["present"] = True
return (is_error, has_changed, meta)

if index_state != "absent":
meta["error"] = "git HEAD for app {0} is of file type {1}".format(
data["app"], index_state
)
# create app (if not exists)
is_error, has_changed, meta = dokku_app_present(data)
meta[
"present"
] = False # should indicate that requested *version* of app is present
if is_error:
return (is_error, has_changed, meta)

command = "dokku git:sync"
if data["build"]:
command += " --build"
sha_old = dokku_git_sha(data)

command += " {app} {repository}".format(
# sync with remote repository
command_git_sync = "dokku git:sync {app} {repository}".format(
app=data["app"], repository=data["repository"]
)

if data["version"]:
command += command + " {version}".format(version=data["version"])

command_git_sync += command_git_sync + " {version}".format(
version=data["version"]
)
try:
subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
subprocess.check_output(command_git_sync, stderr=subprocess.STDOUT, shell=True)
is_error = False
except subprocess.CalledProcessError as e:
is_error = True
meta["error"] = e.output
return (is_error, has_changed, meta)

sha_new = dokku_git_sha(data)
if sha_new == sha_old:
meta["present"] = True
return (is_error, has_changed, meta)
else:
has_changed = True

# rebuild app
command_ps_rebuild = "dokku ps:rebuild {app}".format(app=data["app"])
try:
subprocess.check_output(
command_ps_rebuild, stderr=subprocess.STDOUT, shell=True
)
meta["present"] = True
except subprocess.CalledProcessError as e:
is_error = True
Expand All @@ -111,7 +116,6 @@ def main():
"app": {"required": True, "type": "str"},
"repository": {"required": True, "type": "str"},
"version": {"required": False, "type": "str"},
"build": {"required": False, "type": "bool"},
}

module = AnsibleModule(argument_spec=fields, supports_check_mode=False)
Expand Down
63 changes: 63 additions & 0 deletions module_utils/dokku_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- coding: utf-8 -*-

import subprocess


def dokku_apps_exists(app):
exists = False
error = None
command = "dokku --quiet apps:exists {0}".format(app)
try:
subprocess.check_call(command, shell=True)
exists = True
except subprocess.CalledProcessError as e:
error = str(e)
return exists, error


def dokku_app_present(data):
"""Create app if it does not exist."""
is_error = True
has_changed = False
meta = {"present": False}

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

command = "dokku apps:create {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_app_absent(data=None):
"""Remove app if it exists."""
is_error = True
has_changed = False
meta = {"present": True}

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

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
except subprocess.CalledProcessError as e:
meta["error"] = str(e)

return (is_error, has_changed, meta)

0 comments on commit fe7b1de

Please sign in to comment.