This repository has been archived by the owner on Jun 15, 2019. It is now read-only.
/
node.go
151 lines (132 loc) · 3.96 KB
/
node.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
package hivy
import (
"fmt"
"path/filepath"
"github.com/bitly/go-simplejson"
"github.com/emicklei/go-restful"
"github.com/hivetech/hivy/security"
"github.com/hivetech/hivy/beacon"
)
const (
provider string = "juju"
redisURL string = "127.0.0.1:6379"
labNode string = "hivelab"
)
// vmSshForward returns the host port the service is using for ssh
func vmSshForward(user string, controller *beacon.Controller, status *simplejson.Json) (string, error) {
// We want user's lab hostname, ok like <user>-local-machine-<ID>
// And we have everything but the machine ID where the lab is
//FIXME serviceKey is juju format specific, should use id() method for proper abstraction
serviceKey := fmt.Sprintf("%s-%s", user, labNode)
//machineID, err := status.Get("services").Get(serviceKey).Get("units").Get(serviceKey+"/0").Get("machine").String()
machineID, err := status.GetPath("services", serviceKey, "units", serviceKey+"/0", "machine").String()
if err != nil {
return "", err
}
log.Debugf("got hivelab machine id: %s", machineID)
result, err := controller.Get(filepath.Join("hivy", "mapping", "xavier-local-machine-"+machineID))
if err != nil || len(result) != 1 {
return "", err
}
return result[0].Value, nil
}
//TODO Factorizes 3 functions that are identical but the juju method
// Status fetchs informations about the given node id
func Status(request *restful.Request, response *restful.Response) {
user, _, err := security.Credentials(request)
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
id := request.QueryParameter("id")
if provider == "juju" {
juju, err := NewJuju()
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
report, err := juju.Status(user, id)
if err != nil {
beacon.HTTPInternalError(response, err)
} else {
response.WriteEntity(report)
}
return
}
beacon.HTTPInternalError(response, err)
}
// Deploy creates new nodes
func Deploy(request *restful.Request, response *restful.Response) {
user, _, err := security.Credentials(request)
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
id := request.QueryParameter("id")
if provider == "juju" {
//TODO Deploy command on existing service triggers upgrade-charm
//TODO For suitable charms, it could also deploy other clustered units
juju, err := NewJuju()
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
report, err := juju.Deploy(user, id)
if err != nil {
beacon.HTTPInternalError(response, err)
} else {
response.WriteEntity(report)
}
return
}
beacon.HTTPInternalError(response, err)
}
// Destroy removes nodes
func Destroy(request *restful.Request, response *restful.Response) {
user, _, err := security.Credentials(request)
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
id := request.QueryParameter("id")
if provider == "juju" {
juju, err := NewJuju()
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
report, err := juju.Destroy(user, id)
if err != nil {
beacon.HTTPInternalError(response, err)
} else {
response.WriteEntity(report)
}
return
}
beacon.HTTPInternalError(response, err)
}
// Plug allows interactions between two nodes
func Plug(request *restful.Request, response *restful.Response) {
user, _, err := security.Credentials(request)
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
id := request.QueryParameter("id")
withID := request.QueryParameter("with")
if provider == "juju" {
juju, err := NewJuju()
if err != nil {
beacon.HTTPInternalError(response, err)
return
}
report, err := juju.AddRelation(user, id, withID)
if err != nil {
beacon.HTTPInternalError(response, err)
} else {
response.WriteEntity(report)
}
return
}
beacon.HTTPInternalError(response, err)
}