/
manager.go
164 lines (148 loc) · 4.14 KB
/
manager.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
/*
** Copyright [2013-2015] [Megam Systems]
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
package coordinator
import (
"encoding/json"
log "code.google.com/p/log4go"
"github.com/megamsys/gulp/app"
"github.com/megamsys/gulp/global"
"github.com/megamsys/gulp/policies"
"github.com/tsuru/config"
)
type Coordinator struct {
//RequestHandler(f func(*Message), name ...string) (Handler, error)
//EventsHandler(f func(*Message), name ...string) (Handler, error)
}
type Message struct {
Id string `json:"Id"`
Action string `json:"Action"`
Args string `json:"Args"`
}
func Handler(chann []byte) {
m := &Message{}
parse_err := json.Unmarshal(chann, &m)
log.Info(parse_err)
if parse_err != nil {
log.Error("Error: Message parsing error:\n%s.", parse_err)
return
}
log.Info("============Request entry===========")
apprequest := global.AppRequest{Id: m.Id}
req, err := apprequest.Get(m.Id)
log.Info(req)
if err != nil {
log.Error("Error: Riak didn't cooperate->:\n%s.", err)
return
}
assembly := global.Assembly{Id: req.AppId}
asm, err := assembly.GetAssemblyWithComponents(req.AppId)
if err != nil {
log.Error("Error: Riak didn't cooperate-->:\n%s.", err)
return
}
comp := global.Component{Id: asm.Components[0].Id}
com, err := comp.Get(asm.Components[0].Id)
if err != nil {
log.Error("Error: Riak didn't cooperate--->:\n%s.", err)
return
}
switch req.Action {
case "reboot":
log.Info("============Reboot entry==========")
go app.RebootApp(asm)
break
case "start":
log.Info("============Start entry===========")
go app.StartApp(asm)
break
case "stop":
log.Info("============Stop entry============")
go app.StopApp(asm)
break
case "restart":
log.Info("============Restart entry============")
go app.RestartApp(asm)
break
case "redeploy":
log.Info("=============Redeploying=============")
go app.BuildApp(com)
break
/*case "componentstart":
log.Info("============Component Start entry======")
go app.StartComponent(com)
break
case "componentstop":
log.Info("============Component Stop entry======")
go app.StopComponent(com)
break
case "componentrestart":
log.Info("============Component Restart entry======")
go app.RestartComponent(com)
break */
}
}
/**
** It handles all events from megam engine
**/
func EventsHandler(chann []byte) {
m := &Message{}
parse_err := json.Unmarshal(chann, &m)
log.Info(parse_err)
if parse_err != nil {
log.Error("Error: Message parsing error:\n%s.", parse_err)
return
}
switch m.Action {
case "build":
log.Info("============Build entry============")
comp := global.Component{Id: m.Id}
com, err := comp.Get(m.Id)
if err != nil {
log.Error("Error: Riak didn't cooperate:\n%s.", err)
return
}
go app.BuildApp(com)
break
}
}
func PolicyHandler() {
log.Info("==>Policy Handler entry==")
id, err := config.GetString("id")
if err != nil {
return
}
assembly := global.Assembly{Id: id}
asm, asmerr := assembly.GetAssemblyWithComponents(id)
if asmerr != nil {
log.Error("Error: Riak didn't cooperate:\n%s.", asmerr)
return
}
policies.ApplyPolicies(asm)
}
/*
* handlers to call the docker log stream and network setting action
*
*/
func DockerLogs(container_id string, container_name string) {
log.Info("===>Docker Logs Entry<===")
log := global.DockerLogsInfo{ContainerId: container_id, ContainerName: container_name}
go app.StreamLogs(&log)
}
func DockerNetworks(bridge string, container_id string, ip_addr string, gateway string) {
log.Info("===>Docker Networks Entry<===")
network := global.DockerNetworksInfo{Bridge: bridge, ContainerId: container_id, IpAddr: ip_addr, Gateway: gateway}
go app.ConfigureNetworks(&network)
}