|
1 | 1 | import json
|
| 2 | +import logging |
2 | 3 | import os
|
3 | 4 | from functools import wraps
|
4 | 5 |
|
|
11 | 12 | from .errors import ConfigurationError
|
12 | 13 |
|
13 | 14 |
|
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
14 | 18 | DOCKER_CONFIG_HINTS = {
|
15 | 19 | 'cpu_share': 'cpu_shares',
|
16 | 20 | 'add_host': 'extra_hosts',
|
@@ -44,6 +48,21 @@ def format_ports(instance):
|
44 | 48 | return True
|
45 | 49 |
|
46 | 50 |
|
| 51 | +@FormatChecker.cls_checks(format="environment") |
| 52 | +def format_boolean_in_environment(instance): |
| 53 | + """ |
| 54 | + Check if there is a boolean in the environment and display a warning. |
| 55 | + Always return True here so the validation won't raise an error. |
| 56 | + """ |
| 57 | + if isinstance(instance, bool): |
| 58 | + log.warn( |
| 59 | + "Warning: There is a boolean value, {0} in the 'environment' key.\n" |
| 60 | + "Environment variables can only be strings.\nPlease add quotes to any boolean values to make them string " |
| 61 | + "(eg, '{0}').\nThis warning will become an error in a future release. \r\n".format(instance) |
| 62 | + ) |
| 63 | + return True |
| 64 | + |
| 65 | + |
47 | 66 | def validate_service_names(func):
|
48 | 67 | @wraps(func)
|
49 | 68 | def func_wrapper(config):
|
@@ -259,23 +278,25 @@ def _parse_oneof_validator(error):
|
259 | 278 |
|
260 | 279 | def validate_against_fields_schema(config):
|
261 | 280 | schema_filename = "fields_schema.json"
|
262 |
| - return _validate_against_schema(config, schema_filename) |
| 281 | + format_checkers = ["ports", "environment"] |
| 282 | + return _validate_against_schema(config, schema_filename, format_checkers) |
263 | 283 |
|
264 | 284 |
|
265 | 285 | def validate_against_service_schema(config, service_name):
|
266 | 286 | schema_filename = "service_schema.json"
|
267 |
| - return _validate_against_schema(config, schema_filename, service_name) |
| 287 | + format_checkers = ["ports"] |
| 288 | + return _validate_against_schema(config, schema_filename, format_checkers, service_name) |
268 | 289 |
|
269 | 290 |
|
270 |
| -def _validate_against_schema(config, schema_filename, service_name=None): |
| 291 | +def _validate_against_schema(config, schema_filename, format_checker=[], service_name=None): |
271 | 292 | config_source_dir = os.path.dirname(os.path.abspath(__file__))
|
272 | 293 | schema_file = os.path.join(config_source_dir, schema_filename)
|
273 | 294 |
|
274 | 295 | with open(schema_file, "r") as schema_fh:
|
275 | 296 | schema = json.load(schema_fh)
|
276 | 297 |
|
277 | 298 | resolver = RefResolver('file://' + config_source_dir + '/', schema)
|
278 |
| - validation_output = Draft4Validator(schema, resolver=resolver, format_checker=FormatChecker(["ports"])) |
| 299 | + validation_output = Draft4Validator(schema, resolver=resolver, format_checker=FormatChecker(format_checker)) |
279 | 300 |
|
280 | 301 | errors = [error for error in sorted(validation_output.iter_errors(config), key=str)]
|
281 | 302 | if errors:
|
|
0 commit comments