/
main.go
242 lines (203 loc) · 6.6 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
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
235
236
237
238
239
240
241
242
/*
Copyright 2019 The Cloud-Barista Authors.
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 main is the starting point of CB-Tumblebug
package main
import (
"bufio"
"encoding/csv"
"flag"
"fmt"
"os"
"strconv"
"sync"
"time"
//_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"github.com/spf13/viper"
"github.com/cloud-barista/cb-tumblebug/src/core/common"
"github.com/cloud-barista/cb-tumblebug/src/core/mcir"
"github.com/cloud-barista/cb-tumblebug/src/core/mcis"
restServer "github.com/cloud-barista/cb-tumblebug/src/api/rest/server"
"xorm.io/xorm"
"xorm.io/xorm/names"
)
// init for main
func init() {
profile := "cloud_conf"
setConfig(profile)
}
// setConfig get cloud settings from a config file
func setConfig(profile string) {
viper.AddConfigPath(".")
viper.AddConfigPath("./conf/")
viper.AddConfigPath("../conf/")
viper.SetConfigName(profile)
viper.SetConfigType("yaml")
err := viper.ReadInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}
err = viper.Unmarshal(&common.RuntimeConf)
if err != nil {
panic(err)
}
// const mrttArrayXMax = 300
// const mrttArrayYMax = 300
// common.RuntimeLatancyMap = make([][]string, mrttArrayXMax)
// cloudlatencymap.csv
file, fileErr := os.Open("../assets/cloudlatencymap.csv")
defer file.Close()
if fileErr != nil {
common.CBLog.Error(fileErr)
panic(fileErr)
}
rdr := csv.NewReader(bufio.NewReader(file))
common.RuntimeLatancyMap, _ = rdr.ReadAll()
for i, v := range common.RuntimeLatancyMap {
if i == 0 {
continue
}
if v[0] == "" {
break
}
common.RuntimeLatancyMapIndex[v[0]] = i
}
//fmt.Printf("RuntimeLatancyMap: %v\n\n", common.RuntimeLatancyMap)
fmt.Printf("[RuntimeLatancyMapIndex]\n %v\n", common.RuntimeLatancyMapIndex)
}
// Main Body
// @title CB-Tumblebug REST API
// @version latest
// @description CB-Tumblebug REST API
// @contact.name API Support
// @contact.url http://cloud-barista.github.io
// @contact.email contact-to-cloud-barista@googlegroups.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @BasePath /tumblebug
// @securityDefinitions.basic BasicAuth
func main() {
fmt.Println("")
// giving a default value of "1323"
port := flag.String("port", "1323", "port number for the restapiserver to listen to")
flag.Parse()
// validate arguments from flag
validationFlag := true
// validation: port
// set validationFlag to false if your number is not in [1-65535] range
if portInt, err := strconv.Atoi(*port); err == nil {
if portInt < 1 || portInt > 65535 {
validationFlag = false
}
} else {
validationFlag = false
}
if !validationFlag {
fmt.Printf("%s is not a valid port number.\n", *port)
fmt.Printf("Please retry with a valid port number (ex: -port=[1-65535]).\n")
os.Exit(1)
}
common.SpiderRestUrl = common.NVL(os.Getenv("SPIDER_REST_URL"), "http://localhost:1024/spider")
common.DragonflyRestUrl = common.NVL(os.Getenv("DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
common.DBUrl = common.NVL(os.Getenv("DB_URL"), "localhost:3306")
common.DBDatabase = common.NVL(os.Getenv("DB_DATABASE"), "cb_tumblebug")
common.DBUser = common.NVL(os.Getenv("DB_USER"), "cb_tumblebug")
common.DBPassword = common.NVL(os.Getenv("DB_PASSWORD"), "cb_tumblebug")
common.AutocontrolDurationMs = common.NVL(os.Getenv("AUTOCONTROL_DURATION_MS"), "10000")
// load the latest configuration from DB (if exist)
fmt.Println("")
fmt.Println("[Update system environment]")
common.UpdateGlobalVariable(common.StrDragonflyRestUrl)
common.UpdateGlobalVariable(common.StrSpiderRestUrl)
common.UpdateGlobalVariable(common.StrAutocontrolDurationMs)
// load config
//masterConfigInfos = confighandler.GetMasterConfigInfos()
//Setup database (meta_db/dat/cbtumblebug.s3db)
fmt.Println("")
fmt.Println("[Setup SQL Database]")
err := os.MkdirAll("../meta_db/dat/", os.ModePerm)
if err != nil {
fmt.Println(err.Error())
}
//err = common.OpenSQL("../meta_db/dat/cbtumblebug.s3db") // commented out to move to use XORM
common.ORM, err = xorm.NewEngine("sqlite3", "../meta_db/dat/cbtumblebug.s3db")
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Database access info set successfully")
}
//common.ORM.SetMapper(names.SameMapper{})
common.ORM.SetTableMapper(names.SameMapper{})
common.ORM.SetColumnMapper(names.SameMapper{})
/* // Required if using MySQL // Not required if using SQLite
err = common.SelectDatabase(common.DB_DATABASE)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("DB selected successfully..")
}
*/
// "CREATE Table IF NOT EXISTS spec(...)"
//err = common.CreateSpecTable() // commented out to move to use XORM
err = common.ORM.Sync2(new(mcir.TbSpecInfo))
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Table spec set successfully..")
}
// "CREATE Table IF NOT EXISTS image(...)"
//err = common.CreateImageTable() // commented out to move to use XORM
err = common.ORM.Sync2(new(mcir.TbImageInfo))
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Table image set successfully..")
}
err = common.ORM.Sync2(new(mcir.TbCustomImageInfo))
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println("Table customImage set successfully..")
}
//defer db.Close()
//Ticker for MCIS Orchestration Policy
fmt.Println("")
fmt.Println("[Initiate Multi-Cloud Orchestration]")
autoControlDuration, _ := strconv.Atoi(common.AutocontrolDurationMs) //ms
ticker := time.NewTicker(time.Millisecond * time.Duration(autoControlDuration))
go func() {
for t := range ticker.C {
//display ticker if you need (remove '_ = t')
_ = t
//fmt.Println("- Orchestration Controller ", t.Format("2006-01-02 15:04:05"))
mcis.OrchestrationController()
}
}()
defer ticker.Stop()
// Launch API servers (REST)
wg := new(sync.WaitGroup)
wg.Add(1)
// Start REST Server
go func() {
restServer.RunServer(*port)
wg.Done()
}()
// Note: Deprecated gRPC server
// Start gRPC Server
// go func() {
// grpcServer.RunServer()
// wg.Done()
// }()
// fmt.Println("RuntimeConf: ", common.RuntimeConf.Cloud)
wg.Wait()
}