From c226568ec0fb3f0b9981df28699b07a2d60b3b55 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Thu, 7 Jan 2021 21:27:57 +0800 Subject: [PATCH] fix(region): allowed to manually refresh cloudimage --- cmd/climc/shell/compute/cloudregions.go | 1 + pkg/apis/compute/sku_const.go | 3 ++ pkg/cloudcommon/db/opslog_const.go | 1 + pkg/compute/models/cloudregions.go | 18 +++++++ .../tasks/cloudregion_sync_images_task.go | 53 +++++++++++++++++++ pkg/mcclient/options/skus.go | 12 +++++ 6 files changed, 88 insertions(+) create mode 100644 pkg/compute/tasks/cloudregion_sync_images_task.go diff --git a/cmd/climc/shell/compute/cloudregions.go b/cmd/climc/shell/compute/cloudregions.go index 5ca0395a75..9c46c73e06 100644 --- a/cmd/climc/shell/compute/cloudregions.go +++ b/cmd/climc/shell/compute/cloudregions.go @@ -29,6 +29,7 @@ import ( func init() { cmd := shell.NewResourceCmd(&modules.Cloudregions).WithKeyword("cloud-region") cmd.PerformClass("sync-skus", &options.CloudregionSkuSyncOptions{}) + cmd.Perform("sync-images", &options.CloudregionIdOptions{}) R(&options.SkuTaskQueryOptions{}, "cloud-region-sync-task-show", "Show details of skus sync tasks", func(s *mcclient.ClientSession, args *options.SkuTaskQueryOptions) error { params, err := args.Params() diff --git a/pkg/apis/compute/sku_const.go b/pkg/apis/compute/sku_const.go index 27428a86fa..f36382e615 100644 --- a/pkg/apis/compute/sku_const.go +++ b/pkg/apis/compute/sku_const.go @@ -177,3 +177,6 @@ type CloudregionSkuSyncInput struct { // choices: serversku|elasticcachesku|dbinstance_sku Resource string `json:"resource"` } + +type SyncImagesInput struct { +} diff --git a/pkg/cloudcommon/db/opslog_const.go b/pkg/cloudcommon/db/opslog_const.go index 61a707152d..d75098b679 100644 --- a/pkg/cloudcommon/db/opslog_const.go +++ b/pkg/cloudcommon/db/opslog_const.go @@ -201,6 +201,7 @@ const ( ACT_SYNC_CLOUD_DISK = "sync_cloud_disk" ACT_SYNC_CLOUD_SERVER = "sync_cloud_server" ACT_SYNC_CLOUD_SKUS = "sync_cloud_skus" + ACT_SYNC_CLOUD_IMAGES = "sync_cloud_images" ACT_SYNC_CLOUD_EIP = "sync_cloud_eip" ACT_SYNC_CLOUD_PROJECT = "sync_cloud_project" ACT_SYNC_CLOUD_ELASTIC_CACHE = "sync_cloud_elastic_cache" diff --git a/pkg/compute/models/cloudregions.go b/pkg/compute/models/cloudregions.go index 7d5f176d18..dc0af700f6 100644 --- a/pkg/compute/models/cloudregions.go +++ b/pkg/compute/models/cloudregions.go @@ -30,6 +30,7 @@ import ( api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudcommon/db" "yunion.io/x/onecloud/pkg/cloudcommon/db/lockman" + "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman" "yunion.io/x/onecloud/pkg/cloudprovider" "yunion.io/x/onecloud/pkg/compute/options" "yunion.io/x/onecloud/pkg/httperrors" @@ -1125,6 +1126,23 @@ func (manager *SCloudregionManager) GetPropertySyncTasks(ctx context.Context, us return GetPropertySkusSyncTasks(ctx, userCred, query) } +func (self *SCloudregion) AllowSyncImages(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject) bool { + return db.IsAdminAllowPerform(userCred, self, "sync-images") +} + +func (self *SCloudregion) PerformSyncImages(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.SyncImagesInput) (jsonutils.JSONObject, error) { + return nil, self.StartSyncImagesTask(ctx, userCred, "") +} + +func (self *SCloudregion) StartSyncImagesTask(ctx context.Context, userCred mcclient.TokenCredential, parentId string) error { + task, err := taskman.TaskManager.NewTask(ctx, "CloudregionSyncImagesTask", self, userCred, nil, "", "", nil) + if err != nil { + return errors.Wrapf(err, "NewTask") + } + task.ScheduleRun(nil) + return nil +} + func (self *SCloudregion) GetCloudprovider() (*SCloudprovider, error) { if len(self.ManagerId) == 0 { return nil, sql.ErrNoRows diff --git a/pkg/compute/tasks/cloudregion_sync_images_task.go b/pkg/compute/tasks/cloudregion_sync_images_task.go new file mode 100644 index 0000000000..4f99baa1fa --- /dev/null +++ b/pkg/compute/tasks/cloudregion_sync_images_task.go @@ -0,0 +1,53 @@ +// 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 tasks + +import ( + "context" + + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" + + "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 CloudregionSyncImagesTask struct { + taskman.STask +} + +func init() { + taskman.RegisterTask(CloudregionSyncImagesTask{}) +} + +func (self *CloudregionSyncImagesTask) taskFailed(ctx context.Context, region *models.SCloudregion, err error) { + db.OpsLog.LogEvent(region, db.ACT_SYNC_CLOUD_IMAGES, err, self.GetUserCred()) + logclient.AddActionLogWithStartable(self, region, logclient.ACT_CLOUD_SYNC, err, self.UserCred, false) + self.SetStageFailed(ctx, jsonutils.NewString(err.Error())) +} + +func (self *CloudregionSyncImagesTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) { + region := obj.(*models.SCloudregion) + + err := region.SyncCloudImages(ctx, self.GetUserCred(), true) + if err != nil { + self.taskFailed(ctx, region, errors.Wrapf(err, "SyncCloudImages")) + return + } + + self.SetStageComplete(ctx, nil) +} diff --git a/pkg/mcclient/options/skus.go b/pkg/mcclient/options/skus.go index 6e40372dcb..de9466f3f7 100644 --- a/pkg/mcclient/options/skus.go +++ b/pkg/mcclient/options/skus.go @@ -42,3 +42,15 @@ type CloudregionSkuSyncOptions struct { func (opts *CloudregionSkuSyncOptions) Params() (jsonutils.JSONObject, error) { return jsonutils.Marshal(opts), nil } + +type CloudregionIdOptions struct { + ID string `help:"Cloudregion Id"` +} + +func (opts *CloudregionIdOptions) GetId() string { + return opts.ID +} + +func (opts *CloudregionIdOptions) Params() (jsonutils.JSONObject, error) { + return nil, nil +}