/
main.go
145 lines (118 loc) · 4.07 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
package main
import (
"fmt"
"os"
"strconv"
"sync"
"time"
//_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"github.com/cloud-barista/poc-cicd-tumblebug/src/core/common"
"github.com/cloud-barista/poc-cicd-tumblebug/src/core/mcir"
"github.com/cloud-barista/poc-cicd-tumblebug/src/core/mcis"
grpcserver "github.com/cloud-barista/poc-cicd-tumblebug/src/api/grpc/server"
restapiserver "github.com/cloud-barista/poc-cicd-tumblebug/src/api/rest/server"
"xorm.io/xorm"
"xorm.io/xorm/names"
)
// 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
// @host localhost:1323
// @BasePath /tumblebug
// @securityDefinitions.basic BasicAuth
func main() {
fmt.Println("")
common.SPIDER_REST_URL = common.NVL(os.Getenv("SPIDER_REST_URL"), "http://localhost:1024/spider")
common.DRAGONFLY_REST_URL = common.NVL(os.Getenv("DRAGONFLY_REST_URL"), "http://localhost:9090/dragonfly")
common.DB_URL = common.NVL(os.Getenv("DB_URL"), "localhost:3306")
common.DB_DATABASE = common.NVL(os.Getenv("DB_DATABASE"), "cb_tumblebug")
common.DB_USER = common.NVL(os.Getenv("DB_USER"), "cb_tumblebug")
common.DB_PASSWORD = common.NVL(os.Getenv("DB_PASSWORD"), "cb_tumblebug")
common.AUTOCONTROL_DURATION_MS = 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.StrDRAGONFLY_REST_URL)
common.UpdateGlobalVariable(common.StrSPIDER_REST_URL)
common.UpdateGlobalVariable(common.StrAUTOCONTROL_DURATION_MS)
// 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..")
}
//defer db.Close()
//Ticker for MCIS Orchestration Policy
fmt.Println("")
fmt.Println("[Initiate Multi-Cloud Orchestration]")
autoControlDuration, _ := strconv.Atoi(common.AUTOCONTROL_DURATION_MS) //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 and gRPC)
wg := new(sync.WaitGroup)
wg.Add(2)
// Start REST Server
go func() {
restapiserver.ApiServer()
wg.Done()
}()
// Start gRPC Server
go func() {
grpcserver.RunServer()
//fmt.Println("gRPC server started on " + grpcserver.Port)
wg.Done()
}()
wg.Wait()
}