Skip to content

Commit

Permalink
Merge pull request #241 from kobotoolbox/kobocat-as-django-app
Browse files Browse the repository at this point in the history
Add support to Kobocat as Django app inside KPI
  • Loading branch information
noliveleger authored Sep 10, 2024
2 parents 6327d3d + cfd65ab commit ed60088
Show file tree
Hide file tree
Showing 26 changed files with 464 additions and 856 deletions.
104 changes: 32 additions & 72 deletions helpers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ def help():
' -l, --logs',
' Display docker logs',
' -b, --build',
' Build kpi and kobocat (only on dev/staging mode)',
' -bkf, --build-kpi',
' Build kpi (only on dev/staging mode)',
' -bkc, --build-kobocat',
' Build kobocat (only on dev/staging mode)',
' Build django (kpi) container (only on dev/staging mode)',
' -s, --setup',
' Prompt questions to (re)write configuration files',
' -S, --stop',
Expand All @@ -54,52 +50,27 @@ def help():
print('\n'.join(output))

@classmethod
def build(cls, image=None):
def build(cls):
"""
Builds kpi/kobocat images with `--no-caches` option
Pulls latest `kobotoolbox/koboform_base` as well
:param image: str
Builds kpi image with `--no-caches` option
"""
config = Config()
dict_ = config.get_dict()

if config.dev_mode or config.staging_mode:

def build_image(image_):
frontend_command = run_docker_compose(dict_, [
'-f', 'docker-compose.frontend.yml',
'-f', 'docker-compose.frontend.override.yml',
'-p', config.get_prefix('frontend'),
'build', '--force-rm', '--no-cache',
image_
])

CLI.run_command(frontend_command, dict_['kobodocker_path'])

if image is None or image == 'kf':
prefix = config.get_prefix('frontend')
timestamp = int(time.time())
dict_['kpi_dev_build_id'] = f'{prefix}{timestamp}'
config.write_config()
Template.render(config)
build_image('kpi')

if image is None or image == 'kc':
pull_base_command = [
'docker',
'pull',
'kobotoolbox/koboform_base',
]

CLI.run_command(pull_base_command, dict_['kobodocker_path'])

prefix = config.get_prefix('frontend')
timestamp = int(time.time())
dict_['kc_dev_build_id'] = f'{prefix}{timestamp}'
config.write_config()
Template.render(config)
build_image('kobocat')
prefix = config.get_prefix('frontend')
timestamp = int(time.time())
dict_['kpi_dev_build_id'] = f'{prefix}{timestamp}'
config.write_config()
Template.render(config)
frontend_command = run_docker_compose(dict_, [
'-f', 'docker-compose.frontend.yml',
'-f', 'docker-compose.frontend.override.yml',
'-p', config.get_prefix('frontend'),
'build', '--force-rm', '--no-cache', 'kpi'
])
CLI.run_command(frontend_command, dict_['kobodocker_path'])

@classmethod
def compose_frontend(cls, args):
Expand All @@ -119,10 +90,9 @@ def compose_frontend(cls, args):
def compose_backend(cls, args):
config = Config()
dict_ = config.get_dict()
backend_role = dict_['backend_server_role']
command = run_docker_compose(dict_, [
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-f', f'docker-compose.backend.yml',
'-f', f'docker-compose.backend.override.yml',
'-p', config.get_prefix('backend')
])
cls.__validate_custom_yml(config, command)
Expand Down Expand Up @@ -163,8 +133,8 @@ def info(cls, timeout=600):
elif int(time.time()) - start >= timeout:
if timeout > 0:
CLI.colored_print(
'\n`KoBoToolbox` has not started yet. '
'This is can be normal with low CPU/RAM computers.\n',
'\n`KoboToolbox` has not started yet. '
'This can happen with low CPU/RAM computers.\n',
CLI.COLOR_INFO)
question = f'Wait for another {timeout} seconds?'
response = CLI.yes_no_question(question)
Expand Down Expand Up @@ -209,7 +179,7 @@ def info(cls, timeout=600):

