mirror of
https://github.com/EnumeratedDev/bpm.git
synced 2026-09-16 10:36:12 +00:00
Improve hook system and add pre-transaction hooks
This commit is contained in:
+72
-16
@@ -662,6 +662,9 @@ func installPackages() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get files that will be modifie during this operation
|
||||||
|
operation.GetModifiedFiles()
|
||||||
|
|
||||||
if bpmlib.MainBPMConfig.ShowSourcePackageContents == "always" || bpmlib.MainBPMConfig.ShowSourcePackageContents == "install-only" {
|
if bpmlib.MainBPMConfig.ShowSourcePackageContents == "always" || bpmlib.MainBPMConfig.ShowSourcePackageContents == "install-only" {
|
||||||
// Show source package contents
|
// Show source package contents
|
||||||
sourcePackagesShown, err := operation.ShowSourcePackageContent()
|
sourcePackagesShown, err := operation.ShowSourcePackageContent()
|
||||||
@@ -684,6 +687,15 @@ func installPackages() {
|
|||||||
// Get optional dependencies
|
// Get optional dependencies
|
||||||
optionalDepends := operation.GetOptionalDependencies()
|
optionalDepends := operation.GetOptionalDependencies()
|
||||||
|
|
||||||
|
// Executing pre-operation hooks
|
||||||
|
fmt.Println("Running pre-operation hooks...")
|
||||||
|
err = operation.RunPreHooks(verbose)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: could not run pre-operation hooks: %s\n", err)
|
||||||
|
exitCode = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Execute operation
|
// Execute operation
|
||||||
err = operation.Execute(verbose, force)
|
err = operation.Execute(verbose, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -692,15 +704,17 @@ func installPackages() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executing hooks
|
// Executing post-operation hooks
|
||||||
fmt.Println("Running hooks...")
|
fmt.Println("Running post-operation hooks...")
|
||||||
err = operation.RunHooks(verbose)
|
err = operation.RunPostHooks(verbose)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: could not run hooks: %s\n", err)
|
log.Printf("Error: could not run post-operation hooks: %s\n", err)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Operation complete!")
|
||||||
|
|
||||||
// Show optional dependencies
|
// Show optional dependencies
|
||||||
if len(optionalDepends) != 0 {
|
if len(optionalDepends) != 0 {
|
||||||
// List optional dependencies
|
// List optional dependencies
|
||||||
@@ -801,6 +815,18 @@ func removePackages() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get files that will be modifie during this operation
|
||||||
|
operation.GetModifiedFiles()
|
||||||
|
|
||||||
|
// Executing pre-operation hooks
|
||||||
|
fmt.Println("Running pre-operation hooks...")
|
||||||
|
err = operation.RunPreHooks(verbose)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: could not run pre-operation hooks: %s\n", err)
|
||||||
|
exitCode = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Execute operation
|
// Execute operation
|
||||||
err = operation.Execute(verbose, force)
|
err = operation.Execute(verbose, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -809,14 +835,16 @@ func removePackages() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executing hooks
|
// Executing post-operation hooks
|
||||||
fmt.Println("Running hooks...")
|
fmt.Println("Running post-operation hooks...")
|
||||||
err = operation.RunHooks(verbose)
|
err = operation.RunPostHooks(verbose)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: could not run hooks: %s\n", err)
|
log.Printf("Error: could not run post-operation hooks: %s\n", err)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Operation complete!")
|
||||||
}
|
}
|
||||||
|
|
||||||
func doCleanup() {
|
func doCleanup() {
|
||||||
@@ -922,6 +950,18 @@ func doCleanup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get files that will be modifie during this operation
|
||||||
|
operation.GetModifiedFiles()
|
||||||
|
|
||||||
|
// Executing pre-operation hooks
|
||||||
|
fmt.Println("Running pre-operation hooks...")
|
||||||
|
err = operation.RunPreHooks(verbose)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: could not run pre-operation hooks: %s\n", err)
|
||||||
|
exitCode = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Execute operation
|
// Execute operation
|
||||||
err = operation.Execute(verbose, force)
|
err = operation.Execute(verbose, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -930,14 +970,16 @@ func doCleanup() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executing hooks
|
// Executing post-operation hooks
|
||||||
fmt.Println("Running hooks...")
|
fmt.Println("Running post-operation hooks...")
|
||||||
err = operation.RunHooks(verbose)
|
err = operation.RunPostHooks(verbose)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: could not run hooks: %s\n", err)
|
log.Printf("Error: could not run post-operation hooks: %s\n", err)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Operation complete!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1083,6 +1125,9 @@ func updatePackages() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get files that will be modifie during this operation
|
||||||
|
operation.GetModifiedFiles()
|
||||||
|
|
||||||
if bpmlib.MainBPMConfig.ShowSourcePackageContents == "always" {
|
if bpmlib.MainBPMConfig.ShowSourcePackageContents == "always" {
|
||||||
// Show source package contents
|
// Show source package contents
|
||||||
sourcePackagesShown, err := operation.ShowSourcePackageContent()
|
sourcePackagesShown, err := operation.ShowSourcePackageContent()
|
||||||
@@ -1105,6 +1150,15 @@ func updatePackages() {
|
|||||||
// Get optional dependencies
|
// Get optional dependencies
|
||||||
optionalDepends := operation.GetOptionalDependencies()
|
optionalDepends := operation.GetOptionalDependencies()
|
||||||
|
|
||||||
|
// Executing pre-operation hooks
|
||||||
|
fmt.Println("Running pre-operation hooks...")
|
||||||
|
err = operation.RunPreHooks(verbose)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error: could not run pre-operation hooks: %s\n", err)
|
||||||
|
exitCode = 1
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Execute operation
|
// Execute operation
|
||||||
err = operation.Execute(verbose, force)
|
err = operation.Execute(verbose, force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1113,15 +1167,17 @@ func updatePackages() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executing hooks
|
// Executing post-operation hooks
|
||||||
fmt.Println("Running hooks...")
|
fmt.Println("Running post-operation hooks...")
|
||||||
err = operation.RunHooks(verbose)
|
err = operation.RunPostHooks(verbose)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error: could not run hooks: %s\n", err)
|
log.Printf("Error: could not run post-operation hooks: %s\n", err)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println("Operation complete!")
|
||||||
|
|
||||||
// Show optional dependencies
|
// Show optional dependencies
|
||||||
if len(optionalDepends) != 0 {
|
if len(optionalDepends) != 0 {
|
||||||
// List optional dependencies
|
// List optional dependencies
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func InstallPackages(rootDir string, forceInstallationReason InstallationReason,
|
|||||||
operation = &BPMOperation{
|
operation = &BPMOperation{
|
||||||
Actions: make([]OperationAction, 0),
|
Actions: make([]OperationAction, 0),
|
||||||
UnresolvedDepends: make([]string, 0),
|
UnresolvedDepends: make([]string, 0),
|
||||||
Changes: make(map[string]string),
|
ModifiedFiles: make(map[string]string),
|
||||||
RunChecks: runChecks,
|
RunChecks: runChecks,
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
compiledPackages: make(map[string]string),
|
compiledPackages: make(map[string]string),
|
||||||
@@ -189,7 +189,7 @@ func RemovePackages(rootDir string, force, cleanupDependencies bool, packages ..
|
|||||||
operation = &BPMOperation{
|
operation = &BPMOperation{
|
||||||
Actions: make([]OperationAction, 0),
|
Actions: make([]OperationAction, 0),
|
||||||
UnresolvedDepends: make([]string, 0),
|
UnresolvedDepends: make([]string, 0),
|
||||||
Changes: make(map[string]string),
|
ModifiedFiles: make(map[string]string),
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
compiledPackages: make(map[string]string),
|
compiledPackages: make(map[string]string),
|
||||||
}
|
}
|
||||||
@@ -267,7 +267,7 @@ func CleanupPackages(cleanupMakeDepends bool, rootDir string) (operation *BPMOpe
|
|||||||
operation = &BPMOperation{
|
operation = &BPMOperation{
|
||||||
Actions: make([]OperationAction, 0),
|
Actions: make([]OperationAction, 0),
|
||||||
UnresolvedDepends: make([]string, 0),
|
UnresolvedDepends: make([]string, 0),
|
||||||
Changes: make(map[string]string),
|
ModifiedFiles: make(map[string]string),
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
compiledPackages: make(map[string]string),
|
compiledPackages: make(map[string]string),
|
||||||
}
|
}
|
||||||
@@ -395,7 +395,7 @@ func UpdatePackages(rootDir string, syncDatabase, allowDowngrades, forceInstalla
|
|||||||
operation = &BPMOperation{
|
operation = &BPMOperation{
|
||||||
Actions: make([]OperationAction, 0),
|
Actions: make([]OperationAction, 0),
|
||||||
UnresolvedDepends: make([]string, 0),
|
UnresolvedDepends: make([]string, 0),
|
||||||
Changes: make(map[string]string),
|
ModifiedFiles: make(map[string]string),
|
||||||
RunChecks: runChecks,
|
RunChecks: runChecks,
|
||||||
RootDir: rootDir,
|
RootDir: rootDir,
|
||||||
compiledPackages: make(map[string]string),
|
compiledPackages: make(map[string]string),
|
||||||
|
|||||||
+38
-60
@@ -1,11 +1,11 @@
|
|||||||
package bpmlib
|
package bpmlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -17,11 +17,11 @@ import (
|
|||||||
type BPMHook struct {
|
type BPMHook struct {
|
||||||
SourcePath string
|
SourcePath string
|
||||||
SourceContent string
|
SourceContent string
|
||||||
TriggerOperations []string `yaml:"trigger_operations"`
|
TriggerActions []string `yaml:"trigger_actions"`
|
||||||
TargetType string `yaml:"target_type"`
|
TriggerPreOperation bool `yaml:"trigger_pre_operation"`
|
||||||
Targets []string `yaml:"targets"`
|
Targets []string `yaml:"targets"`
|
||||||
Depends []string `yaml:"depends"`
|
|
||||||
Run string `yaml:"run"`
|
Run string `yaml:"run"`
|
||||||
|
PassTargets bool `yaml:"pass_targets"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// createHook returns a BPMHook instance based on the content of the given string
|
// createHook returns a BPMHook instance based on the content of the given string
|
||||||
@@ -36,10 +36,8 @@ func createHook(sourcePath string) (*BPMHook, error) {
|
|||||||
hook := &BPMHook{
|
hook := &BPMHook{
|
||||||
SourcePath: sourcePath,
|
SourcePath: sourcePath,
|
||||||
SourceContent: string(bytes),
|
SourceContent: string(bytes),
|
||||||
TriggerOperations: nil,
|
TriggerActions: nil,
|
||||||
TargetType: "",
|
|
||||||
Targets: nil,
|
Targets: nil,
|
||||||
Depends: nil,
|
|
||||||
Run: "",
|
Run: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,19 +60,15 @@ func (hook *BPMHook) IsValid() error {
|
|||||||
ValidOperations := []string{"install", "upgrade", "remove"}
|
ValidOperations := []string{"install", "upgrade", "remove"}
|
||||||
|
|
||||||
// Return error if any trigger operation is not valid or none are given
|
// Return error if any trigger operation is not valid or none are given
|
||||||
if len(hook.TriggerOperations) == 0 {
|
if len(hook.TriggerActions) == 0 {
|
||||||
return errors.New("no trigger operations specified")
|
return errors.New("no trigger operations specified")
|
||||||
}
|
}
|
||||||
for _, operation := range hook.TriggerOperations {
|
for _, operation := range hook.TriggerActions {
|
||||||
if !slices.Contains(ValidOperations, operation) {
|
if !slices.Contains(ValidOperations, operation) {
|
||||||
return errors.New("trigger operation '" + operation + "' is not valid")
|
return errors.New("trigger operation '" + operation + "' is not valid")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if hook.TargetType != "package" && hook.TargetType != "path" {
|
|
||||||
return errors.New("target type '" + hook.TargetType + "' is not valid")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(hook.Run) == 0 {
|
if len(hook.Run) == 0 {
|
||||||
return errors.New("command to run is empty")
|
return errors.New("command to run is empty")
|
||||||
}
|
}
|
||||||
@@ -84,55 +78,30 @@ func (hook *BPMHook) IsValid() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Execute hook if all conditions are met
|
// Execute hook if all conditions are met
|
||||||
func (hook *BPMHook) Execute(packageChanges map[string]string, verbose bool, rootDir string) error {
|
func (hook *BPMHook) Execute(modifiedFiles map[string]string, preOperation bool, verbose bool, rootDir string) error {
|
||||||
// Check if package dependencies are met
|
|
||||||
installedPackages, err := GetInstalledPackages(rootDir)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, depend := range hook.Depends {
|
|
||||||
if !slices.Contains(installedPackages, depend) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get modified files slice
|
|
||||||
modifiedFiles := make([]*PackageFileEntry, 0)
|
|
||||||
for pkg := range packageChanges {
|
|
||||||
if GetPackage(pkg, rootDir) != nil {
|
|
||||||
modifiedFiles = append(modifiedFiles, GetPackage(pkg, rootDir).PkgFiles...)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if any targets are met
|
// Check if any targets are met
|
||||||
targetMet := false
|
targetsMet := make([]string, 0)
|
||||||
for _, target := range hook.Targets {
|
for _, target := range hook.Targets {
|
||||||
if targetMet {
|
for modifiedFile, action := range modifiedFiles {
|
||||||
break
|
// Check if this hook is triggered by this file's action
|
||||||
|
if !slices.Contains(hook.TriggerActions, action) {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if hook.TargetType == "package" {
|
|
||||||
for change, operation := range packageChanges {
|
// Check if file has already been checked
|
||||||
if target == change && slices.Contains(hook.TriggerOperations, operation) {
|
if slices.Contains(targetsMet, modifiedFile) {
|
||||||
targetMet = true
|
continue
|
||||||
break
|
}
|
||||||
|
|
||||||
|
if matched, _ := filepath.Match(target, modifiedFile); !matched {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
targetsMet = append(targetsMet, modifiedFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
glob, err := filepath.Glob(path.Join(rootDir, target))
|
if len(targetsMet) == 0 {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, change := range modifiedFiles {
|
|
||||||
if slices.Contains(glob, path.Join(rootDir, change.Path)) {
|
|
||||||
targetMet = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !targetMet {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,16 +110,25 @@ func (hook *BPMHook) Execute(packageChanges map[string]string, verbose bool, roo
|
|||||||
cmd := exec.Command(splitCommand[0], splitCommand[1:]...)
|
cmd := exec.Command(splitCommand[0], splitCommand[1:]...)
|
||||||
// Setup subprocess environment
|
// Setup subprocess environment
|
||||||
cmd.Dir = "/"
|
cmd.Dir = "/"
|
||||||
|
// Pass targets
|
||||||
|
if hook.PassTargets {
|
||||||
|
buffer := bytes.Buffer{}
|
||||||
|
buffer.WriteString(strings.Join(targetsMet, "\n") + "\n")
|
||||||
|
|
||||||
|
cmd.Stdin = &buffer
|
||||||
|
}
|
||||||
// Run hook in chroot if using the -R flag
|
// Run hook in chroot if using the -R flag
|
||||||
if rootDir != "/" {
|
if rootDir != "/" {
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Chroot: rootDir}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Chroot: rootDir}
|
||||||
}
|
}
|
||||||
|
|
||||||
if verbose {
|
if !verbose {
|
||||||
fmt.Printf("Running hook (%s) with run command: %s\n", hook.SourcePath, strings.Join(splitCommand, " "))
|
fmt.Printf("Running hook (%s)\n", filepath.Base(hook.SourcePath))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Running hook (%s) with run command: %s\n", filepath.Base(hook.SourcePath), strings.Join(splitCommand, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
+67
-22
@@ -14,7 +14,7 @@ import (
|
|||||||
type BPMOperation struct {
|
type BPMOperation struct {
|
||||||
Actions []OperationAction
|
Actions []OperationAction
|
||||||
UnresolvedDepends []string
|
UnresolvedDepends []string
|
||||||
Changes map[string]string
|
ModifiedFiles map[string]string
|
||||||
CompilationJobs int
|
CompilationJobs int
|
||||||
RunChecks bool
|
RunChecks bool
|
||||||
RootDir string
|
RootDir string
|
||||||
@@ -53,24 +53,6 @@ func (operation *BPMOperation) InsertActionAt(index int, action OperationAction)
|
|||||||
operation.Actions = append(operation.Actions[:index+1], operation.Actions[index:]...) // index < len(a)
|
operation.Actions = append(operation.Actions[:index+1], operation.Actions[index:]...) // index < len(a)
|
||||||
operation.Actions[index] = action
|
operation.Actions[index] = action
|
||||||
}
|
}
|
||||||
|
|
||||||
if action.GetActionType() == "install" {
|
|
||||||
pkgInfo := action.(*InstallPackageAction).BpmPackage.PkgInfo
|
|
||||||
if !IsPackageInstalled(pkgInfo.Name, operation.RootDir) {
|
|
||||||
operation.Changes[pkgInfo.Name] = "install"
|
|
||||||
} else {
|
|
||||||
operation.Changes[pkgInfo.Name] = "upgrade"
|
|
||||||
}
|
|
||||||
} else if action.GetActionType() == "fetch" {
|
|
||||||
pkgInfo := action.(*FetchPackageAction).DatabaseEntry.Info
|
|
||||||
if !IsPackageInstalled(pkgInfo.Name, operation.RootDir) {
|
|
||||||
operation.Changes[pkgInfo.Name] = "install"
|
|
||||||
} else {
|
|
||||||
operation.Changes[pkgInfo.Name] = "upgrade"
|
|
||||||
}
|
|
||||||
} else if action.GetActionType() == "remove" {
|
|
||||||
operation.Changes[action.(*RemovePackageAction).BpmPackage.PkgInfo.Name] = "remove"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (operation *BPMOperation) RemoveAction(pkg, actionType string) {
|
func (operation *BPMOperation) RemoveAction(pkg, actionType string) {
|
||||||
@@ -552,7 +534,7 @@ func (operation *BPMOperation) GetOptionalDependencies() (optionalDepends map[st
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (operation *BPMOperation) RunHooks(verbose bool) error {
|
func (operation *BPMOperation) RunPreHooks(verbose bool) error {
|
||||||
// Return if hooks directory does not exist
|
// Return if hooks directory does not exist
|
||||||
if stat, err := os.Stat(path.Join(operation.RootDir, "var/lib/bpm/hooks")); err != nil || !stat.IsDir() {
|
if stat, err := os.Stat(path.Join(operation.RootDir, "var/lib/bpm/hooks")); err != nil || !stat.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
@@ -572,7 +554,46 @@ func (operation *BPMOperation) RunHooks(verbose bool) error {
|
|||||||
log.Printf("Error while reading hook (%s): %s", entry.Name(), err)
|
log.Printf("Error while reading hook (%s): %s", entry.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = hook.Execute(operation.Changes, verbose, operation.RootDir)
|
if !hook.TriggerPreOperation {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hook.Execute(operation.ModifiedFiles, false, verbose, operation.RootDir)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Warning: could not execute hook (%s): %s\n", entry.Name(), err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (operation *BPMOperation) RunPostHooks(verbose bool) error {
|
||||||
|
// Return if hooks directory does not exist
|
||||||
|
if stat, err := os.Stat(path.Join(operation.RootDir, "var/lib/bpm/hooks")); err != nil || !stat.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get directory entries in hooks directory
|
||||||
|
dirEntries, err := os.ReadDir(path.Join(operation.RootDir, "var/lib/bpm/hooks"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find all hooks, validate and execute them
|
||||||
|
for _, entry := range dirEntries {
|
||||||
|
if entry.Type().IsRegular() && strings.HasSuffix(entry.Name(), ".bpmhook") {
|
||||||
|
hook, err := createHook(path.Join(operation.RootDir, "var/lib/bpm/hooks", entry.Name()))
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error while reading hook (%s): %s", entry.Name(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hook.TriggerPreOperation {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
err = hook.Execute(operation.ModifiedFiles, false, verbose, operation.RootDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Warning: could not execute hook (%s): %s\n", entry.Name(), err)
|
log.Printf("Warning: could not execute hook (%s): %s\n", entry.Name(), err)
|
||||||
continue
|
continue
|
||||||
@@ -656,9 +677,34 @@ func (operation *BPMOperation) FetchPackages() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
operation.hasFetchedPackages = true
|
operation.hasFetchedPackages = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (operation *BPMOperation) GetModifiedFiles() {
|
||||||
|
// Get modified files
|
||||||
|
for _, action := range operation.Actions {
|
||||||
|
if action.GetActionType() == "install" {
|
||||||
|
installAction := action.(*InstallPackageAction)
|
||||||
|
isUpgrade := IsPackageInstalled(installAction.BpmPackage.PkgInfo.Name, operation.RootDir)
|
||||||
|
|
||||||
|
for _, pkgFile := range installAction.BpmPackage.PkgFiles {
|
||||||
|
operation.ModifiedFiles[pkgFile.Path] = "install"
|
||||||
|
if isUpgrade {
|
||||||
|
operation.ModifiedFiles[pkgFile.Path] = "upgrade"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if action.GetActionType() == "remove" {
|
||||||
|
removeAction := action.(*RemovePackageAction)
|
||||||
|
|
||||||
|
for _, pkgFile := range removeAction.BpmPackage.PkgFiles {
|
||||||
|
operation.ModifiedFiles[pkgFile.Path] = "remove"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (operation *BPMOperation) Execute(verbose, force bool) (err error) {
|
func (operation *BPMOperation) Execute(verbose, force bool) (err error) {
|
||||||
// Fetch packages
|
// Fetch packages
|
||||||
if !operation.hasFetchedPackages {
|
if !operation.hasFetchedPackages {
|
||||||
@@ -751,7 +797,6 @@ func (operation *BPMOperation) Execute(verbose, force bool) (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("Operation complete!")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user