Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm committed Jul 15, 2022
2 parents 01e173b + b7b83bd commit ca878b2
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 146 deletions.
27 changes: 13 additions & 14 deletions helpers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ def colored_print(cls, message, color=NO_COLOR):

@classmethod
def colorize(cls, message, color=NO_COLOR):
return '{}{}{}'.format(color, message, cls.NO_COLOR)
return f'{color}{message}{cls.NO_COLOR}'

@classmethod
def framed_print(cls, message, color=COLOR_WARNING, columns=70):
border = '═' * (columns - 2)
blank_line = ' ' * (columns - 2)
framed_message = [
'╔{}╗'.format(border),
'║ {} ║'.format(' ' * (columns - 4)),
f'╔{border}╗',
f'║{blank_line}║',
]

if not isinstance(message, list):
Expand All @@ -62,19 +63,19 @@ def framed_print(cls, message, color=COLOR_WARNING, columns=70):
for paragraph in paragraphs:
if paragraph == '':
framed_message.append(
'║ {} ║'.format(' ' * (columns - 4))
f'║{blank_line}║'
)
continue

for line in textwrap.wrap(paragraph, columns - 4):
message_length = len(line)
spacer = ' ' * (columns - 4 - message_length)
framed_message.append(
'║ {}{} ║'.format(line, spacer)
f'║ {line}{spacer} ║'
)

framed_message.append('║ {} ║'.format(' ' * (columns - 4)))
framed_message.append('╚{}╝'.format(border))
framed_message.append(f'║{blank_line}║')
framed_message.append(f'╚{border}╝')
cls.colored_print('\n'.join(framed_message), color=color)

@classmethod
Expand Down Expand Up @@ -121,7 +122,7 @@ def get_response(cls, validators=None, default='', to_lower=True,

@classmethod
def get_message_with_default(cls, message, default):
message = '{} '.format(message) if message else ''
message = f'{message} ' if message else ''

if default is None:
default = ''
Expand All @@ -133,9 +134,9 @@ def get_message_with_default(cls, message, default):
)

if message:
message = '{}: '.format(message.strip()) if not default else message
message = f'{message.strip()}: ' if not default else message

return '{}{}'.format(message, default)
return f'{message}{default}'

@classmethod
def run_command(cls, command, cwd=None, polling=False):
Expand Down Expand Up @@ -167,8 +168,6 @@ def yes_no_question(cls, question, default=True,
labels=['Yes', 'No']):
cls.colored_print(question, color=cls.COLOR_QUESTION)
for index, label in enumerate(labels):
cls.colored_print('\t{index}) {label}'.format(
index=index + 1,
label=label
))
choice_number = index + 1
cls.colored_print(f'\t{choice_number}) {label}')
return cls.get_response(default=default)
91 changes: 48 additions & 43 deletions helpers/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Command:
@staticmethod
def help():
output = [
'Usage: python run.py [options]',
'Usage: python3 run.py [options]',
'',
' Options:',
' -i, --info',
Expand Down Expand Up @@ -44,6 +44,8 @@ def help():
' -m, --maintenance',
' Activate maintenance mode. All traffic is '
'redirected to maintenance page',
' -sm, --stop-maintenance',
' Stop maintenance mode',
' -v, --version',
' Display current version',
''
Expand Down Expand Up @@ -78,10 +80,9 @@ def build_image(image_):
CLI.run_command(frontend_command, dict_['kobodocker_path'])

if image is None or image == 'kf':
dict_['kpi_dev_build_id'] = '{prefix}{timestamp}'.format(
prefix=config.get_prefix('frontend'),
timestamp=str(int(time.time()))
)
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')
Expand All @@ -93,10 +94,9 @@ def build_image(image_):

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

dict_['kc_dev_build_id'] = '{prefix}{timestamp}'.format(
prefix=config.get_prefix('frontend'),
timestamp=str(int(time.time()))
)
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')
Expand All @@ -121,9 +121,12 @@ def compose_backend(cls, args):
backend_role = dict_['backend_server_role']
command = [
'docker-compose',
'-f', 'docker-compose.backend.{}.yml'.format(backend_role),
'-f', 'docker-compose.backend.{}.override.yml'.format(backend_role),
'-p', config.get_prefix('backend')
'-f',
f'docker-compose.backend.{backend_role}.yml',
'-f',
f'docker-compose.backend.{backend_role}.override.yml',
'-p',
config.get_prefix('backend')
]
cls.__validate_custom_yml(config, command)
command.extend(args)
Expand All @@ -149,8 +152,7 @@ def info(cls, timeout=600):
stop = False
start = int(time.time())
success = False
hostname = '{}.{}'.format(dict_['kpi_subdomain'],
dict_['public_domain_name'])
hostname = f"{dict_['kpi_subdomain']}.{dict_['public_domain_name']}"
https = dict_['https']
nginx_port = int(Config.DEFAULT_NGINX_HTTPS_PORT) \
if https else int(dict_['exposed_nginx_docker_port'])
Expand All @@ -167,7 +169,7 @@ def info(cls, timeout=600):
'\n`KoBoToolbox` has not started yet. '
'This is can be normal with low CPU/RAM computers.\n',
CLI.COLOR_INFO)
question = 'Wait for another {} seconds?'.format(timeout)
question = f'Wait for another {timeout} seconds?'
response = CLI.yes_no_question(question)
if response:
start = int(time.time())
Expand Down Expand Up @@ -201,13 +203,10 @@ def info(cls, timeout=600):

