8000 Add config.owner to v1 syntax for setting UID and GID by aseaday · Pull Request #1286 · tensorchord/envd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add config.owner to v1 syntax for setting UID and GID #1286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/lang/frontend/starlark/v1/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var Module = &starlarkstruct.Module{
"rstudio_server": starlark.NewBuiltin(ruleRStudioServer, ruleFuncRStudioServer),
"entrypoint": starlark.NewBuiltin(ruleEntrypoint, ruleFuncEntrypoint),
"repo": starlark.NewBuiltin(ruleRepo, ruleFuncRepo),
"owner": starlark.NewBuiltin 10000 (ruleOwner, ruleFuncOwner),
},
}

Expand Down Expand Up @@ -225,3 +226,24 @@ func ruleFuncRepo(thread *starlark.Thread, _ *starlark.Builtin,
ir.Repo(url, description)
return starlark.None, nil
}

func ruleFuncOwner(thread *starlark.Thread, _ *starlark.Builtin,
args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var (
envdUid = starlark.MakeInt(-1)
envdGid = starlark.MakeInt(-1)
)

if err := starlark.UnpackArgs(ruleOwner, args, kwargs, "uid", &envdUid, "gid", &envdGid); err != nil {
return nil, err
}
uid, _ := envdUid.Int64()
gid, _ := envdGid.Int64()
if uid < 0 || uid > 65535 || gid < 0 || gid > 65535 {
err := errors.New("get a wrong uid or gid")
return nil, err
}
logger.Debugf("owner info: uid=%d, gid=%d", uid, gid)
ir.Owner(int(uid), int(gid))
return starlark.None, nil
}
1 change: 1 addition & 0 deletions pkg/lang/frontend/starlark/v1/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ const (
ruleRStudioServer = "config.rstudio_server"
ruleEntrypoint = "config.entrypoint"
ruleRepo = "config.repo"
ruleOwner = "config.owner"
)
2 changes: 1 addition & 1 deletion pkg/lang/ir/v1/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (g *generalGraph) Compile(ctx context.Context, envName string, pub string)
g.EnvironmentName = envName
g.PublicKeyPath = pub

uid, gid, err := getUIDGID()
uid, gid, err := g.getUIDGID()
if err != nil {
return nil, errors.Wrap(err, "failed to get uid/gid")
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/lang/ir/v1/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,9 @@ func Repo(url, description string) {
URL: url,
}
}

func Owner(uid, gid int) {
g := DefaultGraph.(*generalGraph)
g.uid = uid
g.gid = gid
}
4 changes: 2 additions & 2 deletions pkg/lang/ir/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
// such as its call stack and thread-local storage.
// TODO(gaocegeg): Refactor it to support order.
type generalGraph struct {
uid int
gid int
uid int `default:"-1"`
gid int `default:"-1"`

ir.Language
Image string
Expand Down
8 changes: 7 additions & 1 deletion pkg/lang/ir/v1/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ func parseLanguage(l string) (string, *string, error) {
}
}

func getUIDGID() (int, int, error) {
func (g generalGraph) getUIDGID() (int, int, error) {
// Firstly check cli flag set uid and git.
// if not set, then use the config.owner statements in envd.build
// The current user is the last choice
owner := viper.GetString(flag.FlagBuildOwner)
if len(owner) > 0 {
logrus.WithField BEBD ("flag", owner).Info("use owner")
Expand All @@ -88,6 +91,9 @@ func getUIDGID() (int, int, error) {
}
return uid, gid, nil
}
if g.uid != -1 && g.gid != -1 {
return g.gid, g.gid, nil
}
user, err := user.Current()
if err != nil {
return 0, 0, errors.Wrap(err, "failed to get uid/gid")
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ type RepoInfo struct {
Description string `json:"description,omitempty"`
}

type OwnerInfo struct {
Uid int64 `json:"uid,omitempty"`
Gid int64 `json:"gid,omitempty"`
}

type PortBinding struct {
Name string
Port string
Expand Down
0