/
democmd.go
214 lines (185 loc) · 5.87 KB
/
democmd.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
// Copyright 2016 The NorthShore Authors All rights reserved.
//
// 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 cmd
import (
"encoding/json"
"fmt"
"net/http"
"os"
"path"
"time"
"github.com/Mirantis/northshore/blueprint"
"github.com/Mirantis/northshore/server"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/manyminds/api2go"
"github.com/manyminds/api2go-adapter/gingonic"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var demoBlueprintFile string
// demoBlueprintCmd represents the "demo-blueprint" command
var demoBlueprintCmd = &cobra.Command{
Use: "demo-blueprint",
Short: "Run execution of blueprint",
Long: `This command read, parse and process blueprint.`,
Run: func(cmd *cobra.Command, args []string) {
overrideSettings()
bp, err := blueprint.ParseFile(demoBlueprintFile)
if err != nil {
log.WithError(err).Fatal("Blueprint parsing error")
}
log.WithFields(log.Fields{
"path": demoBlueprintFile,
"blueprint": bp,
}).Info("Blueprint parsing")
},
}
// demoCmd represents the "demo" command
var demoCmd = &cobra.Command{
Use: "demo",
Short: "Run demo",
Long: `Run demo on local server.
The local server binds localhost:8998.
Demo Blueprint Pipeline goes thru states.`,
Run: func(cmd *cobra.Command, args []string) {
overrideSettings()
/* Run Blueprint */
bp, err := blueprint.ParseFile(demoBlueprintFile)
if err != nil {
log.WithError(err).Fatal("Blueprint parsing error")
}
log.WithFields(log.Fields{
"path": demoBlueprintFile,
"blueprint": bp,
}).Info("Blueprint parsing")
bp.Save()
log.Infoln("#bp_parsed", blueprintStates(bp), bp.State)
/* Run States */
log.Info("Run blueprint states")
ss := [][]blueprint.StageState{
{blueprint.StageStatePaused},
{blueprint.StageStateRunning, blueprint.StageStatePaused},
{blueprint.StageStateRunning, blueprint.StageStateRunning},
{"badstate", blueprint.StageStateRunning},
}
go func() {
for {
for i, s := range bp.Stages {
time.Sleep(time.Second * 3)
s.State = blueprint.StageStateCreated
bp.Stages[i] = s
bp.Save()
log.Infoln("#bp_update", blueprintStates(bp), bp.State)
}
for i, s := range bp.Stages {
time.Sleep(time.Second * 3)
s.State = blueprint.StageStateRunning
bp.Stages[i] = s
bp.Save()
log.Infoln("#bp_update", blueprintStates(bp), bp.State)
}
for _, v := range ss {
time.Sleep(time.Second * 3)
idx := 0
for i, s := range bp.Stages {
if idx < len(v) {
s.State = v[idx]
bp.Stages[i] = s
}
idx++
}
bp.Save()
log.Infoln("#bp_update", blueprintStates(bp), bp.State)
}
time.Sleep(time.Second * 3)
for i, s := range bp.Stages {
s.State = blueprint.StageStateNew
bp.Stages[i] = s
}
bp.Save()
log.Infoln("#bp_update", blueprintStates(bp), bp.State)
}
}()
/* Run local server */
log.Info("Run local server")
r := gin.Default()
r.NoRoute(func(c *gin.Context) {
c.File(path.Join(viper.GetString("UIRoot"), "index.html"))
})
ui := r.Group("/ui")
ui.Static("/app", path.Join(viper.GetString("UIRoot"), "/app"))
ui.Static("/assets", path.Join(viper.GetString("UIRoot"), "/assets"))
ui.Static("/dist", path.Join(viper.GetString("UIRoot"), "/dist"))
ui.Static("/node_modules", path.Join(viper.GetString("UIRoot"), "/node_modules"))
r.GET("/api", server.APIRootHandler)
r.POST("/api/v1/parse/blueprint", server.APIParseBlueprintHandler)
api1 := api2go.NewAPIWithRouting(
"/api/v1",
api2go.NewStaticResolver("/"),
gingonic.New(r),
)
api1.AddResource(blueprint.Blueprint{}, &server.BlueprintResource{})
// Custom api route
// https://github.com/manyminds/api2go/issues/256#issuecomment-234923954
// it couses to stack overflow
//
// api1Handler := api1.Handler().(*httprouter.Router)
// api1Handler.GET("/a1", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
// w.Header().Set("Content-Type", "application/json")
// json.NewEncoder(w).Encode(gin.H{"txt": "/a1"})
// })
//
// api1Router := api1.Router().(*routing.HTTPRouter)
// it works:
api1Router := api1.Router()
api1Router.Handle("GET", "/api/a", func(w http.ResponseWriter, r *http.Request, params map[string]string) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(gin.H{"/api/a": 1})
})
addr := fmt.Sprintf(
"%s:%s",
viper.GetString("ServerIP"),
viper.GetString("ServerPort"),
)
log.WithFields(log.Fields{
"addr": addr,
"UIRoot": viper.GetString("UIRoot"),
}).Infoln("#http", "Listen And Serve")
http.ListenAndServe(addr, r)
},
}
func init() {
/* Init cobra */
demoBlueprintCmd.Flags().StringVarP(&demoBlueprintFile, "file", "f", "", "Path to blueprint yaml")
demoCmd.Flags().StringVarP(&demoBlueprintFile, "file", "f", "", "Path to blueprint yaml")
demoCmd.Flags().StringVarP(&UIRoot, "ui", "u", "./ui", "Path to UI root directory")
viper.BindPFlag("UIRoot", demoCmd.Flags().Lookup("ui"))
runCmd.AddCommand(demoBlueprintCmd)
runCmd.AddCommand(demoCmd)
}
func blueprintStates(bp blueprint.Blueprint) (ss []blueprint.StageState) {
for _, s := range bp.Stages {
ss = append(ss, s.State)
}
return ss
}
func overrideSettings() {
/* Init DB */
os.Remove("demo.db")
viper.Set("BoltDBPath", "demo.db")
/* Init Logger */
viper.Set("LogLevel", log.DebugLevel)
log.SetLevel(log.DebugLevel)
}