Skip to content

Commit

Permalink
Merge pull request #10 from OpenDataServices/2023-03-17
Browse files Browse the repository at this point in the history
Misc
  • Loading branch information
odscjames authored Mar 21, 2023
2 parents eefb424 + 848af62 commit 57497e7
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 43 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## Added

- nginx / client_max_body_size option - set by command line option or environmental variable.
- nginx / proxy-read-timeout option - set by command line option or environmental variable.
- Dokku app names are cleaned up. Invalid characters are changed to "-". Lower case is enforced.

## Changed

- nginx / client_max_body_size option - setting this by app.json is deprecated and will be removed in a later version.


## Fixed

- When creating new git remote name, check it does not already exist. If it does, add random numbers to name.
Expand Down
25 changes: 0 additions & 25 deletions docs/reference/app-resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,3 @@ HTTP Auth with user and password
--------------------------------

Currently this can only be set on the command line - see the deploy call.

Nginx
-----

https://dokku.com/docs/networking/proxies/nginx/#specifying-a-custom-client_max_body_size

You can set a value with:

.. code-block:: json
"dokkusd": {
"nginx": {
"client_max_body_size": "50m"
}
}
You can clear a value and use the default with:

.. code-block:: json
"dokkusd": {
"nginx": {
"client_max_body_size": "50m"
}
}
18 changes: 18 additions & 0 deletions docs/reference/deploy-command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,21 @@ Be careful to escape any fields:
.. code-block:: bash
DOKKUSD_ENVIRONMENT_VARIABLES_JSON={\"ENV\":\"dev\",\"DATABASE\":\"dev\"} python -m dokkusd.cli deploy
Nginx Client Max body size
~~~~~~~~~~~~~~~~~~~~~~~~~~

Sets the Nginx Client Max body size.

Pass a string to `--nginxclientmaxbodysize` or set the `DOKKUSD_NGINX_CLIENT_MAX_BODY_SIZE` environmental variable.

Should include units. eg `50m` not `50`.

Nginx Proxy Read Timeout
~~~~~~~~~~~~~~~~~~~~~~~~

Sets the Nginx Proxy Read Timeout.

Pass a string to `--nginxproxyreadtimeout` or set the `DOKKUSD_NGINX_PROXY_READ_TIMEOUT` environmental variable.

Should include units. eg `120s` not `120`.
14 changes: 14 additions & 0 deletions docs/reference/dokku-app-names.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Dokku App Names
===============

Any app names that are not allowed in Dokku will be cleaned up automatically.

Names will be made lower case.

Characters that are not allowed will be changed to a dash (`-`). This includes:

* slashes
* underscores
* colons
* spaces

3 changes: 2 additions & 1 deletion docs/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Reference

deploy-command.rst
destroy-command.rst
app-resources.rst
app-resources.rst
dokku-app-names.rst
13 changes: 13 additions & 0 deletions dokkusd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ def main() -> None:
"--environmentvariablesprefixedby",
help="Any Environmental variables prefixed with this will be given to the Dokku app.",
)
deploy_parser.add_argument(
"--nginxclientmaxbodysize",
help="Sets a value for Nginx Client Max Body Size. Include units eg 50m",
default=os.getenv("DOKKUSD_NGINX_CLIENT_MAX_BODY_SIZE"),
)

deploy_parser.add_argument(
"--nginxproxyreadtimeout",
help="Sets a value for Nginx Proxy Read Timeout. Include units eg 120s",
default=os.getenv("DOKKUSD_NGINX_PROXY_READ_TIMEOUT"),
)

### Destroy
destroy_parser = subparsers.add_parser("destroy")
Expand Down Expand Up @@ -104,6 +115,8 @@ def main() -> None:
http_auth_password=args.httpauthpassword,
environment_variables_json_string=args.environmentvariablesjson,
environment_variables=env_vars,
nginx_client_max_body_size=args.nginxclientmaxbodysize,
nginx_proxy_read_timeout=args.nginxproxyreadtimeout,
)
deploy.go()

