From 1d5ce53443a25fca5ecfa5fb8e6eb10b3985c573 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sat, 18 Apr 2020 11:43:14 +0200 Subject: [PATCH] Add luet box command It adds only the 'exec' subcommand to spawn processes in custom boxes Relates to #45 --- cmd/box.go | 36 +++++++++++++++++++++ cmd/box/exec.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 cmd/box.go create mode 100644 cmd/box/exec.go diff --git a/cmd/box.go b/cmd/box.go new file mode 100644 index 00000000..32cf8417 --- /dev/null +++ b/cmd/box.go @@ -0,0 +1,36 @@ +// Copyright © 2019 Ettore Di Giacinto +// Daniele Rondina +// +// 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 . + +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(), + ) +} diff --git a/cmd/box/exec.go b/cmd/box/exec.go new file mode 100644 index 00000000..7f71e0b8 --- /dev/null +++ b/cmd/box/exec.go @@ -0,0 +1,84 @@ +// Copyright © 2019 Ettore Di Giacinto +// Daniele Rondina +// +// 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 . + +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 +}