Skip to content

Commit

Permalink
Improve recipe validation
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Nov 1, 2023
1 parent 6add8a1 commit c0c3350
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,12 @@ func getValidationConfig(tags []string) *executor.ValidationConfig {

if options.GetB(OPT_DRY_RUN) {
vc.IgnoreDependencies = true
vc.IgnorePackages = true
vc.IgnorePrivileges = true
}

if options.GetB(OPT_IGNORE_PACKAGES) {
vc.IgnoreDependencies = true
vc.IgnorePackages = true
}

return vc
Expand Down
7 changes: 6 additions & 1 deletion cli/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Config struct {
// ValidationConfig is config for validation
type ValidationConfig struct {
Tags []string
IgnorePackages bool
IgnoreDependencies bool
IgnorePrivileges bool
}
Expand Down Expand Up @@ -171,10 +172,14 @@ func (e *Executor) Validate(r *recipe.Recipe, cfg *ValidationConfig) []error {
errs.Add(checkRecipePrivileges(r))
}

if !cfg.IgnoreDependencies {
if !cfg.IgnorePackages {
errs.Add(checkPackages(r))
}

if !cfg.IgnoreDependencies {
errs.Add(checkDependencies(r))
}

if !errs.HasErrors() {
return nil
}
Expand Down
61 changes: 60 additions & 1 deletion cli/executor/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func checkRecipeVariables(r *recipe.Recipe) []error {
return errs
}

// checkPackages checks packages
// checkPackages checks if required packages are installed on the system
func checkPackages(r *recipe.Recipe) []error {
if len(r.Packages) == 0 {
return nil
Expand All @@ -137,6 +137,65 @@ func checkPackages(r *recipe.Recipe) []error {
return []error{errors.New("Can't check required packages availability: Unsupported OS")}
}

// checkDependencies checks if all required binaries are present on the system
func checkDependencies(r *recipe.Recipe) []error {
var errs []error

binCache := make(map[string]bool)

for _, c := range r.Commands {
for _, a := range c.Actions {
var binary string

switch a.Name {
case recipe.ACTION_SERVICE_PRESENT:
binary = "systemctl"
case recipe.ACTION_SERVICE_ENABLED:
binary = "systemctl"
case recipe.ACTION_SERVICE_WORKS:
binary = "systemctl"
case recipe.ACTION_WAIT_SERVICE:
binary = "systemctl"
case recipe.ACTION_LIB_LOADED:
binary = "ldconfig"
case recipe.ACTION_LIB_CONFIG:
binary = "pkg-config"
case recipe.ACTION_LIB_LINKED:
binary = "readelf"
case recipe.ACTION_LIB_RPATH:
binary = "readelf"
case recipe.ACTION_LIB_SONAME:
binary = "readelf"
case recipe.ACTION_LIB_EXPORTED:
binary = "nm"
case recipe.ACTION_PYTHON2_PACKAGE:
binary = "python"
case recipe.ACTION_PYTHON3_PACKAGE:
binary = "python3"
}

if !hasBinary(binCache, binary) {
errs = append(errs, fmt.Errorf("Action %q requires %q binary"))
}
}
}

return errs
}

// hasBinary checks if binary is present on the system
func hasBinary(binCache map[string]bool, binary string) bool {
isExist, ok := binCache[binary]

if ok {
return isExist
}

binCache[binary] = env.Which(binary) != ""

return binCache[binary]
}

// getDynamicVars returns slice with dynamic vars
func getDynamicVars(a *recipe.Action) []string {
switch a.Name {
Expand Down

0 comments on commit c0c3350

Please sign in to comment.