Esempio n. 1
0
    def test_get_tron_namespaces_for_cluster_conflict(self, mock_ls, mock_walk):
        cluster_name = 'stage'
        mock_walk.return_value = [
            ('/my_soa_dir/cool', [], ['tron-stage.yaml']),
        ]
        mock_ls.return_value = ['cool.yaml']
        soa_dir = '/my_soa_dir'

        with pytest.raises(tron_tools.ConflictingNamespacesError):
            tron_tools.get_tron_namespaces_for_cluster(
                cluster=cluster_name,
                soa_dir=soa_dir,
            )
Esempio n. 2
0
    def test_get_tron_namespaces_for_cluster_default(self, mock_system_tron_config, mock_ls, mock_walk):
        mock_system_tron_config.return_value.get_cluster_name.return_value = 'this-cluster'
        mock_walk.return_value = [('/my_soa_dir/this-service', [], ['tron-this-cluster.yaml'])]
        soa_dir = '/my_soa_dir'
        expected_namespaces = ['this-service']

        namespaces = tron_tools.get_tron_namespaces_for_cluster(
            soa_dir=soa_dir,
        )
        assert namespaces == expected_namespaces
Esempio n. 3
0
def main():
    args = parse_args()
    log_level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(level=log_level)

    if args.all_namespaces:
        if args.services:
            log.error('Do not pass service names with --all flag')
            sys.exit(1)

        try:
            services = tron_tools.get_tron_namespaces_for_cluster()
        except Exception as e:
            log.error('Failed to list tron namespaces: {error}'.format(
                error=str(e), ))
            sys.exit(1)
    else:
        services = args.services

    if not services:
        log.warning("No namespaces found")
        sys.exit(0)

    client = tron_tools.get_tron_client()

    updated = []
    failed = []
    skipped = []

    for service in services:
        try:
            new_config = tron_tools.create_complete_config(
                service=service,
                soa_dir=args.soa_dir,
            )
            if client.update_namespace(service, new_config):
                updated.append(service)
                log.debug(f'Updated {service}')
            else:
                skipped.append(service)
                log.debug(f'Skipped {service}')
        except Exception as e:
            log.error(f'Update for {service} failed: {str(e)}')
            log.debug(f'Exception while updating {service}', exc_info=1)
            failed.append(service)

    skipped_report = skipped if args.verbose else len(skipped)
    log.info(
        f'Updated following namespaces: {updated}, '
        f'failed: {failed}, skipped: {skipped_report}', )

    sys.exit(1 if failed else 0)
Esempio n. 4
0
    def test_get_tron_namespaces_for_cluster(self, mock_ls, mock_walk):
        cluster_name = 'stage'
        expected_namespaces = ['app', 'foo', 'cool']
        mock_walk.return_value = [
            ('/my_soa_dir/foo', [], ['tron-stage.yaml']),
            ('/my_soa_dir/app', [], ['tron-stage.yaml']),
            ('my_soa_dir/woo', [], ['something-else.yaml']),
        ]
        mock_ls.return_value = ['cool.yaml']
        soa_dir = '/my_soa_dir'

        namespaces = tron_tools.get_tron_namespaces_for_cluster(
            cluster=cluster_name,
            soa_dir=soa_dir,
        )
        for expected_namespace in expected_namespaces:
            assert expected_namespace in namespaces
        assert len(namespaces) == 3
Esempio n. 5
0
def main():
    args = parse_args()

    client = tron_tools.get_tron_client()
    namespaces = client.list_namespaces()
    expected_namespaces = tron_tools.get_tron_namespaces_for_cluster(
        soa_dir=args.soa_dir)
    to_delete = set(namespaces) - set(expected_namespaces)

    if not to_delete:
        paasta_print('No Tron namespaces to remove')
        sys.exit(0)

    if args.dry_run:
        paasta_print('Dry run, would have removed namespaces:\n  ' +
                     '\n  '.join(to_delete))
        sys.exit(0)

    successes = []
    errors = []
    for namespace in to_delete:
        try:
            client.update_namespace(namespace, '')
            successes.append(namespace)
        except Exception as e:
            errors.append((namespace, e))

    if successes:
        paasta_print('Successfully removed namespaces:\n',
                     '\n  '.join(successes))

    if errors:
        paasta_print(
            'Failed to remove namespaces:\n  ' + '\n  '.join([
                '{namespace}: {error}'.format(namespace=namespace,
                                              error=str(error))
                for namespace, error in errors
            ], ), )
        sys.exit(1)
Esempio n. 6
0
def main():
    args = parse_args()
    namespaces = tron_tools.get_tron_namespaces_for_cluster(
        cluster=args.tron_cluster, soa_dir=args.soa_dir)
    paasta_print('\n'.join(namespaces))