host-deployer: customize user cloudroot home dir

This commit is contained in:
wanyaoqi
2020-11-16 20:44:06 +08:00
parent a3f1f77496
commit 6e66a129d6
9 changed files with 28 additions and 14 deletions

View File

@@ -64,7 +64,7 @@ func (s *BaremetalService) StartService() {
log.Infof("auth complete")
})
fsdriver.Init(nil)
fsdriver.Init(nil, "")
app := app_common.InitApp(&o.Options.BaseOptions, false)
common_options.StartOptionManager(&o.Options, o.Options.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange)

View File

@@ -69,7 +69,7 @@ func (s *SExsiAgentService) StartService() {
log.Infof("auth complete")
})
fsdriver.Init(nil)
fsdriver.Init(nil, "")
deployclient.Init(options.Options.DeployServerSocketPath)
hostutils.InitWorkerManagerWithCount(options.Options.HostDelayTaskWorkerCount)

View File

@@ -25,16 +25,17 @@ import (
type newRootFsDriverFunc func(part IDiskPartition) IRootFsDriver
var (
privatePrefixes []string
rootfsDrivers = make([]newRootFsDriverFunc, 0)
hostCpuArch string
privatePrefixes []string
rootfsDrivers = make([]newRootFsDriverFunc, 0)
hostCpuArch string
cloudrootDirectory string
)
func GetRootfsDrivers() []newRootFsDriverFunc {
return rootfsDrivers
}
func Init(initPrivatePrefixes []string) error {
func Init(initPrivatePrefixes []string, cloudrootDir string) error {
if len(initPrivatePrefixes) > 0 {
privatePrefixes = make([]string, len(initPrivatePrefixes))
copy(privatePrefixes, initPrivatePrefixes)
@@ -55,5 +56,6 @@ func Init(initPrivatePrefixes []string) error {
return errors.Wrap(err, "get cpu architecture")
}
hostCpuArch = strings.TrimSpace(string(cpuArch))
cloudrootDirectory = cloudrootDir
return nil
}

View File

@@ -34,7 +34,7 @@ type IDiskPartition interface {
Exists(sPath string, caseInsensitive bool) bool
Chown(sPath string, uid, gid int, caseInsensitive bool) error
Chmod(sPath string, mode uint32, caseInsensitive bool) error
UserAdd(user string, caseInsensitive bool) error
UserAdd(user, homeDir string, caseInsensitive bool) error
Stat(sPath string, caseInsensitive bool) os.FileInfo
Symlink(src, dst string, caseInsensitive bool) error

View File

@@ -94,7 +94,7 @@ func (l *sLinuxRootFs) DeployHosts(rootFs IDiskPartition, hostname, domain strin
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") {
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 {
@@ -154,10 +154,11 @@ func (l *sLinuxRootFs) DeployYunionroot(rootFs IDiskPartition, pubkeys *deployap
l.DisableCloudinit(rootFs)
}
var yunionroot = YUNIONROOT_USER
if err := rootFs.UserAdd(yunionroot, false); err != nil && !strings.Contains(err.Error(), "already exists") {
rootdir := path.Join(cloudrootDirectory, yunionroot)
if err := rootFs.UserAdd(yunionroot, cloudrootDirectory, false); err != nil && !strings.Contains(err.Error(), "already exists") {
log.Errorf("UserAdd %s: %v", yunionroot, err)
}
err := DeployAuthorizedKeys(rootFs, path.Join("/home", yunionroot), pubkeys, true)
err := DeployAuthorizedKeys(rootFs, rootdir, pubkeys, true)
if err != nil {
return fmt.Errorf("DeployAuthorizedKeys: %v", err)
}

View File

@@ -222,8 +222,15 @@ func (f *SLocalGuestFS) Chmod(sPath string, mode uint32, caseInsensitive bool) e
return nil
}
func (f *SLocalGuestFS) UserAdd(user string, caseInsensitive bool) error {
output, err := procutils.NewCommand("chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user).Output()
func (f *SLocalGuestFS) UserAdd(user, homeDir string, caseInsensitive bool) error {
if err := f.Mkdir(homeDir, 0755, false); err != nil {
return errors.Wrap(err, "Mkdir")
}
cmd := []string{"chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user}
if len(homeDir) > 0 {
cmd = append(cmd, "-d", path.Join(homeDir, user))
}
output, err := procutils.NewCommand(cmd[0], cmd[1:]...).Output()
if err != nil {
log.Errorf("Useradd fail: %s, %s", err, output)
return fmt.Errorf("%s", output)

View File

@@ -390,8 +390,11 @@ func (p *SSHPartition) Remove(sPath string, caseInsensitive bool) {
}
}
func (p *SSHPartition) UserAdd(user string, caseInsensitive bool) error {
func (p *SSHPartition) UserAdd(user, homeDir string, caseInsensitive bool) error {
cmd := fmt.Sprintf("/usr/sbin/chroot %s /usr/sbin/useradd -m -s /bin/bash %s", p.mountPath, user)
if len(homeDir) > 0 {
cmd += fmt.Sprintf(" -d %s", path.Join(homeDir, user))
}
_, err := p.term.Run(cmd)
return err
}

View File

@@ -382,7 +382,7 @@ func (s *SDeployService) InitService() {
if err := s.PrepareEnv(); err != nil {
log.Fatalln(err)
}
if err := fsdriver.Init(DeployOption.PrivatePrefixes); err != nil {
if err := fsdriver.Init(DeployOption.PrivatePrefixes, DeployOption.CloudrootDir); err != nil {
log.Fatalln(err)
}
s.O = &DeployOption.BaseOptions

View File

@@ -24,6 +24,7 @@ type SDeployOptions struct {
ChntpwPath string `help:"path to chntpw tool" default:"/usr/local/bin/chntpw.static"`
EnableRemoteExecutor bool `help:"Enable remote executor" default:"false"`
ExecSocketPath string `help:"Exec socket paht" default:"/var/run/exec.sock"`
CloudrootDir string `help:"User cloudroot home dir" default:"/opt"`
}
var DeployOption SDeployOptions