def _annotate(self, *annotations, overwrite=False):
     cmd = f'kubectl -n ckan-cloud annotate {self.resource_kind} {self.resource_id}'
     for annotation in annotations:
         cmd += f' ckan-cloud/{annotation}'
     if overwrite:
         cmd += ' --overwrite'
     logs.subprocess_check_call(cmd, shell=True)
def create_ckan_admin_user(instance_id_or_name, name, email=None, password=None, dry_run=False, use_paster=False):
    if not email:
        default_root_domain = routers_manager.get_default_root_domain()
        email = f'{name}@{instance_id_or_name}.{default_root_domain}'
    if not password:
        password = _generate_password(8)
    instance_id, instance_type, instance = _get_instance_id_and_type(instance_id_or_name)
    user = {
        'name': name,
        'email': email,
        'password': password
    }
    if not dry_run:
        pod_name = _get_running_pod_name(instance_id)
        name, password, email = [user[k] for k in ['name', 'password', 'email']]
        logs.info(f'Creating CKAN admin user with {name} ({email}) on pod {pod_name}')

        if use_paster:
            logs.subprocess_check_call(
                f'echo y | kubectl -n {instance_id} exec -i {pod_name} -- ckan-paster --plugin=ckan sysadmin -c /etc/ckan/production.ini add {name} password={password} email={email}',
                shell=True
            )
        else:
            logs.subprocess_check_call(
                f'echo y | kubectl -n {instance_id} exec -i {pod_name} -- ckan --config /etc/ckan/production.ini sysadmin add {name} password={password} email={email}',
                shell=True
            )

    return {
        'instance-id': instance_id,
        'instance-type': instance_type,
        **{f'admin-{k}': v for k, v in user.items()},
        **({'dry-run': True} if dry_run else {}),
    }
def edit(what, *edit_args, namespace='ckan-cloud', **edit_kwargs):
    extra_edit_args = ' '.join(edit_args)
    extra_edit_kwargs = ' '.join([f'{k} {v}' for k, v in edit_kwargs.items()])
    res = get(what, namespace=namespace, required=True)
    items = res.get('items', [res])
    assert len(items) > 0, f'no items found to edit for: {what}'
    for item in items:
        name = item['metadata']['name']
        kind = item['kind']
        logs.subprocess_check_call(
            f'kubectl -n {namespace} edit {kind}/{name} {extra_edit_args} {extra_edit_kwargs}',
            shell=True)
def create_ckan_admin_user(instance_id, instance, user):
    pod_name = None
    while not pod_name:
        try:
            pod_name = kubectl.get_deployment_pod_name('ckan', instance_id, use_first_pod=True, required_phase='Running')
            break
        except Exception as e:
            logs.warning('Failed to find running ckan pod', str(e))
        time.sleep(20)

    name, password, email = [user[k] for k in ['name', 'password', 'email']]
    logs.info(f'Creating CKAN admin user with {name} ({email}) and {password} on pod {pod_name}')
    logs.subprocess_check_call(
        f'echo y | kubectl -n {instance_id} exec -i {pod_name} -- ckan-paster --plugin=ckan sysadmin -c /etc/ckan/production.ini add {name} password={password} email={email}',
        shell=True
    )
def delete_ckan_admin_user(instance_id_or_name, name, dry_run=False, use_paster=False):
    instance_id, instance_type, instance = _get_instance_id_and_type(instance_id_or_name)

    if not dry_run:
        pod_name = _get_running_pod_name(instance_id)
        logs.info(f'Removing CKAN admin user {name} from sys-admins')

        if use_paster:
            logs.subprocess_check_call(
                f'echo y | kubectl -n {instance_id} exec -i {pod_name} -- ckan-paster --plugin=ckan sysadmin -c /etc/ckan/production.ini remove {name}',
                shell=True
            )
        else:
            logs.subprocess_check_call(
                f'echo y | kubectl -n {instance_id} exec -i {pod_name} -- ckan --config /etc/ckan/production.ini sysadmin remove {name}',
                shell=True
            )
def check_call(cmd, namespace='ckan-cloud', use_first_pod=False):
    cmd = _parse_call_cmd(cmd, namespace, use_first_pod)
    logs.subprocess_check_call(f'kubectl -n {namespace} {cmd}', shell=True)