2021-10-20 22:13:02 +00:00
|
|
|
// Copyright © 2019-2021 Ettore Di Giacinto <mudler@gentoo.org>
|
2020-03-28 15:57:40 +00:00
|
|
|
//
|
|
|
|
// 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 installer
|
|
|
|
|
|
|
|
import (
|
2021-04-08 08:22:21 +00:00
|
|
|
"os"
|
2020-03-28 15:57:40 +00:00
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
2021-10-20 22:13:02 +00:00
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
2020-03-28 15:57:40 +00:00
|
|
|
box "github.com/mudler/luet/pkg/box"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LuetFinalizer struct {
|
2020-04-14 15:46:34 +00:00
|
|
|
Shell []string `json:"shell"`
|
2020-03-28 15:57:40 +00:00
|
|
|
Install []string `json:"install"`
|
|
|
|
Uninstall []string `json:"uninstall"` // TODO: Where to store?
|
|
|
|
}
|
|
|
|
|
2021-10-20 22:13:02 +00:00
|
|
|
func (f *LuetFinalizer) RunInstall(ctx *types.Context, s *System) error {
|
2020-04-14 15:46:34 +00:00
|
|
|
var cmd string
|
|
|
|
var args []string
|
|
|
|
if len(f.Shell) == 0 {
|
|
|
|
// Default to sh otherwise
|
|
|
|
cmd = "sh"
|
|
|
|
args = []string{"-c"}
|
|
|
|
} else {
|
|
|
|
cmd = f.Shell[0]
|
|
|
|
if len(f.Shell) > 1 {
|
|
|
|
args = f.Shell[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:57:40 +00:00
|
|
|
for _, c := range f.Install {
|
2020-04-14 15:46:34 +00:00
|
|
|
toRun := append(args, c)
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Info(":shell: Executing finalizer on ", s.Target, cmd, toRun)
|
2021-04-08 08:22:21 +00:00
|
|
|
if s.Target == string(os.PathSeparator) {
|
2020-04-14 15:46:34 +00:00
|
|
|
cmd := exec.Command(cmd, toRun...)
|
2021-10-20 22:13:02 +00:00
|
|
|
cmd.Env = ctx.Config.GetFinalizerEnvs()
|
2020-03-28 15:57:40 +00:00
|
|
|
stdoutStderr, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Info(string(stdoutStderr))
|
2020-03-28 15:57:40 +00:00
|
|
|
} else {
|
2021-10-20 22:13:02 +00:00
|
|
|
b := box.NewBox(cmd, toRun, []string{}, ctx.Config.GetFinalizerEnvs(), s.Target, false, true, true)
|
2020-03-28 15:57:40 +00:00
|
|
|
err := b.Run()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed running command ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We don't store uninstall finalizers ?!
|
2021-10-20 22:13:02 +00:00
|
|
|
func (f *LuetFinalizer) RunUnInstall(ctx *types.Context) error {
|
2020-03-28 15:57:40 +00:00
|
|
|
for _, c := range f.Uninstall {
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Debug("finalizer:", "sh", "-c", c)
|
2020-03-28 15:57:40 +00:00
|
|
|
cmd := exec.Command("sh", "-c", c)
|
|
|
|
stdoutStderr, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
|
|
|
}
|
2021-10-20 22:13:02 +00:00
|
|
|
ctx.Info(string(stdoutStderr))
|
2020-03-28 15:57:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLuetFinalizerFromYaml(data []byte) (*LuetFinalizer, error) {
|
|
|
|
var p LuetFinalizer
|
|
|
|
err := yaml.Unmarshal(data, &p)
|
|
|
|
if err != nil {
|
|
|
|
return &p, err
|
|
|
|
}
|
|
|
|
return &p, err
|
|
|
|
}
|