def main(): registry_config = parse_base_registry_config() ssl_crt_file = get_config_json('SSL_CRT_FILE') ssl_key_file = get_config_json('SSL_KEY_FILE') if ssl_crt_file: registry_config['http']['tls']['certificate'] = hermes.get_config_file_path(ssl_crt_file) if ssl_key_file: registry_config['http']['tls']['key'] = hermes.get_config_file_path(ssl_key_file) if get_config_json('HTTP_AUTH_USER') and get_config_json('HTTP_AUTH_PASSWORD'): user = get_config_json('HTTP_AUTH_USER') password = get_config_json('HTTP_AUTH_PASSWORD') subprocess.check_output('htpasswd -Bbn "{}" "{}" > /tmp/htpasswd'.format(user, password), shell=True) registry_config['auth']['htpasswd']['realm'] = 'Dockyard' registry_config['auth']['htpasswd']['path'] = '/tmp/htpasswd' if get_config_json('READ_ONLY'): registry_config['storage']['maintenance']['readonly']['enabled'] = True storage_path = get_config_json('REPOSITORY_PATH') or DEFAULT_STORAGE_PATH parsed_storage_path = urlparse(storage_path) if parsed_storage_path.scheme == 's3': # Fix s3 schemas with too many "/": if storage_path.startswith('s3:///'): parsed_storage_path = urlparse(storage_path.replace('s3:///', 's3://')) s3_bucket = parsed_storage_path.netloc s3_directory = parsed_storage_path.path registry_config['storage']['s3'] = { 'bucket': s3_bucket, 'rootdirectory': s3_directory, 'region': get_config_json('AWS_REGION'), 'accesskey': get_config_json('AWS_ACCESS_KEY'), 'secretkey': get_config_json('AWS_ACCESS_SECRET'), 'secure': True } else: if not os.path.exists(storage_path): os.makedirs(storage_path) os.chmod(storage_path, 0o777) registry_config['storage']['filesystem']['rootdirectory'] = storage_path saved_dict = registry_config.to_dict() with open(REGISTRY_CONFIG_PATH, 'w') as f: f.write(yaml.safe_dump(saved_dict, default_flow_style=False)) sys.stdout.flush() command = "/go/bin/registry serve {}".format(REGISTRY_CONFIG_PATH).split() os.execv(command[0], command)
def get_destinations_for_alias(destination_alias): destination_dicts = hermes.get_config('destinations.json') if destination_dicts is None: raise DestinationException('Could not find destinations.json.') if destination_alias not in destination_dicts: logging.error( 'Destination alias {0} is not defined in destinations.json.'. format(destination_alias)) return [] destination_config_dir = os.path.dirname( hermes.get_config_file_path('destinations.json')) if isinstance(destination_dicts[destination_alias], dict): return [ Destination(destination_dicts[destination_alias], destination_config_dir) ] elif isinstance(destination_dicts[destination_alias], list): result = [] for destination_dict in destination_dicts[destination_alias]: result.append(Destination(destination_dict, destination_config_dir)) return result else: logging.error( 'Destination definition for alias {0} is neither list nor dict.'. format(destination_alias)) return []
def _create_all_sources(): sources_config_dir = hermes.get_config_file_path('sources') sources_configs_keys = hermes.get_configs_keys('sources') result = [] were_errors = False if not sources_configs_keys: logging.warning('sources_configs_keys is empty') return result, were_errors for source_config_key in sources_configs_keys: logging.debug('source_config_key: {}'.format(source_config_key)) try: sources_dict = hermes.get_config(source_config_key) logging.debug('sources_dict: {}'.format(sources_dict)) assert isinstance(sources_dict, list) except: logging.error( 'Config {source_config_key} does not contain json with list of sources.'.format(**locals())) traceback.print_exc() were_errors = True continue for source_dict in sources_dict: try: sources = list(_create_sources_from_dict(source_dict, sources_config_dir)) logging.debug('adding sources: {}'.format(sources)) result.extend(sources) except: logging.error('Invalid source configuration:\n{}'.format(source_dict)) traceback.print_exc() were_errors = True return result, were_errors
def get_ssh_key_path(filename, config_dir): if config_dir: ssh_key_path = os.path.join(config_dir, filename) else: ssh_key_path = hermes.get_config_file_path(filename) os.chmod(ssh_key_path, 0o600) return ssh_key_path
def _create_all_sources(): sources_config_dir = hermes.get_config_file_path('sources') sources_configs_keys = hermes.get_configs_keys('sources') result = [] were_errors = False if not sources_configs_keys: logging.warning('sources_configs_keys is empty') return result, were_errors for source_config_key in sources_configs_keys: logging.debug('source_config_key: {}'.format(source_config_key)) try: sources_dict = hermes.get_config(source_config_key) logging.debug('sources_dict: {}'.format(sources_dict)) assert isinstance(sources_dict, list) except Exception as e: logging.exception( 'Config {source_config_key} does not contain json with list of sources.' .format(**locals())) were_errors = True continue for source_dict in sources_dict: try: sources = list( _create_sources_from_dict(source_dict, sources_config_dir)) logging.debug('adding sources: {}'.format(sources)) result.extend(sources) except Exception as e: logging.exception( 'Invalid source configuration:\n{}'.format(source_dict)) were_errors = True return result, were_errors
def get_destinations_for_alias(destination_alias): destination_dicts = hermes.get_config('destinations.json') if destination_dicts is None: raise DestinationException('Could not find destinations.json.') if destination_alias not in destination_dicts: logging.error('Destination alias {0} is not defined in destinations.json.'.format(destination_alias)) return [] destination_config_dir = os.path.dirname(hermes.get_config_file_path('destinations.json')) if isinstance(destination_dicts[destination_alias], dict): return [Destination(destination_dicts[destination_alias], destination_config_dir)] elif isinstance(destination_dicts[destination_alias], list): result = [] for destination_dict in destination_dicts[destination_alias]: result.append(Destination(destination_dict, destination_config_dir)) return result else: logging.error('Destination definition for alias {0} is neither list nor dict.'.format(destination_alias)) return []
def main(): configs = hermes.get_config('ssh.json') if not configs: return processes = [] for config in configs: key_path = hermes.get_config_file_path(config['key']) key_dest_path = '/tmp/{}.key'.format( hashlib.md5(key_path.encode()).hexdigest()) shutil.copy(key_path, key_dest_path) os.chmod(key_dest_path, 0o600) autossh = 'autossh' command_params = ( autossh, '-M 0', '-N', '-C', '-o', 'ServerAliveInterval=60', '-o', 'ServerAliveCountMax=3', '-o', 'StrictHostKeyChecking=no', '-p {}'.format(config['proxy_port']), '-i', '{}'.format(key_dest_path), '{}@{}'.format(config['proxy_user'], config['proxy_host']), '-L 0.0.0.0:{}:{}:{}'.format(config['local_port'], config['destination_host'], config['destination_port']), ) process = Popen(command_params) processes.append(process) for process in processes: process.wait() if processes: sys.exit(1)