def migrate(session, task, id, revision): if id == 'all': instances = Instance.all(session) else: instances = [Instance.get(session, id)] for instance in instances: instance.upgrade_schema(revision)
def reload(session, task, id, force=False, kill=False): if id == 'all': instances = Instance.all(session) else: instances = [Instance.get(session, id)] for instance in instances: if instance.enabled: instance.reload(force=force, kill=kill)
def empty_instance_groups(context, request): domain = request.matchdict['domain'] group = Group.get(request.db_session, domain) instance = Instance.get_by_domain(request.db_session, domain) instance.groups = [group] request.db_session.commit() raise HTTPNoContent()
def create(session, task, source, instance_id, http_code=301, target_path=''): instance = Instance.get(session, instance_id) redirect = Redirect(source=source, instance=instance, http_code=http_code, target_path=target_path) session.add(redirect) session.flush() return redirect.source
def update(context, request): domain = request.matchdict['domain'] # prefer 404 upon 400, so try first to get the instance params = dict(request.params) params.pop('action', '') instance = Instance.get_by_domain(request.db_session, domain) if request.method == 'DELETE': taskname = 'instance.delete' params['archive'] = True if request.params.get('archive') else '' elif 'action' not in request.params: raise ParamsError('No action provided') elif request.params['action'] in ('enable', 'disable', 'reload', 'kill', 'sentence', 'flush_cache', 'archive', 'migrate', 'switch_environment', 'rewrite', 'change_domain'): taskname = "instance.{}".format(request.params['action']) else: raise ParamsError('invalid action {}'.format(request.params['action'])) try: if request.params['action'] == 'change_domain': check_domain_not_used(request, params['domain']) except KeyError: pass return request.submit_task(taskname, id=instance.id, **params)
def restore(context, request): # FIXME: re-enable when restore issues are sorted out raise NotImplementedError() domain = request.matchdict['domain'] instance = Instance.get_by_domain(request.db_session, domain) archive = request.params.get('archive') archive_name = request.params.get('archive_name') if archive and archive_name: raise ParamsError('archive and archive_name are mutually exclusive') elif archive_name and not archive: raise ParamsError('missing both archive and archive name') elif archive: # TODO handle archive upload raise NotImplementedError() if not archive_name.endswith("tar.gz"): archive_name = "{}.tar.gz".format(archive_name) archives = Environment.settings['paths.archives'] archive_path = os.path.join(archives, archive_name) if not os.path.exists(archive_path): raise ParamsError('{} is not a valid archive'.format(archive_name)) return request.submit_task('instance.restore', id=instance.id, archive_name=archive_name)
def update(context, request): params = dict() domain = request.matchdict['domain'] Alias.get(request.db_session, domain) try: if "destination" in request.params: params['instance_id'] = Instance.get_by_domain( request.db_session, request.params['destination']).id if "new_domain" in request.params: check_domain_not_used(request, request.params['new_domain']) params['new_domain'] = validate_hostname( request.params['new_domain']) except NoResultFound: raise ParamsError("No instance for domain {}"\ .format(request.params['destination'])) if not params: raise ParamsError("Missing update fields") params['domain'] = domain return request.submit_task('alias.update', **params)
def login(context, request): email = urllib.unquote(request.matchdict['email']) user = User.get(request.db_session, email) # non-admin users cannot check if another user has permissions on a # given instance if authenticated_userid(request) != email and \ 'admin' not in effective_principals(request): return generate_empty_response(HTTPForbidden(), request, 403) try: # the domain could be an alias. We need the instance domain domain = Alias.get(request.db_session, request.params['domain'])\ .instance.domain except NoResultFound: domain = request.params['domain'] except KeyError: log.error('No domain in request for users.login') return generate_empty_response(HTTPForbidden(), request, 403) instance = Instance.get_by_domain(request.db_session, domain) if not user.can_access(instance): log.error('%s cannot login on %s', email, domain) return generate_empty_response(HTTPForbidden(), request, 403) return user.to_dict()
def rewrite(session, task, id): if id == "all": instances = Instance.all(session) else: instances = [Instance.get(session, id)] envs = {} for instance in instances: if not instance.enabled: continue env = instance.environment envs[env.name] = env instance.rewrite(restart_services=False) for env in envs.values(): env.restart_services()
def instance_group_info(context, request): domain = request.matchdict['domain'] group = request.matchdict['group'] instance = Instance.get_by_domain(request.db_session, domain) if not group.name in (g.name for g in instance.groups): raise NoResultFound() return group.to_dict()
def instances_allowed_for_user(context, request): email = urllib.unquote(request.matchdict['email']) user = User.get(request.db_session, email) res = {} for instance in Instance.all(request.db_session): if user.can_access(instance): res[instance.domain] = instance.to_dict() return res
def allowed_users_for_instance(context, request): domain = request.matchdict['domain'] instance = Instance.get_by_domain(request.db_session, domain) res = {} for user in User.all(request.db_session): if user.can_access(instance): res[user.email] = user.to_dict() return res
def update(session, task, domain, new_domain=None, instance_id=None): try: alias = Alias.get(session, domain) if instance_id: alias.instance = Instance.get(session, instance_id) if new_domain: alias.domain = new_domain session.flush() except: log.exception('alias.update')
def change_domain(session, task, id, domain): instance = Instance.get(session, id) reenable = False if instance.enabled: reenable = True instance.enabled = False instance.change_domain(domain) if reenable: instance.enabled = True
def switch_environment(session, task, id, environment): instance = Instance.get(session, id) new_env = Environment.get(session, environment) reenable = False if instance.enabled: reenable = True instance.enabled = False instance.environment = new_env if reenable: instance.enabled = True
def deploy(session, task, domain, owner_email, environment_name, technical_contact_email, theme_name=None, default_language=u'it', database_password=None, enabled=True): env = Environment.get(session, environment_name) theme = None if not theme_name else Theme.get(session, theme_name) owner = User.get(session, owner_email) technical_contact = User.get(session, technical_contact_email) instance = Instance.deploy(session, domain, owner, env, technical_contact, theme, default_language, database_password, enabled) return instance.id
def create(context, request): try: domain = request.params['domain'] now = datetime.datetime.now().strftime('%Y%m%d-%H.%M.%S') default_name = "{}-{}".format(domain, now) name = request.params.get('name', default_name) name.replace(":", ".") except KeyError as e: raise ParamsError(e) instance = Instance.get_by_domain(request.db_session, domain) return request.submit_task('instance.archive', id=instance.id, name=name)
def update(session, task, source, instance_id=None, http_code=None, target_path=None, new_source=None): try: redirect = Redirect.get(session, source) if instance_id: redirect.instance = Instance.get(session, instance_id) if http_code: redirect.http_code = http_code if target_path: redirect.target_path = target_path if new_source: redirect.source = new_source except: log.exception('redirect.update')
def remove_group_from_instance(context, request): domain = request.matchdict['domain'] group_name = request.matchdict['group'] instance = Instance.get_by_domain(request.db_session, domain) try: group = Group.get(request.db_session, group_name) except NoResultFound: raise HTTPNoContent() if group.name == domain: raise HTTPConflict(headers={'X-Request-Error': \ "group {} cannot be removed".format(group_name)}) instance.groups = [g for g in instance.groups if g.name != group.name] request.db_session.commit() raise HTTPNoContent()
def create(context, request): try: domain = request.params['domain'] instance = Instance.get_by_domain(request.db_session, request.params['destination']) check_domain_not_used(request, domain) except KeyError as e: raise ParamsError(e) except NoResultFound: raise ParamsError('No instance for domain {}'\ .format(request.params['destination'])) else: params = dict(domain=domain, instance_id=instance.id) return request.submit_task('alias.create', **params)
def set_instance_groups(context, request): domain = request.matchdict['domain'] request_groups = set(request.params.getall('groups')) if domain not in request_groups: request_groups.add(domain) instance = Instance.get_by_domain(request.db_session, domain) groups = Group.search( request.db_session, filters=(Group.name.in_(request_groups), ) ) if len(groups) != len(request_groups): raise HTTPPreconditionFailed( headers={'X-Request-Error': 'Invalid groups {}'\ .format(','.join(request_groups))}) instance.groups = groups request.db_session.commit() raise HTTPNoContent()
def add_group_to_instance(context, request): domain = request.matchdict['domain'] group_name = request.matchdict['group'] instance = Instance.get_by_domain(request.db_session, domain) try: group = Group.get(request.db_session, group_name) except NoResultFound: raise HTTPPreconditionFailed( headers={'X-Request-Error': 'No such group {}'\ .format(group_name)} ) if group_name in (g.name for g in instance.groups): raise HTTPNoContent() instance.groups.append(group) request.db_session.commit() raise HTTPNoContent()
def create(context, request): try: source = validate_hostname(request.params['source']) instance = Instance.get_by_domain(request.db_session, request.params['destination']) http_code = request.params.get('http_code', 301) target_path = request.params.get('target_path', '') check_domain_not_used(request, source) except KeyError as e: raise ParamsError(e) except NoResultFound: raise ParamsError('No instance for domain {}'\ .format(request.params['destination'])) else: params = dict(source=source, instance_id=instance.id, http_code=http_code, target_path=target_path) return request.submit_task('redirect.create', **params)
def restore(session, task, id, archive_name): instance = Instance.get(session, id) instance.restore(archive_name)
def archive(session, task, id, name): instance = Instance.get(session, id) instance.archive(name)
def create(session, task, domain, instance_id): instance = Instance.get(session, instance_id) alias = Alias(domain=domain, instance=instance) session.add(alias) session.flush() return alias.domain
def delete(session, task, id, archive=False): instance = Instance.get(session, id) instance.delete(archive=archive)
def flush_cache(session, task, id): instance = Instance.get(session, id) instance.flush_cache()
def disable(session, task, id): instance = Instance.get(session, id) instance.enabled = False
def enable(session, task, id): instance = Instance.get(session, id) instance.enabled = True