message = (
'Ready\n'
'URL: {url}\n'
'User: {username}\n'
'Password: {password}'
).format(
url=main_url,
username=username,
password=password)
f'URL: {main_url}\n'
f'User: {username}\n'
f'Password: {password}'
)
CLI.framed_print(message,
color=CLI.COLOR_SUCCESS)

Expand All @@ -227,13 +226,16 @@ def logs(cls):

if config.primary_backend or config.secondary_backend:
backend_role = dict_['backend_server_role']

backend_command = [
'docker-compose',
'-f', 'docker-compose.backend.{}.yml'.format(backend_role),
'-f', 'docker-compose.backend.{}.override.yml'.format(backend_role),
'-p', config.get_prefix('backend'),
'logs', '-f',
'-f',
f'docker-compose.backend.{backend_role}.yml',
'-f',
f'docker-compose.backend.{backend_role}.override.yml',
'-p',
config.get_prefix('backend'),
'logs',
'-f'
]
cls.__validate_custom_yml(config, backend_command)
CLI.run_command(backend_command, dict_['kobodocker_path'], True)
Expand Down Expand Up @@ -333,8 +335,8 @@ def start(cls, frontend_only=False, force_setup=False):

for port in ports:
if Network.is_port_open(port):
CLI.colored_print('Port {} is already open. '
'KoboToolbox cannot start'.format(port),
CLI.colored_print(f'Port {port} is already open. '
'KoboToolbox cannot start',
CLI.COLOR_ERROR)
sys.exit(1)

Expand All @@ -345,10 +347,11 @@ def start(cls, frontend_only=False, force_setup=False):

backend_command = [
'docker-compose',
'-f', 'docker-compose.backend.{}.yml'.format(backend_role),
'-f', 'docker-compose.backend.{}.override.yml'.format(backend_role),
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-p', config.get_prefix('backend'),
'up', '-d'
'up',
'-d'
]

cls.__validate_custom_yml(config, backend_command)
Expand Down Expand Up @@ -402,11 +405,12 @@ 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(
('{} 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`'.format(
dict_['backend_server_role'])),
(f'{backend_server_role} backend server is starting up '
'and should be up & running soon!\nPlease look at docker '
'logs for further information: '
'`python3 run.py -cb logs -f`'),
CLI.COLOR_WARNING)

@classmethod
Expand Down Expand Up @@ -452,8 +456,8 @@ def stop(cls, output=True, frontend_only=False):

backend_command = [
'docker-compose',
'-f', 'docker-compose.backend.{}.yml'.format(backend_role),
'-f', 'docker-compose.backend.{}.override.yml'.format(backend_role),
'-f', f'docker-compose.backend.{backend_role}.yml',
'-f', f'docker-compose.backend.{backend_role}.override.yml',
'-p', config.get_prefix('backend'),
'down'
]
Expand Down Expand Up @@ -507,11 +511,12 @@ def stop_maintenance(cls):
def version(cls):
git_commit_version_command = ['git', 'rev-parse', 'HEAD']
stdout = CLI.run_command(git_commit_version_command)

CLI.colored_print('kobo-install Version: {} (build {})'.format(
Config.KOBO_INSTALL_VERSION,
stdout.strip()[0:7],
), CLI.COLOR_SUCCESS)
build = stdout.strip()[0:7]
version = Config.KOBO_INSTALL_VERSION
CLI.colored_print(
f'kobo-install Version: {version} (build {build})',
CLI.COLOR_SUCCESS,
)

@staticmethod
def __validate_custom_yml(config, command):
Expand Down
Loading

0 comments on commit ca878b2

Please sign in to comment.