This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
uyunirpc.go
234 lines (199 loc) · 4.94 KB
/
uyunirpc.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package uyuniapi
import (
"crypto/tls"
"fmt"
"github.com/kolo/xmlrpc"
"net/http"
"net/url"
"strconv"
)
type RPCClient struct {
conn *xmlrpc.Client
user string
password string
port int
host string
uri string
skipSslCheck bool
tls bool
sid string
}
// NewRPCClient is a constructor for the RPCClient object
func NewRPCClient(skipSslCheck bool) *RPCClient {
rpc := new(RPCClient)
rpc.skipSslCheck = skipSslCheck
return rpc
}
func (rpc *RPCClient) Connect() *RPCClient {
if rpc.conn == nil {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: rpc.skipSslCheck,
},
}
rpc.conn, _ = xmlrpc.NewClient(rpc.getURL(), transport)
}
return rpc
}
// Construct connection URL
func (rpc *RPCClient) getURL() string {
u, _ := url.Parse("http://localhost/rpc/api")
if rpc.tls {
u.Scheme = "https"
} else {
u.Scheme = "http"
}
u.Host = rpc.host
if rpc.port > 0 {
u.Host += ":" + strconv.Itoa(rpc.port)
}
return fmt.Sprint(u)
}
// SetUser sets the username for the authentication
func (rpc *RPCClient) SetUser(user string) *RPCClient {
rpc.user = user
return rpc
}
// SetPassword sets the password for the authentication
func (rpc *RPCClient) SetPassword(password string) *RPCClient {
rpc.password = password
return rpc
}
// SetPort sets the port for the URL
func (rpc *RPCClient) SetPort(port int) *RPCClient {
rpc.port = port
return rpc
}
// SetHost sets the host for the URL
func (rpc *RPCClient) SetHost(host string) *RPCClient {
rpc.host = host
return rpc
}
// SetURI sets the URI for the URL
func (rpc *RPCClient) SetURI(uri string) *RPCClient {
rpc.uri = uri
return rpc
}
// SetTLS sets the SSL/TLS use and checking its certificate
func (rpc *RPCClient) SetTLS(tls bool, check bool) *RPCClient {
rpc.tls = tls
rpc.skipSslCheck = check
return rpc
}
// Obtain authentication token
func (rpc *RPCClient) authenticate() {
var err error
var res interface{}
if rpc.user != "" && rpc.password != "" {
res, err = rpc.Call("auth.login", rpc.user, rpc.password)
if err != nil {
panic(err)
} else {
rpc.sid = res.(string)
}
} else {
panic("No credentials found")
}
}
// Get current session ID or create new one
func (rpc *RPCClient) getSID() string {
if rpc.sid == "" {
rpc.authenticate()
}
return rpc.sid
}
// Refresh session ID
func (rpc *RPCClient) refreshSID() string {
rpc.sid = ""
return rpc.getSID()
}
// Call XML-RPC on Uyuni side
func (rpc *RPCClient) Call(method string, args ...interface{}) (interface{}, error) {
var ret interface{}
err := rpc.conn.Call(method, args, &ret)
return ret, err
}
/////////////
// Token marker
type RPCSessionToken struct{}
///////////////////////
// XML-RPC mux/demux
type RPCDemux struct {
clients map[string]*RPCClient
vidmanager *VIDManager
config *APIConfig
}
func NewRPCDemux(vm *VIDManager) *RPCDemux {
rpmc := new(RPCDemux)
rpmc.clients = make(map[string]*RPCClient)
rpmc.vidmanager = vm
rpmc.ReloadVIDManager()
return rpmc
}
// Set current configuration
func (rpmc *RPCDemux) SetConfig(config *APIConfig) *RPCDemux {
if rpmc.config == nil {
rpmc.config = config
}
return rpmc
}
func (rpmc *RPCDemux) ReloadVIDManager() {
for _, fqdn := range rpmc.vidmanager.GetContextFQDNs() {
fmt.Println("Registering connection to", fqdn)
if _, isSet := rpmc.clients[fqdn]; !isSet {
serverConf := rpmc.config.GetRefServerConfig(fqdn)
rpcClient := NewRPCClient(true).SetHost(fqdn).
SetUser(serverConf["login"].(string)).
SetPassword(serverConf["password"].(string)).
SetPort(serverConf["port"].(int)).
SetTLS(serverConf["use_ssl"].(bool), serverConf["verify_ssl"].(bool))
rpmc.clients[fqdn] = rpcClient.Connect()
}
}
}
// Marker for a token. This is just a dummy type that is used to be a placeholder
// for the real token within the demuxer call.
func (rpmc *RPCDemux) GetSIDMarker() *RPCSessionToken {
return new(RPCSessionToken)
}
// Call multiple systems at once, aggregate
func (rpmc *RPCDemux) Call(method string, args ...interface{}) (interface{}, error) {
demux := NewDataAggregator(rpmc.vidmanager)
errors := 0
var lastError error
for fqdn := range rpmc.clients {
c_ref := rpmc.clients[fqdn]
// Inject session ID
_args := make([]interface{}, 0)
for _, arg := range args {
switch arg.(type) {
case *RPCSessionToken:
_args = append(_args, c_ref.getSID())
case int: // This is just a proof of concept for now. Need to find out ID reliably.
id, ctx := rpmc.vidmanager.ToSystemId(arg.(int))
if ctx != fqdn {
_args = nil
break
}
_args = append(_args, id)
default:
_args = append(_args, arg)
}
}
if _args == nil {
continue
}
ret, err := c_ref.Call(method, _args...)
if err != nil {
errors++
lastError = err
} else {
demux.aggregate(fqdn, ret)
}
}
// Fail only when all servers failed at once
if errors == len(rpmc.clients) {
panic(lastError)
}
return demux.Multiplex(), nil
}