mirror of
https://github.com/mudler/luet.git
synced 2025-09-16 07:10:29 +00:00
Add support for env and hostmounts in box
This commit is contained in:
@@ -25,7 +25,6 @@ import (
|
|||||||
. "github.com/mudler/luet/pkg/logger"
|
. "github.com/mudler/luet/pkg/logger"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewBoxExecCommand() *cobra.Command {
|
func NewBoxExecCommand() *cobra.Command {
|
||||||
@@ -34,21 +33,17 @@ func NewBoxExecCommand() *cobra.Command {
|
|||||||
Short: "Execute a binary in a box",
|
Short: "Execute a binary in a box",
|
||||||
Args: cobra.OnlyValidArgs,
|
Args: cobra.OnlyValidArgs,
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
viper.BindPFlag("stdin", cmd.Flags().Lookup("stdin"))
|
|
||||||
viper.BindPFlag("stdout", cmd.Flags().Lookup("stdout"))
|
|
||||||
viper.BindPFlag("stderr", cmd.Flags().Lookup("stderr"))
|
|
||||||
viper.BindPFlag("rootfs", cmd.Flags().Lookup("rootfs"))
|
|
||||||
viper.BindPFlag("decode", cmd.Flags().Lookup("decode"))
|
|
||||||
viper.BindPFlag("entrypoint", cmd.Flags().Lookup("entrypoint"))
|
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
stdin := viper.GetBool("stdin")
|
stdin, _ := cmd.Flags().GetBool("stdin")
|
||||||
stdout := viper.GetBool("stdout")
|
stdout, _ := cmd.Flags().GetBool("stdout")
|
||||||
stderr := viper.GetBool("stderr")
|
stderr, _ := cmd.Flags().GetBool("stderr")
|
||||||
rootfs := viper.GetString("rootfs")
|
rootfs, _ := cmd.Flags().GetString("rootfs")
|
||||||
base := viper.GetBool("decode")
|
base, _ := cmd.Flags().GetBool("decode")
|
||||||
|
entrypoint, _ := cmd.Flags().GetString("entrypoint")
|
||||||
|
envs, _ := cmd.Flags().GetStringArray("env")
|
||||||
|
mounts, _ := cmd.Flags().GetStringArray("mount")
|
||||||
|
|
||||||
entrypoint := viper.GetString("entrypoint")
|
|
||||||
if base {
|
if base {
|
||||||
var ss []string
|
var ss []string
|
||||||
for _, a := range args {
|
for _, a := range args {
|
||||||
|
@@ -33,23 +33,25 @@ type Box interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DefaultBox struct {
|
type DefaultBox struct {
|
||||||
Name string
|
Name string
|
||||||
Root string
|
Root string
|
||||||
Env []string
|
Env []string
|
||||||
Cmd string
|
Cmd string
|
||||||
Args []string
|
Args []string
|
||||||
|
HostMounts []string
|
||||||
Stdin, Stdout, Stderr bool
|
Stdin, Stdout, Stderr bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBox(cmd string, args []string, rootfs string, stdin, stdout, stderr bool) Box {
|
func NewBox(cmd string, args, hostmounts, env []string, rootfs string, stdin, stdout, stderr bool) Box {
|
||||||
return &DefaultBox{
|
return &DefaultBox{
|
||||||
Stdin: stdin,
|
Stdin: stdin,
|
||||||
Stdout: stdout,
|
Stdout: stdout,
|
||||||
Stderr: stderr,
|
Stderr: stderr,
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
Args: args,
|
Args: args,
|
||||||
Root: rootfs,
|
Root: rootfs,
|
||||||
|
HostMounts: hostmounts,
|
||||||
|
Env: env,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +66,11 @@ func (b *DefaultBox) Exec() error {
|
|||||||
if err := PivotRoot(b.Root); err != nil {
|
if err := PivotRoot(b.Root); err != nil {
|
||||||
return errors.Wrap(err, "Failed switching pivot on rootfs")
|
return errors.Wrap(err, "Failed switching pivot on rootfs")
|
||||||
}
|
}
|
||||||
|
for _, hostMount := range b.HostMounts {
|
||||||
|
if err := mountBind(hostMount, b.Root); err != nil {
|
||||||
|
return errors.Wrap(err, fmt.Sprintf("Failed mounting %s on rootfs", hostMount))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cmd := exec.Command(b.Cmd, b.Args...)
|
cmd := exec.Command(b.Cmd, b.Args...)
|
||||||
|
|
||||||
@@ -82,7 +89,7 @@ func (b *DefaultBox) Exec() error {
|
|||||||
cmd.Env = b.Env
|
cmd.Env = b.Env
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return errors.Wrap(err, fmt.Sprintf("Error running the %s command", b.Cmd))
|
return errors.Wrap(err, fmt.Sprintf("Error running the %s command in box.Exec", b.Cmd))
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -111,6 +118,16 @@ func (b *DefaultBox) Run() error {
|
|||||||
// Encode the command in base64 to avoid bad input from the args given
|
// Encode the command in base64 to avoid bad input from the args given
|
||||||
execCmd = append(execCmd, "--decode")
|
execCmd = append(execCmd, "--decode")
|
||||||
|
|
||||||
|
for _, m := range b.HostMounts {
|
||||||
|
execCmd = append(execCmd, "--mount")
|
||||||
|
execCmd = append(execCmd, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, e := range b.Env {
|
||||||
|
execCmd = append(execCmd, "--env")
|
||||||
|
execCmd = append(execCmd, e)
|
||||||
|
}
|
||||||
|
|
||||||
for _, a := range b.Args {
|
for _, a := range b.Args {
|
||||||
execCmd = append(execCmd, b64.StdEncoding.EncodeToString([]byte(a)))
|
execCmd = append(execCmd, b64.StdEncoding.EncodeToString([]byte(a)))
|
||||||
}
|
}
|
||||||
@@ -151,7 +168,7 @@ func (b *DefaultBox) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return errors.Wrap(err, "Failed running Box command")
|
return errors.Wrap(err, "Failed running Box command in box.Run")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -75,10 +75,9 @@ func mountProc(newroot string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mountDev(newroot string) error {
|
func mountBind(hostfolder, newroot string) error {
|
||||||
|
source := hostfolder
|
||||||
source := "/dev"
|
target := filepath.Join(newroot, hostfolder)
|
||||||
target := filepath.Join(newroot, "/dev")
|
|
||||||
fstype := "bind"
|
fstype := "bind"
|
||||||
data := ""
|
data := ""
|
||||||
|
|
||||||
@@ -89,3 +88,7 @@ func mountDev(newroot string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mountDev(newroot string) error {
|
||||||
|
return mountBind("/dev", newroot)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user