search for tag in /proc/*/environ to find the correct chroot

This commit is contained in:
jandre 2021-06-03 17:18:32 +02:00
parent 4e1009fae1
commit ba80d2c83c
3 changed files with 31 additions and 14 deletions

View File

@ -146,7 +146,7 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req reconcile.R
}
}
}
if err := formolcliutils.RunChroot(function.Spec.Command[0], function.Spec.Command[1:]...); err != nil {
if err := formolcliutils.RunChroot(target.ContainerName != "", function.Spec.Command[0], function.Spec.Command[1:]...); err != nil {
log.Error(err, "unable to run function command", "command", function.Spec.Command)
result = formolv1alpha1.Failure
break
@ -190,7 +190,7 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req reconcile.R
log.Error(err, "unable to get function", "function", step.Name)
return reconcile.Result{}, err
}
if err := formolcliutils.RunChroot(function.Spec.Command[0], function.Spec.Command[1:]...); err != nil {
if err := formolcliutils.RunChroot(target.ContainerName != "", function.Spec.Command[0], function.Spec.Command[1:]...); err != nil {
log.Error(err, "unable to run function command", "command", function.Spec.Command)
result = formolv1alpha1.Failure
break

View File

@ -124,7 +124,7 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req reconcile.
}
if len(restoreFunction.Spec.Command) > 1 {
log.V(0).Info("Running the restore function", "name", restoreFunction.Name, "command", restoreFunction.Spec.Command)
if err := formolcliutils.RunChroot(restoreFunction.Spec.Command[0], restoreFunction.Spec.Command[1:]...); err != nil {
if err := formolcliutils.RunChroot(currentTarget.ContainerName != "", restoreFunction.Spec.Command[0], restoreFunction.Spec.Command[1:]...); err != nil {
log.Error(err, "unable to run function command", "command", restoreFunction.Spec.Command)
result = formolv1alpha1.Failure
break

View File

@ -1,10 +1,12 @@
package utils
import (
"bytes"
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"go.uber.org/zap"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@ -19,16 +21,6 @@ func init() {
logger = zapr.NewLogger(zapLog)
}
func RunHooks(hooks []formolv1alpha1.Hook) error {
for _, hook := range hooks {
err := RunChroot(hook.Cmd, hook.Args...)
if err != nil {
return err
}
}
return nil
}
func Run(runCmd string, args []string) error {
log := logger.WithValues("Run", runCmd, "Args", args)
cmd := exec.Command(runCmd, args...)
@ -41,9 +33,10 @@ func Run(runCmd string, args []string) error {
return nil
}
func RunChroot(runCmd string, args ...string) error {
func RunChroot(lookForTag bool, runCmd string, args ...string) error {
log := logger.WithValues("RunChroot", runCmd, "Args", args)
root := regexp.MustCompile(`/proc/[0-9]+/root`)
env := regexp.MustCompile(`/proc/[0-9]+/environ`)
pid := strconv.Itoa(os.Getpid())
skip := false
if err := filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
@ -56,10 +49,34 @@ func RunChroot(runCmd string, args ...string) error {
if info.IsDir() && (info.Name() == "1" || info.Name() == pid) {
return filepath.SkipDir
}
if lookForTag && env.MatchString(path) {
log.V(0).Info("Looking for tag", "file", path)
content, err := ioutil.ReadFile(path)
if err != nil {
return filepath.SkipDir
}
var matched bool
for _, envVar := range bytes.Split(content, []byte{'\000'}) {
matched, err = regexp.Match(formolv1alpha1.TARGETCONTAINER_TAG, envVar)
if err != nil {
log.Error(err, "cannot regexp")
return err
}
if matched {
log.V(0).Info("Found the target tag", "file", path)
break
}
}
if matched == false {
return filepath.SkipDir
}
}
if root.MatchString(path) {
if _, err := filepath.EvalSymlinks(path); err != nil {
return filepath.SkipDir
}
log.V(0).Info("running chroot in", "path", path)
cmd := exec.Command("chroot", append([]string{path, runCmd}, args...)...)
output, err := cmd.CombinedOutput()
log.V(0).Info("result", "output", string(output))