else:
message = (
'KoBoToolbox could not start!\n'
'KoboToolbox could not start!\n'
'Please try `python3 run.py --logs` to see the logs.'
)
CLI.framed_print(message, color=CLI.COLOR_ERROR)
Expand All @@ -221,11 +191,10 @@ def logs(cls):
config = Config()
dict_ = config.get_dict()

if config.primary_backend or config.secondary_backend:
backend_role = dict_['backend_server_role']
if config.backend:
backend_command = run_docker_compose(dict_, [
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-f', f'docker-compose.backend.yml',
'-f', f'docker-compose.backend.override.yml',
'-p', config.get_prefix('backend'),
'logs', '-f'
])
Expand Down Expand Up @@ -310,13 +279,10 @@ def start(cls, frontend_only=False, force_setup=False):
else:
nginx_port = int(dict_['exposed_nginx_docker_port'])

if frontend_only or config.frontend or \
not config.multi_servers:
if frontend_only or config.frontend or not config.multi_servers:
ports.append(nginx_port)

if (not frontend_only or config.primary_backend or
config.secondary_backend) and \
config.expose_backend_ports:
if not frontend_only and config.expose_backend_ports and config.backend:
ports.append(dict_['postgresql_port'])
ports.append(dict_['mongo_port'])
ports.append(dict_['redis_main_port'])
Expand All @@ -332,11 +298,9 @@ def start(cls, frontend_only=False, force_setup=False):
# Start the back-end containers
if not frontend_only and config.backend:

backend_role = dict_['backend_server_role']

backend_command = run_docker_compose(dict_, [
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-f', f'docker-compose.backend.yml',
'-f', f'docker-compose.backend.override.yml',
'-p', config.get_prefix('backend'),
'up', '-d'
])
Expand Down Expand Up @@ -392,9 +356,8 @@ def start(cls, frontend_only=False, force_setup=False):
'It can take a few minutes.', CLI.COLOR_INFO)
cls.info()
else:
backend_server_role = dict_['backend_server_role']
CLI.colored_print(
(f'{backend_server_role} backend server is starting up '
(f'Back-end server is starting up '
'and should be up & running soon!\nPlease look at docker '
'logs for further information: '
'`python3 run.py -cb logs -f`'),
Expand Down Expand Up @@ -443,7 +406,6 @@ def stop_containers(cls, group: str, down: bool = False):

config = Config()
dict_ = config.get_dict()
backend_role = dict_['backend_server_role']

if group not in ['frontend', 'backend', 'certbot', 'maintenance']:
raise Exception('Unknown group')
Expand All @@ -459,8 +421,8 @@ def stop_containers(cls, group: str, down: bool = False):
},
'backend': {
'options': [
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-f', f'docker-compose.backend.yml',
'-f', f'docker-compose.backend.override.yml',
'-p', config.get_prefix('backend'),
],
'custom_yml': True,
Expand Down Expand Up @@ -570,10 +532,8 @@ def __validate_custom_yml(config, command):
command.insert(start_index + 1, 'docker-compose.frontend.custom.yml')

if not frontend_command and dict_['use_backend_custom_yml']:
backend_server_role = dict_['backend_server_role']
custom_file = '{}/docker-compose.backend.{}.custom.yml'.format(
custom_file = '{}/docker-compose.backend.custom.yml'.format(
dict_['kobodocker_path'],
backend_server_role
)

does_custom_file_exist = os.path.exists(custom_file)
Expand All @@ -590,5 +550,5 @@ def __validate_custom_yml(config, command):
command.insert(start_index, '-f')
command.insert(
start_index + 1,
'docker-compose.backend.{}.custom.yml'.format(backend_server_role),
'docker-compose.backend.custom.yml',
)
Loading

0 comments on commit ed60088

Please sign in to comment.