2026-06-05 19:23:28 +08:00
package api
import (
2026-11-02 22:47:44 +08:00
"context"
2026-06-05 19:23:28 +08:00
"encoding/json"
"fmt"
2026-09-19 01:34:13 +08:00
"math"
2026-06-05 19:23:28 +08:00
"net/http"
"strings"
"sync"
"time"
"clicd/internal/config"
"clicd/internal/lxc"
)
type TaskType string
const (
TaskCreate TaskType = "create"
TaskStart TaskType = "start"
TaskStop TaskType = "stop"
TaskRestart TaskType = "restart"
TaskDelete TaskType = "delete"
TaskReinstall TaskType = "reinstall"
)
type Task struct {
2026-11-03 23:17:46 +08:00
ID string `json:"id"`
Type TaskType `json:"type"`
ContainerID int `json:"container_id"`
ContainerName string `json:"container_name"`
ContainerUUID string `json:"container_uuid,omitempty"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
Stage string `json:"stage,omitempty"`
StageDetail string `json:"stage_detail,omitempty"`
CreatedAt string `json:"created_at"`
StartedAt string `json:"started_at,omitempty"`
FinishedAt string `json:"finished_at,omitempty"`
DurationMS int64 `json:"duration_ms,omitempty"`
Logs [] TaskEvent `json:"logs,omitempty"`
Result json . RawMessage `json:"result,omitempty"`
Payload json . RawMessage `json:"-"`
AdditionalTargets [] string `json:"-"`
TemplateID string `json:"template_id,omitempty"`
Config lxc . ContainerConfig `json:"config,omitempty"`
Name string `json:"name,omitempty"`
User string `json:"user,omitempty"` // who created this task
IP string `json:"ip,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
activeKey string
activeKeys [] string
revision uint64
ctx context . Context
cancel context . CancelFunc
deleteRequested bool
2026-11-02 22:47:44 +08:00
}
type taskRuntime struct {
create func ( lxc . ContainerConfig ) error
start func ( context . Context , int ) error
destroy func ( int ) error
2026-06-05 19:23:28 +08:00
}
type TaskQueue struct {
2026-07-18 19:44:32 +08:00
mu sync . Mutex
createQueue [] * Task
opQueue [] * Task
tasks map [ string ] * Task
nextID int
createCond * sync . Cond
opCond * sync . Cond
maxConcurrency int
activeTasks int
activeTargets map [ string ] bool
2026-11-02 22:47:44 +08:00
runningTasks map [ * Task ] bool
2026-07-18 19:44:32 +08:00
stop chan struct {}
2026-11-02 22:47:44 +08:00
runtime taskRuntime
2026-11-03 23:17:46 +08:00
historySaved map [ string ] uint64
2026-07-18 19:44:32 +08:00
}
type TaskQueueSettings struct {
Concurrency int `json:"concurrency"`
Active int `json:"active"`
Pending int `json:"pending"`
2026-06-05 19:23:28 +08:00
}
var globalQueue * TaskQueue
func init () {
2026-07-18 19:44:32 +08:00
globalQueue = newTaskQueue ( config . DefaultTaskConcurrency )
go globalQueue . createDispatcher ()
go globalQueue . opDispatcher ()
}
func newTaskQueue ( concurrency int ) * TaskQueue {
q := & TaskQueue {
tasks : make ( map [ string ] * Task ),
2026-11-03 23:17:46 +08:00
historySaved : make ( map [ string ] uint64 ),
2026-07-18 19:44:32 +08:00
maxConcurrency : config . NormalizeTaskConcurrency ( concurrency ),
activeTargets : make ( map [ string ] bool ),
2026-11-02 22:47:44 +08:00
runningTasks : make ( map [ * Task ] bool ),
2026-07-18 19:44:32 +08:00
stop : make ( chan struct {}),
2026-11-02 22:47:44 +08:00
runtime : taskRuntime {
create : createByRuntime ,
start : startByRuntimeContext ,
destroy : destroyByRuntime ,
},
2026-06-05 19:23:28 +08:00
}
2026-07-18 19:44:32 +08:00
q . createCond = sync . NewCond ( & q . mu )
q . opCond = sync . NewCond ( & q . mu )
return q
}
func ConfigureTaskQueue ( concurrency int ) {
globalQueue . SetConcurrency ( concurrency )
}
func ( q * TaskQueue ) SetConcurrency ( concurrency int ) {
q . mu . Lock ()
q . maxConcurrency = config . NormalizeTaskConcurrency ( concurrency )
q . createCond . Broadcast ()
q . opCond . Broadcast ()
q . mu . Unlock ()
}
func ( q * TaskQueue ) Settings () TaskQueueSettings {
q . mu . Lock ()
defer q . mu . Unlock ()
return TaskQueueSettings {
Concurrency : q . maxConcurrency ,
Active : q . activeTasks ,
Pending : len ( q . createQueue ) + len ( q . opQueue ),
}
}
func ( q * TaskQueue ) signalDispatchers () {
q . createCond . Broadcast ()
q . opCond . Broadcast ()
2026-06-05 19:23:28 +08:00
}
func ( q * TaskQueue ) enqueueTask ( task * Task ) {
2026-11-02 22:47:44 +08:00
if task . ContainerID > 0 && config . AppConfig != nil {
if c := config . FindContainer ( task . ContainerID ); c != nil {
2026-11-03 23:17:46 +08:00
if task . ContainerUUID != "" && task . ContainerUUID != c . UUID {
task . Status = "failed"
task . Error = "operation target identity changed"
task . FinishedAt = time . Now (). Format ( time . RFC3339Nano )
addTaskEvent ( task , "failed" , task . Error )
q . tasks [ task . ID ] = task
return
}
2026-11-02 22:47:44 +08:00
task . ContainerName = c . Name
2026-11-03 23:17:46 +08:00
task . ContainerUUID = c . UUID
2026-11-02 22:47:44 +08:00
}
}
2026-06-05 19:23:28 +08:00
q . tasks [ task . ID ] = task
2026-11-03 23:17:46 +08:00
addTaskEvent ( task , task . Stage , task . StageDetail )
2026-11-02 22:47:44 +08:00
if task . Type == TaskDelete {
q . cancelTasksForDelete ( task )
} else {
for _ , existing := range q . tasks {
if existing . Type == TaskDelete && ( existing . Status == "pending" || existing . Status == "running" ) && sameTaskTarget ( existing , task ) {
cancelPendingTaskForDelete ( task )
return
}
}
}
2026-06-05 19:23:28 +08:00
if task . Type == TaskCreate {
q . createQueue = append ( q . createQueue , task )
q . createCond . Signal ()
} else {
q . opQueue = append ( q . opQueue , task )
q . opCond . Signal ()
}
}
2026-11-02 22:47:44 +08:00
// Cancellation is requested while enqueueing, even when all dispatch slots are
// occupied. The active worker retains its target key until it has fully exited.
func ( q * TaskQueue ) cancelTasksForDelete ( del * Task ) {
for task := range q . runningTasks {
if ! sameTaskTarget ( task , del ) {
continue
}
if task . Type == TaskCreate || task . Type == TaskStart {
task . deleteRequested = true
if task . cancel != nil {
task . cancel ()
}
}
}
for _ , task := range q . tasks {
if task . Type != TaskDelete && task . Status == "pending" && sameTaskTarget ( task , del ) {
cancelPendingTaskForDelete ( task )
}
}
q . createQueue = pendingTasks ( q . createQueue )
q . opQueue = pendingTasks ( q . opQueue )
}
func cancelPendingTaskForDelete ( task * Task ) {
task . Status = "failed"
task . Error = "cancelled because container deletion was requested"
task . Stage = "failed"
task . StageDetail = "已取消,等待删除"
2026-11-03 23:17:46 +08:00
task . FinishedAt = time . Now (). Format ( time . RFC3339Nano )
addTaskEvent ( task , task . Stage , task . Error )
2026-11-02 22:47:44 +08:00
if task . Type == TaskCreate {
name := task . Config . Name
if name == "" {
name = task . ContainerName
}
lxc . ReleaseQueuedCreateNATPorts ( name )
}
}
func sameTaskTarget ( a , b * Task ) bool {
if a . ContainerID > 0 && b . ContainerID > 0 {
return a . ContainerID == b . ContainerID
}
name := func ( task * Task ) string {
if value := strings . TrimSpace ( task . ContainerName ); value != "" {
return value
}
return strings . TrimSpace ( task . Config . Name )
}
aName , bName := name ( a ), name ( b )
return aName != "" && aName == bName
}
func pendingTasks ( tasks [] * Task ) [] * Task {
pending := tasks [: 0 ]
for _ , task := range tasks {
if task . Status == "pending" {
pending = append ( pending , task )
}
}
return pending
}
2026-06-05 19:23:28 +08:00
func ( q * TaskQueue ) Enqueue ( containerID int , containerName string , taskType TaskType , templateID string , cfg * lxc . ContainerConfig ) [] string {
2026-06-09 23:25:19 +08:00
return q . EnqueueWithAudit ( containerID , containerName , taskType , templateID , cfg , "admin" , "" , "" )
}
func ( q * TaskQueue ) EnqueueWithAudit ( containerID int , containerName string , taskType TaskType , templateID string , cfg * lxc . ContainerConfig , user string , ip string , userAgent string ) [] string {
2026-06-05 19:23:28 +08:00
q . mu . Lock ()
defer q . mu . Unlock ()
2026-11-03 23:17:46 +08:00
id := q . allocateTaskID ()
2026-06-05 19:23:28 +08:00
task := & Task {
ID : fmt . Sprintf ( "task-%d" , id ),
Type : taskType ,
ContainerID : containerID ,
ContainerName : containerName ,
Status : "pending" ,
2026-07-18 19:44:32 +08:00
Stage : "queued" ,
StageDetail : "排队等待" ,
2026-06-05 19:23:28 +08:00
CreatedAt : time . Now (). Format ( "2006-01-02 15:04:05" ),
TemplateID : templateID ,
2026-06-09 23:25:19 +08:00
User : user ,
IP : ip ,
UserAgent : userAgent ,
2026-06-05 19:23:28 +08:00
}
if cfg != nil {
task . Config = * cfg
2026-06-12 15:53:31 +08:00
task . Config . NormalizeResourceAliases ()
2026-06-05 19:23:28 +08:00
}
q . enqueueTask ( task )
q . persistTasks ()
return [] string { task . ID }
}
func ( q * TaskQueue ) EnqueueBatch ( taskType TaskType , ids [] int , templateID string ) [] string {
return q . EnqueueBatchWithUser ( taskType , ids , templateID , "admin" )
}
func ( q * TaskQueue ) EnqueueBatchWithUser ( taskType TaskType , ids [] int , templateID string , user string ) [] string {
2026-06-06 15:10:24 +08:00
return q . EnqueueBatchWithAudit ( taskType , ids , templateID , user , "" , "" )
}
func ( q * TaskQueue ) EnqueueBatchWithAudit ( taskType TaskType , ids [] int , templateID string , user string , ip string , userAgent string ) [] string {
2026-06-05 19:23:28 +08:00
q . mu . Lock ()
defer q . mu . Unlock ()
var result [] string
for _ , id := range ids {
c := config . FindContainer ( id )
name := ""
if c != nil {
name = c . Name
}
2026-06-06 15:10:24 +08:00
result = append ( result , q . enqueueSingleWithAudit ( id , name , taskType , templateID , user , ip , userAgent ))
2026-06-05 19:23:28 +08:00
}
q . persistTasks ()
return result
}
func ( q * TaskQueue ) EnqueueBatchCreate ( configs [] lxc . ContainerConfig ) [] string {
2026-06-08 19:25:55 +08:00
return q . EnqueueBatchCreateWithAudit ( configs , "admin" , "" , "" )
}
func ( q * TaskQueue ) EnqueueBatchCreateWithAudit ( configs [] lxc . ContainerConfig , user string , ip string , userAgent string ) [] string {
2026-06-05 19:23:28 +08:00
q . mu . Lock ()
defer q . mu . Unlock ()
2026-06-08 19:25:55 +08:00
return q . enqueueBatchCreateList ( configs , user , ip , userAgent )
2026-06-05 19:23:28 +08:00
}
func ( q * TaskQueue ) ActiveCreateNames () map [ string ] bool {
q . mu . Lock ()
defer q . mu . Unlock ()
names := make ( map [ string ] bool )
for _ , task := range q . tasks {
if task . Type != TaskCreate || ( task . Status != "pending" && task . Status != "running" ) {
continue
}
name := task . Config . Name
if name == "" {
name = task . ContainerName
}
if name != "" {
names [ name ] = true
}
}
return names
}
2026-06-08 19:25:55 +08:00
func ( q * TaskQueue ) enqueueBatchCreateList ( configs [] lxc . ContainerConfig , user string , ip string , userAgent string ) [] string {
2026-06-05 19:23:28 +08:00
var result [] string
for _ , cfg := range configs {
cfgCopy := cfg
2026-06-12 15:53:31 +08:00
cfgCopy . NormalizeResourceAliases ()
2026-11-03 23:17:46 +08:00
id := q . allocateTaskID ()
2026-06-05 19:23:28 +08:00
task := & Task {
ID : fmt . Sprintf ( "task-%d" , id ),
Type : TaskCreate ,
ContainerID : 0 ,
ContainerName : cfgCopy . Name ,
Status : "pending" ,
2026-07-18 19:44:32 +08:00
Stage : "queued" ,
StageDetail : "排队等待" ,
2026-06-05 19:23:28 +08:00
CreatedAt : time . Now (). Format ( "2006-01-02 15:04:05" ),
Config : cfgCopy ,
2026-06-08 19:25:55 +08:00
User : user ,
IP : ip ,
UserAgent : userAgent ,
2026-06-05 19:23:28 +08:00
}
q . enqueueTask ( task )
result = append ( result , task . ID )
}
q . persistTasks ()
return result
}
func ( q * TaskQueue ) enqueueSingle ( containerID int , containerName string , taskType TaskType , templateID string ) string {
return q . enqueueSingleWithUser ( containerID , containerName , taskType , templateID , "admin" )
}
func ( q * TaskQueue ) enqueueSingleWithUser ( containerID int , containerName string , taskType TaskType , templateID string , user string ) string {
2026-06-06 15:10:24 +08:00
return q . enqueueSingleWithAudit ( containerID , containerName , taskType , templateID , user , "" , "" )
}
func ( q * TaskQueue ) enqueueSingleWithAudit ( containerID int , containerName string , taskType TaskType , templateID string , user string , ip string , userAgent string ) string {
2026-11-03 23:17:46 +08:00
id := q . allocateTaskID ()
2026-06-05 19:23:28 +08:00
task := & Task {
ID : fmt . Sprintf ( "task-%d" , id ),
Type : taskType ,
ContainerID : containerID ,
ContainerName : containerName ,
Status : "pending" ,
2026-07-18 19:44:32 +08:00
Stage : "queued" ,
StageDetail : "排队等待" ,
2026-06-05 19:23:28 +08:00
CreatedAt : time . Now (). Format ( "2006-01-02 15:04:05" ),
TemplateID : templateID ,
User : user ,
2026-06-06 15:10:24 +08:00
IP : ip ,
UserAgent : userAgent ,
2026-06-05 19:23:28 +08:00
}
q . enqueueTask ( task )
return task . ID
}
2026-07-18 19:44:32 +08:00
// The two dispatchers keep long-running creates from blocking power operations,
// while sharing one global concurrency budget.
func ( q * TaskQueue ) createDispatcher () {
2026-06-05 19:23:28 +08:00
for {
2026-07-18 19:44:32 +08:00
task := q . takeNextTask ( true )
2026-11-02 22:47:44 +08:00
if task == nil {
return
}
2026-07-18 19:44:32 +08:00
go q . runCreateTask ( task )
2026-06-05 19:23:28 +08:00
}
}
2026-07-18 19:44:32 +08:00
func ( q * TaskQueue ) opDispatcher () {
2026-06-05 19:23:28 +08:00
for {
2026-07-18 19:44:32 +08:00
task := q . takeNextTask ( false )
2026-11-02 22:47:44 +08:00
if task == nil {
return
}
2026-07-18 19:44:32 +08:00
go q . runOperationTask ( task )
2026-06-05 19:23:28 +08:00
}
}
2026-07-18 19:44:32 +08:00
func ( q * TaskQueue ) takeNextTask ( create bool ) * Task {
q . mu . Lock ()
defer q . mu . Unlock ()
cond := q . opCond
if create {
cond = q . createCond
}
for {
2026-11-02 22:47:44 +08:00
select {
case <- q . stop :
return nil
default :
}
2026-07-18 19:44:32 +08:00
queue := q . opQueue
if create {
queue = q . createQueue
}
if q . activeTasks < q . maxConcurrency {
2026-11-02 22:47:44 +08:00
index := runnableTaskIndex ( queue , q . activeTargets )
deleteIndex := runnableDeleteTaskIndex ( q . opQueue , q . activeTargets )
if deleteIndex >= 0 {
if create {
index = - 1
q . opCond . Signal ()
} else {
index = deleteIndex
}
}
if index >= 0 {
2026-07-18 19:44:32 +08:00
task := queue [ index ]
queue = append ( queue [: index ], queue [ index + 1 :] ... )
if create {
q . createQueue = queue
} else {
q . opQueue = queue
}
task . Status = "running"
task . Error = ""
task . Stage = "preparing"
task . StageDetail = "准备初始化环境"
2026-11-03 23:17:46 +08:00
task . StartedAt = time . Now (). Format ( time . RFC3339Nano )
addTaskEvent ( task , task . Stage , task . StageDetail )
2026-07-18 19:44:32 +08:00
task . activeKey = taskConcurrencyKey ( task )
2026-11-03 23:17:46 +08:00
task . activeKeys = taskConcurrencyKeys ( task )
2026-11-02 22:47:44 +08:00
task . ctx , task . cancel = context . WithCancel ( context . Background ())
q . runningTasks [ task ] = true
2026-11-03 23:17:46 +08:00
for _ , key := range task . activeKeys {
q . activeTargets [ key ] = true
}
2026-07-18 19:44:32 +08:00
q . activeTasks ++
q . persistTasks ()
return task
}
}
cond . Wait ()
}
}
2026-11-02 22:47:44 +08:00
func runnableDeleteTaskIndex ( queue [] * Task , activeTargets map [ string ] bool ) int {
for index , task := range queue {
2026-11-03 23:17:46 +08:00
if task . Type == TaskDelete && ! taskTargetsActive ( task , activeTargets ) {
2026-11-02 22:47:44 +08:00
return index
}
}
return - 1
}
2026-07-18 19:44:32 +08:00
func runnableTaskIndex ( queue [] * Task , activeTargets map [ string ] bool ) int {
for index , task := range queue {
2026-11-03 23:17:46 +08:00
if ! taskTargetsActive ( task , activeTargets ) {
2026-07-18 19:44:32 +08:00
return index
}
}
return - 1
}
func taskConcurrencyKey ( task * Task ) string {
if task == nil {
return "task:nil"
}
2026-11-02 22:47:44 +08:00
// A delete cancels active startup work, then waits on the same key until
// that worker has exited; resource removal must never race initialization.
2026-07-18 19:44:32 +08:00
name := strings . TrimSpace ( task . ContainerName )
if name == "" {
name = strings . TrimSpace ( task . Config . Name )
}
if name != "" {
return "name:" + strings . ToLower ( name )
}
if task . ContainerID > 0 {
return fmt . Sprintf ( "id:%d" , task . ContainerID )
}
return "task:" + task . ID
}
func ( q * TaskQueue ) finishTask ( task * Task , status string , taskErr error ) {
q . mu . Lock ()
2026-11-02 22:47:44 +08:00
if task . cancel != nil {
task . cancel ()
task . cancel = nil
}
2026-07-18 19:44:32 +08:00
task . Status = status
2026-11-03 23:17:46 +08:00
task . FinishedAt = time . Now (). Format ( time . RFC3339Nano )
if started := taskTime ( task . StartedAt ); ! started . IsZero () {
task . DurationMS = max ( 0 , time . Since ( started ). Milliseconds ())
}
2026-07-18 19:44:32 +08:00
if taskErr != nil {
task . Error = taskErr . Error ()
2026-11-03 23:17:46 +08:00
if len ( task . Error ) > 8192 {
task . Error = task . Error [: 8192 ]
}
2026-07-18 19:44:32 +08:00
if task . Type == TaskCreate {
task . Stage = "failed"
task . StageDetail = "初始化失败"
}
} else {
task . Error = ""
if task . Type == TaskCreate {
task . Stage = "completed"
task . StageDetail = "初始化完成"
}
}
2026-11-03 23:17:46 +08:00
if status == "failed" {
task . Stage = "failed"
if taskErr != nil {
task . StageDetail = taskErr . Error ()
}
} else {
task . Stage = "completed"
task . StageDetail = "操作完成"
}
addTaskEvent ( task , task . Stage , task . StageDetail )
for _ , key := range task . activeKeys {
delete ( q . activeTargets , key )
}
task . activeKeys = nil
2026-07-18 19:44:32 +08:00
if task . activeKey != "" {
delete ( q . activeTargets , task . activeKey )
task . activeKey = ""
}
2026-11-02 22:47:44 +08:00
delete ( q . runningTasks , task )
2026-07-18 19:44:32 +08:00
if q . activeTasks > 0 {
q . activeTasks --
}
q . persistTasks ()
q . signalDispatchers ()
q . mu . Unlock ()
}
func ( q * TaskQueue ) updateTaskStage ( task * Task , stage , detail string ) {
q . mu . Lock ()
2026-11-03 23:17:46 +08:00
addTaskEvent ( task , stage , detail )
q . persistTaskHistoryOnly ()
2026-07-18 19:44:32 +08:00
q . mu . Unlock ()
}
// runCreateTask handles lxc-create, resource setup, start, and SSH init. A
// restored task resumes initialization when the same-name container exists.
func ( q * TaskQueue ) runCreateTask ( task * Task ) {
q . mu . Lock ()
createdByTask := false
if task . Config . Name == "" {
task . Config . Name = task . ContainerName
}
task . Config . NormalizeResourceAliases ()
cfg := task . Config
2026-11-02 22:47:44 +08:00
ctx := task . ctx
if ctx == nil {
ctx = context . Background ()
}
2026-07-18 19:44:32 +08:00
q . mu . Unlock ()
cfg . Progress = func ( stage , detail string ) {
q . updateTaskStage ( task , stage , detail )
}
if cfg . Name == "" {
err := fmt . Errorf ( "container name is required" )
config . AddAuditLog ( string ( task . Type ), task . ContainerName , "failed: " + err . Error (), "admin" )
q . finishTask ( task , "failed" , err )
return
}
2026-11-02 22:47:44 +08:00
if err := ctx . Err (); err != nil {
lxc . ReleaseQueuedCreateNATPorts ( cfg . Name )
q . finishTask ( task , "failed" , err )
return
}
2026-07-18 19:44:32 +08:00
c := config . FindContainerByName ( cfg . Name )
if c == nil {
2026-11-02 22:47:44 +08:00
if err := q . runtime . create ( cfg ); err != nil {
2026-07-18 19:44:32 +08:00
config . AddAuditLog ( string ( task . Type ), cfg . Name , "失败: " + err . Error (), "admin" )
q . finishTask ( task , "failed" , err )
return
}
createdByTask = true
c = config . FindContainerByName ( cfg . Name )
if c == nil {
err := fmt . Errorf ( "created but not found in config" )
config . AddAuditLog ( string ( task . Type ), task . Config . Name , "失败: " + err . Error (), "admin" )
q . finishTask ( task , "failed" , err )
return
}
2026-07-26 04:04:45 +08:00
} else {
lxc . ReleaseQueuedCreateNATPorts ( cfg . Name )
2026-07-18 19:44:32 +08:00
}
2026-11-02 22:47:44 +08:00
// FindContainer returns a slice-backed pointer. Another container's removal
// can shift that slice while startup waits, so never retain it for cleanup.
2026-11-03 23:17:46 +08:00
containerID , containerName , containerUUID := c . ID , c . Name , c . UUID
2026-07-18 19:44:32 +08:00
q . mu . Lock ()
2026-11-02 22:47:44 +08:00
task . ContainerID = containerID
task . ContainerName = containerName
2026-11-03 23:17:46 +08:00
task . ContainerUUID = containerUUID
2026-07-18 19:44:32 +08:00
q . mu . Unlock ()
startDetail := "启动容器并等待网络就绪"
if strings . EqualFold ( cfg . Virtualization , config . VirtualizationKVM ) {
startDetail = "启动虚拟机并等待网络就绪"
}
q . updateTaskStage ( task , "starting" , startDetail )
2026-11-02 22:47:44 +08:00
if err := q . runtime . start ( ctx , containerID ); err != nil {
q . mu . Lock ()
deleting := task . deleteRequested
q . mu . Unlock ()
if createdByTask && ! deleting {
_ = q . runtime . destroy ( containerID )
2026-07-18 19:44:32 +08:00
}
config . AddAuditLog ( string ( task . Type ), task . ContainerName , "初始化失败: " + err . Error (), "admin" )
q . finishTask ( task , "failed" , err )
return
}
config . AddAuditLog ( string ( task . Type ), task . ContainerName , "成功" , "admin" )
q . finishTask ( task , "done" , nil )
}
func ( q * TaskQueue ) runOperationTask ( task * Task ) {
q . mu . Lock ()
2026-11-03 23:17:46 +08:00
var err error
if builtInTask ( task . Type ) || task . ContainerID > 0 {
err = resolveTaskContainer ( task )
}
2026-11-02 22:47:44 +08:00
ctx := task . ctx
if ctx == nil {
ctx = context . Background ()
}
2026-07-18 19:44:32 +08:00
q . mu . Unlock ()
if err == nil && ( task . Type == TaskStart || task . Type == TaskRestart || task . Type == TaskReinstall ) {
c := config . FindContainer ( task . ContainerID )
if c != nil {
if lxc . IsExpired ( * c ) {
err = fmt . Errorf ( "容器已到期,不允许此操作" )
} else if lxc . IsTrafficExceeded ( * c ) {
err = fmt . Errorf ( "容器流量已超限,不允许此操作" )
}
}
}
2026-11-03 01:40:45 +08:00
if err == nil {
2026-07-18 19:44:32 +08:00
switch task . Type {
case TaskStart :
2026-11-02 22:47:44 +08:00
err = q . runtime . start ( ctx , task . ContainerID )
2026-07-18 19:44:32 +08:00
case TaskStop :
err = stopByRuntime ( task . ContainerID )
case TaskRestart :
err = restartByRuntime ( task . ContainerID )
case TaskDelete :
2026-11-02 22:47:44 +08:00
err = q . runtime . destroy ( task . ContainerID )
2026-07-18 19:44:32 +08:00
if err == nil {
time . Sleep ( time . Second )
if config . FindContainer ( task . ContainerID ) != nil {
err = fmt . Errorf ( "container still exists after delete: %d" , task . ContainerID )
}
}
case TaskReinstall :
if lxc . HasSSHAuthOptions ( task . Config ) {
err = reinstallByRuntime ( task . ContainerID , task . TemplateID , task . Config )
} else {
err = reinstallByRuntime ( task . ContainerID , task . TemplateID )
}
2026-11-03 23:17:46 +08:00
default :
if handler := operationHandler ( task . Type ); handler != nil {
ctx = context . WithValue ( ctx , operationResultKey {}, func ( result json . RawMessage ) error {
q . mu . Lock ()
defer q . mu . Unlock ()
task . Result = append ( json . RawMessage ( nil ), result ... )
task . revision ++
return q . persistTaskHistoryOnly ()
})
err = handler ( ctx , task . ContainerID , task . Payload , func ( stage , detail string ) { q . updateTaskStage ( task , stage , detail ) })
} else {
err = fmt . Errorf ( "operation handler unavailable: %s" , task . Type )
}
2026-07-18 19:44:32 +08:00
}
}
auditUser := task . User
if auditUser == "" {
auditUser = "admin"
}
if err != nil {
config . AddAuditLogFull ( string ( task . Type ), task . ContainerName , "失败: " + err . Error (), auditUser , task . IP , task . UserAgent , false , err . Error ())
q . finishTask ( task , "failed" , err )
return
}
config . AddAuditLogFull ( string ( task . Type ), task . ContainerName , "成功" , auditUser , task . IP , task . UserAgent , true , "" )
switch task . Type {
case TaskStart :
config . UpdateContainerStatus ( task . ContainerID , "running" )
case TaskStop :
config . UpdateContainerStatus ( task . ContainerID , "stopped" )
case TaskRestart :
config . UpdateContainerStatus ( task . ContainerID , "running" )
}
q . finishTask ( task , "done" , nil )
}
2026-06-05 19:23:28 +08:00
func resolveTaskContainer ( task * Task ) error {
if task . Type == TaskCreate {
return nil
}
if task . ContainerID > 0 {
if c := config . FindContainer ( task . ContainerID ); c != nil {
2026-11-03 23:17:46 +08:00
if task . ContainerUUID != "" && c . UUID != task . ContainerUUID {
return fmt . Errorf ( "container identity changed: %d" , task . ContainerID )
}
2026-06-05 19:23:28 +08:00
if task . ContainerName == "" {
task . ContainerName = c . Name
}
return nil
}
}
2026-11-03 23:17:46 +08:00
if task . ContainerUUID != "" {
return fmt . Errorf ( "container no longer exists: %s" , task . ContainerUUID )
}
2026-06-05 19:23:28 +08:00
if task . ContainerName != "" {
if c := config . FindContainerByName ( task . ContainerName ); c != nil {
task . ContainerID = c . ID
task . ContainerName = c . Name
return nil
}
return fmt . Errorf ( "container not found: %s" , task . ContainerName )
}
return fmt . Errorf ( "container not found: %d" , task . ContainerID )
}
2026-11-03 23:17:46 +08:00
func ( q * TaskQueue ) persistTasks () error {
return q . persistTaskState ()
2026-06-05 19:23:28 +08:00
}
func ( q * TaskQueue ) GetTasks () [] * Task {
q . mu . Lock ()
defer q . mu . Unlock ()
result := make ([] * Task , 0 , len ( q . tasks ))
// Collect all task IDs, sort by creation time (extracted from ID number)
for _ , t := range q . tasks {
2026-11-07 23:33:48 +08:00
result = append ( result , snapshotTask ( t ))
2026-06-05 19:23:28 +08:00
}
// Stable sort by ID number (task-N where N is sequential)
for i := 0 ; i < len ( result ); i ++ {
for j := i + 1 ; j < len ( result ); j ++ {
if parseIDNum ( result [ i ]. ID ) > parseIDNum ( result [ j ]. ID ) {
result [ i ], result [ j ] = result [ j ], result [ i ]
}
}
}
return result
}
2026-11-07 23:33:48 +08:00
// snapshotTask copies a task while the queue lock is held.
func snapshotTask ( task * Task ) * Task {
snapshot := * task
snapshot . Logs = append ([] TaskEvent ( nil ), task . Logs ... )
snapshot . Result = append ( json . RawMessage ( nil ), task . Result ... )
if start := taskTime ( task . StartedAt ); task . Status == "running" && ! start . IsZero () {
snapshot . DurationMS = max ( 0 , time . Since ( start ). Milliseconds ())
}
return & snapshot
}
2026-06-05 19:23:28 +08:00
// HandleSingleTaskAction creates a task for a single container action
func HandleSingleTaskAction ( w http . ResponseWriter , r * http . Request , id int , action string ) {
c := config . FindContainer ( id )
name := ""
if c != nil {
name = c . Name
}
2026-06-08 19:25:55 +08:00
// Determine user from authenticated request context.
user := requestActor ( r )
2026-06-06 15:10:24 +08:00
ip := clientIP ( r )
userAgent := r . Header . Get ( "User-Agent" )
2026-06-05 19:23:28 +08:00
var taskType TaskType
var templateID string
2026-06-09 23:25:19 +08:00
var taskConfig * lxc . ContainerConfig
2026-06-05 19:23:28 +08:00
switch action {
case "start" :
taskType = TaskStart
case "stop" :
taskType = TaskStop
case "restart" :
taskType = TaskRestart
case "delete" :
taskType = TaskDelete
case "reinstall" :
var req struct {
2026-06-09 23:25:19 +08:00
TemplateID string `json:"template_id"`
SSHAuthMode string `json:"ssh_auth_mode,omitempty"`
SSHPassword string `json:"ssh_password,omitempty"`
SSHPublicKey string `json:"ssh_public_key,omitempty"`
2026-06-05 19:23:28 +08:00
}
json . NewDecoder ( r . Body ). Decode ( & req )
templateID = req . TemplateID
if templateID == "" {
c := config . FindContainer ( id )
if c != nil {
templateID = c . Template
}
}
2026-06-07 09:24:09 +08:00
runtime := runtimeFromTemplateID ( templateID )
if c := config . FindContainer ( id ); c != nil {
runtime = c . Runtime ()
}
2026-07-16 20:47:26 +08:00
if ! isTemplateAllowedForRequest ( r , c , templateID ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Template is not allowed for this user" })
return
}
if ! isTemplateAvailableForRequest ( r , c , templateID , runtime ) {
2026-06-06 10:08:31 +08:00
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Template is not enabled or downloaded" })
return
}
2026-06-09 23:25:19 +08:00
authCfg := lxc . ContainerConfig {
TemplateID : templateID ,
SSHAuthMode : req . SSHAuthMode ,
SSHPassword : req . SSHPassword ,
SSHPublicKey : req . SSHPublicKey ,
}
if lxc . HasSSHAuthOptions ( authCfg ) {
if err := validateReinstallSSHAuth ( c , templateID , authCfg ); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : err . Error ()})
return
}
taskConfig = & authCfg
}
2026-06-05 19:23:28 +08:00
taskType = TaskReinstall
default :
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Unknown action" })
return
}
2026-06-09 23:25:19 +08:00
ids := globalQueue . EnqueueWithAudit ( id , name , taskType , templateID , taskConfig , user , ip , userAgent )
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusAccepted , APIResponse {
Success : true ,
Message : "Task queued" ,
Data : map [ string ] interface {}{ "task_id" : ids [ 0 ], "container_name" : name , "status" : "pending" },
})
}
// HandleBatchCreate handles batch container creation
func HandleBatchCreate ( w http . ResponseWriter , r * http . Request ) {
if r . Method != http . MethodPost {
jsonResponse ( w , http . StatusMethodNotAllowed , APIResponse { Success : false , Message : "Method not allowed" })
return
}
2026-06-08 19:25:55 +08:00
if ! requireScope ( w , r , "container:create" ) {
return
}
if isAccessRestrictedRequest ( r ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Container-bound API keys cannot create containers" })
return
}
2026-06-05 19:23:28 +08:00
var req struct {
Containers [] lxc . ContainerConfig `json:"containers"`
}
if err := json . NewDecoder ( r . Body ). Decode ( & req ); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Invalid request body" })
return
}
if len ( req . Containers ) == 0 {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "No containers requested" })
return
}
activeCreateNames := globalQueue . ActiveCreateNames ()
requestNames := make ( map [ string ] bool )
2026-07-24 18:51:52 +08:00
requestNATPorts := make ( map [ string ] string )
2026-06-05 19:23:28 +08:00
for i := range req . Containers {
name := strings . TrimSpace ( req . Containers [ i ]. Name )
req . Containers [ i ]. Name = name
if ! config . IsValidContainerNameSyntax ( name ) {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Invalid container name: " + name })
return
}
if requestNames [ name ] {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Duplicate container name in request: " + name })
return
}
if config . FindContainerByName ( name ) != nil {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : "Container name already exists: " + name })
return
}
if activeCreateNames [ name ] {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : "Container creation already queued: " + name })
return
}
if req . Containers [ i ]. VCPU <= 0 {
req . Containers [ i ]. VCPU = 1
}
2026-06-12 15:53:31 +08:00
if err := rejectNegativeCreateLimits ( req . Containers [ i ]); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
req . Containers [ i ]. NormalizeResourceAliases ()
2026-11-14 10:31:14 +08:00
if strings . EqualFold ( strings . TrimSpace ( req . Containers [ i ]. Virtualization ), "podman" ) {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Message : "Use the Podman container endpoint for OCI applications" })
return
}
2026-06-07 09:24:09 +08:00
req . Containers [ i ]. Virtualization = runtimeFromRequest ( req . Containers [ i ]. Virtualization )
2026-11-09 03:32:57 +08:00
if req . Containers [ i ]. Virtualization != config . VirtualizationLXC && ( req . Containers [ i ]. Privileged || req . Containers [ i ]. Nesting ) {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": privileged and nesting options are only supported for LXC containers" })
return
}
if req . Containers [ i ]. Privileged && ! isAdministratorRequest ( r ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : name + ": privileged LXC containers require administrator permission" })
return
}
2026-07-16 23:27:26 +08:00
if req . Containers [ i ]. WantsLANIPv4 () && req . Containers [ i ]. Virtualization != config . VirtualizationLXC {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": LAN IPv4 is only supported for LXC containers" })
return
}
2026-06-05 19:23:28 +08:00
if req . Containers [ i ]. RAMMB < 128 {
req . Containers [ i ]. RAMMB = 512
}
2026-09-19 01:34:13 +08:00
if req . Containers [ i ]. DiskGB <= 0 {
2026-06-05 19:23:28 +08:00
req . Containers [ i ]. DiskGB = 5
}
2026-07-18 19:44:32 +08:00
if err := validateCreateStoragePool ( & req . Containers [ i ]); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
2026-06-07 09:24:09 +08:00
if ! isImageEnabledAndDownloaded ( req . Containers [ i ]. TemplateID , req . Containers [ i ]. Virtualization ) {
2026-06-06 10:08:31 +08:00
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : name + ": template is not enabled or downloaded" })
return
}
2026-07-16 20:47:26 +08:00
if ids , err := normalizeAllowedImageIDs ( req . Containers [ i ]. AllowedImageIDs ); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
} else {
req . Containers [ i ]. AllowedImageIDs = ids
}
2026-06-09 22:22:40 +08:00
if req . Containers [ i ]. PortMappingCount < 0 {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": port mapping count cannot be negative" })
return
}
2026-07-24 18:51:52 +08:00
if err := req . Containers [ i ]. NormalizeCreateNATMappings (); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
if err := lxc . ValidateCreateNATPortAvailability ( req . Containers [ i ]); err != nil {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
if req . Containers [ i ]. ManagementPort > 0 {
key := fmt . Sprintf ( "%d/tcp" , req . Containers [ i ]. ManagementPort )
if owner := requestNATPorts [ key ]; owner != "" {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : fmt . Sprintf ( "%s: NAT management port %s is also requested by %s" , name , key , owner )})
return
}
requestNATPorts [ key ] = name
}
for _ , mapping := range req . Containers [ i ]. NATPortMappings {
key := fmt . Sprintf ( "%d/%s" , mapping . HostPort , mapping . Protocol )
if owner := requestNATPorts [ key ]; owner != "" {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : fmt . Sprintf ( "%s: NAT host port %s is also requested by %s" , name , key , owner )})
return
}
requestNATPorts [ key ] = name
2026-06-09 22:22:40 +08:00
}
if req . Containers [ i ]. PortMappingCount > 64 {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": port mapping count cannot exceed 64" })
return
}
if req . Containers [ i ]. IPv4Count < 0 || req . Containers [ i ]. IPv6Count < 0 {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": IP address count cannot be negative" })
return
}
if req . Containers [ i ]. IPv4Count > 64 || req . Containers [ i ]. IPv6Count > 64 {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": IP address count cannot exceed 64" })
return
}
if ! req . Containers [ i ]. AssignIPv4 && len ( req . Containers [ i ]. PublicIPv4s ) == 0 {
req . Containers [ i ]. IPv4Count = 0
}
if ! req . Containers [ i ]. AssignIPv6 && len ( req . Containers [ i ]. IPv6Addresses ) == 0 {
req . Containers [ i ]. IPv6Count = 0
}
if ! hasRequestedNetwork ( req . Containers [ i ]) {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + noNetworkSelectedMessage })
return
2026-06-06 09:24:19 +08:00
}
if req . Containers [ i ]. SnapshotLimit <= 0 {
req . Containers [ i ]. SnapshotLimit = config . DefaultSnapshotLimit
}
2026-09-19 01:34:13 +08:00
if err := validateRuntimeResourceRequest ( req . Containers [ i ]. Virtualization , req . Containers [ i ]. TemplateID , req . Containers [ i ]. VCPU , req . Containers [ i ]. RAMMB , req . Containers [ i ]. DiskGB ); err != nil {
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
2026-06-09 23:25:19 +08:00
if err := validateCreateSSHAuth ( req . Containers [ i ]); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : name + ": " + err . Error ()})
return
}
2026-06-05 19:23:28 +08:00
requestNames [ name ] = true
}
2026-07-26 04:04:45 +08:00
planned , err := lxc . ReserveBatchCreateNATPorts ( req . Containers )
if err != nil {
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : err . Error ()})
return
}
ids := globalQueue . EnqueueBatchCreateWithAudit ( planned , requestActor ( r ), clientIP ( r ), r . UserAgent ())
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusAccepted , APIResponse { Success : true , Data : ids })
}
// HandleBatchAction handles batch container actions
func HandleBatchAction ( w http . ResponseWriter , r * http . Request ) {
if r . Method != http . MethodPost {
jsonResponse ( w , http . StatusMethodNotAllowed , APIResponse { Success : false , Message : "Method not allowed" })
return
}
2026-06-08 19:25:55 +08:00
if ! hasAnyScope ( r , "container:power" , "container:delete" , "container:reinstall" ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Insufficient API key scope" })
return
}
2026-06-05 19:23:28 +08:00
var req struct {
2026-06-09 23:25:19 +08:00
Action string `json:"action"`
Containers [] int `json:"containers"`
TemplateID string `json:"template_id,omitempty"`
SSHAuthMode string `json:"ssh_auth_mode,omitempty"`
SSHPassword string `json:"ssh_password,omitempty"`
SSHPublicKey string `json:"ssh_public_key,omitempty"`
2026-06-05 19:23:28 +08:00
}
if err := json . NewDecoder ( r . Body ). Decode ( & req ); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Invalid request body" })
return
}
var taskType TaskType
2026-06-08 19:25:55 +08:00
var requiredScope string
2026-06-09 23:25:19 +08:00
var taskConfig * lxc . ContainerConfig
2026-06-05 19:23:28 +08:00
switch req . Action {
case "start" :
taskType = TaskStart
2026-06-08 19:25:55 +08:00
requiredScope = "container:power"
2026-06-05 19:23:28 +08:00
case "stop" :
taskType = TaskStop
2026-06-08 19:25:55 +08:00
requiredScope = "container:power"
2026-06-05 19:23:28 +08:00
case "restart" :
taskType = TaskRestart
2026-06-08 19:25:55 +08:00
requiredScope = "container:power"
2026-06-05 19:23:28 +08:00
case "delete" :
taskType = TaskDelete
2026-06-08 19:25:55 +08:00
requiredScope = "container:delete"
case "reinstall" :
if req . TemplateID == "" {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "template_id required" })
return
}
if ! isTemplateEnabledAndDownloaded ( req . TemplateID ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Template is not enabled or downloaded" })
return
}
2026-06-09 23:25:19 +08:00
authCfg := lxc . ContainerConfig {
TemplateID : req . TemplateID ,
SSHAuthMode : req . SSHAuthMode ,
SSHPassword : req . SSHPassword ,
SSHPublicKey : req . SSHPublicKey ,
}
if lxc . HasSSHAuthOptions ( authCfg ) {
taskConfig = & authCfg
}
2026-06-08 19:25:55 +08:00
taskType = TaskReinstall
requiredScope = "container:reinstall"
2026-06-05 19:23:28 +08:00
default :
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Unknown action" })
return
}
2026-06-08 19:25:55 +08:00
if ! requireScope ( w , r , requiredScope ) {
return
}
for _ , id := range req . Containers {
c := config . FindContainer ( id )
if c == nil || ! isContainerAllowedForRequest ( r , c . UUID ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Access denied to one or more containers" })
return
}
2026-07-16 20:47:26 +08:00
if taskType == TaskReinstall && ! isTemplateAllowedForRequest ( r , c , req . TemplateID ) {
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : c . Name + ": template is not allowed for this user" })
return
}
2026-06-09 23:25:19 +08:00
if taskConfig != nil {
if err := validateReinstallSSHAuth ( c , req . TemplateID , * taskConfig ); err != nil {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : c . Name + ": " + err . Error ()})
return
}
}
2026-06-08 19:25:55 +08:00
}
2026-06-05 19:23:28 +08:00
2026-06-09 23:25:19 +08:00
var ids [] string
if taskConfig != nil {
for _ , id := range req . Containers {
c := config . FindContainer ( id )
name := ""
if c != nil {
name = c . Name
}
queued := globalQueue . EnqueueWithAudit ( id , name , taskType , req . TemplateID , taskConfig , requestActor ( r ), clientIP ( r ), r . UserAgent ())
ids = append ( ids , queued ... )
}
} else {
ids = globalQueue . EnqueueBatchWithAudit ( taskType , req . Containers , req . TemplateID , requestActor ( r ), clientIP ( r ), r . UserAgent ())
}
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusAccepted , APIResponse { Success : true , Data : ids })
}
2026-11-07 23:33:48 +08:00
// HandleTask reads or removes a task by its ID.
func HandleTask ( w http . ResponseWriter , r * http . Request ) {
if r . Method != http . MethodGet {
HandleTaskDelete ( w , r )
return
}
if ! requireScope ( w , r , "task:read" ) {
return
}
id := taskIDFromPath ( r . URL . Path )
globalQueue . mu . Lock ()
var snapshot * Task
if task := globalQueue . tasks [ id ]; task != nil {
snapshot = snapshotTask ( task )
}
globalQueue . mu . Unlock ()
if snapshot == nil || ! isHistoricalTaskAllowed ( r , snapshot ) {
jsonResponse ( w , http . StatusNotFound , APIResponse { Success : false , Message : "Task not found" })
return
}
jsonResponse ( w , http . StatusOK , APIResponse { Success : true , Data : snapshot })
}
func taskIDFromPath ( path string ) string {
path = strings . TrimPrefix ( path , "/api/v1/tasks/" )
return strings . TrimPrefix ( path , "/api/tasks/" )
}
// HandleTaskDelete deletes a specific task by ID.
2026-06-05 19:23:28 +08:00
func HandleTaskDelete ( w http . ResponseWriter , r * http . Request ) {
if r . Method != http . MethodDelete {
jsonResponse ( w , http . StatusMethodNotAllowed , APIResponse { Success : false , Message : "Method not allowed" })
return
}
2026-06-08 19:25:55 +08:00
if ! requireScope ( w , r , "task:delete" ) {
return
}
// URL: /api/tasks/{id} or /api/v1/tasks/{id}
2026-11-07 23:33:48 +08:00
taskID := taskIDFromPath ( r . URL . Path )
2026-06-05 19:23:28 +08:00
if taskID == "" {
jsonResponse ( w , http . StatusBadRequest , APIResponse { Success : false , Message : "Task ID required" })
return
}
globalQueue . mu . Lock ()
2026-07-26 04:04:45 +08:00
task := globalQueue . tasks [ taskID ]
2026-11-03 23:17:46 +08:00
if task != nil && ! isHistoricalTaskAllowed ( r , task ) {
2026-06-08 19:25:55 +08:00
globalQueue . mu . Unlock ()
jsonResponse ( w , http . StatusForbidden , APIResponse { Success : false , Message : "Access denied to this task" })
return
}
2026-11-03 23:17:46 +08:00
if task != nil && task . Status == "running" {
globalQueue . mu . Unlock ()
jsonResponse ( w , http . StatusConflict , APIResponse { Success : false , Message : "A running task cannot be removed" })
return
}
if err := config . DeleteTaskHistory ( taskID ); err != nil && err != config . ErrHistoryStoreUnavailable {
globalQueue . mu . Unlock ()
jsonResponse ( w , http . StatusInternalServerError , APIResponse { Success : false , Message : "Failed to remove task history" })
return
}
2026-06-05 19:23:28 +08:00
delete ( globalQueue . tasks , taskID )
2026-11-03 23:17:46 +08:00
delete ( globalQueue . historySaved , taskID )
2026-06-05 19:23:28 +08:00
// Also remove from both queues if pending
newCreate := make ([] * Task , 0 , len ( globalQueue . createQueue ))
for _ , t := range globalQueue . createQueue {
if t . ID != taskID {
newCreate = append ( newCreate , t )
}
}
globalQueue . createQueue = newCreate
newOp := make ([] * Task , 0 , len ( globalQueue . opQueue ))
for _ , t := range globalQueue . opQueue {
if t . ID != taskID {
newOp = append ( newOp , t )
}
}
globalQueue . opQueue = newOp
globalQueue . persistTasks ()
globalQueue . mu . Unlock ()
2026-07-26 04:04:45 +08:00
if task != nil && task . Type == TaskCreate {
lxc . ReleaseQueuedCreateNATPorts ( task . Config . Name )
}
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusOK , APIResponse { Success : true , Message : "Task deleted" })
}
// HandleTasks returns the current task queue
func HandleTasks ( w http . ResponseWriter , r * http . Request ) {
if r . Method != http . MethodGet {
jsonResponse ( w , http . StatusMethodNotAllowed , APIResponse { Success : false , Message : "Method not allowed" })
return
}
2026-06-08 19:25:55 +08:00
if ! requireScope ( w , r , "task:read" ) {
return
}
2026-06-05 19:23:28 +08:00
tasks := globalQueue . GetTasks ()
2026-11-03 23:17:46 +08:00
filtered := make ([] * Task , 0 , len ( tasks ))
for _ , task := range tasks {
if isHistoricalTaskAllowed ( r , task ) {
filtered = append ( filtered , task )
}
}
tasks = filtered
2026-06-05 19:23:28 +08:00
jsonResponse ( w , http . StatusOK , APIResponse { Success : true , Data : tasks })
}
// RestoreTasks restores task queue from config
func RestoreTasks () {
2026-11-03 23:17:46 +08:00
globalQueue . restoreTaskState ()
2026-06-05 19:23:28 +08:00
}
func parseIDNum ( id string ) int {
var num int
for _ , c := range id {
if c >= '0' && c <= '9' {
num = num * 10 + int ( c - '0' )
}
}
return num
}
2026-07-18 19:44:32 +08:00
func validateCreateStoragePool ( cfg * lxc . ContainerConfig ) error {
required := config . StorageContentLXC
if cfg . Virtualization == config . VirtualizationKVM {
required = config . StorageContentKVM
}
2026-09-19 01:34:13 +08:00
requiredBytes := int64 ( math . Round ( cfg . DiskGB * 1024 )) * 1024 * 1024
2026-07-18 19:44:32 +08:00
pool , err := config . SelectStoragePoolForContent ( required , cfg . StoragePoolID , requiredBytes )
if err != nil {
return err
}
cfg . StoragePoolID = pool . ID
return nil
}