def get_branches_from_config_file(file_dir, filename): """Get all branches defined in a single service configuration file. A branch is defined for an instance if it has a 'branch' key, or the branch name is paasta-{cluster}.{instance}, where cluster is the cluster the marathon or chronos file is defined for (i.e. marathon-hab.yaml is for hab), and instance is the instance name. :param file_dir: The directory that the filename argument is in :param filename: The name of the service configuration file to read from :returns: A set of branch names listed in the configuration file """ valid_branches = set([]) config = service_configuration_lib.read_service_information( os.path.join(file_dir, filename)) for instance in config: target_branch = None if 'branch' in config[instance]: target_branch = config[instance]['branch'] else: try: # cluster may contain dashes (and frequently does) so # reassemble the cluster after stripping the chronos/marathon prefix cluster = '-'.join(filename.split('-')[1:]).split('.')[0] target_branch = get_default_branch(cluster, instance) except IndexError: pass if target_branch: valid_branches.add(target_branch) return valid_branches
def load_chronos_job_config(service, instance, cluster, load_deployments=True, soa_dir=DEFAULT_SOA_DIR): service_chronos_jobs = read_chronos_jobs_for_service(service, cluster, soa_dir=soa_dir) if instance not in service_chronos_jobs: raise InvalidChronosConfigError('No job named "%s" in config file chronos-%s.yaml' % (instance, cluster)) branch_dict = {} if load_deployments: deployments_json = load_deployments_json(service, soa_dir=soa_dir) branch = get_default_branch(cluster, instance) branch_dict = deployments_json.get_branch_dict(service, branch) return ChronosJobConfig(service, instance, service_chronos_jobs[instance], branch_dict)
def load_marathon_service_config(service, instance, cluster, load_deployments=True, soa_dir=DEFAULT_SOA_DIR): """Read a service instance's configuration for marathon. If a branch isn't specified for a config, the 'branch' key defaults to paasta-${cluster}.${instance}. :param name: The service name :param instance: The instance of the service to retrieve :param cluster: The cluster to read the configuration for :param load_deployments: A boolean indicating if the corresponding deployments.json for this service should also be loaded :param soa_dir: The SOA configuration directory to read from :returns: A dictionary of whatever was in the config for the service instance""" log.info("Reading service configuration files from dir %s/ in %s" % (service, soa_dir)) log.info("Reading general configuration file: service.yaml") general_config = service_configuration_lib.read_service_configuration( service, soa_dir=soa_dir) marathon_conf_file = "marathon-%s" % cluster log.info("Reading marathon configuration file: %s.yaml", marathon_conf_file) instance_configs = service_configuration_lib.read_extra_service_information( service, marathon_conf_file, soa_dir=soa_dir) if instance not in instance_configs: raise NoConfigurationForServiceError( "%s not found in config file %s/%s/%s.yaml." % (instance, soa_dir, service, marathon_conf_file)) general_config.update(instance_configs[instance]) branch_dict = {} if load_deployments: deployments_json = load_deployments_json(service, soa_dir=soa_dir) branch = general_config.get('branch', get_default_branch(cluster, instance)) branch_dict = deployments_json.get_branch_dict(service, branch) return MarathonServiceConfig( service, instance, general_config, branch_dict, )
def write_soa_dir_chronos_deployments(context, service, disabled, instance): if disabled == 'disabled': desired_state = 'stop' else: desired_state = 'start' if not os.path.exists(os.path.join(context.soa_dir, service)): os.makedirs(os.path.join(context.soa_dir, service)) with open(os.path.join(context.soa_dir, service, 'deployments.json'), 'w') as dp: dp.write(json.dumps({ 'v1': { '%s:%s' % (service, utils.get_default_branch(context.cluster, instance)): { 'docker_image': 'test-image-foobar%d' % context.tag_version, 'desired_state': desired_state, } } }))
def load_marathon_service_config(service, instance, cluster, load_deployments=True, soa_dir=DEFAULT_SOA_DIR): """Read a service instance's configuration for marathon. If a branch isn't specified for a config, the 'branch' key defaults to paasta-${cluster}.${instance}. :param name: The service name :param instance: The instance of the service to retrieve :param cluster: The cluster to read the configuration for :param load_deployments: A boolean indicating if the corresponding deployments.json for this service should also be loaded :param soa_dir: The SOA configuration directory to read from :returns: A dictionary of whatever was in the config for the service instance""" log.info("Reading service configuration files from dir %s/ in %s" % (service, soa_dir)) log.info("Reading general configuration file: service.yaml") general_config = service_configuration_lib.read_service_configuration( service, soa_dir=soa_dir ) marathon_conf_file = "marathon-%s" % cluster log.info("Reading marathon configuration file: %s.yaml", marathon_conf_file) instance_configs = service_configuration_lib.read_extra_service_information( service, marathon_conf_file, soa_dir=soa_dir ) if instance not in instance_configs: raise NoConfigurationForServiceError( "%s not found in config file %s/%s/%s.yaml." % (instance, soa_dir, service, marathon_conf_file) ) general_config.update(instance_configs[instance]) branch_dict = {} if load_deployments: deployments_json = load_deployments_json(service, soa_dir=soa_dir) branch = general_config.get('branch', get_default_branch(cluster, instance)) branch_dict = deployments_json.get_branch_dict(service, branch) return MarathonServiceConfig( service, instance, general_config, branch_dict, )
def load_chronos_job_config(service, instance, cluster, load_deployments=True, soa_dir=DEFAULT_SOA_DIR): service_chronos_jobs = read_chronos_jobs_for_service(service, cluster, soa_dir=soa_dir) if instance not in service_chronos_jobs: raise InvalidChronosConfigError( 'No job named "%s" in config file chronos-%s.yaml' % (instance, cluster)) branch_dict = {} if load_deployments: deployments_json = load_deployments_json(service, soa_dir=soa_dir) branch = get_default_branch(cluster, instance) branch_dict = deployments_json.get_branch_dict(service, branch) return ChronosJobConfig(service, instance, service_chronos_jobs[instance], branch_dict)
def write_soa_dir_chronos_deployments(context, service, disabled, instance): if disabled == 'disabled': desired_state = 'stop' else: desired_state = 'start' if not os.path.exists(os.path.join(context.soa_dir, service)): os.makedirs(os.path.join(context.soa_dir, service)) with open(os.path.join(context.soa_dir, service, 'deployments.json'), 'w') as dp: dp.write( json.dumps({ 'v1': { '%s:%s' % (service, utils.get_default_branch( context.cluster, instance)): { 'docker_image': 'test-image-foobar%d' % context.tag_version, 'desired_state': desired_state, } } }))