summaryrefslogtreecommitdiff
path: root/actions/recipe_action.go
blob: e48f65075766bd1f66a163ead3b0879a8f88a4b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
Recipe Action

Include a recipe.

Yaml syntax:
 - action: recipe
   recipe: path to recipe
   variables:
     key: value

Mandatory properties:

- recipe -- includes the recipe actions at the given path.

Optional properties:

- variables -- overrides or adds new template variables.

*/
package actions

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"github.com/go-debos/debos"
	"github.com/go-debos/fakemachine"
)

type RecipeAction struct {
	debos.BaseAction `yaml:",inline"`
	Recipe           string
	Variables        map[string]string
	Actions          Recipe `yaml:"-"`
	templateVars     map[string]string
}

func (recipe *RecipeAction) Verify(context *debos.DebosContext) error {
	if len(recipe.Recipe) == 0 {
		return errors.New("'recipe' property can't be empty")
	}

	file := recipe.Recipe
	if !filepath.IsAbs(file) {
		file = filepath.Clean(context.RecipeDir + "/" + recipe.Recipe)
	}

	if _, err := os.Stat(file); os.IsNotExist(err) {
		return err
	}

	// Initialise template vars
	recipe.templateVars = make(map[string]string)
	recipe.templateVars["included_recipe"] = "true"
	recipe.templateVars["architecture"] = context.Architecture

	// Add Variables to template vars
	for k, v := range recipe.Variables {
		recipe.templateVars[k] = v
	}

	if err := recipe.Actions.Parse(file, context.PrintRecipe, context.Verbose, recipe.templateVars); err != nil {
		return err
	}

	if context.Architecture != recipe.Actions.Architecture {
		return fmt.Errorf("Expect architecture '%s' but got '%s'", context.Architecture, recipe.Actions.Architecture)
	}

	for _, a := range recipe.Actions.Actions {
		if err := a.Verify(context); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) PreMachine(context *debos.DebosContext, m *fakemachine.Machine, args *[]string) error {
	// TODO: check args?

	for _, a := range recipe.Actions.Actions {
		if err := a.PreMachine(context, m, args); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) PreNoMachine(context *debos.DebosContext) error {
	for _, a := range recipe.Actions.Actions {
		if err := a.PreNoMachine(context); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) Run(context *debos.DebosContext) error {
	recipe.LogStart()

	for _, a := range recipe.Actions.Actions {
		if err := a.Run(context); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) Cleanup(context *debos.DebosContext) error {
	for _, a := range recipe.Actions.Actions {
		if err := a.Cleanup(context); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) PostMachine(context *debos.DebosContext) error {
	for _, a := range recipe.Actions.Actions {
		if err := a.PostMachine(context); err != nil {
			return err
		}
	}

	return nil
}

func (recipe *RecipeAction) PostMachineCleanup(context *debos.DebosContext) error {
	for _, a := range recipe.Actions.Actions {
		if err := a.PostMachineCleanup(context); err != nil {
			return err
		}
	}

	return nil
}