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(deploy): every deploy is a job — async deploy_app, retry endpoint, plan-in-GET

Juan Denisebdb7cc16fbc
diff --git a/backend/app/api/builds.py b/backend/app/api/builds.py
index 8c4b7dbb..9b38bf99 100644
--- a/backend/app/api/builds.py
+++ b/backend/app/api/builds.py
@@ -201,7 +201,13 @@ def clear_build_cache(app_id):
@builds_bp.route('/apps/<int:app_id>/deploy', methods=['POST'])
@jwt_required()
def deploy_app(app_id):
- """Deploy an application (build + deploy)."""
+ """Deploy an application (build + deploy).
+
+ Every deploy is a job (plan 51 D7): by default this enqueues an `app_deploy`
+ DeploymentJob and returns 202 + {deploy_job_id} so the UI can open the live
+ Deploy Console. `?wait=true` preserves the old synchronous contract for
+ CLI/tests (runs inline, returns the deployment result body).
+ """
current_user_id = get_jwt_identity()
user = User.query.get(current_user_id)
app = Application.query.get(app_id)
@@ -213,16 +219,32 @@ def deploy_app(app_id):
return jsonify({'error': 'Access denied'}), 403
data = request.get_json() or {}
-
- result = DeploymentService.deploy(
- app_id=app_id,
- user_id=current_user_id,
- no_cache=data.get('no_cache', False),
- trigger='manual',
- version_tag=data.get('version_tag')
+ no_cache = data.get('no_cache', False)
+ version_tag = data.get('version_tag')
+ wait = request.args.get('wait', 'false').lower() == 'true'
+
+ if wait:
+ result = DeploymentService.deploy(
+ app_id=app_id,
+ user_id=current_user_id,
+ no_cache=no_cache,
+ trigger='manual',
+ version_tag=version_tag,
+ )
+ return jsonify(result), 200 if result.get('success') else 400
+
+ from app.services.deployment_job_service import DeploymentJobService
+ job_result = DeploymentJobService.enqueue_app_deploy(
+ app, user_id=current_user_id, trigger='manual',
+ no_cache=no_cache, version_tag=version_tag,
)
-
- return jsonify(result), 200 if result.get('success') else 400
+ if not job_result.get('success'):
+ return jsonify({'error': job_result.get('error') or 'Failed to queue deployment'}), 400
+ return jsonify({
+ 'success': True,
+ 'deploy_job_id': job_result['job_id'],
+ 'job': job_result.get('job'),
+ }), 202
@builds_bp.route('/apps/<int:app_id>/deployments', methods=['GET'])
diff --git a/backend/app/api/deployment_jobs.py b/backend/app/api/deployment_jobs.py
index ef5a043f..5b7ab83f 100644
--- a/backend/app/api/deployment_jobs.py
+++ b/backend/app/api/deployment_jobs.py
@@ -29,12 +29,29 @@ def list_deployment_jobs():
@jwt_required()
def get_deployment_job(job_id):
include_logs = request.args.get('logs', 'true').lower() == 'true'
- job = DeploymentJobService.get_job(job_id, include_logs=include_logs)
+ include_plan = request.args.get('plan', 'false').lower() == 'true'
+ job = DeploymentJobService.get_job(
+ job_id, include_logs=include_logs, include_plan=include_plan
+ )
if not job:
return jsonify({'error': 'Deployment job not found'}), 404
return jsonify({'job': job}), 200
+@deployment_jobs_bp.route('/<job_id>/retry', methods=['POST'])
+@jwt_required()
+def retry_deployment_job(job_id):
+ """Retry a failed deployment job by cloning it and enqueuing a fresh run
+ (plan 51 D8). Only failed jobs may be retried."""
+ from flask_jwt_extended import get_jwt_identity
+ result = DeploymentJobService.retry_job(job_id, user_id=get_jwt_identity())
+ if not result.get('success'):
+ # 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
+ return jsonify(result), 202
+
+
@deployment_jobs_bp.route('/<job_id>/logs', methods=['GET'])
@jwt_required()
def get_deployment_job_logs(job_id):
diff --git a/backend/app/services/deployment_job_service.py b/backend/app/services/deployment_job_service.py
index dfc8a8be..036500e9 100644
--- a/backend/app/services/deployment_job_service.py
+++ b/backend/app/services/deployment_job_service.py
@@ -9,6 +9,7 @@ from app import db
from app.models import Application, Server
from app.models.deployment_job import DeploymentJob
from app.services.deployment_runner import DeploymentPlanRunner
+from app.services.run_log_service import append_log
from app.services.docker_service import DockerService
from app.services.template_service import TemplateService
from app.services.telemetry_service import generate_correlation_id
@@ -96,7 +97,8 @@ class DeploymentJobService:
}
@classmethod
- def enqueue_app_deploy(cls, app, user_id: int = None, trigger: str = 'install') -> Dict:
+ def enqueue_app_deploy(cls, app, user_id: int = None, trigger: str = 'install',
+ no_cache: bool = False, version_tag: str = None) -> Dict:
"""Create a deploy job for an existing app and queue it asynchronously.
Used by the repo-based create flow (POST /apps/from-repository): the
@@ -119,9 +121,12 @@ class DeploymentJobService:
)
# Execution is delegated to DeploymentService.deploy; these steps only
# drive the progress bar and mirror the milestones _run_app_deploy logs.
+ # Deploy options ride in the plan so the async runner can honor them.
job.set_plan({
'app_id': app.id,
'app_name': app.name,
+ 'no_cache': bool(no_cache),
+ 'version_tag': version_tag,
'steps': [
{'name': 'Prepare deployment'},
{'name': 'Build application'},
@@ -176,7 +181,9 @@ class DeploymentJobService:
job.error_message = str(exc)
job.completed_at = datetime.utcnow()
db.session.commit()
- runner.log('error', f'Deployment crashed: {exc}')
+ # append_log persists immediately: the runner's own stream may
+ # never have reached close() on this crash path.
+ append_log(job, 'error', f'Deployment crashed: {exc}')
except Exception:
db.session.rollback()
return {'success': False, 'error': str(exc)}
@@ -191,13 +198,58 @@ class DeploymentJobService:
job.error_message = str(exc)
job.completed_at = datetime.utcnow()
db.session.commit()
- runner.log('error', f'Failed to finalize deployment: {exc}')
+ # Stream already closed after a successful run(); flush this line now.
+ append_log(job, 'error', f'Failed to finalize deployment: {exc}')
return {'success': False, 'error': str(exc)}
@classmethod
- def get_job(cls, job_id: str, include_logs: bool = True) -> Optional[Dict]:
+ def get_job(cls, job_id: str, include_logs: bool = True,
+ include_plan: bool = False) -> Optional[Dict]:
job = DeploymentJob.query.get(job_id)
- return job.to_dict(include_logs=include_logs) if job else None
+ return job.to_dict(include_logs=include_logs, include_plan=include_plan) if job else None
+
+ @classmethod
+ def retry_job(cls, job_id: str, user_id: int = None) -> Dict:
+ """Clone a FAILED deployment job and enqueue a fresh run (plan 51 D8).
+
+ Uniform for both kinds: the queue payload is just {deployment_job_id}
+ and template plans are self-contained (compose content lives in
+ file.write steps), so cloning the row + re-enqueuing the same unified
+ kind re-runs the deploy from scratch on a new console URL.
+ """
+ job = DeploymentJob.query.get(job_id)
+ if not job:
+ return {'success': False, 'error': 'Deployment job not found'}
+ if job.status != 'failed':
+ return {'success': False,
+ 'error': 'Only failed deployments can be retried'}
+
+ clone = DeploymentJob(
+ id=str(uuid.uuid4()),
+ kind=job.kind,
+ status='pending',
+ target_server_id=job.target_server_id,
+ app_id=job.app_id,
+ requested_by=user_id or job.requested_by,
+ trigger='retry',
+ correlation_id=generate_correlation_id(),
+ )
+ clone.set_plan(job.get_plan())
+ db.session.add(clone)
+ db.session.commit()
+
+ enqueue = cls._enqueue_app_deploy if clone.kind == 'app_deploy' else cls._enqueue_install
+ try:
+ enqueue(clone)
+ except Exception as exc:
+ db.session.rollback()
+ clone.status = 'failed'
+ clone.error_message = f'Failed to queue retry: {exc}'
+ clone.completed_at = datetime.utcnow()
+ db.session.commit()
+ return {'success': False, 'error': clone.error_message, 'job_id': clone.id}
+
+ return {'success': True, 'job_id': clone.id, 'job': clone.to_dict(include_logs=True)}
@classmethod
def list_jobs(cls, status: str = None, target_server_id: str = None,
@@ -225,28 +277,27 @@ class DeploymentJobService:
job.status = 'running'
job.started_at = datetime.utcnow()
- job.current_step = 1
- job.current_step_name = 'Prepare deployment'
- db.session.commit()
+ # set_step handles the current_step/name row update + commit, flushes
+ # buffered lines, records per-step timings, and emits a live status.
+ runner.stream.set_step(1, 'Prepare deployment')
runner.log('info', f"Deploying application '{app.name}' (trigger: {job.trigger})")
- job.current_step = 2
- job.current_step_name = 'Build application'
- db.session.commit()
+ runner.stream.set_step(2, 'Build application')
+ plan = job.get_plan()
from app.services.deployment_service import DeploymentService
result = DeploymentService.deploy(
app.id,
user_id=job.requested_by,
trigger=job.trigger or 'manual',
+ no_cache=bool(plan.get('no_cache')),
+ version_tag=plan.get('version_tag'),
log_callback=lambda line: runner.log('info', line),
)
if not result.get('success'):
raise RuntimeError(result.get('error') or 'Deployment failed')
- job.current_step = 3
- job.current_step_name = 'Start containers'
- db.session.commit()
+ runner.stream.set_step(3, 'Start containers')
runner.log('info', 'Containers started')
deployment = result.get('deployment') or {}
@@ -258,6 +309,7 @@ class DeploymentJobService:
'deployment_id': deployment.get('id')})
db.session.commit()
runner.log('info', f"Deployment completed: {app.name} is now live")
+ runner.stream.close('succeeded')
return {'success': True, 'app_id': app.id, 'job': job.to_dict(include_logs=True)}
except Exception as exc:
# Never leave the job stuck 'running': mark it failed with a
@@ -272,6 +324,8 @@ class DeploymentJobService:
runner.log('error', f'Deployment failed: {exc}')
except Exception:
db.session.rollback()
+ # Flush + persist failure tail/hint/timings; terminal status emit.
+ runner.stream.close('failed', error_message=str(exc))
return {'success': False, 'error': str(exc)}
@classmethod
@@ -340,20 +394,18 @@ class DeploymentJobService:
dom = SiteDomainService.give_subdomain(app)
result['auto_domain'] = dom
if dom.get('success'):
- DeploymentPlanRunner(job).log(
- 'info', f"Published at {dom.get('url')}", dom)
+ append_log(job, 'info', f"Published at {dom.get('url')}", dom)
else:
- DeploymentPlanRunner(job).log(
- 'warn', f"Auto-domain skipped: {dom.get('error')}", dom)
+ append_log(job, 'warn', f"Auto-domain skipped: {dom.get('error')}", dom)
except Exception as exc:
result['auto_domain'] = {'success': False, 'error': str(exc)}
- DeploymentPlanRunner(job).log('warn', f"Auto-domain failed: {exc}")
+ append_log(job, 'warn', f"Auto-domain failed: {exc}")
job.app_id = app.id
job.set_result({**job.get_result(), **result})
db.session.commit()
- DeploymentPlanRunner(job).log('info', f'Application record created: {app.name}', result)
+ append_log(job, 'info', f'Application record created: {app.name}', result)
return {'success': True, 'job': job.to_dict(include_logs=True), **result}
diff --git a/backend/tests/test_deploy_console.py b/backend/tests/test_deploy_console.py
new file mode 100644
index 00000000..d9eede99
--- /dev/null
+++ b/backend/tests/test_deploy_console.py
@@ -0,0 +1,338 @@
+"""Deploy Console backend surface (plan 51 Phases 1-3):
+
+- socket emit helpers push to the per-job room with the right payload;
+- RunLogStream's default emitter reaches socketio;
+- subscribe_deploy requires an authenticated socket;
+- POST /builds/apps/<id>/deploy is 202+job by default, sync under ?wait=true;
+- POST /deployment-jobs/<id>/retry clones failed jobs (both kinds) and guards.
+"""
+import uuid
+
+import pytest
+
+from app import db
+from app.models.deployment_job import DeploymentJob
+from app.jobs.models import Job
+
+
+def _make_app(name='consvc'):
+ from app.models.application import Application
+ a = Application(name=name, app_type='docker', status='stopped',
+ root_path='/srv/' + name, user_id=1)
+ db.session.add(a)
+ db.session.commit()
+ return a
+
+
+def _make_job(kind='app_deploy', status='failed', app_id=None):
+ job = DeploymentJob(id=str(uuid.uuid4()), kind=kind, status=status,
+ app_id=app_id, trigger='manual')
+ job.set_plan({'app_id': app_id, 'steps': [{'name': 'Prepare deployment'},
+ {'name': 'Build application'},
+ {'name': 'Start containers'}]})
+ db.session.add(job)
+ db.session.commit()
+ return job
+
+
+# ---------------------------------------------------------------- Phase 1
+
+class TestSocketEmitHelpers:
+ def test_emit_deploy_log_room_and_payload(self, app, monkeypatch):
+ import app.sockets as sk
+ calls = []
+ monkeypatch.setattr(sk.socketio, 'emit', lambda *a, **k: calls.append((a, k)))
+ lines = [{'id': 7, 'level': 'info', 'message': 'hello', 'step_index': 1, 'ts': None}]
+ sk.emit_deploy_log('job-1', lines)
+ assert calls, 'expected a socketio emit'
+ (args, kwargs) = calls[0]
+ assert args[0] == 'deploy_log'
+ assert args[1]['job_id'] == 'job-1'
+ assert args[1]['lines'][0]['message'] == 'hello'
+ assert kwargs['room'] == 'deploy_job-1'
+
+ def test_emit_deploy_status_room_and_payload(self, app, monkeypatch):
+ import app.sockets as sk
+ calls = []
+ monkeypatch.setattr(sk.socketio, 'emit', lambda *a, **k: calls.append((a, k)))
+ sk.emit_deploy_status('job-2', {'status': 'running', 'current_step': 2})
+ (args, kwargs) = calls[0]
+ assert args[0] == 'deploy_status'
+ assert args[1]['status']['status'] == 'running'
+ assert kwargs['room'] == 'deploy_job-2'
+
+ def test_stream_default_emitter_reaches_socketio(self, app, monkeypatch):
+ import app.sockets as sk
+ from app.services.run_log_service import RunLogStream
+ calls = []
+ monkeypatch.setattr(sk.socketio, 'emit', lambda *a, **k: calls.append((a, k)))
+ job = _make_job(kind='template_install', status='running')
+ stream = RunLogStream.for_job(job) # default (socket) emitters
+ for i in range(50): # cross the flush-on-count threshold
+ stream.log('info', f'line {i}')
+ log_events = [c for c in calls if c[0][0] == 'deploy_log']
+ assert log_events, 'expected a deploy_log emit from the default emitter'
+ assert log_events[0][1]['room'] == f'deploy_{job.id}'
+
+ def test_old_build_socket_surface_removed(self, app):
+ import app.sockets as sk
+ # The orphaned build_* channel is gone (D3).
+ assert not hasattr(sk, 'emit_build_log')
+ assert not hasattr(sk, 'emit_build_status')
+ assert not hasattr(sk, 'create_build_log_callback')
+
+
+class _FakeReq:
+ def __init__(self, sid):
+ self.sid = sid
+
+
+class TestSubscribeDeployAuth:
+ """The socketio test client is unusable on this Flask/Werkzeug pairing
+ (`ctx.session` became read-only), so exercise the handler's auth guard
+ directly by faking the socket context (request.sid / emit / join_room)."""
+
+ def _patch(self, monkeypatch, sid):
+ import app.sockets as sk
+ emitted, joined = [], []
+ monkeypatch.setattr(sk, 'emit', lambda *a, **k: emitted.append((a, k)))
+ monkeypatch.setattr(sk, 'join_room', lambda room: joined.append(room))
+ monkeypatch.setattr(sk, 'request', _FakeReq(sid))
+ return sk, emitted, joined
+
+ def test_unauthenticated_join_rejected(self, app, monkeypatch):
+ sk, emitted, joined = self._patch(monkeypatch, 'sid-unauth')
+ sk.connected_clients.pop('sid-unauth', None) # not authenticated
+ sk.handle_subscribe_deploy({'job_id': 'abc'})
+ assert joined == [] # never joined a room
+ assert any(a and a[0] == 'error' for a, k in emitted)
+
+ def test_authenticated_join_succeeds(self, app, monkeypatch):
+ sk, emitted, joined = self._patch(monkeypatch, 'sid-auth')
+ sk.connected_clients['sid-auth'] = {'user_id': 1, 'role': 'admin'}
+ try:
+ sk.handle_subscribe_deploy({'job_id': 'job-xyz'})
+ finally:
+ sk.connected_clients.pop('sid-auth', None)
+ assert 'deploy_job-xyz' in joined
+ assert any(a and a[0] == 'subscribed' for a, k in emitted)
+
+ def test_missing_job_id_rejected(self, app, monkeypatch):
+ sk, emitted, joined = self._patch(monkeypatch, 'sid-auth2')
+ sk.connected_clients['sid-auth2'] = {'user_id': 1, 'role': 'admin'}
+ try:
+ sk.handle_subscribe_deploy({})
+ finally:
+ sk.connected_clients.pop('sid-auth2', None)
+ assert joined == []
+ assert any(a and a[0] == 'error' for a, k in emitted)
+
+
+# ---------------------------------------------------------------- Phase 2
+
+class TestDeployAppJobified:
+ def test_default_deploy_returns_202_and_job(self, app, client, auth_headers, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ a = _make_app('depsvc')
+
+ seen = {}
+
+ def fake_enqueue(cls, application, user_id=None, trigger='install',
+ no_cache=False, version_tag=None):
+ seen['no_cache'] = no_cache
+ seen['version_tag'] = version_tag
+ return {'success': True, 'job_id': 'dj-async'}
+
+ monkeypatch.setattr(DeploymentJobService, 'enqueue_app_deploy',
+ classmethod(fake_enqueue))
+ res = client.post(f'/api/v1/builds/apps/{a.id}/deploy', headers=auth_headers,
+ json={'no_cache': True, 'version_tag': 'v9'})
+ assert res.status_code == 202
+ body = res.get_json()
+ assert body['deploy_job_id'] == 'dj-async'
+ assert seen['no_cache'] is True
+ assert seen['version_tag'] == 'v9'
+
+ def test_wait_true_runs_synchronously(self, app, client, auth_headers, monkeypatch):
+ from app.services.deployment_service import DeploymentService
+ a = _make_app('depsvc2')
+
+ monkeypatch.setattr(DeploymentService, 'deploy',
+ classmethod(lambda cls, app_id, **kw: {
+ 'success': True, 'deployment': {'id': 1, 'version': 3}}))
+ res = client.post(f'/api/v1/builds/apps/{a.id}/deploy?wait=true',
+ headers=auth_headers, json={})
+ assert res.status_code == 200
+ body = res.get_json()
+ assert body['success'] is True
+ assert 'deploy_job_id' not in body # sync path, no job id
+
+
+# ---------------------------------------------------------------- Phase 3
+
+class TestRetryJob:
+ def test_retry_clones_failed_app_deploy(self, app, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ monkeypatch.setattr(DeploymentJobService, '_enqueue_app_deploy',
+ classmethod(lambda cls, job: None))
+ a = _make_app('retrysvc')
+ job = _make_job(kind='app_deploy', status='failed', app_id=a.id)
+
+ result = DeploymentJobService.retry_job(job.id, user_id=5)
+ assert result['success'] is True
+ clone = DeploymentJob.query.get(result['job_id'])
+ assert clone.id != job.id
+ assert clone.kind == 'app_deploy'
+ assert clone.app_id == a.id
+ assert clone.status == 'pending'
+ assert clone.trigger == 'retry'
+ assert clone.requested_by == 5
+ assert clone.get_plan()['steps'] == job.get_plan()['steps']
+
+ def test_retry_clones_failed_template_install(self, app, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ monkeypatch.setattr(DeploymentJobService, '_enqueue_install',
+ classmethod(lambda cls, job: None))
+ job = _make_job(kind='template_install', status='failed')
+
+ result = DeploymentJobService.retry_job(job.id, user_id=1)
+ assert result['success'] is True
+ clone = DeploymentJob.query.get(result['job_id'])
+ assert clone.kind == 'template_install'
+ assert clone.trigger == 'retry'
+
+ def test_retry_rejects_non_failed(self, app):
+ from app.services.deployment_job_service import DeploymentJobService
+ job = _make_job(kind='app_deploy', status='succeeded')
+ result = DeploymentJobService.retry_job(job.id)
+ assert result['success'] is False
+ assert 'failed' in result['error'].lower()
+
+ def test_retry_missing_job(self, app):
+ from app.services.deployment_job_service import DeploymentJobService
+ result = DeploymentJobService.retry_job('no-such-job')
+ assert result['success'] is False
+ assert result['error'] == 'Deployment job not found'
+
+ def test_retry_endpoint_status_codes(self, app, client, auth_headers, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ monkeypatch.setattr(DeploymentJobService, '_enqueue_app_deploy',
+ classmethod(lambda cls, job: None))
+ a = _make_app('retrysvc2')
+ failed = _make_job(kind='app_deploy', status='failed', app_id=a.id)
+ ok = _make_job(kind='app_deploy', status='succeeded', app_id=a.id)
+
+ r1 = client.post(f'/api/v1/deployment-jobs/{failed.id}/retry', headers=auth_headers)
+ assert r1.status_code == 202
+ assert r1.get_json()['job_id']
+
+ r2 = client.post(f'/api/v1/deployment-jobs/{ok.id}/retry', headers=auth_headers)
+ assert r2.status_code == 400
+
+ r3 = client.post('/api/v1/deployment-jobs/nope/retry', headers=auth_headers)
+ assert r3.status_code == 404
+
+
+# ---------------------------------------------------------------- Phase 4
+
+class _FakeStdout:
+ def __init__(self, lines):
+ self._lines = list(lines)
+
+ def readline(self):
+ return self._lines.pop(0) if self._lines else ''
+
+ def close(self):
+ pass
+
+
+class _FakeProc:
+ def __init__(self, lines, code):
+ self.stdout = _FakeStdout(lines)
+ self._code = code
+
+ def wait(self):
+ return self._code
+
+
+class TestComposeUpStreaming:
+ def test_streams_lines_in_order_with_plain_flags(self, app, monkeypatch):
+ import app.services.docker_service as ds
+ from app.services.docker_service import DockerService
+
+ captured = {}
+
+ def fake_popen(cmd, **kw):
+ captured['cmd'] = cmd
+ return _FakeProc(['#1 pulling\n', 'Step 1/3 : FROM\n', 'done\n'], 0)
+
+ monkeypatch.setattr(ds.subprocess, 'Popen', fake_popen)
+ monkeypatch.setattr(DockerService, '_compose_cmd_with_overlay',
+ classmethod(lambda cls, p, c=None: ['docker', 'compose']))
+
+ got = []
+ result = DockerService.compose_up_streaming('/srv/x', on_line=got.append, build=True)
+ assert result == {'success': True, 'exit_code': 0}
+ assert got == ['#1 pulling', 'Step 1/3 : FROM', 'done'] # order preserved
+ # D5 flags present.
+ assert '--ansi' in captured['cmd'] and 'never' in captured['cmd']
+ assert '--progress' in captured['cmd'] and 'plain' in captured['cmd']
+ assert '--build' in captured['cmd']
+
+ def test_nonzero_exit_is_failure(self, app, monkeypatch):
+ import app.services.docker_service as ds
+ from app.services.docker_service import DockerService
+
+ monkeypatch.setattr(ds.subprocess, 'Popen',
+ lambda cmd, **kw: _FakeProc(['boom\n'], 1))
+ monkeypatch.setattr(DockerService, '_compose_cmd_with_overlay',
+ classmethod(lambda cls, p, c=None: ['docker', 'compose']))
+ got = []
+ result = DockerService.compose_up_streaming('/srv/x', on_line=got.append)
+ assert result['success'] is False
+ assert result['exit_code'] == 1
+ assert got == ['boom']
+
+ def test_streaming_failure_persists_stdout_tail(self, app, monkeypatch):
+ """Regression-proof the #2 gap: a template-install compose failure now
+ persists a real, stdout-bearing failure tail (+ a matching hint)."""
+ from app.services.deployment_runner import DeploymentPlanRunner
+ from app.services.docker_service import DockerService
+
+ def fake_streaming(cls, project_dir, on_line, detach=True, build=False, compose_file=None):
+ on_line('#5 [build 3/5] RUN npm ci')
+ on_line('npm ERR! missing script: build')
+ return {'success': False, 'exit_code': 1}
+
+ monkeypatch.setattr(DockerService, 'compose_up_streaming', classmethod(fake_streaming))
+
+ job = DeploymentJob(id=str(uuid.uuid4()), kind='template_install', status='pending')
+ job.set_plan({'steps': [
+ {'type': 'docker.compose.up', 'name': 'Start containers', 'project_dir': '/srv/x'},
+ ]})
+ db.session.add(job)
+ db.session.commit()
+
+ result = DeploymentPlanRunner(job).run()
+ assert result['success'] is False
+ refreshed = DeploymentJob.query.get(job.id)
+ assert refreshed.status == 'failed'
+ tail = refreshed.get_result().get('failure_tail') or []
+ assert any('npm ERR!' in line for line in tail) # the REAL reason, streamed
+ assert 'Node build' in (refreshed.get_result().get('hint') or '')
+
+
+class TestPlanIncludedInGet:
+ def test_plan_returned_when_requested(self, app, client, auth_headers):
+ job = _make_job(kind='app_deploy', status='running')
+ res = client.get(f'/api/v1/deployment-jobs/{job.id}?logs=false&plan=true',
+ headers=auth_headers)
+ assert res.status_code == 200
+ body = res.get_json()['job']
+ assert 'plan' in body
+ assert body['plan']['steps'][0]['name'] == 'Prepare deployment'
+
+ def test_plan_omitted_by_default(self, app, client, auth_headers):
+ job = _make_job(kind='app_deploy', status='running')
+ res = client.get(f'/api/v1/deployment-jobs/{job.id}?logs=false', headers=auth_headers)
+ assert 'plan' not in res.get_json()['job']