Skip to content

Commit

Permalink
General improvements (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbgf authored Nov 25, 2024
2 parents 7cc8f13 + fd665c3 commit 698eb74
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 40 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/host_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ qubesome host-run -profile <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
},
Expand Down
1 change: 1 addition & 0 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 21 additions & 6 deletions cmd/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -36,16 +38,29 @@ qubesome run -profile <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()),
Expand Down
3 changes: 0 additions & 3 deletions cmd/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
20 changes: 15 additions & 5 deletions cmd/cli/xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -28,12 +30,20 @@ qubesome xdg-open -profile <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),
Expand Down
2 changes: 2 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package env

import (
"fmt"
"log/slog"
"os"
)

Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/inception/inception.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inception

import (
"log/slog"
"os"

"github.com/qubesome/cli/internal/files"
Expand All @@ -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
}
7 changes: 0 additions & 7 deletions internal/profiles/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package profiles

import (
"github.com/qubesome/cli/internal/command"
"github.com/qubesome/cli/internal/types"
)

type Options struct {
Expand All @@ -11,7 +10,6 @@ type Options struct {
Local string
Profile string
Runner string
Config *types.Config
}

func WithGitURL(gitURL string) command.Option[Options] {
Expand All @@ -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) {
Expand Down
23 changes: 20 additions & 3 deletions internal/profiles/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -157,6 +167,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 {
Expand Down Expand Up @@ -462,6 +476,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)
}

Expand Down
10 changes: 1 addition & 9 deletions internal/qubesome/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions internal/runners/podman/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -237,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)
Expand Down
2 changes: 1 addition & 1 deletion internal/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 698eb74

Please sign in to comment.