-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
179 lines (139 loc) · 5.32 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"os"
"strings"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
// We need to strip namespaces off answer file sections to talk to the
// front end, but atmoicapp 0.6.4 expects them to be in the answerfile when
// we go to run it, so it will need to be written out to answer.conf.gen before
// running a user's answers. We'll keep track of that bookkeeping here
// TODO: Consider longer term approach than a gross global manifest!
var namespaceManifest NamespaceManifest
const MSG_BUFFER_SIZE = 20
func main() {
namespaceManifest = make(NamespaceManifest)
engine := NewWorkEngine(MSG_BUFFER_SIZE)
subscriber := NewSocketWorkSubscriber()
engine.AttachSubscriber(subscriber)
http.Handle("/socket.io/", subscriber.Server)
api := mux.NewRouter()
api.HandleFunc("/api/nulecules", Nulecules).Methods("POST")
api.HandleFunc("/api/nulecules/{registry}/{id}", NuleculeDetails).Methods("GET")
api.HandleFunc("/api/nulecules/{registry}/{id}", NuleculeUpdate).Methods("POST")
api.HandleFunc("/api/nulecules/{registry}/{id}/deploy",
createEngineHandler(engine, NuleculeDeploy)).Methods("POST")
allowedHeaders := handlers.AllowedHeaders([]string{"Content-Type"})
http.Handle("/api/", handlers.CORS(allowedHeaders)(api))
fs := http.StripPrefix("/static/", http.FileServer(http.Dir("./static")))
http.Handle("/static/", fs)
http.HandleFunc("/", IndexHandler)
fmt.Println("Listening on localhost:3001")
http.ListenAndServe(":3001", nil)
}
type GorillaRouteHandler func(http.ResponseWriter, *http.Request)
type EngineRouteHandler func(http.ResponseWriter, *http.Request, *WorkEngine)
func createEngineHandler(engine *WorkEngine, r EngineRouteHandler) GorillaRouteHandler {
return func(writer http.ResponseWriter, request *http.Request) {
r(writer, request, engine)
}
}
func IndexHandler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("static/index.html")
t.Execute(w, nil)
}
func Nulecules(w http.ResponseWriter, r *http.Request) {
fmt.Println("Entered Nulecules method")
res_map := make(map[string]string)
json.NewDecoder(r.Body).Decode(&res_map)
organization := res_map["org"]
username := res_map["username"]
password := res_map["password"]
channel := make(chan string)
list := getNuleculeList(organization, username, password)
responses := make([]string, len(list.Nulecules))
filteredList := NuleculeList{}
for _, nules := range list.Nulecules {
go IsImageAtomicApp("docker://"+nules, channel)
}
counter := 1
for response := range channel {
responses = append(responses, response)
if counter != len(list.Nulecules) {
counter++
} else {
close(channel)
}
}
for _, value := range responses {
if strings.Compare(value, "ignore\n") != 0 && value != "" {
// Strip docker:// protocol, 9 chars
filteredList.Nulecules = append(filteredList.Nulecules, value[9:len(value)])
}
}
json.NewEncoder(w).Encode(filteredList)
}
func NuleculeDetails(w http.ResponseWriter, r *http.Request) {
fmt.Println("Entered NuleculeDetails method")
vars := mux.Vars(r)
registry := vars["registry"]
nuleculeId := vars["id"]
downloadNulecule(registry, nuleculeId)
// atomicapp 0.6.4 changed the answer.conf file format and namespaces
// component names with their container name, i.e.
// "mariadb-app" vs "mariadb-centos7-atomicapp:mariadb-app"
// stripContainerNamespace will strip off the container namespace
// to maintain backwards compatibility for the cap UI.
strippedNamespaces, answers := stripNamespaces(
getAnswersFromFile(registry, nuleculeId),
)
for _, strippedNamespace := range strippedNamespaces {
namespaceManifest.insert(registry, nuleculeId,
strippedNamespace.namespace, strippedNamespace.nodeName)
}
details := NuleculeDetail{
answers,
getBindings(registry, nuleculeId),
}
json.NewEncoder(os.Stdout).Encode(details)
json.NewEncoder(w).Encode(details)
}
func NuleculeUpdate(w http.ResponseWriter, r *http.Request) {
fmt.Println("Entered NuleculeUpdate method")
// update the nulecule answers file
vars := mux.Vars(r)
nuleculeId := vars["id"]
registry := vars["registry"]
// get the posted answers
// Answers is a map of maps
res_map := make(map[string]Answers)
json.NewDecoder(r.Body).Decode(&res_map)
// TODO: Consider better way to uniquely ID projects instead of a UUID
// Could also use UUIDs as bookkeeping on the backend with a more friendly
// project name provided by the user on the front end.
projectName := addProviderDetails(res_map["nulecule"])
createNewProject(projectName)
injectNamespaces(namespaceManifest, res_map["nulecule"], registry, nuleculeId)
writeUserAnswersToFile(registry, nuleculeId, res_map)
json.NewEncoder(w).Encode(res_map) // Success, fail?
}
func NuleculeDeploy(w http.ResponseWriter, r *http.Request, engine *WorkEngine) {
fmt.Println("Entered NuleculeDeploy method")
vars := mux.Vars(r)
nuleculeId := vars["id"]
registry := vars["registry"]
// TODO: probably a much better way to get the host
answers := getGeneratedAnswersFile(registry, nuleculeId)
entry_map := findEntry(answers, "endpoint")
jobToken := engine.StartNewJob(NewDeployJob(registry, nuleculeId, entry_map["endpoint"]))
// TODO: Error handling!
res_map := make(map[string]interface{})
res_map["job_token"] = jobToken
res_map["result"] = "success"
json.NewEncoder(w).Encode(res_map) // Success, fail?
}