From 64ee7177dc57b7f15c9ba812eca18128e656a6fd Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Fri, 22 Nov 2024 17:40:50 +0000 Subject: [PATCH 1/4] inception: Ignore config and profile selection Signed-off-by: Paulo Gomes --- cmd/cli/run.go | 27 +++++++++++++++++++++------ cmd/cli/xdg.go | 20 +++++++++++++++----- internal/inception/inception.go | 6 +++++- internal/profiles/profiles.go | 7 +++++++ internal/runners/podman/run.go | 1 - internal/types/config.go | 2 +- 6 files changed, 49 insertions(+), 14 deletions(-) diff --git a/cmd/cli/run.go b/cmd/cli/run.go index 17ae559..535a61c 100644 --- a/cmd/cli/run.go +++ b/cmd/cli/run.go @@ -3,7 +3,9 @@ package cli import ( "context" + "github.com/qubesome/cli/internal/inception" "github.com/qubesome/cli/internal/qubesome" + "github.com/qubesome/cli/internal/types" "github.com/urfave/cli/v3" ) @@ -36,16 +38,29 @@ qubesome run -profile chrome - Run the chrome workload on a specif }, }, Action: func(ctx context.Context, cmd *cli.Command) error { - prof, err := profileOrActive(targetProfile) - if err != nil { - return err - } + var cfg *types.Config + + // Commands that can be executed from within a profile + // (a.k.a. inception mode) should not check for profile + // names nor configs, as those are imposed by the inception + // server. + if !inception.Inside() { + prof, err := profileOrActive(targetProfile) + if err != nil { + return err + } - cfg := profileConfigOrDefault(prof.Name) + targetProfile = prof.Name + cfg = profileConfigOrDefault(targetProfile) + + if runner == "" { + runner = prof.Runner + } + } return qubesome.Run( qubesome.WithWorkload(workload), - qubesome.WithProfile(prof.Name), + qubesome.WithProfile(targetProfile), qubesome.WithConfig(cfg), qubesome.WithRunner(runner), qubesome.WithExtraArgs(cmd.Args().Slice()), diff --git a/cmd/cli/xdg.go b/cmd/cli/xdg.go index f630f35..b2b4324 100644 --- a/cmd/cli/xdg.go +++ b/cmd/cli/xdg.go @@ -3,7 +3,9 @@ package cli import ( "context" + "github.com/qubesome/cli/internal/inception" "github.com/qubesome/cli/internal/qubesome" + "github.com/qubesome/cli/internal/types" "github.com/urfave/cli/v3" ) @@ -28,12 +30,20 @@ qubesome xdg-open -profile https://github.com/qubesome - Opens the }, }, Action: func(ctx context.Context, cmd *cli.Command) error { - prof, err := profileOrActive(targetProfile) - if err != nil { - return err - } + var cfg *types.Config + + // Commands that can be executed from within a profile + // (a.k.a. inception mode) should not check for profile + // names nor configs, as those are imposed by the inception + // server. + if !inception.Inside() { + prof, err := profileOrActive(targetProfile) + if err != nil { + return err + } - cfg := profileConfigOrDefault(prof.Name) + cfg = profileConfigOrDefault(prof.Name) + } return qubesome.XdgRun( qubesome.WithConfig(cfg), diff --git a/internal/inception/inception.go b/internal/inception/inception.go index 156f81f..170863a 100644 --- a/internal/inception/inception.go +++ b/internal/inception/inception.go @@ -1,6 +1,7 @@ package inception import ( + "log/slog" "os" "github.com/qubesome/cli/internal/files" @@ -9,5 +10,8 @@ import ( func Inside() bool { path := files.InProfileSocketPath() _, err := os.Stat(path) - return (err == nil) + inside := (err == nil) + + slog.Debug("inception check", "inside", inside) + return inside } diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.go index 3cd749b..a7123ce 100644 --- a/internal/profiles/profiles.go +++ b/internal/profiles/profiles.go @@ -157,6 +157,10 @@ func StartFromGit(runner, name, gitURL, path, local string) error { return fmt.Errorf("cannot file profile %q in config %q", name, cfgPath) } + if p.Runner != "" { + runner = p.Runner + } + // When sourcing from git, ensure profile path is relative to the git repository. pp, err := securejoin.SecureJoin(filepath.Dir(cfgPath), p.Path) if err != nil { @@ -462,6 +466,9 @@ func createNewDisplay(bin string, profile *types.Profile, display string) error dockerArgs = append(dockerArgs, "-v="+xdgRuntimeDir+":/run/user/1000") } if profile.HostAccess.Gpus != "" { + if strings.HasSuffix(bin, "podman") { + dockerArgs = append(dockerArgs, "--runtime=nvidia.com/gpu=all") + } dockerArgs = append(dockerArgs, "--gpus", profile.HostAccess.Gpus) } diff --git a/internal/runners/podman/run.go b/internal/runners/podman/run.go index 6d827fc..323ae5e 100644 --- a/internal/runners/podman/run.go +++ b/internal/runners/podman/run.go @@ -83,7 +83,6 @@ func Run(ew types.EffectiveWorkload) error { if wl.HostAccess.Gpus != "" { args = append(args, "--gpus", wl.HostAccess.Gpus) - args = append(args, "--runtime=nvidia") } for _, cap := range wl.HostAccess.CapsAdd { diff --git a/internal/types/config.go b/internal/types/config.go index 5a908a7..9df6fec 100644 --- a/internal/types/config.go +++ b/internal/types/config.go @@ -107,7 +107,7 @@ type Profile struct { // config is being consumed. When sourcing from git, it descends // from the git repository directory. Path string `yaml:"path"` - Runner string // TODO: Better name runner + Runner string `yaml:"runner"` // HostAccess defines all the access request which are allowed for // its workloads. From 29b88ea2437ba28250efb8788b52d80c13947b8b Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Fri, 22 Nov 2024 19:35:09 +0000 Subject: [PATCH 2/4] run: Simplify GITDIR sourcing Signed-off-by: Paulo Gomes --- internal/env/env.go | 2 ++ internal/qubesome/run.go | 10 +--------- internal/runners/podman/run.go | 4 ++-- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/internal/env/env.go b/internal/env/env.go index efe7e46..fc0f550 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -2,6 +2,7 @@ package env import ( "fmt" + "log/slog" "os" ) @@ -16,6 +17,7 @@ var mapping = map[string]string{ } func Update(k, v string) error { + slog.Debug("setting env", k, v) if _, ok := mapping[k]; ok { mapping[k] = v return nil diff --git a/internal/qubesome/run.go b/internal/qubesome/run.go index 69660cf..af1165e 100644 --- a/internal/qubesome/run.go +++ b/internal/qubesome/run.go @@ -91,15 +91,7 @@ func runner(in WorkloadInfo, runnerOverride string) error { return fmt.Errorf("profile %q does not exist", in.Profile) } - path := files.ProfileConfig(in.Profile) - target, err := os.Readlink(path) - if err != nil { - slog.Debug("not able find profile path", "path", path, "error", err) - return nil - } - - gitdir := filepath.Dir(filepath.Dir(target)) - err = env.Update("GITDIR", gitdir) + err := env.Update("GITDIR", in.Config.RootDir) if err != nil { return err } diff --git a/internal/runners/podman/run.go b/internal/runners/podman/run.go index 323ae5e..14b04ca 100644 --- a/internal/runners/podman/run.go +++ b/internal/runners/podman/run.go @@ -236,12 +236,12 @@ func Run(ew types.EffectiveWorkload) error { src := env.Expand(ps[0]) if _, err := os.Stat(src); err != nil { - slog.Warn("failed to mount path", "path", src, "error", err) + slog.Warn("failed to mount path", "path", src, "error", err, "state", ps[0]) continue } dst := ps[1] - args = append(args, fmt.Sprintf("-v=%s:%s:z", src, dst)) + args = append(args, fmt.Sprintf("-v=%s:%s", src, dst)) } args = append(args, wl.Image) From fcdd6521e7341116158460b1851d7fdb9c129618 Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Fri, 22 Nov 2024 20:05:11 +0000 Subject: [PATCH 3/4] start: Refactor Config loading Config loading needs to behave in different ways than other commands like xdg-open and run. Move it inside the start package instead. Signed-off-by: Paulo Gomes --- cmd/cli/root.go | 1 + cmd/cli/start.go | 3 --- internal/profiles/options.go | 7 ------- internal/profiles/profiles.go | 16 +++++++++++++--- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cmd/cli/root.go b/cmd/cli/root.go index 283bba4..4575a00 100644 --- a/cmd/cli/root.go +++ b/cmd/cli/root.go @@ -87,6 +87,7 @@ func profileConfigOrDefault(profile string) *types.Config { // Try to load the profile specific config. path := files.ProfileConfig(profile) target, err := os.Readlink(path) + slog.Debug("try to load profile config", "profile", profile, "path", path, target, "target") if err == nil { c := config(target) diff --git a/cmd/cli/start.go b/cmd/cli/start.go index 472fbe9..bee7e88 100644 --- a/cmd/cli/start.go +++ b/cmd/cli/start.go @@ -47,10 +47,7 @@ qubesome start -git https://github.com/qubesome/sample-dotfiles i3 }, }, Action: func(ctx context.Context, cmd *cli.Command) error { - cfg := profileConfigOrDefault(targetProfile) - return profiles.Run( - profiles.WithConfig(cfg), profiles.WithProfile(targetProfile), profiles.WithGitURL(gitURL), profiles.WithPath(path), diff --git a/internal/profiles/options.go b/internal/profiles/options.go index e5f00f2..548d114 100644 --- a/internal/profiles/options.go +++ b/internal/profiles/options.go @@ -2,7 +2,6 @@ package profiles import ( "github.com/qubesome/cli/internal/command" - "github.com/qubesome/cli/internal/types" ) type Options struct { @@ -11,7 +10,6 @@ type Options struct { Local string Profile string Runner string - Config *types.Config } func WithGitURL(gitURL string) command.Option[Options] { @@ -37,11 +35,6 @@ func WithProfile(profile string) command.Option[Options] { o.Profile = profile } } -func WithConfig(config *types.Config) command.Option[Options] { - return func(o *Options) { - o.Config = config - } -} func WithRunner(runner string) command.Option[Options] { return func(o *Options) { diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.go index a7123ce..8449c57 100644 --- a/internal/profiles/profiles.go +++ b/internal/profiles/profiles.go @@ -45,15 +45,25 @@ func Run(opts ...command.Option[Options]) error { return StartFromGit(o.Runner, o.Profile, o.GitURL, o.Path, o.Local) } - if o.Config == nil { + path := filepath.Join(o.Local, o.Path, "qubesome.config") + if _, err := os.Stat(path); err != nil { + return err + } + cfg, err := types.LoadConfig(path) + if err != nil { + return err + } + cfg.RootDir = filepath.Dir(path) + + if cfg == nil { return fmt.Errorf("cannot start profile: nil config") } - profile, ok := o.Config.Profile(o.Profile) + profile, ok := cfg.Profile(o.Profile) if !ok { return fmt.Errorf("cannot start profile: profile %q not found", o.Profile) } - return Start(o.Runner, profile, o.Config) + return Start(o.Runner, profile, cfg) } func validGitDir(path string) bool { From fd665c394a0580807dbfdc1bd8a1f230751d0a1b Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Fri, 22 Nov 2024 21:44:48 +0000 Subject: [PATCH 4/4] host-run: Convert output to string Signed-off-by: Paulo Gomes --- cmd/cli/host_run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/cli/host_run.go b/cmd/cli/host_run.go index 8b8b1c0..167309a 100644 --- a/cmd/cli/host_run.go +++ b/cmd/cli/host_run.go @@ -39,10 +39,10 @@ qubesome host-run -profile firefox - Run firefox on the host and d return err } - c := exec.Command(commandName) + c := exec.Command(commandName, cmd.Args().Slice()...) //nolint c.Env = append(c.Env, fmt.Sprintf("DISPLAY=:%d", prof.Display)) out, err := c.CombinedOutput() - fmt.Println(out) + fmt.Println(string(out)) return err },