Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(operations): expose authoritative run actions

Juan Denis3f85945cd222
diff --git a/backend/app/api/deployment_jobs.py b/backend/app/api/deployment_jobs.py
index 2612c5bf..c39e641d 100644
--- a/backend/app/api/deployment_jobs.py
+++ b/backend/app/api/deployment_jobs.py
@@ -34,6 +34,19 @@ def _job_operable_by(user, job):
return user.is_admin or job.requested_by == user.id
+def _job_payload(job, user, include_plan=False, include_logs=False):
+ """Serialize action authority beside the deployment record.
+
+ Deployments have a retry endpoint but no cancellation endpoint. Permission
+ is evaluated per caller here so viewers never receive an optimistic action
+ that the mutation route would reject.
+ """
+ payload = job.to_dict(include_plan=include_plan, include_logs=include_logs)
+ payload['can_cancel'] = False
+ payload['can_retry'] = job.status == 'failed' and _job_operable_by(user, job)
+ return payload
+
+
@deployment_jobs_bp.route('/simulate', methods=['GET'])
@jwt_required()
def get_simulate_info():
@@ -89,12 +102,14 @@ def list_deployment_jobs():
app_id = request.args.get('app_id', type=int)
limit = request.args.get('limit', 50, type=int)
+ scoped_app = None
if app_id:
app = Application.query_active().filter_by(id=app_id).first()
if not app:
return jsonify({'error': 'Application not found'}), 404
if not user or app_access_tier(user, app) is None:
return jsonify({'error': 'Access denied'}), 403
+ scoped_app = app
elif not user or not user.is_admin:
return jsonify({'error': 'Access denied'}), 403
@@ -104,6 +119,14 @@ def list_deployment_jobs():
app_id=app_id,
limit=min(limit, 200),
)
+ can_retry_scoped = bool(
+ user and scoped_app and ResourceGrantService.can_operate_app(user, scoped_app)
+ )
+ for job in jobs:
+ job['can_cancel'] = False
+ job['can_retry'] = job.get('status') == 'failed' and (
+ bool(user and user.is_admin) if scoped_app is None else can_retry_scoped
+ )
return jsonify({'jobs': jobs}), 200
@@ -118,8 +141,9 @@ def get_deployment_job(job_id):
user = get_current_user()
if not user or not _job_visible_to(user, job):
return jsonify({'error': 'Access denied'}), 403
- return jsonify({'job': job.to_dict(include_logs=include_logs,
- include_plan=include_plan)}), 200
+ return jsonify({'job': _job_payload(
+ job, user, include_logs=include_logs, include_plan=include_plan,
+ )}), 200
@deployment_jobs_bp.route('/<job_id>/retry', methods=['POST'])
@@ -138,6 +162,11 @@ def retry_deployment_job(job_id):
# Distinguish "no such job" from a state/enqueue error.
status = 404 if result.get('error') == 'Deployment job not found' else 400
return jsonify({'error': result.get('error')}), status
+ if result.get('job'):
+ # A retry creates a pending clone. It has neither a retry nor cancel
+ # endpoint at this point, matching the capability contract above.
+ result['job']['can_cancel'] = False
+ result['job']['can_retry'] = False
return jsonify(result), 202
diff --git a/backend/app/api/jobs.py b/backend/app/api/jobs.py
index feb8b175..07dc5e6b 100644
--- a/backend/app/api/jobs.py
+++ b/backend/app/api/jobs.py
@@ -4,12 +4,26 @@ from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required
from app.jobs import registry
+from app.jobs.models import Job
from app.jobs.service import JobService, ScheduledJobService
from app.middleware.rbac import require_admin_user
jobs_bp = Blueprint('jobs', __name__)
+def _job_payload(job, include_payload=False):
+ """Serialize the operation actions this admin may invoke right now.
+
+ The Jobs blueprint is admin-only, so permission is already settled at the
+ route boundary. These flags are the remaining state/capability decision the
+ shell needs; clients must not infer it from a status string.
+ """
+ payload = job.to_dict(include_payload=include_payload)
+ payload['can_cancel'] = job.status in (Job.STATUS_PENDING, Job.STATUS_RUNNING)
+ payload['can_retry'] = job.status in (Job.STATUS_FAILED, Job.STATUS_CANCELLED)
+ return payload
+
+
# --- Static routes first; Werkzeug ranks these above the dynamic /<job_id>. ---
@jobs_bp.route('', methods=['GET'])
@jobs_bp.route('/', methods=['GET'])
@@ -31,7 +45,7 @@ def list_jobs():
jobs = JobService.list(limit=limit, offset=offset, **filters)
total = JobService.count(**filters)
return jsonify({
- 'jobs': [j.to_dict() for j in jobs],
+ 'jobs': [_job_payload(j) for j in jobs],
'total': total,
'limit': limit,
'offset': offset,
@@ -66,7 +80,7 @@ def run_scheduled(scheduled_id):
job = ScheduledJobService.run_now(scheduled_id)
if not job:
return jsonify({'error': 'Scheduled job not found'}), 404
- return jsonify({'job': job.to_dict()})
+ return jsonify({'job': _job_payload(job)})
@jobs_bp.route('/scheduled/<int:scheduled_id>/enabled', methods=['POST'])
@@ -87,7 +101,7 @@ def get_job(job_id):
job = JobService.get(job_id)
if not job:
return jsonify({'error': 'Job not found'}), 404
- return jsonify({'job': job.to_dict(include_payload=True)})
+ return jsonify({'job': _job_payload(job, include_payload=True)})
@jobs_bp.route('/<job_id>/cancel', methods=['POST'])
@@ -97,7 +111,7 @@ def cancel_job(job_id):
job = JobService.cancel(job_id)
if not job:
return jsonify({'error': 'Job not found'}), 404
- return jsonify({'job': job.to_dict()})
+ return jsonify({'job': _job_payload(job)})
@jobs_bp.route('/<job_id>/retry', methods=['POST'])
@@ -107,4 +121,4 @@ def retry_job(job_id):
job = JobService.retry(job_id)
if not job:
return jsonify({'error': 'Job not found'}), 404
- return jsonify({'job': job.to_dict()})
+ return jsonify({'job': _job_payload(job)})
diff --git a/backend/tests/test_deployment_jobs_authz.py b/backend/tests/test_deployment_jobs_authz.py
index 63533e19..56896b7d 100644
--- a/backend/tests/test_deployment_jobs_authz.py
+++ b/backend/tests/test_deployment_jobs_authz.py
@@ -69,7 +69,11 @@ def test_list_jobs_app_scoped_requires_access(client, job_rbac):
s = job_rbac.s
url = f'/api/v1/deployment-jobs?app_id={s.app_id}'
for persona in ('owner', 'member', 'viewer', 'admin'):
- assert client.get(url, headers=getattr(s, persona)).status_code == 200, persona
+ response = client.get(url, headers=getattr(s, persona))
+ assert response.status_code == 200, persona
+ job = response.get_json()['jobs'][0]
+ assert job['can_cancel'] is False
+ assert job['can_retry'] is (persona != 'viewer')
assert client.get(url, headers=s.foreign).status_code == 403
@@ -94,7 +98,11 @@ def test_job_detail_requires_app_access(client, job_rbac):
s = job_rbac.s
url = f'/api/v1/deployment-jobs/{job_rbac.app_job_id}?logs=true'
for persona in ('owner', 'admin'):
- assert client.get(url, headers=getattr(s, persona)).status_code == 200, persona
+ response = client.get(url, headers=getattr(s, persona))
+ assert response.status_code == 200, persona
+ job = response.get_json()['job']
+ assert job['can_cancel'] is False
+ assert job['can_retry'] is True
for persona in ('member', 'viewer', 'foreign'):
assert client.get(url, headers=getattr(s, persona)).status_code == 403, persona
diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.py
index b333f5f9..ee96b869 100644
--- a/backend/tests/test_jobs.py
+++ b/backend/tests/test_jobs.py
@@ -202,11 +202,22 @@ class TestApi:
resp = client.get('/api/v1/jobs', headers=auth_headers)
assert resp.status_code == 200
- assert any(j['id'] == job.id for j in resp.get_json()['jobs'])
+ listed = next(j for j in resp.get_json()['jobs'] if j['id'] == job.id)
+ assert listed['can_cancel'] is True
+ assert listed['can_retry'] is False
resp = client.get(f'/api/v1/jobs/{job.id}', headers=auth_headers)
assert resp.status_code == 200
- assert resp.get_json()['job']['payload'] == {'hello': 'world'}
+ detail = resp.get_json()['job']
+ assert detail['payload'] == {'hello': 'world'}
+ assert detail['can_cancel'] is True
+ assert detail['can_retry'] is False
+
+ job.status = Job.STATUS_FAILED
+ db.session.commit()
+ failed = client.get(f'/api/v1/jobs/{job.id}', headers=auth_headers).get_json()['job']
+ assert failed['can_cancel'] is False
+ assert failed['can_retry'] is True
def test_stats_endpoint(self, client, auth_headers, app):
JobService.enqueue('test.api', {})
@@ -218,7 +229,10 @@ class TestApi:
job = JobService.enqueue('test.api', {})
resp = client.post(f'/api/v1/jobs/{job.id}/cancel', headers=auth_headers)
assert resp.status_code == 200
- assert resp.get_json()['job']['status'] == Job.STATUS_CANCELLED
+ payload = resp.get_json()['job']
+ assert payload['status'] == Job.STATUS_CANCELLED
+ assert payload['can_cancel'] is False
+ assert payload['can_retry'] is True
def test_scheduled_listing(self, client, auth_headers, app):
ScheduledJobService.ensure('api-sched', 'test.x', interval_seconds=60)