Skip to content

Adds labels and network aliases #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/scenario_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ def remove_docker_images(self):
and then link itself in the runs table accordingly.
'''
def register_machine_id(self):

if self._dev_no_save:
return

config = GlobalConfig().config
if config['machine'].get('id') is None \
or not isinstance(config['machine']['id'], int) \
Expand Down Expand Up @@ -973,10 +977,56 @@ def setup_services(self):
if env_var_check_errors:
raise RuntimeError('Docker container environment setup has problems:\n\n'.join(env_var_check_errors))

if 'labels' in service:
labels_check_errors = []
for docker_label_var in service['labels']:
# https://docs.docker.com/reference/compose-file/services/#labels
if isinstance(docker_label_var, str) and '=' in docker_label_var:
label_key, label_value = docker_label_var.split('=')
elif isinstance(service['labels'], dict):
label_key, label_value = str(docker_label_var), str(service['labels'][docker_label_var])
else:

raise RuntimeError('Environment variable needs to be a string with = or dict and non-empty. We do not allow the feature of forwarding variables from the host OS!')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Error message should refer to 'label' instead of 'Environment variable'

Suggested change
raise RuntimeError('Environment variable needs to be a string with = or dict and non-empty. We do not allow the feature of forwarding variables from the host OS!')
raise RuntimeError('Label needs to be a string with = or dict and non-empty. We do not allow the feature of forwarding variables from the host OS!')


# Check the key of the environment var
if not self._allow_unsafe and re.search(r'^[A-Za-z_]+[A-Za-z0-9_.]*$', label_key) is None:
if self._skip_unsafe:
warn_message= arrows(f"Found label key with wrong format. Only ^[A-Za-z_]+[A-Za-z0-9_.]*$ allowed: {label_key} - Skipping")
print(TerminalColors.WARNING, warn_message, TerminalColors.ENDC)
continue
labels_check_errors.append(f"- key '{label_key}' has wrong format. Only ^[A-Za-z_]+[A-Za-z0-9_.]*$ is allowed - Maybe consider using --allow-unsafe or --skip-unsafe")

# Check the value of the environment var
# We only forbid long values (>1024), every character is allowed.
# The value is directly passed to the container and is not evaluated on the host system, so there is no security related reason to forbid special characters.
if not self._allow_unsafe and len(label_value) > 1024:
if self._skip_unsafe:
print(TerminalColors.WARNING, arrows(f"Found label value with size {len(label_value)} (max allowed length is 1024) - Skipping label '{label_value}'"), TerminalColors.ENDC)
continue
labels_check_errors.append(f"- value of label '{label_key}' is too long {len(label_key)} (max allowed length is 1024) - Maybe consider using --allow-unsafe or --skip-unsafe")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Using label_key length instead of label_value length in error message

Suggested change
labels_check_errors.append(f"- value of label '{label_key}' is too long {len(label_key)} (max allowed length is 1024) - Maybe consider using --allow-unsafe or --skip-unsafe")
labels_check_errors.append(f"- value of label '{label_key}' is too long {len(label_value)} (max allowed length is 1024) - Maybe consider using --allow-unsafe or --skip-unsafe")


docker_run_string.append('-l')
docker_run_string.append(f"{label_key}={label_value}")

if labels_check_errors:
raise RuntimeError('Docker container labels that have problems:\n\n'.join(labels_check_errors))

if 'networks' in service:
for network in service['networks']:
docker_run_string.append('--net')
docker_run_string.append(network)
print('-------------------------------------------------------------')
print(service['networks'])
print(network)
print('-------------------------------------------------------------')
if service['networks'][network]:
if service['networks'][network].get('aliases', None):
for alias in service['networks'][network]['aliases']:
docker_run_string.append('--network-alias')
docker_run_string.append(alias)
print(f"Adding network alias {alias} for network {network} in service {service_name}")

elif self.__join_default_network:
# only join default network if no other networks provided
# if this is true only one entry is in self.__networks
Expand Down
12 changes: 11 additions & 1 deletion lib/schema_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ def check_usage_scenario(self, usage_scenario):
Optional("type"): Use(self.valid_service_types),
Optional("image"): And(str, Use(self.not_empty)),
Optional("build"): Or(Or({And(str, Use(self.not_empty)):And(str, Use(self.not_empty))},list),And(str, Use(self.not_empty))),
Optional("networks"): self.single_or_list(Use(self.contains_no_invalid_chars)),
Optional("networks"): Or(
self.single_or_list(Use(self.contains_no_invalid_chars)),
{
Use(self.contains_no_invalid_chars):
Or(
None,
{ "aliases": [Use(self.contains_no_invalid_chars)] }
)
}
),
Optional("environment"): self.single_or_list(Or(dict,And(str, Use(self.not_empty)))),
Optional("labels"): self.single_or_list(Or(dict,And(str, Use(self.not_empty)))),
Optional("ports"): self.single_or_list(Or(And(str, Use(self.not_empty)), int)),
Optional('depends_on'): Or([And(str, Use(self.not_empty))],dict),
Optional('deploy'):Or({
Expand Down
Loading