Skip to content

Commit

Permalink
Merge pull request #670 from ioito/hotfix/qx-struct-deploy-options
Browse files Browse the repository at this point in the history
fix: 结构化部署接口参数
  • Loading branch information
ioito authored Dec 27, 2023
2 parents d35740f + 5a81026 commit 4b8aef0
Show file tree
Hide file tree
Showing 31 changed files with 153 additions and 295 deletions.
10 changes: 10 additions & 0 deletions pkg/cloudprovider/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ type SManagedVMRebuildRootConfig struct {
PublicKey string
SysSizeGB int
OsType string
UserData string
}

func (vmConfig *SManagedVMCreateConfig) GetConfig(config *jsonutils.JSONDict) error {
Expand Down Expand Up @@ -360,3 +361,12 @@ type SInstanceUpdateOptions struct {
NAME string
Description string
}

type SInstanceDeployOptions struct {
Username string
Password string
PublicKey string
KeypairName string
DeleteKeypair bool
UserData string
}
2 changes: 1 addition & 1 deletion pkg/cloudprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ type ICloudVM interface {

RebuildRoot(ctx context.Context, config *SManagedVMRebuildRootConfig) (string, error)

DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error
DeployVM(ctx context.Context, opts *SInstanceDeployOptions) error

ChangeConfig(ctx context.Context, config *SManagedVMChangeConfig) error

Expand Down
50 changes: 16 additions & 34 deletions pkg/multicloud/aliyun/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,17 +459,8 @@ func (self *SInstance) UpdateVM(ctx context.Context, input cloudprovider.SInstan
return self.host.zone.region.UpdateVM(self.InstanceId, input, self.OSType)
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
var keypairName string
if len(publicKey) > 0 {
var err error
keypairName, err = self.host.zone.region.syncKeypair(publicKey)
if err != nil {
return err
}
}

return self.host.zone.region.DeployVM(self.InstanceId, name, password, keypairName, deleteKeypair, description)
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
return self.host.zone.region.DeployVM(self.InstanceId, opts)
}

func (self *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
Expand Down Expand Up @@ -829,50 +820,41 @@ func (self *SRegion) DeleteVM(instanceId string) error {
// }
}

func (self *SRegion) DeployVM(instanceId string, name string, password string, keypairName string, deleteKeypair bool, description string) error {
func (self *SRegion) DeployVM(instanceId string, opts *cloudprovider.SInstanceDeployOptions) error {
instance, err := self.GetInstance(instanceId)
if err != nil {
return err
}

// 修改密钥时直接返回
if deleteKeypair {
if opts.DeleteKeypair {
err = self.DetachKeyPair(instanceId, instance.KeyPairName)
if err != nil {
return err
}
}

if len(keypairName) > 0 {
var keypairName string
if len(opts.PublicKey) > 0 {
var err error
keypairName, err = self.syncKeypair(opts.PublicKey)
if err != nil {
return err
}
err = self.AttachKeypair(instanceId, keypairName)
if err != nil {
return err
}
}

params := make(map[string]string)

// if resetPassword {
// params["Password"] = seclib2.RandomPassword2(12)
// }
// 指定密码的情况下,使用指定的密码
if len(password) > 0 {
params["Password"] = password
}

if len(name) > 0 && instance.InstanceName != name {
params["InstanceName"] = name
}

if len(description) > 0 && instance.Description != description {
params["Description"] = description
}

if len(params) > 0 {
if len(opts.Password) > 0 {
params := make(map[string]string)
params["Password"] = opts.Password
return self.modifyInstanceAttribute(instanceId, params)
} else {
return nil
}

return nil
}

func (self *SInstance) DeleteVM(ctx context.Context) error {
Expand Down
8 changes: 2 additions & 6 deletions pkg/multicloud/aliyun/shell/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,13 @@ func init() {
*/
type InstanceDeployOptions struct {
ID string `help:"instance ID"`
Name string `help:"new instance name"`
Hostname string `help:"new hostname"`
Keypair string `help:"Keypair Name"`
PublicKey string `help:"Keypair Name"`
DeleteKeypair bool `help:"Remove SSH keypair"`
Password string `help:"new password"`
// ResetPassword bool `help:"Force reset password"`
Description string `help:"new instances description"`
}

shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *aliyun.SRegion, args *InstanceDeployOptions) error {
err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description)
err := cli.DeployVM(args.ID, &cloudprovider.SInstanceDeployOptions{DeleteKeypair: args.DeleteKeypair, Password: args.Password, PublicKey: args.PublicKey})
if err != nil {
return err
}
Expand Down
50 changes: 16 additions & 34 deletions pkg/multicloud/apsara/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,8 @@ func (self *SInstance) UpdateVM(ctx context.Context, input cloudprovider.SInstan
return self.host.zone.region.UpdateVM(self.InstanceId, input)
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
var keypairName string
if len(publicKey) > 0 {
var err error
keypairName, err = self.host.zone.region.syncKeypair(publicKey)
if err != nil {
return err
}
}

return self.host.zone.region.DeployVM(self.InstanceId, name, password, keypairName, deleteKeypair, description)
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
return self.host.zone.region.DeployVM(self.InstanceId, opts)
}

func (self *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SManagedVMRebuildRootConfig) (string, error) {
Expand Down Expand Up @@ -717,50 +708,41 @@ func (self *SRegion) DeleteVM(instanceId string) error {
// }
}

func (self *SRegion) DeployVM(instanceId string, name string, password string, keypairName string, deleteKeypair bool, description string) error {
func (self *SRegion) DeployVM(instanceId string, opts *cloudprovider.SInstanceDeployOptions) error {
instance, err := self.GetInstance(instanceId)
if err != nil {
return err
}

// 修改密钥时直接返回
if deleteKeypair {
if opts.DeleteKeypair {
err = self.DetachKeyPair(instanceId, instance.KeyPairName)
if err != nil {
return err
}
}

if len(keypairName) > 0 {
var keypairName string
if len(opts.PublicKey) > 0 {
var err error
keypairName, err = self.syncKeypair(opts.PublicKey)
if err != nil {
return err
}
err = self.AttachKeypair(instanceId, keypairName)
if err != nil {
return err
}
}

params := make(map[string]string)

// if resetPassword {
// params["Password"] = seclib2.RandomPassword2(12)
// }
// 指定密码的情况下,使用指定的密码
if len(password) > 0 {
params["Password"] = password
}

if len(name) > 0 && instance.InstanceName != name {
params["InstanceName"] = name
}

if len(description) > 0 && instance.Description != description {
params["Description"] = description
}

if len(params) > 0 {
if len(opts.Password) > 0 {
params := make(map[string]string)
params["Password"] = opts.Password
return self.modifyInstanceAttribute(instanceId, params)
} else {
return nil
}

return nil
}

func (self *SInstance) DeleteVM(ctx context.Context) error {
Expand Down
8 changes: 2 additions & 6 deletions pkg/multicloud/apsara/shell/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,13 @@ func init() {
*/
type InstanceDeployOptions struct {
ID string `help:"instance ID"`
Name string `help:"new instance name"`
Hostname string `help:"new hostname"`
Keypair string `help:"Keypair Name"`
PublicKey string `help:"Keypair Name"`
DeleteKeypair bool `help:"Remove SSH keypair"`
Password string `help:"new password"`
// ResetPassword bool `help:"Force reset password"`
Description string `help:"new instances description"`
}

shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *apsara.SRegion, args *InstanceDeployOptions) error {
err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description)
err := cli.DeployVM(args.ID, &cloudprovider.SInstanceDeployOptions{PublicKey: args.PublicKey, DeleteKeypair: args.DeleteKeypair, Password: args.Password})
if err != nil {
return err
}
Expand Down
21 changes: 2 additions & 19 deletions pkg/multicloud/aws/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,25 +609,8 @@ func (self *SInstance) RebuildRoot(ctx context.Context, desc *cloudprovider.SMan
return diskId, nil
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, desc string) error {
del := map[string]string{}
if name != self.GetName() {
del["Name"] = self.GetName()
}
if desc != self.GetDescription() {
del["Desription"] = self.GetDescription()
}
if len(del) > 0 {
self.host.zone.region.DeleteTags(self.InstanceId, del)
}
add := map[string]string{}
if len(name) > 0 {
add["Name"] = name
}
if len(desc) > 0 {
add["Description"] = desc
}
return self.host.zone.region.CreateTags(self.InstanceId, add)
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
return cloudprovider.ErrNotSupported
}

func (self *SInstance) ChangeConfig(ctx context.Context, config *cloudprovider.SManagedVMChangeConfig) error {
Expand Down
18 changes: 9 additions & 9 deletions pkg/multicloud/azure/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,15 +499,15 @@ func (region *SRegion) ChangeVMConfig(ctx context.Context, instanceId string, nc
return instacen.ChangeConfig(ctx, &cloudprovider.SManagedVMChangeConfig{Cpu: ncpu, MemoryMB: vmem})
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
if len(publicKey) > 0 || len(password) > 0 {
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
if len(opts.PublicKey) > 0 || len(opts.Password) > 0 {
// 先判断系统是否安装了vmAgent,然后等待扩展准备完成后再重置密码
err := self.WaitEnableVMAccessReady()
if err != nil {
return err
}
}
return self.host.zone.region.DeployVM(ctx, self.ID, string(self.GetOsType()), name, password, publicKey, deleteKeypair, description)
return self.host.zone.region.DeployVM(ctx, self.ID, string(self.GetOsType()), opts)
}

type VirtualMachineExtensionProperties struct {
Expand Down Expand Up @@ -655,19 +655,19 @@ func (region *SRegion) resetPassword(osType, instanceId, username, password stri
return region.resetLoginInfo(osType, instanceId, setting)
}

func (region *SRegion) DeployVM(ctx context.Context, instanceId, osType, name, password, publicKey string, deleteKeypair bool, description string) error {
func (region *SRegion) DeployVM(ctx context.Context, instanceId, osType string, opts *cloudprovider.SInstanceDeployOptions) error {
instance, err := region.GetInstance(instanceId)
if err != nil {
return err
}
if deleteKeypair {
if opts.DeleteKeypair {
return nil
}
if len(publicKey) > 0 {
return region.resetPublicKey(osType, instanceId, instance.Properties.OsProfile.AdminUsername, publicKey)
if len(opts.PublicKey) > 0 {
return region.resetPublicKey(osType, instanceId, instance.Properties.OsProfile.AdminUsername, opts.PublicKey)
}
if len(password) > 0 {
return region.resetPassword(osType, instanceId, instance.Properties.OsProfile.AdminUsername, password)
if len(opts.Password) > 0 {
return region.resetPassword(osType, instanceId, instance.Properties.OsProfile.AdminUsername, opts.Password)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/azure/shell/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func init() {
}

shellutils.R(&InstanceDeployOptions{}, "instance-reset-password", "Reset intance password", func(cli *azure.SRegion, args *InstanceDeployOptions) error {
return cli.DeployVM(context.Background(), args.ID, args.OsType, "", args.Password, args.PublicKey, false, "")
return cli.DeployVM(context.Background(), args.ID, args.OsType, &cloudprovider.SInstanceDeployOptions{Password: args.Password, PublicKey: args.PublicKey})
})

type InstanceSecurityGroupOptions struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/multicloud/bingocloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func (self *SInstance) ChangeConfig(ctx context.Context, config *cloudprovider.S
return cloudprovider.ErrNotImplemented
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
attrs := make(map[string]string)
if password != "" {
if opts.Password != "" {
attrs["InstanceAction"] = "ResetPassword"
}
return self.node.cluster.region.modifyInstanceAttribute(self.InstancesSet.InstanceId, attrs)
Expand Down
12 changes: 6 additions & 6 deletions pkg/multicloud/ctyun/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,18 +439,18 @@ func (self *SRegion) DetachKeypair(vmId, keyName string) error {
return err
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
if len(password) > 0 {
return self.host.zone.region.ResetVMPassword(self.GetId(), password)
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
if len(opts.Password) > 0 {
return self.host.zone.region.ResetVMPassword(self.GetId(), opts.Password)
}
if len(publicKey) > 0 {
keypair, err := self.host.zone.region.syncKeypair(publicKey)
if len(opts.PublicKey) > 0 {
keypair, err := self.host.zone.region.syncKeypair(opts.Password)
if err != nil {
return errors.Wrapf(err, "syncKeypair")
}
return self.host.zone.region.AttachKeypair(self.InstanceId, keypair.KeyPairName)
}
if deleteKeypair && len(self.KeypairName) > 0 {
if opts.DeleteKeypair && len(self.KeypairName) > 0 {
return self.host.zone.region.DetachKeypair(self.InstanceId, self.KeypairName)
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/ecloud/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (self *SInstance) RebuildRoot(ctx context.Context, config *cloudprovider.SM
return "", cloudprovider.ErrNotImplemented
}

func (self *SInstance) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
func (self *SInstance) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
return cloudprovider.ErrNotImplemented
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/multicloud/esxi/virtualmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (svm *SVirtualMachine) GetInstanceType() string {
return ""
}

func (svm *SVirtualMachine) DeployVM(ctx context.Context, name string, username string, password string, publicKey string, deleteKeypair bool, description string) error {
func (svm *SVirtualMachine) DeployVM(ctx context.Context, opts *cloudprovider.SInstanceDeployOptions) error {
return cloudprovider.ErrNotImplemented
}

Expand Down
Loading

0 comments on commit 4b8aef0

Please sign in to comment.