| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // 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 guest
- import (
- "context"
- "yunion.io/x/jsonutils"
- "yunion.io/x/pkg/errors"
- 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"
- "yunion.io/x/onecloud/pkg/util/logclient"
- )
- type PodSyncstatusTask struct {
- SGuestBaseTask
- }
- func init() {
- taskman.RegisterTask(PodSyncstatusTask{})
- }
- func (t *PodSyncstatusTask) taskFailed(ctx context.Context, pod *models.SGuest, err string) {
- pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_UPLOADING_STATUS_FAILED, err)
- db.OpsLog.LogEvent(pod, db.ACT_SYNC_STATUS, err, t.GetUserCred())
- logclient.AddActionLogWithStartable(t, pod, logclient.ACT_SYNC_STATUS, err, t.GetUserCred(), false)
- t.SetStageFailed(ctx, jsonutils.NewString(err))
- }
- func (t *PodSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
- pod := obj.(*models.SGuest)
- pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_UPLOADING_STATUS, "by PodSyncstatusTask")
- t.SetStage("OnUploadGuestStatus", nil)
- if err := t.requestUploadStatus(ctx, pod); err != nil {
- t.taskFailed(ctx, pod, err.Error())
- }
- }
- func (t *PodSyncstatusTask) requestUploadStatus(ctx context.Context, pod *models.SGuest) error {
- drv, err := pod.GetDriver()
- if err != nil {
- return errors.Wrap(err, "pod.GetDriver")
- }
- if err := drv.RequestUploadGuestStatus(ctx, pod, t); err != nil {
- return errors.Wrap(err, "drv.RequestUploadGuestStatus")
- }
- return nil
- }
- func (t *PodSyncstatusTask) OnUploadGuestStatus(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
- t.SetStageComplete(ctx, nil)
- }
- func (t *PodSyncstatusTask) OnUploadGuestStatusFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
- t.taskFailed(ctx, pod, reason.String())
- }
- /*func (t *PodSyncstatusTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
- t.SetStage("OnWaitContainerSynced", nil)
- pod := obj.(*models.SGuest)
- pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_SYNCING_CONTAINER_STATUS, "")
- ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
- if err != nil {
- t.OnWaitContainerSyncedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
- return
- }
- if len(ctrs) == 0 {
- t.OnWaitContainerSynced(ctx, pod, nil)
- } else {
- for i := range ctrs {
- curCtr := ctrs[i]
- curCtr.StartSyncStatusTask(ctx, t.GetUserCred(), t.GetTaskId())
- }
- }
- }
- func (t *PodSyncstatusTask) OnWaitContainerSynced(ctx context.Context, pod *models.SGuest, _ jsonutils.JSONObject) {
- isAllSynced := true
- ctrs, err := models.GetContainerManager().GetContainersByPod(pod.GetId())
- if err != nil {
- t.OnWaitContainerSyncedFailed(ctx, pod, jsonutils.NewString(errors.Wrap(err, "GetContainersByPod").Error()))
- return
- }
- for i := range ctrs {
- curCtr := ctrs[i]
- if curCtr.GetStatus() == api.CONTAINER_STATUS_SYNC_STATUS {
- isAllSynced = false
- }
- }
- if isAllSynced {
- t.OnContainerSynced(ctx, pod)
- }
- }
- func (t *PodSyncstatusTask) OnWaitContainerSyncedFailed(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
- pod.SetStatus(ctx, t.GetUserCred(), api.POD_STATUS_SYNCING_CONTAINER_STATUS_FAILED, data.String())
- t.SetStageFailed(ctx, data)
- }
- func (t *PodSyncstatusTask) OnContainerSynced(ctx context.Context, pod *models.SGuest) {
- t.SetStage("OnPodSynced", nil)
- models.StartResourceSyncStatusTask(ctx, t.GetUserCred(), pod, "GuestSyncstatusTask", t.GetTaskId())
- }
- func (t *PodSyncstatusTask) OnPodSynced(ctx context.Context, pod *models.SGuest, data jsonutils.JSONObject) {
- t.SetStageComplete(ctx, nil)
- }
- func (t *PodSyncstatusTask) OnPodSyncedFailed(ctx context.Context, pod *models.SGuest, reason jsonutils.JSONObject) {
- t.SetStageFailed(ctx, reason)
- }*/
|