mirror of
https://github.com/mudler/luet.git
synced 2025-09-04 16:50:50 +00:00
Move finalizer in its own struct
Also distinguish when we run in a root target dir ("/") or when we want to run the commands in a separate folder
This commit is contained in:
76
pkg/installer/finalizer.go
Normal file
76
pkg/installer/finalizer.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
// Copyright © 2019 Ettore Di Giacinto <mudler@gentoo.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 installer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
|
"github.com/ghodss/yaml"
|
||||||
|
box "github.com/mudler/luet/pkg/box"
|
||||||
|
. "github.com/mudler/luet/pkg/logger"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LuetFinalizer struct {
|
||||||
|
Install []string `json:"install"`
|
||||||
|
Uninstall []string `json:"uninstall"` // TODO: Where to store?
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *LuetFinalizer) RunInstall(s *System) error {
|
||||||
|
for _, c := range f.Install {
|
||||||
|
if s.Target == "/" {
|
||||||
|
|
||||||
|
Info("finalizer on / :", "sh", "-c", c)
|
||||||
|
cmd := exec.Command("sh", "-c", c)
|
||||||
|
stdoutStderr, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
||||||
|
}
|
||||||
|
Info(string(stdoutStderr))
|
||||||
|
} else {
|
||||||
|
b := box.NewBox("sh", []string{"-c", c}, s.Target, false, true, true)
|
||||||
|
err := b.Run()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed running command ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: We don't store uninstall finalizers ?!
|
||||||
|
func (f *LuetFinalizer) RunUnInstall() error {
|
||||||
|
for _, c := range f.Uninstall {
|
||||||
|
Debug("finalizer:", "sh", "-c", c)
|
||||||
|
cmd := exec.Command("sh", "-c", c)
|
||||||
|
stdoutStderr, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
||||||
|
}
|
||||||
|
Info(string(stdoutStderr))
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
@@ -18,13 +18,11 @@ package installer
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/ghodss/yaml"
|
|
||||||
compiler "github.com/mudler/luet/pkg/compiler"
|
compiler "github.com/mudler/luet/pkg/compiler"
|
||||||
"github.com/mudler/luet/pkg/config"
|
"github.com/mudler/luet/pkg/config"
|
||||||
"github.com/mudler/luet/pkg/helpers"
|
"github.com/mudler/luet/pkg/helpers"
|
||||||
@@ -57,47 +55,6 @@ type ArtifactMatch struct {
|
|||||||
Repository Repository
|
Repository Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
type LuetFinalizer struct {
|
|
||||||
Install []string `json:"install"`
|
|
||||||
Uninstall []string `json:"uninstall"` // TODO: Where to store?
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *LuetFinalizer) RunInstall() error {
|
|
||||||
for _, c := range f.Install {
|
|
||||||
Debug("finalizer:", "sh", "-c", c)
|
|
||||||
cmd := exec.Command("sh", "-c", c)
|
|
||||||
stdoutStderr, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
|
||||||
}
|
|
||||||
Info(string(stdoutStderr))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: We don't store uninstall finalizers ?!
|
|
||||||
func (f *LuetFinalizer) RunUnInstall() error {
|
|
||||||
for _, c := range f.Install {
|
|
||||||
Debug("finalizer:", "sh", "-c", c)
|
|
||||||
cmd := exec.Command("sh", "-c", c)
|
|
||||||
stdoutStderr, err := cmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "Failed running command: "+string(stdoutStderr))
|
|
||||||
}
|
|
||||||
Info(string(stdoutStderr))
|
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewLuetInstaller(opts LuetInstallerOptions) Installer {
|
func NewLuetInstaller(opts LuetInstallerOptions) Installer {
|
||||||
return &LuetInstaller{Options: opts}
|
return &LuetInstaller{Options: opts}
|
||||||
}
|
}
|
||||||
@@ -368,6 +325,8 @@ func (l *LuetInstaller) install(syncedRepos Repositories, cp []pkg.Package, s *S
|
|||||||
if !l.Options.NoDeps {
|
if !l.Options.NoDeps {
|
||||||
// TODO: Lower those errors as warning
|
// TODO: Lower those errors as warning
|
||||||
for _, w := range p {
|
for _, w := range p {
|
||||||
|
Info("Getting finalizer for " + w.HumanReadableString())
|
||||||
|
|
||||||
// Finalizers needs to run in order and in sequence.
|
// Finalizers needs to run in order and in sequence.
|
||||||
ordered := solution.Order(allRepos, w.GetFingerPrint())
|
ordered := solution.Order(allRepos, w.GetFingerPrint())
|
||||||
ORDER:
|
ORDER:
|
||||||
@@ -395,13 +354,14 @@ func (l *LuetInstaller) install(syncedRepos Repositories, cp []pkg.Package, s *S
|
|||||||
if err != nil && !l.Options.Force {
|
if err != nil && !l.Options.Force {
|
||||||
return errors.Wrap(err, "Error reading finalizer "+treePackage.Rel(tree.FinalizerFile))
|
return errors.Wrap(err, "Error reading finalizer "+treePackage.Rel(tree.FinalizerFile))
|
||||||
}
|
}
|
||||||
err = finalizer.RunInstall()
|
err = finalizer.RunInstall(s)
|
||||||
if err != nil && !l.Options.Force {
|
if err != nil && !l.Options.Force {
|
||||||
return errors.Wrap(err, "Error executing install finalizer "+treePackage.Rel(tree.FinalizerFile))
|
return errors.Wrap(err, "Error executing install finalizer "+treePackage.Rel(tree.FinalizerFile))
|
||||||
}
|
}
|
||||||
executedFinalizer[ass.Package.GetFingerPrint()] = true
|
executedFinalizer[ass.Package.GetFingerPrint()] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,7 +383,7 @@ func (l *LuetInstaller) install(syncedRepos Repositories, cp []pkg.Package, s *S
|
|||||||
if err != nil && !l.Options.Force {
|
if err != nil && !l.Options.Force {
|
||||||
return errors.Wrap(err, "Error reading finalizer "+treePackage.Rel(tree.FinalizerFile))
|
return errors.Wrap(err, "Error reading finalizer "+treePackage.Rel(tree.FinalizerFile))
|
||||||
}
|
}
|
||||||
err = finalizer.RunInstall()
|
err = finalizer.RunInstall(s)
|
||||||
if err != nil && !l.Options.Force {
|
if err != nil && !l.Options.Force {
|
||||||
return errors.Wrap(err, "Error executing install finalizer "+treePackage.Rel(tree.FinalizerFile))
|
return errors.Wrap(err, "Error executing install finalizer "+treePackage.Rel(tree.FinalizerFile))
|
||||||
}
|
}
|
||||||
@@ -432,6 +392,7 @@ func (l *LuetInstaller) install(syncedRepos Repositories, cp []pkg.Package, s *S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user