mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-30 17:13:08 +08:00
feat(container): container-restart (#23588)
This commit is contained in:
@@ -43,6 +43,7 @@ func init() {
|
||||
cmd.BatchDelete(new(options.ContainerDeleteOptions))
|
||||
cmd.BatchPerform("stop", new(options.ContainerStopOptions))
|
||||
cmd.BatchPerform("start", new(options.ContainerStartOptions))
|
||||
cmd.BatchPerform("restart", new(options.ContainerRestartOptions))
|
||||
cmd.BatchPerform("syncstatus", new(options.ContainerIdsOptions))
|
||||
cmd.Perform("save-volume-mount-image", new(options.ContainerSaveVolumeMountImage))
|
||||
cmd.Perform("exec-sync", new(options.ContainerExecSyncOptions))
|
||||
|
||||
@@ -174,6 +174,11 @@ type ContainerStopInput struct {
|
||||
Force bool `json:"force"`
|
||||
}
|
||||
|
||||
type ContainerRestartInput struct {
|
||||
Timeout int `json:"timeout"`
|
||||
Force bool `json:"force"`
|
||||
}
|
||||
|
||||
type ContainerSyncStatusResponse struct {
|
||||
Status string `json:"status"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
|
||||
@@ -429,6 +429,20 @@ func (m *SContainerManager) StartBatchStopTask(ctx context.Context, userCred mcc
|
||||
return m.startBatchTask(ctx, userCred, "ContainerBatchStopTask", ctrs, taskParams, parentTaskId)
|
||||
}
|
||||
|
||||
func (m *SContainerManager) StartBatchRestartTask(ctx context.Context, userCred mcclient.TokenCredential, ctrs []SContainer, timeout int, force bool, parentTaskId string) error {
|
||||
params := make([]api.ContainerRestartInput, len(ctrs))
|
||||
for i := range ctrs {
|
||||
params[i] = api.ContainerRestartInput{
|
||||
Timeout: timeout,
|
||||
Force: force,
|
||||
}
|
||||
}
|
||||
taskParams := jsonutils.NewDict()
|
||||
taskParams.Add(jsonutils.Marshal(params), "params")
|
||||
|
||||
return m.startBatchTask(ctx, userCred, "ContainerBatchRestartTask", ctrs, taskParams, parentTaskId)
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -647,6 +661,27 @@ func (c *SContainer) StartStopTask(ctx context.Context, userCred mcclient.TokenC
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (c *SContainer) PerformRestart(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data *api.ContainerRestartInput) (jsonutils.JSONObject, error) {
|
||||
if !data.Force {
|
||||
if !sets.NewString(
|
||||
api.CONTAINER_STATUS_RUNNING,
|
||||
api.CONTAINER_STATUS_PROBING,
|
||||
api.CONTAINER_STATUS_PROBE_FAILED,
|
||||
api.CONTAINER_STATUS_STOP_FAILED).Has(c.Status) {
|
||||
return nil, httperrors.NewInvalidStatusError("Can't restart container in status %s", c.Status)
|
||||
}
|
||||
}
|
||||
return nil, c.StartRestartTask(ctx, userCred, data, "")
|
||||
}
|
||||
|
||||
func (c *SContainer) StartRestartTask(ctx context.Context, userCred mcclient.TokenCredential, data *api.ContainerRestartInput, parentTaskId string) error {
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "ContainerRestartTask", c, userCred, jsonutils.Marshal(data).(*jsonutils.JSONDict), parentTaskId, "", nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "NewTask")
|
||||
}
|
||||
return task.ScheduleRun(nil)
|
||||
}
|
||||
|
||||
func (c *SContainer) PerformSyncstatus(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
|
||||
return nil, c.StartSyncStatusTask(ctx, userCred, "")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(ContainerBatchRestartTask{})
|
||||
}
|
||||
|
||||
type ContainerBatchRestartTask struct {
|
||||
taskman.STask
|
||||
}
|
||||
|
||||
func (t *ContainerBatchRestartTask) OnInit(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
t.SetStage("OnContainersRestartComplete", nil)
|
||||
|
||||
params := make([]api.ContainerRestartInput, 0)
|
||||
t.GetParams().Unmarshal(¶ms, "params")
|
||||
|
||||
for i := range objs {
|
||||
ctr := objs[i].(*models.SContainer)
|
||||
if err := ctr.StartRestartTask(ctx, t.GetUserCred(), ¶ms[i], t.GetId()); err != nil {
|
||||
t.SetStageFailed(ctx, jsonutils.NewString(fmt.Sprintf("restart container %s: %s", ctr.GetName(), err.Error())))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ContainerBatchRestartTask) OnContainersRestartComplete(ctx context.Context, objs []db.IStandaloneModel, data jsonutils.JSONObject) {
|
||||
t.SetStageComplete(ctx, nil)
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package container
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
|
||||
api "yunion.io/x/onecloud/pkg/apis/compute"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db"
|
||||
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
|
||||
"yunion.io/x/onecloud/pkg/compute/models"
|
||||
)
|
||||
|
||||
type ContainerRestartTask struct {
|
||||
ContainerBaseTask
|
||||
}
|
||||
|
||||
func init() {
|
||||
taskman.RegisterTask(ContainerRestartTask{})
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
|
||||
t.requestStop(ctx, obj.(*models.SContainer))
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) requestStop(ctx context.Context, container *models.SContainer) {
|
||||
t.SetStage("OnStopped", nil)
|
||||
|
||||
input := &api.ContainerStopInput{}
|
||||
t.GetParams().Unmarshal(input)
|
||||
|
||||
if err := container.StartStopTask(ctx, t.GetUserCred(), input, t.GetId()); err != nil {
|
||||
t.OnStoppedFailed(ctx, container, jsonutils.NewString(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) OnStoppedFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
|
||||
container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_STOP_FAILED, reason.String())
|
||||
t.SetStageFailed(ctx, reason)
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) OnStopped(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
|
||||
t.SetStage("OnStarted", nil)
|
||||
|
||||
if err := container.StartStartTask(ctx, t.GetUserCred(), t.GetTaskId()); err != nil {
|
||||
t.OnStartedFailed(ctx, container, jsonutils.NewString(err.Error()))
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) OnStarted(ctx context.Context, container *models.SContainer, data jsonutils.JSONObject) {
|
||||
t.SetStageComplete(ctx, nil)
|
||||
}
|
||||
|
||||
func (t *ContainerRestartTask) OnStartedFailed(ctx context.Context, container *models.SContainer, reason jsonutils.JSONObject) {
|
||||
container.SetStatus(ctx, t.GetUserCred(), api.CONTAINER_STATUS_START_FAILED, reason.String())
|
||||
t.SetStageFailed(ctx, reason)
|
||||
}
|
||||
@@ -342,6 +342,16 @@ type ContainerStartOptions struct {
|
||||
ContainerIdsOptions
|
||||
}
|
||||
|
||||
type ContainerRestartOptions struct {
|
||||
ContainerIdsOptions
|
||||
Timeout int `help:"Stopping timeout" json:"timeout"`
|
||||
Force bool `help:"Force stop container" json:"force"`
|
||||
}
|
||||
|
||||
func (o *ContainerRestartOptions) Params() (jsonutils.JSONObject, error) {
|
||||
return jsonutils.Marshal(o), nil
|
||||
}
|
||||
|
||||
type ContainerSaveVolumeMountImage struct {
|
||||
options.ResourceIdOptions
|
||||
IMAGENAME string `help:"Image name"`
|
||||
|
||||
Reference in New Issue
Block a user