Skip to content

Commit

Permalink
guest set login account
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed May 11, 2020
1 parent 0c45818 commit 8144d2d
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 141 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ type ServerCreateInput struct {
// requried: false
Password string `json:"password"`

// 登录账户
// required: false
LoginAccount string `json:"login_account"`

// 使用ISO光盘启动, 仅KVM平台支持
// required: false
Cdrom string `json:"cdrom"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/baremetal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2377,7 +2377,7 @@ func (s *SBaremetalServer) DoDeploy(term *ssh.Client, data jsonutils.JSONObject,
password = seclib.RandomPassword(12)
}
deployInfo := deployapi.NewDeployInfo(publicKey, deployapi.JsonDeploysToStructs(deploys),
password, isInit, true, o.Options.LinuxDefaultRootUser, o.Options.WindowsDefaultAdminUser, false)
password, isInit, true, o.Options.LinuxDefaultRootUser, o.Options.WindowsDefaultAdminUser, false, "")
return s.deployFs(term, deployInfo)
}

Expand Down
13 changes: 13 additions & 0 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,15 @@ func (manager *SGuestManager) validateCreateData(
input.ResetPassword = &resetPassword
}

if resetPassword && len(input.LoginAccount) > 0 {
if len(input.LoginAccount) > 32 {
return nil, httperrors.NewInputParameterError("login_account is longer than 32 chars")
}
if err := manager.ValidateName(input.LoginAccount); err != nil {
return nil, err
}
}

// check group
if input.InstanceGroupIds != nil && len(input.InstanceGroupIds) != 0 {
newGroupIds := make([]string, len(input.InstanceGroupIds))
Expand Down Expand Up @@ -3565,6 +3574,10 @@ func (self *SGuest) GetDeployConfigOnHost(ctx context.Context, userCred mcclient

config.Add(jsonutils.NewBool(jsonutils.QueryBoolean(params, "enable_cloud_init", false)), "enable_cloud_init")

if account, _ := params.GetString("login_account"); len(account) > 0 {
config.Set("login_account", jsonutils.NewString(account))
}

resetPasswd := jsonutils.QueryBoolean(params, "reset_password", true)
if deployAction == "create" && resetPasswd {
resetPasswd = self.isNeedDoResetPasswd()
Expand Down
8 changes: 6 additions & 2 deletions pkg/hostman/guestfs/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ func DoDeployGuestFs(rootfs fsdriver.IRootFsDriver, guestDesc *deployapi.GuestDe
}

if len(deployInfo.Password) > 0 {
if account := rootfs.GetLoginAccount(partition,
deployInfo.DefaultRootUser, deployInfo.WindowsDefaultAdminUser); len(account) > 0 {
account, err := rootfs.GetLoginAccount(partition, deployInfo.LoginAccount,
deployInfo.DefaultRootUser, deployInfo.WindowsDefaultAdminUser)
if err != nil {
return nil, errors.Wrap(err, "get login account")
}
if len(account) > 0 {
if err = rootfs.DeployPublicKey(partition, account, deployInfo.PublicKey); err != nil {
return nil, fmt.Errorf("DeployPublicKey: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/guestfs/fsdriver/android.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func (m *SAndroidRootFs) RootSignatures() []string {
}
}

func (m *SAndroidRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
return ""
func (m *SAndroidRootFs) GetLoginAccount(rootFs IDiskPartition, user string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
return "", nil
}

func (m *SAndroidRootFs) DeployPublicKey(rootfs IDiskPartition, uname string, pubkeys *deployapi.SSHKeys) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/guestfs/fsdriver/esxi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (m *SEsxiRootFs) RootSignatures() []string {
}
}

func (m *SEsxiRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
return "root"
func (m *SEsxiRootFs) GetLoginAccount(rootFs IDiskPartition, user string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
return "root", nil
}

func (m *SEsxiRootFs) GetOs() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hostman/guestfs/fsdriver/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type IRootFsDriver interface {
DeployStandbyNetworkingScripts(part IDiskPartition, nics, nicsStandby []*types.SServerNic) error
DeployUdevSubsystemScripts(IDiskPartition) error
DeployFstabScripts(IDiskPartition, []*deployapi.Disk) error
GetLoginAccount(IDiskPartition, bool, bool) string
GetLoginAccount(IDiskPartition, string, bool, bool) (string, error)
DeployPublicKey(IDiskPartition, string, *deployapi.SSHKeys) error
ChangeUserPasswd(part IDiskPartition, account, gid, publicKey, password string) (string, error)
DeployYunionroot(rootFs IDiskPartition, pubkeys *deployapi.SSHKeys, isInit bool, enableCloudInit bool) error
Expand Down
17 changes: 13 additions & 4 deletions pkg/hostman/guestfs/fsdriver/linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,16 @@ func (l *sLinuxRootFs) DeployHosts(rootFs IDiskPartition, hostname, domain strin
return rootFs.FilePutContents(etcHosts, hf.String(), false, false)
}

func (l *sLinuxRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
func (l *sLinuxRootFs) GetLoginAccount(rootFs IDiskPartition, sUser string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
if len(sUser) > 0 {
if err := rootFs.UserAdd(sUser, false); err != nil && !strings.Contains(err.Error(), "already exists") {
return "", fmt.Errorf("UserAdd %s: %v", sUser, err)
}
if err := l.EnableUserSudo(rootFs, sUser); err != nil {
return "", fmt.Errorf("EnableUserSudo: %s", err)
}
return sUser, nil
}
var selUsr string
if defaultRootUser && rootFs.Exists("/root", false) {
selUsr = ROOT_USER
Expand All @@ -110,7 +119,7 @@ func (l *sLinuxRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bo
selUsr = ROOT_USER
}
}
return selUsr
return selUsr, nil
}

func (l *sLinuxRootFs) ChangeUserPasswd(rootFs IDiskPartition, account, gid, publicKey, password string) (string, error) {
Expand Down Expand Up @@ -1607,8 +1616,8 @@ func (d *SCoreOsRootFs) ChangeUserPasswd(rootFs IDiskPartition, account, gid, pu
}
}

func (d *SCoreOsRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
return "core"
func (d *SCoreOsRootFs) GetLoginAccount(rootFs IDiskPartition, user string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
return "core", nil
}

func (d *SCoreOsRootFs) DeployFiles(deploys []*deployapi.DeployContent) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/guestfs/fsdriver/macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m *SMacOSRootFs) String() string {
return "MacOSRootFs"
}

func (m *SMacOSRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
func (m *SMacOSRootFs) GetLoginAccount(rootFs IDiskPartition, user string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
selUsr := ""
usrs := m.rootFs.ListDir("/Users", false)
if len(usrs) > 0 {
Expand All @@ -61,7 +61,7 @@ func (m *SMacOSRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bo
}
}
}
return selUsr
return selUsr, nil
}

func (m *SMacOSRootFs) RootSignatures() []string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/guestfs/fsdriver/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (w *SWindowsRootFs) GetReleaseInfo(IDiskPartition) *deployapi.ReleaseInfo {
}
}

func (w *SWindowsRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser bool, windowsDefaultAdminUser bool) string {
func (w *SWindowsRootFs) GetLoginAccount(rootFs IDiskPartition, sUser string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) {
confPath := w.rootFs.GetLocalPath("/windows/system32/config", true)
tool := winutils.NewWinRegTool(confPath)
tool.CheckPath()
Expand All @@ -134,7 +134,7 @@ func (w *SWindowsRootFs) GetLoginAccount(rootFs IDiskPartition, defaultRootUser
tool.UnlockUser(selUsr)
}
}
return selUsr
return selUsr, nil
}

func (w *SWindowsRootFs) IsWindows10() bool {
Expand Down
3 changes: 2 additions & 1 deletion pkg/hostman/guestman/guestman.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,11 @@ func (m *SGuestManager) startDeploy(
password = seclib.RandomPassword(12)
}
enableCloudInit := jsonutils.QueryBoolean(deployParams.Body, "enable_cloud_init", false)
loginAccount, _ := deployParams.Body.GetString("login_account")

guestInfo, err := guest.DeployFs(deployapi.NewDeployInfo(
publicKey, deployapi.JsonDeploysToStructs(deploys), password, deployParams.IsInit, false,
options.HostOptions.LinuxDefaultRootUser, options.HostOptions.WindowsDefaultAdminUser, enableCloudInit))
options.HostOptions.LinuxDefaultRootUser, options.HostOptions.WindowsDefaultAdminUser, enableCloudInit, loginAccount))
if err != nil {
return nil, errors.Wrap(err, "Deploy guest fs")
} else {
Expand Down

0 comments on commit 8144d2d

Please sign in to comment.