feat(region): set password use same interface

Signed-off-by: wanyaoqi <d3lx.yq@gmail.com>
This commit is contained in:
wanyaoqi
2022-09-06 15:51:29 +08:00
parent a4e9f86c2a
commit 10bf6e574f
6 changed files with 57 additions and 8 deletions
+1
View File
@@ -109,6 +109,7 @@ func init() {
cmd.BatchPerform("enable-memclean", new(options.ServerIdsOptions))
cmd.Perform("qga-set-password", &options.ServerQgaSetPassword{})
cmd.Perform("qga-command", &options.ServerQgaCommand{})
cmd.Perform("set-password", &options.ServerSetPasswordOptions{})
cmd.Get("vnc", new(options.ServerIdOptions))
cmd.Get("desc", new(options.ServerIdOptions))
+9
View File
@@ -906,3 +906,12 @@ type ServerQgaSetPasswordInput struct {
type ServerQgaCommandInput struct {
Command string
}
type ServerSetPasswordInput struct {
Username string
Password string
// deploy params
ResetPassword bool
AutoStart bool
}
+31
View File
@@ -32,6 +32,7 @@ import (
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/tristate"
"yunion.io/x/pkg/util/fileutils"
"yunion.io/x/pkg/util/osprofile"
"yunion.io/x/pkg/util/regutils"
"yunion.io/x/pkg/util/sets"
"yunion.io/x/pkg/utils"
@@ -665,6 +666,36 @@ func (self *SGuest) GetDetailsCreateParams(ctx context.Context, userCred mcclien
return input.JSON(input), nil
}
func (self *SGuest) PerformSetPassword(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, input api.ServerSetPasswordInput) (jsonutils.JSONObject, error) {
switch self.Status {
case api.VM_RUNNING:
inputQga := &api.ServerQgaSetPasswordInput{
Username: input.Username,
Password: input.Password,
}
if inputQga.Username == "" {
if self.OsType == osprofile.OS_TYPE_WINDOWS {
inputQga.Username = api.VM_DEFAULT_WINDOWS_LOGIN_USER
} else {
inputQga.Username = api.VM_DEFAULT_LINUX_LOGIN_USER
}
}
if inputQga.Password == "" && input.ResetPassword {
inputQga.Password = seclib2.RandomPassword2(12)
}
return self.PerformQgaSetPassword(ctx, userCred, query, inputQga)
case api.VM_READY:
inputDeploy := api.ServerDeployInput{
Password: input.Password,
ResetPassword: input.ResetPassword,
AutoStart: input.AutoStart,
}
return self.PerformDeploy(ctx, userCred, query, inputDeploy)
default:
return nil, httperrors.NewServerStatusError("Cannot deploy in status %s", self.Status)
}
}
func (self *SGuest) saveOldPassword(ctx context.Context, userCred mcclient.TokenCredential) {
loginKey := self.GetMetadata(ctx, api.VM_METADATA_LOGIN_KEY, userCred)
if len(loginKey) > 0 {
@@ -59,7 +59,7 @@ func (self *GuestQgaSetPasswordTask) OnInit(ctx context.Context, obj db.IStandal
guest := obj.(*models.SGuest)
self.SetStage("OnQgaGuestPing", nil)
if err := self.guestPing(ctx, guest); err != nil {
self.OnQgaGuestPingFailed(ctx, guest, nil)
self.taskFailed(ctx, guest, err.Error())
}
}
+2 -7
View File
@@ -26,8 +26,8 @@ import (
)
const (
QGA_LOCK_TIMEOUT = time.Second * 10
QGA_EXEC_TIMEOUT = time.Second * 5
QGA_LOCK_TIMEOUT = time.Second * 5
QGA_EXEC_TIMEOUT = time.Second * 10
)
func qgaExec(timeout time.Duration, qgaFunc func(chan error)) error {
@@ -64,11 +64,6 @@ func (m *SGuestManager) QgaGuestSetPassword(ctx context.Context, params interfac
return nil, err
}
if guest.guestAgent.TryLock(QGA_LOCK_TIMEOUT) {
defer guest.guestAgent.Unlock()
} else {
return nil, errors.Wrap(err, "qga unfinished last cmd, is qga unavailable?")
}
f := func(c chan error) {
if guest.guestAgent.TryLock(QGA_LOCK_TIMEOUT) {
defer guest.guestAgent.Unlock()
+13
View File
@@ -820,6 +820,19 @@ func (o *ServerQgaCommand) Params() (jsonutils.JSONObject, error) {
return options.StructToParams(o)
}
type ServerSetPasswordOptions struct {
ServerIdOptions
Username string `help:"Which user to set password" json:"username"`
Password string `help:"Password content" json:"password"`
ResetPassword bool `help:"Force reset password"`
AutoStart bool `help:"Auto start server after reset password"`
}
func (o *ServerSetPasswordOptions) Params() (jsonutils.JSONObject, error) {
return options.StructToParams(o)
}
type ServerSaveImageOptions struct {
ServerIdOptions
IMAGE string `help:"Image name" json:"name"`