mirror of
https://github.com/mudler/luet.git
synced 2025-04-27 19:25:15 +00:00
Add luet box command
It adds only the 'exec' subcommand to spawn processes in custom boxes Relates to #45
This commit is contained in:
parent
a14f0abb5c
commit
1d5ce53443
36
cmd/box.go
Normal file
36
cmd/box.go
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||
// Daniele Rondina <geaaru@sabayonlinux.org>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
. "github.com/mudler/luet/cmd/box"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var boxGroupCmd = &cobra.Command{
|
||||
Use: "box [command] [OPTIONS]",
|
||||
Short: "Manage luet boxes",
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(boxGroupCmd)
|
||||
|
||||
boxGroupCmd.AddCommand(
|
||||
NewBoxExecCommand(),
|
||||
)
|
||||
}
|
84
cmd/box/exec.go
Normal file
84
cmd/box/exec.go
Normal file
@ -0,0 +1,84 @@
|
||||
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.org>
|
||||
// Daniele Rondina <geaaru@sabayonlinux.org>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package cmd_box
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
b64 "encoding/base64"
|
||||
|
||||
"github.com/mudler/luet/pkg/box"
|
||||
. "github.com/mudler/luet/pkg/logger"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func NewBoxExecCommand() *cobra.Command {
|
||||
var ans = &cobra.Command{
|
||||
Use: "exec [OPTIONS]",
|
||||
Short: "Execute a binary in a box",
|
||||
Args: cobra.OnlyValidArgs,
|
||||
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) {
|
||||
stdin := viper.GetBool("stdin")
|
||||
stdout := viper.GetBool("stdout")
|
||||
stderr := viper.GetBool("stderr")
|
||||
rootfs := viper.GetString("rootfs")
|
||||
base := viper.GetBool("decode")
|
||||
|
||||
entrypoint := viper.GetString("entrypoint")
|
||||
if base {
|
||||
var ss []string
|
||||
for _, a := range args {
|
||||
sDec, _ := b64.StdEncoding.DecodeString(a)
|
||||
ss = append(ss, string(sDec))
|
||||
}
|
||||
//If the command to run is complex,using base64 to avoid bad input
|
||||
|
||||
args = ss
|
||||
}
|
||||
Info("Executing", args, "in", rootfs)
|
||||
|
||||
b := box.NewBox(entrypoint, args, rootfs, stdin, stdout, stderr)
|
||||
err := b.Run()
|
||||
if err != nil {
|
||||
Fatal(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
Fatal(err)
|
||||
}
|
||||
ans.Flags().String("rootfs", path, "Rootfs path")
|
||||
ans.Flags().Bool("stdin", false, "Attach to stdin")
|
||||
ans.Flags().Bool("stdout", true, "Attach to stdout")
|
||||
ans.Flags().Bool("stderr", true, "Attach to stderr")
|
||||
ans.Flags().Bool("decode", false, "Base64 decode")
|
||||
|
||||
ans.Flags().String("entrypoint", "/bin/sh", "Entrypoint command (/bin/sh)")
|
||||
|
||||
return ans
|
||||
}
|
Loading…
Reference in New Issue
Block a user