forked from radekg/terraform-provisioner-ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_provisioner.go
190 lines (164 loc) · 5.76 KB
/
resource_provisioner.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"context"
"errors"
"fmt"
"log"
"github.com/radekg/terraform-provisioner-ansible/mode"
"github.com/radekg/terraform-provisioner-ansible/types"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
type provisioner struct {
defaults *types.Defaults
plays []*types.Play
ansibleSSHSettings *types.AnsibleSSHSettings
remote *types.RemoteSettings
}
// Provisioner describes this provisioner configuration.
func Provisioner() terraform.ResourceProvisioner {
return &schema.Provisioner{
Schema: map[string]*schema.Schema{
"plays": types.NewPlaySchema(),
"defaults": types.NewDefaultsSchema(),
"remote": types.NewRemoteSchema(),
"ansible_ssh_settings": types.NewAnsibleSSHSettingsSchema(),
},
ValidateFunc: validateFn,
ApplyFunc: applyFn,
}
}
func validateFn(c *terraform.ResourceConfig) (ws []string, es []error) {
defer func() {
if r := recover(); r != nil {
es = append(es, fmt.Errorf("error while validating the provisioner, reason: %+v", r))
}
}()
if !c.IsSet("plays") {
ws = append(ws, "nothing to play")
return
}
value, _ := c.Get("plays")
_, isArrayOfMaps := value.([]map[string]interface{}) // in Terraform 0.11.x types are concrete
_, isArrayOfIfs := value.([]interface{}) // in Terraform 0.12+ types are wrapped in interface{}
value, _ = c.Get("plays.#")
numPlays, ok := value.(int)
if (!isArrayOfIfs && !isArrayOfMaps) || value == nil || !ok {
es = append(es, fmt.Errorf("`plays` must be an array isarr=%v/%v ok=%v val=%v num=%d", isArrayOfIfs, isArrayOfMaps, ok, value, numPlays))
return
}
validPlaysCount := 0
for playNo := 0; playNo < numPlays; playNo++ {
playLoc := fmt.Sprintf("plays.%d", playNo)
playHasPlaybook := c.IsSet(playLoc + ".playbook")
playHasModule := c.IsSet(playLoc + ".module")
if playHasPlaybook && playHasModule {
es = append(es, errors.New("playbook and module can't be used together"))
continue
}
if !playHasPlaybook && !playHasModule {
es = append(es, errors.New("playbook or module must be set"))
continue
}
if playHasModule {
validPlaysCount++
continue
}
value, _ := c.Get(playLoc + ".playbook")
_, isArrayOfMaps = value.([]map[string]interface{}) // in Terraform 0.11.x types are concrete
_, isArrayOfIfs = value.([]interface{}) // in Terraform 0.12+ types are wrapped in interface{}
value, _ = c.Get(playLoc + ".playbook.#")
numPlaybooks, ok := value.(int)
if (!isArrayOfIfs && !isArrayOfMaps) || value == nil || !ok {
es = append(es, errors.New("`plays.playbook` must be an array"))
continue
}
playIsOK := true
for playbookNo := 0; playbookNo < numPlaybooks; playbookNo++ {
playbookLoc := fmt.Sprintf("%s.playbook.%d", playLoc, playbookNo)
if !c.IsSet(playbookLoc + ".roles_path") {
continue
}
// TODO investigate this:
// If a value in roles_path is computed, an attempt to query the
// array length using the `*.roles_path.#` returns a UUID string.
// As a temporary workaround, lets use len() of the castedArray.
value, _ := c.Get(playbookLoc + ".roles_path")
rolesPathAsArray, rolesPathArrayIsOK := value.([]interface{})
if !rolesPathArrayIsOK {
es = append(es, errors.New("`plays.playbook.roles_path` must be an array"))
playIsOK = false
continue
}
for pathNo := 0; pathNo < len(rolesPathAsArray); pathNo++ {
// Terraform 0.11.x attempts to interpolate everything ASAP, but
// Terraform 0.12+ will lazily return an UUID for computed values.
if c.IsComputed(fmt.Sprintf("%s.roles_path.%d", playbookLoc, pathNo)) {
//ws = append(ws, "Cannot validate roles_path as it is computed")
log.Printf("[WARN] Cannot validate roles_path as it is computed")
continue
}
if value, ok = rolesPathAsArray[pathNo].(string); !ok {
es = append(es, errors.New("`plays.playbook.roles_path` must be an array of strings"))
playIsOK = false
continue
}
wsDir, esDir := types.VfPathDirectory(value, "roles_path")
ws = append(ws, wsDir...)
es = append(es, esDir...)
if esDir != nil {
playIsOK = false
}
}
}
if playIsOK {
validPlaysCount++
}
}
if validPlaysCount == 0 {
ws = append(ws, "nothing to play")
}
return ws, es
}
func applyFn(ctx context.Context) error {
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
s := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
d := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
// Decode the provisioner config
p, err := decodeConfig(d)
if err != nil {
return err
}
if p.remote.IsRemoteInUse() {
remoteMode, err := mode.NewRemoteMode(o, s, p.remote)
if err != nil {
o.Output(fmt.Sprintf("%+v", err))
return err
}
return remoteMode.Run(p.plays)
}
localMode, err := mode.NewLocalMode(o, s)
if err != nil {
o.Output(fmt.Sprintf("%+v", err))
return err
}
return localMode.Run(p.plays, p.ansibleSSHSettings)
}
func decodeConfig(d *schema.ResourceData) (*provisioner, error) {
vRemoteSettings := types.NewRemoteSettingsFromInterface(d.GetOk("remote"))
vAnsibleSSHSettings := types.NewAnsibleSSHSettingsFromInterface(d.GetOk("ansible_ssh_settings"))
vDefaults := types.NewDefaultsFromInterface(d.GetOk("defaults"))
plays := make([]*types.Play, 0)
if rawPlays, ok := d.GetOk("plays"); ok {
playSchema := types.NewPlaySchema()
for _, iface := range rawPlays.([]interface{}) {
plays = append(plays, types.NewPlayFromInterface(schema.NewSet(schema.HashResource(playSchema.Elem.(*schema.Resource)), []interface{}{iface}), vDefaults))
}
}
return &provisioner{
defaults: vDefaults,
remote: vRemoteSettings,
ansibleSSHSettings: vAnsibleSSHSettings,
plays: plays,
}, nil
}