From 66df0b471ca4be0dd1db8a82952fef8d06780413 Mon Sep 17 00:00:00 2001 From: wanyaoqi <18528551+wanyaoqi@users.noreply.github.com> Date: Tue, 25 Jun 2024 10:57:09 +0800 Subject: [PATCH] Automated cherry pick of #20595: Automated cherry pick of #20594: Fix/deploy reset password support loginaccount (#20596) * fix(host): lvm resize add encrypt support * fix(region): deploy reset password add loginaccount --- pkg/apis/compute/guests.go | 2 ++ pkg/compute/models/guest_actions.go | 21 +++++++++++++++++++++ pkg/hostman/storageman/disk_lvm.go | 20 +++++++++++++++++--- pkg/mcclient/options/compute/servers.go | 3 +++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/pkg/apis/compute/guests.go b/pkg/apis/compute/guests.go index fd9e6362da..ab6a426a85 100644 --- a/pkg/apis/compute/guests.go +++ b/pkg/apis/compute/guests.go @@ -753,6 +753,8 @@ type ServerDeployInputBase struct { // 支持特殊user data平台: Aliyun, Qcloud, Azure, Apsara, Ucloud // required: false UserData string `json:"user_data"` + // swagger: ignore + LoginAccount string `json:"login_account"` // swagger: ignore Restart bool `json:"restart"` diff --git a/pkg/compute/models/guest_actions.go b/pkg/compute/models/guest_actions.go index fe5fac62c6..3fcc5c236d 100644 --- a/pkg/compute/models/guest_actions.go +++ b/pkg/compute/models/guest_actions.go @@ -779,6 +779,9 @@ func (self *SGuest) PerformSetPassword(ctx context.Context, userCred mcclient.To inputDeploy.AutoStart = input.AutoStart inputDeploy.Password = input.Password inputDeploy.ResetPassword = input.ResetPassword + if input.Username != "" { + inputDeploy.LoginAccount = input.Username + } return self.PerformDeploy(ctx, userCred, query, inputDeploy) } } @@ -861,6 +864,14 @@ func (self *SGuest) PerformDeploy( doRestart := false if input.ResetPassword { doRestart = driver.IsNeedRestartForResetLoginInfo() + if len(input.LoginAccount) > 0 { + if len(input.LoginAccount) > 32 { + return nil, httperrors.NewInputParameterError("login_account is longer than 32 chars") + } + if err := GuestManager.ValidateNameLoginAccount(input.LoginAccount); err != nil { + return nil, err + } + } } deployStatus, err := driver.GetDeployStatus() @@ -1806,6 +1817,16 @@ func (self *SGuest) PerformRebuildRoot( } input.ResetPassword = resetPasswd + if input.ResetPassword { + if len(input.LoginAccount) > 0 { + if len(input.LoginAccount) > 32 { + return nil, httperrors.NewInputParameterError("login_account is longer than 32 chars") + } + if err := GuestManager.ValidateNameLoginAccount(input.LoginAccount); err != nil { + return nil, err + } + } + } return nil, self.StartRebuildRootTask(ctx, userCred, input.ImageId, needStop, autoStart, allDisks, &input.ServerDeployInputBase) } diff --git a/pkg/hostman/storageman/disk_lvm.go b/pkg/hostman/storageman/disk_lvm.go index ed8afa1555..17b36b052f 100644 --- a/pkg/hostman/storageman/disk_lvm.go +++ b/pkg/hostman/storageman/disk_lvm.go @@ -276,14 +276,28 @@ func (d *SLVMDisk) Resize(ctx context.Context, params interface{}) (jsonutils.JS if err != nil { return nil, errors.Wrap(err, "lv resize") } + + resizeFsInfo := &deployapi.DiskInfo{ + Path: d.GetPath(), + } + if diskInfo.Contains("encrypt_info") { + var encryptInfo apis.SEncryptInfo + err := diskInfo.Unmarshal(&encryptInfo, "encrypt_info") + if err != nil { + log.Errorf("fail to fetch encryptInfo %s", err) + return nil, errors.Wrap(err, "Unmarshal encrpt_info") + } else { + qemuImg.SetPassword(encryptInfo.Key) + resizeFsInfo.EncryptPassword = encryptInfo.Key + resizeFsInfo.EncryptAlg = string(encryptInfo.Alg) + } + } + err = qemuImg.Resize(int(sizeMb)) if err != nil { return nil, errors.Wrap(err, "qemuImg resize") } - resizeFsInfo := &deployapi.DiskInfo{ - Path: d.GetPath(), - } if err := d.ResizeFs(resizeFsInfo); err != nil { log.Errorf("Resize fs %s fail %s", d.GetPath(), err) } diff --git a/pkg/mcclient/options/compute/servers.go b/pkg/mcclient/options/compute/servers.go index fb28590be7..197724abe9 100644 --- a/pkg/mcclient/options/compute/servers.go +++ b/pkg/mcclient/options/compute/servers.go @@ -744,6 +744,7 @@ type ServerDeployOptions struct { Deploy []string `help:"Specify deploy files in virtual server file system" json:"-"` ResetPassword bool `help:"Force reset password"` Password string `help:"Default user password"` + LoginAccount string `help:"Guest login account"` AutoStart bool `help:"Auto start server after deployed"` DeployTelegraf bool `help:"Deploy telegraf if guest os supported"` } @@ -760,6 +761,7 @@ func (opts *ServerDeployOptions) Params() (jsonutils.JSONObject, error) { params.ResetPassword = opts.ResetPassword params.Password = opts.Password params.DeployTelegraf = opts.DeployTelegraf + params.LoginAccount = opts.LoginAccount } { deployInfos, err := ParseServerDeployInfoList(opts.Deploy) @@ -992,6 +994,7 @@ type ServerRebuildRootOptions struct { ImageId string `help:"New root Image template ID" json:"image_id" token:"image"` Keypair string `help:"ssh Keypair used for login"` Password string `help:"Default user password"` + LoginAccount string `help:"Guest login account"` NoAccountInit *bool `help:"Not reset account password"` AutoStart *bool `help:"Auto start server after it is created"` AllDisks *bool `help:"Rebuild all disks including data disks"`