Exemplo n.º 1
0
    def ckan(self):
        """Initializes the ckan object for managing running CKAN instances

        :return: DeisCkanInstanceCKAN
        """
        ckan = getattr(self, '_ckan', None)
        if not ckan:
            self._ckan = ckan = DeisCkanInstanceCKAN(self)
        return ckan
Exemplo n.º 2
0
def check_cluster_url(url, verify_site_url=False, deis_instance=None):
    from ckan_cloud_operator.deis_ckan.ckan import DeisCkanInstanceCKAN
    from ckan_cloud_operator.providers.routers import manager as routers_manager
    env_id = routers_manager.get_env_id()
    root_domain = routers_manager.get_default_root_domain()
    assert url.startswith(f'https://cc-{env_id}-') and url.rstrip('/').endswith(root_domain), f'invalid cluster url: {url}'
    if verify_site_url:
        logs.info('checking site url in running instance...')
        site_url = None
        for line in DeisCkanInstanceCKAN(deis_instance).exec('env', check_output=True).decode().splitlines():
            if line.startswith('CKAN_SITE_URL='):
                site_url = line.replace('CKAN_SITE_URL=', '').strip()
        assert site_url == url, f'mismatch between envvar and instance env: {site_url} != {url}'
Exemplo n.º 3
0
def check_envvars(envvars, deis_instance):
    from ckan_cloud_operator.providers.db import manager as db_manager
    from ckan_cloud_operator.deis_ckan.ckan import DeisCkanInstanceCKAN
    logs.info('waiting for instance to be executable...')
    for i in range(20):
        try:
            DeisCkanInstanceCKAN(deis_instance).exec('true')
            break
        except Exception:
            traceback.print_exc()
            logs.warning(f'waiting for instance exec ({i}/20)...')
            time.sleep(5)
            continue
    for url_type, url in {
            'site': envvars.get('CKAN_SITE_URL'),
            'db': envvars.get('CKAN_SQLALCHEMY_URL'),
            'datastore': envvars.get('CKAN__DATASTORE__WRITE_URL'),
            'datastore-ro': envvars.get('CKAN__DATASTORE__READ_URL'),
            'beaker': envvars.get('CKAN___BEAKER__SESSION__URL'),
            'datapusher': envvars.get('CKAN__DATAPUSHER__URL'),
            's3': envvars.get('CKANEXT__S3FILESTORE__HOST_NAME'),
            'solr': envvars.get('CKAN_SOLR_URL'),
    }.items():
        logs.info(f'checking envvar: {url_type}')
        assert url and 'None' not in url, f'invalid url: {url}'
        if url_type in ['site', 'datapusher', 's3']:
            check_cluster_url(url,
                              verify_site_url=url_type == 'site',
                              deis_instance=deis_instance)
        elif url_type in ['db', 'datastore', 'datastore-ro', 'beaker']:
            if '@ckan-cloud-provider-db-proxy-pgbouncer.ckan-cloud:5432' in url:
                db_manager.check_connection_string(
                    url.replace(
                        'ckan-cloud-provider-db-proxy-pgbouncer.ckan-cloud',
                        'localhost'))
        elif url_type == 'solr':
            from ckan_cloud_operator.providers.solr import manager as solr_manager
            assert url.startswith(solr_manager.get_internal_http_endpoint())
        else:
            raise Exception(f'unknown url type: {url_type}')
Exemplo n.º 4
0
def ckan_admin_credentials(instance_id):
    from ckan_cloud_operator.deis_ckan.instance import DeisCkanInstance
    from ckan_cloud_operator.deis_ckan.ckan import DeisCkanInstanceCKAN
    return DeisCkanInstanceCKAN(
        DeisCkanInstance(instance_id)).admin_credentials()
Exemplo n.º 5
0
 def setUp(self):
     self.ckan = DeisCkanInstanceCKAN(DeisCkanInstance('montreal'))
Exemplo n.º 6
0
class CkanTestCase(unittest.TestCase):
    def setUp(self):
        self.ckan = DeisCkanInstanceCKAN(DeisCkanInstance('montreal'))

    def test_run_exec_command(self):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.run('exec', 'ls', '-lrt')
        self.ckan.instance.kubectl.assert_called_once_with(
            'exec montreal-asd-qwe ls -lrt', check_output=False)

    def test_run_paster_command(self):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.run('paster', 'initdb')
        self.ckan.instance.kubectl.assert_called_once_with(
            'exec montreal-asd-qwe -it -- paster --plugin=ckan initdb -c /srv/app/production.ini ',
            check_output=False)

    def test_run_logs_command(self):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.run('logs')
        self.ckan.instance.kubectl.assert_called_once_with(
            'logs montreal-asd-qwe ')

    @patch('ckan_cloud_operator.deis_ckan.ckan.subprocess.check_call')
    def test_run_port_forward_command(self, check_call):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.run('port-forward', '5001')
        self.ckan.instance.kubectl.assert_called_once_with(
            'port-forward montreal-asd-qwe 5001')
        check_call.assert_called_once_with([
            'kubectl', '-n', 'montreal', 'port-forward', 'deployment/montreal',
            '5001'
        ])

    def test_run_with_unsupported_command(self):
        with self.assertRaises(AssertionError):
            self.ckan.run('delete', 'everything')

    def test_init_creation_command(self):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.instance._spec = MagicMock()
        self.ckan.instance.spec.spec = {
            'ckan': {
                'init': [['paster', 'initdb'], ['paster', 'test', '-v']]
            }
        }
        self.ckan._create()
        self.assertEqual(self.ckan.instance.kubectl.call_count, 2)
        self.ckan.instance.kubectl.assert_has_calls([
            call(
                'exec montreal-asd-qwe -it -- paster --plugin=ckan initdb -c /srv/app/production.ini ',
                check_output=False),
            call(
                'exec montreal-asd-qwe -it -- paster --plugin=ckan test -c /srv/app/production.ini -v',
                check_output=False)
        ])

    def test_init_creation_with_non_paster_command(self):
        self.ckan._get_ckan_pod_name = lambda: 'montreal-asd-qwe'
        self.ckan.instance.kubectl = MagicMock()
        self.ckan.instance._spec = MagicMock()
        self.ckan.instance.spec.spec = {
            'ckan': {
                'init': [['paster', 'initdb'], ['ls', '-lrt']]
            }
        }
        with self.assertRaisesRegex(ValueError,
                                    "Invalid ckan init cmd: ['ls', '-lrt']"):
            self.ckan._create()
        self.ckan.instance.kubectl.assert_called_once_with(
            'exec montreal-asd-qwe -it -- paster --plugin=ckan initdb -c /srv/app/production.ini ',
            check_output=False)