mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
feat(region): allow containers to be created with auto_start param (#23572)
This commit is contained in:
@@ -74,6 +74,8 @@ const (
|
||||
CONTAINER_STATUS_SAVE_IMAGE_FAILED = "save_image_failed"
|
||||
CONTAINER_STATUS_STARTING = "starting"
|
||||
CONTAINER_STATUS_START_FAILED = "start_failed"
|
||||
CONTAINER_STATUS_SYNCING_CONF = "syncing_conf"
|
||||
CONTAINER_STATUS_SYNC_CONF_FAILED = "sync_conf_failed"
|
||||
CONTAINER_STATUS_STOPPING = "stopping"
|
||||
CONTAINER_STATUS_STOP_FAILED = "stop_failed"
|
||||
CONTAINER_STATUS_SYNC_STATUS = "sync_status"
|
||||
@@ -149,8 +151,9 @@ func (c *ContainerSpec) IsZero() bool {
|
||||
type ContainerCreateInput struct {
|
||||
apis.VirtualResourceCreateInput
|
||||
|
||||
GuestId string `json:"guest_id"`
|
||||
Spec ContainerSpec `json:"spec"`
|
||||
GuestId string `json:"guest_id"`
|
||||
Spec ContainerSpec `json:"spec"`
|
||||
AutoStart bool `json:"auto_start"`
|
||||
// swagger:ignore
|
||||
SkipTask bool `json:"skip_task"`
|
||||
}
|
||||
|
||||
@@ -432,7 +432,7 @@ func (m *SContainerManager) StartBatchStopTask(ctx context.Context, userCred mcc
|
||||
func (c *SContainer) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
c.SVirtualResourceBase.PostCreate(ctx, userCred, ownerId, query, data)
|
||||
if !jsonutils.QueryBoolean(data, "skip_task", false) {
|
||||
if err := c.StartCreateTask(ctx, userCred, "", nil); err != nil {
|
||||
if err := c.StartCreateTask(ctx, userCred, "", data.(*jsonutils.JSONDict)); err != nil {
|
||||
log.Errorf("StartCreateTask error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ type ContainerBatchStartTask struct {
|
||||
}
|
||||
|
||||
func (t *ContainerBatchStartTask) OnInit(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
t.SetStage("OnContainersRestartComplete", nil)
|
||||
t.SetStage("OnContainersStartComplete", nil)
|
||||
for i := range objs {
|
||||
ctr := objs[i].(*models.SContainer)
|
||||
if err := ctr.StartStartTask(ctx, t.GetUserCred(), t.GetId()); err != nil {
|
||||
@@ -44,6 +44,6 @@ func (t *ContainerBatchStartTask) OnInit(ctx context.Context, objs []db.IStandal
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ContainerBatchStartTask) OnContainersRestartComplete(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
func (t *ContainerBatchStartTask) OnContainersStartComplete(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
t.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
@@ -55,13 +55,31 @@ func (t *ContainerStartTask) startCacheImages(ctx context.Context, ctr *models.S
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) OnCacheImagesComplete(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
|
||||
t.requestStart(ctx, ctr)
|
||||
t.requestSyncConf(ctx, ctr)
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) OnCacheImagesCompleteFailed(ctx context.Context, ctr *models.SContainer, data jsonutils.JSONObject) {
|
||||
t.OnStartedFailed(ctx, ctr, jsonutils.NewString(data.String()))
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) requestSyncConf(ctx context.Context, container *models.SContainer) {
|
||||
// sync configuration to make server of host to refresh desc file
|
||||
t.SetStage("OnSyncConf", nil)
|
||||
if err := container.GetPod().StartSyncTaskWithoutSyncstatus(ctx, t.GetUserCred(), false, t.GetTaskId()); err != nil {
|
||||
t.OnSyncConfFailed(ctx, container, jsonutils.NewString(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) OnSyncConf(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
|
||||
t.requestStart(ctx, container)
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) OnSyncConfFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
|
||||
container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_SYNC_CONF_FAILED, reason.String())
|
||||
t.SetStageFailed(ctx, reason)
|
||||
}
|
||||
|
||||
func (t *ContainerStartTask) requestStart(ctx context.Context, container *models.SContainer) {
|
||||
t.SetStage("OnStarted", nil)
|
||||
if err := t.GetPodDriver().RequestStartContainer(ctx, t.GetUserCred(), t); err != nil {
|
||||
|
||||
@@ -16,7 +16,6 @@ package guest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
@@ -44,7 +43,7 @@ func (t *PodCreateTask) OnInit(ctx context.Context, obj db.IStandaloneModel, bod
|
||||
func (t *PodCreateTask) OnWaitPodCreated(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "GuestCreateTask", obj, t.GetUserCred(), t.GetParams(), t.GetTaskId(), "", nil)
|
||||
if err != nil {
|
||||
t.SetStageFailed(ctx, jsonutils.NewString(fmt.Sprintf("New GuestCreateTask")))
|
||||
t.SetStageFailed(ctx, jsonutils.NewString(errors.Wrap(err, "New GuestCreateTask").Error()))
|
||||
return
|
||||
}
|
||||
if err := task.ScheduleRun(nil); err != nil {
|
||||
|
||||
@@ -154,8 +154,9 @@ func (o ContainerCreateCommonOptions) getCreateSpec() (*computeapi.ContainerSpec
|
||||
|
||||
type ContainerCreateOptions struct {
|
||||
ContainerCreateCommonOptions
|
||||
PODID string `help:"Name or id of server pod" json:"-"`
|
||||
NAME string `help:"Name of container" json:"-"`
|
||||
AutoStart bool `help:"Auto start container" json:"auto_start"`
|
||||
PODID string `help:"Name or id of server pod" json:"-"`
|
||||
NAME string `help:"Name of container" json:"-"`
|
||||
}
|
||||
|
||||
func (o *ContainerCreateOptions) Params() (jsonutils.JSONObject, error) {
|
||||
@@ -164,8 +165,9 @@ func (o *ContainerCreateOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return nil, errors.Wrap(err, "get container create spec")
|
||||
}
|
||||
req := computeapi.ContainerCreateInput{
|
||||
GuestId: o.PODID,
|
||||
Spec: *spec,
|
||||
GuestId: o.PODID,
|
||||
Spec: *spec,
|
||||
AutoStart: o.AutoStart,
|
||||
}
|
||||
req.Name = o.NAME
|
||||
return jsonutils.Marshal(req), nil
|
||||
|
||||
Reference in New Issue
Block a user