summaryrefslogtreecommitdiff
path: root/actions/recipe_action.go
blob: 9fa3b1c9582aea2353f58029af8e187fe4de02db (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
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Recipe Action

This action includes the recipe at the given path, and can optionally
override or set template variables.

To ensure compatibility, both the parent recipe and all included recipes have
to be for the same architecture. For convenience the parent architecture is
passed in the "architecture" template variable.

Limitations of combined recipes are equivalent to limitations within a
single recipe (e.g. there can only be one image partition action).

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
	context          debos.DebosContext
}

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

	recipe.context = *context

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

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

	// Initialise template vars
	recipe.templateVars = make(map[string]string)
	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 recipe.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(&recipe.context); err != nil {
			return err
		}
	}

	return nil
}

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

	m.AddVolume(recipe.context.RecipeDir)

	for _, a := range recipe.Actions.Actions {
		if err := a.PreMachine(&recipe.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(&recipe.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(&recipe.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(&recipe.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(&recipe.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(&recipe.context); err != nil {
			return err
		}
	}

	return nil
}