Expand Down
65 changes: 49 additions & 16 deletions dokkusd/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def __init__(
http_auth_password: str = None,
environment_variables_json_string: str = None,
environment_variables: dict = {},
nginx_client_max_body_size=None,
nginx_proxy_read_timeout=None,
):
super().__init__(
directory=directory,
Expand All @@ -34,6 +36,8 @@ def __init__(
self.http_auth_password = http_auth_password
self._environment_variables: dict = environment_variables
self.environment_variables_json_string = environment_variables_json_string
self._nginx_client_max_body_size = nginx_client_max_body_size
self._nginx_proxy_read_timeout = nginx_proxy_read_timeout

def go(self) -> None:

Expand Down Expand Up @@ -123,22 +127,51 @@ def go(self) -> None:
print(stdout)
print(stderr)

# --------------------- Nginx
if "nginx" in app_json.get("dokkusd", {}):
nginx = app_json.get("dokkusd", {}).get("nginx")
if isinstance(nginx, dict):
if "client_max_body_size" in nginx:
print("Nginx: client-max-body-size ...")
stdout, stderr = self._dokku_command(
[
"nginx:set",
self.app_name,
"client-max-body-size",
str(nginx.get("client_max_body_size")),
]
)
print(stdout)
print(stderr)
# --------------------- Nginx Client Max Body Size
# If not already passed, look for it in app.json
# This way things passed to us take priority over things set in app.json
# Setting in app.json is deprecated and undocumented.
# This code block will be removed in a later version.
if not self._nginx_client_max_body_size:
if "nginx" in app_json.get("dokkusd", {}):
nginx = app_json.get("dokkusd", {}).get("nginx")
if isinstance(nginx, dict):
if "client_max_body_size" in nginx:
self._nginx_client_max_body_size = str(
nginx.get("client_max_body_size")
)

# If set, process
if self._nginx_client_max_body_size:
print("Nginx: client-max-body-size ...")
stdout, stderr = self._dokku_command(
[
"nginx:set",
self.app_name,
"client-max-body-size",
str(self._nginx_client_max_body_size),
]
)
print(stdout)
print(stderr)

# --------------------- Nginx Proxy Read Timeout
if self._nginx_proxy_read_timeout:
print("Nginx: proxy-read-timeout ...")
stdout, stderr = self._dokku_command(
[
"nginx:set",
self.app_name,
"proxy-read-timeout",
str(self._nginx_proxy_read_timeout),
]
)
print(stdout)
print(stderr)
print("proxy:build-config after Nginx: proxy-read-timeout ...")
stdout, stderr = self._dokku_command(["proxy:build-config", self.app_name])
print(stdout)
print(stderr)

# --------------------- Deploy
print("Deploy ...")
Expand Down
11 changes: 10 additions & 1 deletion dokkusd/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ def get_remote_names_configured(git_remote_verbose_output):
return out


def clean_dokku_app_name(app_name: str) -> str:
app_name = app_name.replace(" ", "-")
app_name = app_name.replace("_", "-")
app_name = app_name.replace(":", "-")
app_name = app_name.replace("/", "-")
app_name = app_name.replace("\\", "-")
return app_name.lower()


class Task:
def __init__(
self,
Expand All @@ -31,7 +40,7 @@ def __init__(
self.remote_user = remote_user
self.remote_host = remote_host
self.remote_port = remote_port
self.app_name = app_name
self.app_name = clean_dokku_app_name(app_name)

def _dokku_command(self, command):
full_command = [
Expand Down
29 changes: 29 additions & 0 deletions test/test_clean_dokku_app_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from dokkusd.util import clean_dokku_app_name


def test_fine_1():
assert "cat" == clean_dokku_app_name("cat")


def test_bad_1():
assert "cat-" == clean_dokku_app_name("cat_")


def test_bad_2():
assert "cat-" == clean_dokku_app_name("cat:")


def test_bad_3():
assert "cat-" == clean_dokku_app_name("cat/")


def test_bad_4():
assert "cat-" == clean_dokku_app_name("cat\\")


def test_bad_5():
assert "cat" == clean_dokku_app_name("CAT")


def test_bad_6():
assert "cat-" == clean_dokku_app_name("cat ")

0 comments on commit 57497e7

Please sign in to comment.