This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tower.go
145 lines (127 loc) · 3.39 KB
/
tower.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
package main
//go:generate sh -c "cd content ; drpcli contents bundle ../content.go"
//go:generate sh -c "cd content ; drpcli contents bundle ../content.yaml"
//go:generate sh -c "drpcli contents document content.yaml > tower.rst"
//go:generate rm content.yaml
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"github.com/digitalrebar/logger"
"github.com/digitalrebar/provision-plugins/v4"
"github.com/digitalrebar/provision-plugins/v4/utils"
"github.com/digitalrebar/provision/v4/api"
"github.com/digitalrebar/provision/v4/models"
"github.com/digitalrebar/provision/v4/plugin"
)
var (
version = v4.RSVersion
def = models.PluginProvider{
Name: "tower",
Version: version,
PluginVersion: 4,
HasPublish: false,
AvailableActions: []models.AvailableAction{
models.AvailableAction{Command: "tower-register",
Model: "machines",
RequiredParams: []string{
"tower/inventory",
"tower/group",
},
},
models.AvailableAction{Command: "tower-delete",
Model: "machines",
},
models.AvailableAction{Command: "tower-invoke",
Model: "machines",
RequiredParams: []string{
"tower/job-template",
},
OptionalParams: []string{
"tower/job-timeout",
"tower/extra-vars",
},
},
models.AvailableAction{Command: "tower-job-status",
Model: "machines",
OptionalParams: []string{
"tower/job-id",
},
},
},
RequiredParams: []string{
"tower/login",
"tower/password",
"tower/url",
},
OptionalParams: []string{},
Content: contentYamlString,
}
)
type Plugin struct {
towerLogin string
towerPassword string
towerUrl string
name string
session *api.Client
}
func (p *Plugin) Config(l logger.Logger, session *api.Client, config map[string]interface{}) (err *models.Error) {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
p.session = session
if name, err := utils.ValidateStringValue("Name", config["Name"]); err != nil {
p.name = "unknown"
} else {
p.name = name
}
utils.SetErrorName(p.name)
err = &models.Error{Type: "plugin", Model: "plugins", Key: p.name}
var ok bool
p.towerLogin, ok = config["tower/login"].(string)
if !ok {
err.Code = 400
err.Errorf("Plugin %s is missing tower/login", p.name)
}
p.towerPassword, ok = config["tower/password"].(string)
if !ok {
err.Code = 400
err.Errorf("Plugin %s is missing tower/password", p.name)
}
p.towerUrl, ok = config["tower/url"].(string)
if !ok {
err.Code = 400
err.Errorf("Plugin %s is missing tower/url", p.name)
}
// Return the error
if err.HasError() != nil {
return
}
err = nil
return
}
func (p *Plugin) Action(l logger.Logger, ma *models.Action) (answer interface{}, err *models.Error) {
switch ma.Command {
case "tower-register":
answer, err = p.registerTower(ma.Model, ma.Params)
case "tower-invoke":
answer, err = p.invokeTower(ma.Model, ma.Params)
case "tower-delete":
answer, err = p.deleteTower(ma.Model, ma.Params)
case "tower-job-status":
answer, err = p.towerJobStatus(ma.Model, ma.Params)
default:
err = &models.Error{Code: 404,
Model: "plugin",
Key: "ipmi",
Type: "rpc",
Messages: []string{fmt.Sprintf("Unknown command: %s", ma.Command)}}
}
return
}
func main() {
plugin.InitApp("tower", "Provides controls for Tower/AWX", version, &def, &Plugin{})
err := plugin.App.Execute()
if err != nil {
os.Exit(1)
}
}