def capture_exception(ex: Exception): sentry_sdk.init(env('SENTRY_DSN'), release=env('APP_VERSION')) with sentry_sdk.push_scope() as scope: scope.set_extra('debug', DEBUG) return sentry_sdk.capture_exception(ex)
def capture_message(message: str): sentry_sdk.init(env('SENTRY_DSN'), release=env('APP_VERSION')) with sentry_sdk.push_scope() as scope: scope.set_extra('debug', DEBUG) return sentry_sdk.capture_message(message)
def test_os_environ(mock_os): expected_value = 'environment_value' mock_os.environ.__contains__.return_value = True # patch in statement mock_os.environ.__getitem__.return_value = expected_value os_variable = env('a_key') assert expected_value == os_variable mock_os.environ.__getitem__.assert_called_with('a_key')
def generate_docker(self, print_console, for_env=env.env()): compose = self._config.get_compose(for_env) compose_file_path = env.docker_compose_file_path(for_env) gen_path = os.path.dirname(compose_file_path) if not os.path.isdir(gen_path): os.mkdir(gen_path) if print_console: print(yaml.dump(compose)) with open(compose_file_path, 'w') as docker_compose_file: yaml.dump(compose, docker_compose_file) versions_file = env.project_path('.versions') if os.path.isfile(versions_file): os.remove(versions_file)
def argparse(parser, subparsers): parser_main = subparsers.add_parser( 'config', help="environment and project config helper") subparser = parser_main.add_subparsers(required=True, dest="config_command", help="config sub-commands") parser_init = subparser.add_parser( 'init', help="initializes a new dev environment") parser_init.add_argument( '-r', '--repo', nargs="?", default="[email protected]:vertexportus/devdock.git", help="devdock repository to use") if ProjectConfigManager.config_exists(): parser_docker = subparser.add_parser( 'docker', help="generates docker config") parser_docker.add_argument('-g', '--generate', action="store_true", help="generate docker config") parser_docker.add_argument( '-p', '--print', action="store_true", help="print docker config on console on " "generate") parser_docker.add_argument( '-e', '--env', nargs="?", default=env.env(), help="generate docker config for specific env") parser_env = subparser.add_parser('env', help="manages env configs") parser_env.add_argument('-g', '--generate', action="store_true", help="generate/update env files") parser_hosts = subparser.add_parser( 'hosts', help="generates host information")
import pytz import os import logging.config from utils.env import env from datetime import datetime DEBUG = env("DEBUG", False) HOME_DIR = os.path.dirname(os.path.abspath(__file__)) TIME_ZONE = env("TIME_ZONE", 'Australia/Perth') TZ = datetime.now(tz=pytz.timezone(TIME_ZONE)).tzinfo START_WORKING_HOUR = env("START_WORKING_HOUR", vtype=int) END_WORKING_HOUR = env("END_WORKING_HOUR", vtype=int) logging.basicConfig(level="WARNING") LOG_CONFIG = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'console': { 'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s' }, }, 'handlers': { 'console': { 'level': 'DEBUG' if DEBUG else 'INFO', 'class': 'logging.StreamHandler',
import time from azure.eventhub import EventHubClient, EventData from utils.env import env from utils.logging_service import LoggingService logger = LoggingService('Send').logger CONNECTION_STRING = env('EVENT_HUB_CONNECTION_STRING') EVENT_HUB_TOPIC = env('EVENT_HUB_TOPIC_HELLO_WORLD_NAME') EVENT_HUB_PARTITION = env('EVENT_HUB_TOPIC_HELLO_WORLD_PARTITION') message = "Hello, World!" def send(): client, sender = build_sender_client() logger.info(f"Sending message: {message}") start_time = time.time() sender.send(EventData(message)) end_time = time.time() client.stop() run_time = end_time - start_time logger.info("Runtime: {} seconds".format(run_time))
import logging import sys from utils.env import env default_level = env('DEFAULT_LOGGING_LEVEL', default=logging.INFO) default_logging_format = env( 'DEFAULT_LOGGING_FORMAT', default='%(asctime)s - %(name)s - %(levelname)s - %(message)s') class LoggingService: def __init__(self, name: str, *, logging_format: str = default_logging_format, level: int = default_level): self.name = name self.logging_format = logging_format self.level = level self._logger = None @property def logger(self): if self._logger: return self._logger self._logger = self._build_logger() return self._logger
class ProjectConfig(YamlDataObject): yaml_tag = '!ProjectConfig' hidden_fields = ['templates', 'defaults'] current_env = env.env() defaults: dict templates: Templates docker: dict projects: dict services: dict devdock: ProjectRepo def __init__(self): super().__init__(file_path=env.project_config_file_path()) self.defaults = self.load(env.devdock_path('docker/defaults.yaml')) self.templates = Templates(env.docker_template_path(), project_config=self) self.docker = self.try_get('docker', {}) self.devdock = ProjectRepo(None, self.get_required('devdock')) self.projects = { k: Project(k, master=self, data=v) for k, v in self.try_get('projects', {}).items() } self.services = { k: Service(k, master=self, data=v) for k, v in self.try_get('services', {}).items() } # container names for service in self.services.values(): service.define_container_names() for project in self.projects.values(): project.define_container_names() # post load init for service in self.services.values(): service.post_load_init() for project in self.projects.values(): project.post_load_init() def get_compose(self, for_env): self.current_env = for_env compose = { 'version': self.get_compose_version(), 'services': {}, 'volumes': {} } rebuild_marker_load() for service in self.services.values(): service.generate_compose(compose['services'], compose['volumes']) for project in self.projects.values(): project.generate_compose(compose['services'], compose['volumes']) rebuild = rebuild_marker_save() return compose def get_compose_version(self): compose_version = deep_get(self.docker, 'compose.version') if compose_version: if float(compose_version) < deep_get( self.defaults, 'docker-compose.version.minimum'): raise Exception( f"devdock requires at least version " f"{deep_get(self.defaults, 'docker-compose.version.minimum')} " f"for docker-compose format") else: compose_version = str( deep_get(self.defaults, 'docker-compose.version.auto')) return compose_version def get_container_by_path(self, container_path): container = None if '.' in container_path: [project_path, service_path] = container_path.split('.') if project_path in self.projects: project = self.projects[project_path] if service_path in project.services: container = project.services[ service_path].get_container_by_path(service_path) else: if container_path in self.services: container = self.services[ container_path].get_container_by_path(container_path) elif container_path in self.projects: if container_path in self.projects[container_path].services: container = self.projects[container_path].services[container_path] \ .get_container_by_path(container_path) if not container: raise Exception(f"can't find container by path '{container_path}'") return container def get_container_name_by_path(self, container_path): return self.get_container_by_path(container_path).fullname def get_container_templates(self): container_templates = {} for project in self.projects.values(): for service in project.services.values(): container_templates = { **container_templates, **{ v.fullname: v for v in service.get_container_templates().values() } } for service in self.services.values(): container_templates = { **container_templates, **{ v.fullname: v for v in service.get_container_templates().values() } } return container_templates def convert_service_url_to_full_url(self, service_url): https = 'https://' in service_url url_split = service_url.replace('https://' if https else 'http://', '').split('/') container_name = self.get_container_name_by_path(url_split.pop(0)) return f'{"https" if https else "http"}://{container_name}/{"/".join(url_split)}' def get_service_port_http(self, service): container = self.get_container_by_path(service) if 'http' not in container.ports: raise Exception( f"service {service} does not contain http port config") http = container.ports['http'] return f"{http['env']}:-{http['default']}"
def test_os_environ_key_missing_with_default(): expected_value = 'a_default' os_variable = env('a_key', default=expected_value) assert expected_value == os_variable
def test_os_environ_key_missing(): expected_value = None os_variable = env('a_key') assert expected_value == os_variable