search for tag in /proc/*/environ to find the correct chroot
This commit is contained in:
parent
4e1009fae1
commit
ba80d2c83c
@ -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)
|
log.Error(err, "unable to run function command", "command", function.Spec.Command)
|
||||||
result = formolv1alpha1.Failure
|
result = formolv1alpha1.Failure
|
||||||
break
|
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)
|
log.Error(err, "unable to get function", "function", step.Name)
|
||||||
return reconcile.Result{}, err
|
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)
|
log.Error(err, "unable to run function command", "command", function.Spec.Command)
|
||||||
result = formolv1alpha1.Failure
|
result = formolv1alpha1.Failure
|
||||||
break
|
break
|
||||||
|
|||||||
@ -124,7 +124,7 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req reconcile.
|
|||||||
}
|
}
|
||||||
if len(restoreFunction.Spec.Command) > 1 {
|
if len(restoreFunction.Spec.Command) > 1 {
|
||||||
log.V(0).Info("Running the restore function", "name", restoreFunction.Name, "command", restoreFunction.Spec.Command)
|
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)
|
log.Error(err, "unable to run function command", "command", restoreFunction.Spec.Command)
|
||||||
result = formolv1alpha1.Failure
|
result = formolv1alpha1.Failure
|
||||||
break
|
break
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
"github.com/go-logr/zapr"
|
"github.com/go-logr/zapr"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -19,16 +21,6 @@ func init() {
|
|||||||
logger = zapr.NewLogger(zapLog)
|
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 {
|
func Run(runCmd string, args []string) error {
|
||||||
log := logger.WithValues("Run", runCmd, "Args", args)
|
log := logger.WithValues("Run", runCmd, "Args", args)
|
||||||
cmd := exec.Command(runCmd, args...)
|
cmd := exec.Command(runCmd, args...)
|
||||||
@ -41,9 +33,10 @@ func Run(runCmd string, args []string) error {
|
|||||||
return nil
|
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)
|
log := logger.WithValues("RunChroot", runCmd, "Args", args)
|
||||||
root := regexp.MustCompile(`/proc/[0-9]+/root`)
|
root := regexp.MustCompile(`/proc/[0-9]+/root`)
|
||||||
|
env := regexp.MustCompile(`/proc/[0-9]+/environ`)
|
||||||
pid := strconv.Itoa(os.Getpid())
|
pid := strconv.Itoa(os.Getpid())
|
||||||
skip := false
|
skip := false
|
||||||
if err := filepath.Walk("/proc", func(path string, info os.FileInfo, err error) error {
|
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) {
|
if info.IsDir() && (info.Name() == "1" || info.Name() == pid) {
|
||||||
return filepath.SkipDir
|
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 root.MatchString(path) {
|
||||||
if _, err := filepath.EvalSymlinks(path); err != nil {
|
if _, err := filepath.EvalSymlinks(path); err != nil {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
|
log.V(0).Info("running chroot in", "path", path)
|
||||||
cmd := exec.Command("chroot", append([]string{path, runCmd}, args...)...)
|
cmd := exec.Command("chroot", append([]string{path, runCmd}, args...)...)
|
||||||
output, err := cmd.CombinedOutput()
|
output, err := cmd.CombinedOutput()
|
||||||
log.V(0).Info("result", "output", string(output))
|
log.V(0).Info("result", "output", string(output))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user