Example #1
0
func WriteVersion(da *model.DesiredAgent) (err error) {
	if CurrentUser == da.RunUser {
		file.WriteString(path.Join(da.AgentDir, ".version"), da.Version)
	} else {
		file.WriteString(path.Join(file.SelfDir(), ".version"), da.Version)

		_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo mv .version %s/", da.AgentDir))
		if nil != err {
			return
		}
		_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo chown -R %s:%s %s", da.RunUser, da.RunUser, path.Join(da.AgentDir, ".version")))
	}
	return
}
Example #2
0
func init() {
	_, err := utils.ExecuteCommand(g.SelfDir, "sudo -n true")
	if nil == err {
		HasSudoPermission = true
	}

	CurrentUser = utils.GetUserByPid(os.Getpid())
}
Example #3
0
func InsureRunUserExists(da *model.DesiredAgent) error {
	if CurrentUser == da.RunUser { //ops-updater和 Agent运行用户一致
		return nil
	} else if HasSudoPermission {
		if utils.CheckUserExists(da.RunUser) {
			return nil
		}
		_, err := utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo useradd %s", da.RunUser))
		return err
	}
	return fmt.Errorf("you donot have permission to insure user %s", da.RunUser)
}
Example #4
0
func InsureUserDir(fp, username string, createByRoot bool) error {
	var err error
	if CheckDirectoryExists(username, fp) {
		return nil
	}

	if CurrentUser == username {
		return os.MkdirAll(fp, os.ModePerm)
	} else if HasSudoPermission {
		if createByRoot {
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo mkdir -p %s", fp))
			if nil != err {
				return err
			}
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo chown -R %s:%s %s", username, username, fp))
		} else {
			_, err = utils.ExecuteCommand(file.SelfDir(), fmt.Sprintf("sudo -u %s mkdir -p %s", username, fp))
		}
	}
	return err
}