Skip to content

Commit

Permalink
Skip port mappings when no changes are detected (#145)
Browse files Browse the repository at this point in the history
* Fix reading existing dokku proxy ports

* Add test for dokku_ports
  • Loading branch information
erikvdv1 authored Oct 17, 2022
1 parent fb8174f commit 590d47b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
18 changes: 14 additions & 4 deletions library/dokku_ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@


def dokku_proxy_port_mappings(data):
command = "dokku --quiet proxy:ports {0}".format(data["app"])
mappings = []
command = "dokku --quiet proxy:report {0}".format(data["app"])
output, error = subprocess_check_output(command)
if error is None:
output = [re.sub(r"\s\s+", ":", line).strip(":") for line in output]
return output, error
for line in output:
match = re.match("Proxy port map:(?P<mapping>.+)", line.strip())
if match:
mappings = match.group("mapping").strip().split(" ")
return mappings, error


def dokku_proxy_ports_absent(data):
Expand Down Expand Up @@ -125,9 +129,15 @@ def dokku_proxy_ports_present(data):
meta["error"] = "missing required arguments: mappings"
return (is_error, has_changed, meta)

existing, error = dokku_proxy_port_mappings(data)
if error:
meta["error"] = error
return (is_error, has_changed, meta)

to_add = [m for m in data["mappings"] if m not in existing]
to_set = [pipes.quote(m) for m in data["mappings"]]

if len(to_set) == 0:
if len(to_add) == 0:
is_error = False
meta["present"] = True
return (is_error, has_changed, meta)
Expand Down
54 changes: 54 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,60 @@
'attach post create:' not found in output of 'dokku network:report example-app':
{{ example_app_network_report.stdout }}
# Testing dokku_ports
- name: Set port mapping for an app
dokku_ports:
app: example-app
mappings:
- http:80:5000
- http:8080:5000

- name: Get proxy output
command: dokku proxy:report example-app
register: dokku_proxy_ports

- name: Check that port mapping was set
assert:
that:
- "'http:80:5000' in dokku_proxy_ports.stdout"
- "'http:8080:5000' in dokku_proxy_ports.stdout"
msg: |-
port mapping 'http:80:5000' or 'http:8080:5000' was not set in output of 'dokku proxy:report':
{{ dokku_proxy_ports.stdout }}
- name: Set port mapping that already exists
dokku_ports:
app: example-app
mappings:
- http:80:5000
register: existing_proxy_ports

- name: Check that setting existing port mapping did not change anything
assert:
that:
- not existing_proxy_ports.changed
msg: |
Setting existing port mapping resulted in changed status
- name: Remove port mapping
dokku_ports:
app: example-app
mappings:
- http:8080:5000
state: absent

- name: Get proxy output
command: dokku proxy:report example-app
register: dokku_proxy_ports

- name: Check that the port mapping was removed
assert:
that:
- "'http:8080:5000' not in dokku_proxy_ports.stdout"
msg: |-
port mapping 'http:8080:5000' was not removed in output of 'dokku proxy:report':
{{ dokku_proxy_ports.stdout }}
# Testing dokku_ps_scale
- name: Scaling application processes
dokku_ps_scale:
Expand Down

0 comments on commit 590d47b

Please sign in to comment.