fix deploy and logs

This commit is contained in:
pavel 2026-05-14 22:00:04 +02:00
commit af8bbf536f
5 changed files with 40 additions and 6 deletions

View file

@ -78,6 +78,9 @@ func (a *App) deployProjectFromMain(_ context.Context, p Project) error {
return err
}
repoDir := filepath.Join(projectDir, "repo")
if _, err := os.Stat(repoDir); err != nil {
return fmt.Errorf("repo checkout missing at %s (trigger deploy webhook/push first)", repoDir)
}
binDir := filepath.Join(projectDir, "bin")
if err := os.MkdirAll(binDir, 0o755); err != nil {
return err
@ -97,6 +100,25 @@ func (a *App) deployProjectFromMain(_ context.Context, p Project) error {
return err
}
}
if err := a.buildProjectBinary(p); err != nil {
return err
}
return runSystemctlUser(p.UnixUser, "restart", p.ServiceName)
}
func (a *App) buildProjectBinary(p Project) error {
projectDir, err := a.projectDirFor(p)
if err != nil {
return err
}
repoDir := filepath.Join(projectDir, "repo")
binDir := filepath.Join(projectDir, "bin")
if err := os.MkdirAll(binDir, 0o755); err != nil {
return err
}
if err := ensureOwnedByUnixUser(binDir, p.UnixUser, 0o755); err != nil {
return err
}
binaryPath := filepath.Join(binDir, "app")
if err := runCmd("go", "-C", repoDir, "build", "-o", binaryPath, "."); err != nil {
return fmt.Errorf("build failed: %w", err)
@ -104,5 +126,5 @@ func (a *App) deployProjectFromMain(_ context.Context, p Project) error {
if err := ensureOwnedByUnixUser(binaryPath, p.UnixUser, 0o755); err != nil {
return err
}
return runSystemctlUser(p.UnixUser, "restart", p.ServiceName)
return nil
}

View file

@ -103,6 +103,14 @@ func (a *App) provisionProject(ctx context.Context, p *Project) error {
p.ProvisionError = err.Error()
return err
}
if p.RepoURL != "" {
if err := a.buildProjectBinary(*p); err != nil {
_ = a.updateProvisioningState(ctx, p.ID, "failed", err.Error())
p.ProvisionState = "failed"
p.ProvisionError = err.Error()
return err
}
}
if err := a.renderSystemdForProject(ctx, *p); err != nil {
_ = a.updateProvisioningState(ctx, p.ID, "failed", err.Error())

View file

@ -248,11 +248,12 @@ func (a *App) handleProjectLogs(w http.ResponseWriter, r *http.Request, user Use
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
lines := 200
lines := 100
const maxLogBytes = 64 * 1024
if raw := r.URL.Query().Get("lines"); raw != "" {
n, err := strconv.Atoi(raw)
if err != nil || n < 1 || n > 1000 {
http.Error(w, "lines must be 1-1000", http.StatusBadRequest)
if err != nil || n < 1 || n > 300 {
http.Error(w, "lines must be 1-300", http.StatusBadRequest)
return
}
lines = n
@ -262,5 +263,8 @@ func (a *App) handleProjectLogs(w http.ResponseWriter, r *http.Request, user Use
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
if len(logs) > maxLogBytes {
logs = logs[:maxLogBytes] + "\n... (truncated)"
}
writeJSON(w, http.StatusOK, map[string]any{"lines": lines, "logs": logs})
}

View file

@ -246,5 +246,5 @@ func (a *App) getServiceStatus(p Project) (ServiceStatus, error) {
}
func (a *App) getServiceLogs(p Project, lines int) (string, error) {
return runJournalctlUser(p.UnixUser, "-u", p.ServiceName, "--no-pager", "-n", strconv.Itoa(lines), "-o", "short-iso")
return runJournalctlUser(p.UnixUser, "-u", p.ServiceName, "--no-pager", "-n", strconv.Itoa(lines), "-r", "-o", "short-iso")
}

View file

@ -138,7 +138,7 @@ async function loadLogs(projectNode, projectID) {
const out = projectNode.querySelector('[data-logs-out]');
out.textContent = 'loading logs...';
try {
const res = await api(`/api/projects/${projectID}/logs?lines=200`);
const res = await api(`/api/projects/${projectID}/logs?lines=100`);
out.textContent = res.logs || '(no logs)';
} catch (err) {
out.textContent = `logs error: ${err.message}`;