From 63b7d9f58ed68a011ce8bd264112daa157d2a83f Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Fri, 22 Sep 2023 20:14:34 +0800 Subject: [PATCH] feat(region,glance): from cloudaccount to enable vmdk target format --- pkg/compute/models/cloudaccounts.go | 8 ++++++++ pkg/image/models/images.go | 8 ++++++++ pkg/image/options/options.go | 2 +- pkg/image/service/service.go | 25 +++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pkg/compute/models/cloudaccounts.go b/pkg/compute/models/cloudaccounts.go index 0fbf0efad9..28e18171eb 100644 --- a/pkg/compute/models/cloudaccounts.go +++ b/pkg/compute/models/cloudaccounts.go @@ -52,6 +52,7 @@ import ( "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/mcclient/modules/image" "yunion.io/x/onecloud/pkg/util/logclient" "yunion.io/x/onecloud/pkg/util/stringutils2" ) @@ -635,6 +636,13 @@ func (self *SCloudaccount) PostCreate(ctx context.Context, userCred mcclient.Tok } else { self.SubmitSyncAccountTask(ctx, userCred, nil) } + + if self.Brand == api.CLOUD_PROVIDER_VMWARE { + _, err := image.Images.PerformClassAction(auth.GetAdminSession(ctx, options.Options.Region), "vmware-account-added", nil) + if err != nil { + log.Errorf("failed inform glance vmware account added: %s", err) + } + } } func (self *SCloudaccount) savePassword(secret string) error { diff --git a/pkg/image/models/images.go b/pkg/image/models/images.go index c2221a626f..5d47bde025 100644 --- a/pkg/image/models/images.go +++ b/pkg/image/models/images.go @@ -1657,6 +1657,14 @@ func (img *SImage) PerformChangeOwner(ctx context.Context, userCred mcclient.Tok return ret, nil } +func (m *SImageManager) PerformVmwareAccountAdded(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input apis.PerformChangeProjectOwnerInput) (jsonutils.JSONObject, error) { + log.Infof("perform vmware account added") + if !utils.IsInStringArray(string(qemuimgfmt.VMDK), options.Options.TargetImageFormats) { + options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK)) + } + return nil, nil +} + /*func (image *SImage) getRealPath() string { diskPath := image.GetPath("") if !fileutils2.Exists(diskPath) { diff --git a/pkg/image/options/options.go b/pkg/image/options/options.go index 649f4981c3..bd1a155bc3 100644 --- a/pkg/image/options/options.go +++ b/pkg/image/options/options.go @@ -36,7 +36,7 @@ type SImageOptions struct { EnableTorrentService bool `help:"Enable torrent service" default:"false"` - TargetImageFormats []string `help:"target image formats that the system will automatically convert to" default:"qcow2,vmdk"` + TargetImageFormats []string `help:"target image formats that the system will automatically convert to" default:"qcow2"` TorrentClientPath string `help:"path to torrent executable" default:"/opt/yunion/bin/torrent"` diff --git a/pkg/image/service/service.go b/pkg/image/service/service.go index 81a430cb36..18c4e7c493 100644 --- a/pkg/image/service/service.go +++ b/pkg/image/service/service.go @@ -15,6 +15,7 @@ package service import ( + "context" "fmt" "os" "path/filepath" @@ -22,7 +23,10 @@ import ( "time" execlient "yunion.io/x/executor/client" + "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/util/qemuimgfmt" + "yunion.io/x/pkg/utils" _ "yunion.io/x/sqlchemy/backends" api "yunion.io/x/onecloud/pkg/apis/image" @@ -38,6 +42,8 @@ import ( _ "yunion.io/x/onecloud/pkg/image/policy" _ "yunion.io/x/onecloud/pkg/image/tasks" "yunion.io/x/onecloud/pkg/image/torrent" + "yunion.io/x/onecloud/pkg/mcclient/auth" + "yunion.io/x/onecloud/pkg/mcclient/modules/compute" "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/procutils" ) @@ -94,6 +100,14 @@ func StartService() { log.Infof("Auth complete!!") }) + if ok, err := hasVmwareAccount(); err != nil { + log.Errorf("failed get vmware cloudaccounts") + } else if ok { + if !utils.IsInStringArray(string(qemuimgfmt.VMDK), options.Options.TargetImageFormats) { + options.Options.TargetImageFormats = append(options.Options.TargetImageFormats, string(qemuimgfmt.VMDK)) + } + } + trackers := torrent.GetTrackers() if len(trackers) == 0 { log.Errorf("no valid torrent-tracker") @@ -150,6 +164,17 @@ func StartService() { }) } +func hasVmwareAccount() (bool, error) { + q := jsonutils.NewDict() + q.Add(jsonutils.NewString("system"), "scope") + q.Add(jsonutils.NewString("brand"), "VMware") + res, err := compute.Cloudaccounts.List(auth.GetAdminSession(context.Background(), options.Options.Region), q) + if err != nil { + return false, err + } + return res.Total > 0, nil +} + func initS3() { url := options.Options.S3Endpoint if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {