Skip to content

Commit

Permalink
CH-87 Add detection of gunicorn in Dockerfile or requirements.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Oct 17, 2023
1 parent f210c6b commit 717bb12
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 9 deletions.
28 changes: 20 additions & 8 deletions tools/deployment-cli-tools/ch_cli_tools/skaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import time

from os.path import join, dirname, relpath, basename
from os.path import join, relpath, basename, exists, abspath
from cloudharness_model import ApplicationTestConfig, HarnessMainConfig

from cloudharness_utils.constants import APPS_PATH, DEPLOYMENT_CONFIGURATION_PATH, \
Expand Down Expand Up @@ -140,24 +140,36 @@ def process_build_dockerfile(dockerfile_path, root_path, global_context=False, r

mains_candidates = find_file_paths(context_path, '__main__.py')

def identify_flask_main(candidates):
def identify_unicorn_based_main(candidates):
import re
init_flask_pattern = re.compile(r"init_flask\(")
gunicorn_pattern = re.compile(r"gunicorn")
for candidate in candidates:
with open(f"{candidate}/__main__.py", 'r') as file:
if re.search(init_flask_pattern, file.read()):
dockerfile_path = f"{candidate}/.."
while not exists(f"{dockerfile_path}/Dockerfile") and abspath(dockerfile_path) != abspath(root_path):
dockerfile_path += "/.."
dockerfile = f"{dockerfile_path}/Dockerfile"
if not exists(dockerfile):
continue
with open(dockerfile, 'r') as file:
if re.search(gunicorn_pattern, file.read()):
return candidate
requirements = f"{candidate}/../requirements.txt"
if not exists(requirements):
continue
with open(requirements, 'r') as file:
if re.search(gunicorn_pattern, file.read()):
return candidate
return None

flask_main = identify_flask_main(mains_candidates)
task_main_file = identify_unicorn_based_main(mains_candidates)

if flask_main:
if task_main_file:
release_config['overrides']['apps'][app_key] = \
{
'harness': {
'deployment': {
'command': ['python'],
'args': [f'/usr/src/app/{os.path.basename(flask_main)}/__main__.py']
'args': [f'/usr/src/app/{os.path.basename(task_main_file)}/__main__.py']
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
ARG CLOUDHARNESS_FLASK
FROM $CLOUDHARNESS_FLASK
FROM $CLOUDHARNESS_FLASK

RUN pip install gunicorn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ignored
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARG CLOUDHARNESS_FLASK
FROM $CLOUDHARNESS_FLASK

RUN pip install -r requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
harness:
name: "I'm useless"
subdomain: mysubdomain
dependencies:
soft:
- legacy
build:
- cloudharness-flask
- my-common
a: b
dev: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a: test
test: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
harness:
database:
auto: true
type: mongo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
harness:
database: {auto: true, type: postgres}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
harness:
name: "I'm useless"
subdomain: mysubdomain
dependencies:
soft:
- legacy
build:
- cloudharness-flask
test:
unit:
commands:
- tox
- echo "hello"
a: b
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python3

from cloudharness.utils.server import init_flask, main


app = init_flask(title="Cloudharness sample application", webapp=True)

if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gunicorn
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python3

from something import something_else


def fake_content():
...
36 changes: 36 additions & 0 deletions tools/deployment-cli-tools/tests/test_skaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,39 @@ def test_create_skaffold_configuration_with_conflicting_dependencies(tmp_path):

myapp_config = release['overrides']['apps']['myapp']
assert myapp_config['harness']['deployment']['args'][0] == '/usr/src/app/myapp_code/__main__.py'


def test_create_skaffold_configuration_with_conflicting_dependencies_requirements_file(tmp_path):
values = create_helm_chart(
[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
output_path=OUT,
include=['myapp2'],
exclude=['events'],
domain="my.local",
namespace='test',
env='dev',
local=False,
tag=1,
registry='reg'
)
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES_BUGGY],
helm_values=values,
merge_build_path=str(tmp_path)
)

sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=OUT
)

releases = sk['deploy']['helm']['releases']
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)

release = releases[0]
assert 'myapp2' in release['overrides']['apps']
assert 'matplotlib' not in release['overrides']['apps']

myapp_config = release['overrides']['apps']['myapp2']
assert myapp_config['harness']['deployment']['args'][0] == '/usr/src/app/myapp_code/__main__.py'

0 comments on commit 717bb12

Please sign in to comment.