This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CBSpiderGRPCRuntime.go
153 lines (126 loc) · 4.27 KB
/
CBSpiderGRPCRuntime.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
// gRPC Runtime of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2020.09.
package grpcruntime
import (
"errors"
"fmt"
"net"
"os"
cr "github.com/cloud-barista/poc-cicd-spider/api-runtime/common-runtime"
gc "github.com/cloud-barista/poc-cicd-spider/api-runtime/grpc-runtime/common"
"github.com/cloud-barista/poc-cicd-spider/api-runtime/grpc-runtime/config"
"github.com/cloud-barista/poc-cicd-spider/api-runtime/grpc-runtime/logger"
grpc_service "github.com/cloud-barista/poc-cicd-spider/api-runtime/grpc-runtime/service"
pb "github.com/cloud-barista/poc-cicd-spider/api-runtime/grpc-runtime/stub/cbspider"
"google.golang.org/grpc/reflection"
)
// RunServer - GRPC 서버 실행..
func RunServer() {
logger := logger.NewLogger()
cbspiderRoot := os.Getenv("CBSPIDER_ROOT")
if cbspiderRoot == "" {
logger.Error("$CBSPIDER_ROOT is not set!!")
os.Exit(1)
}
configPath := cbspiderRoot + "/conf/grpc_conf.yaml"
gConf, err := configLoad(configPath)
if err != nil {
logger.Error("failed to load config : ", err)
return
}
spidersrv := gConf.GSL.SpiderSrv
conn, err := net.Listen("tcp", spidersrv.Addr)
if err != nil {
logger.Error("failed to listen: ", err)
return
}
cbserver, closer, err := gc.NewCBServer(spidersrv)
if err != nil {
logger.Error("failed to create grpc server: ", err)
return
}
if closer != nil {
defer closer.Close()
}
gs := cbserver.Server
pb.RegisterCIMServer(gs, &grpc_service.CIMService{})
pb.RegisterCCMServer(gs, &grpc_service.CCMService{})
pb.RegisterSSHServer(gs, &grpc_service.SSHService{})
if spidersrv.Reflection == "enable" {
if spidersrv.Interceptors != nil && spidersrv.Interceptors.AuthJWT != nil {
fmt.Printf("\n\n*** you can run reflection when jwt auth interceptor is not used ***\n\n")
} else {
reflection.Register(gs)
}
}
//fmt.Printf("\n\n => grpc server started on %s\n\n", spidersrv.Addr)
cr.GoServicePort = spidersrv.Addr
spiderBanner(cr.ServiceIPorName + spidersrv.Addr)
if err := gs.Serve(conn); err != nil {
logger.Error("failed to serve: ", err)
}
}
func spiderBanner(server string) {
gRPCServer := "Go API: grpc://" + server
fmt.Printf(" - %s\n", gRPCServer)
}
func configLoad(cf string) (config.GrpcConfig, error) {
logger := logger.NewLogger()
// Viper 를 사용하는 설정 파서 생성
parser := config.MakeParser()
var (
gConf config.GrpcConfig
err error
)
if cf == "" {
logger.Error("Please, provide the path to your configuration file")
return gConf, errors.New("configuration file are not specified")
}
logger.Debug("Parsing configuration file: ", cf)
if gConf, err = parser.GrpcParse(cf); err != nil {
logger.Error("ERROR - Parsing the configuration file.\n", err.Error())
return gConf, err
}
// Command line 에 지정된 옵션을 설정에 적용 (우선권)
// SPIDER 필수 입력 항목 체크
spidersrv := gConf.GSL.SpiderSrv
if spidersrv == nil {
return gConf, errors.New("spidersrv field are not specified")
}
if spidersrv.Addr == "" {
return gConf, errors.New("spidersrv.addr field are not specified")
}
if spidersrv.TLS != nil {
if spidersrv.TLS.TLSCert == "" {
return gConf, errors.New("spidersrv.tls.tls_cert field are not specified")
}
if spidersrv.TLS.TLSKey == "" {
return gConf, errors.New("spidersrv.tls.tls_key field are not specified")
}
}
if spidersrv.Interceptors != nil {
if spidersrv.Interceptors.AuthJWT != nil {
if spidersrv.Interceptors.AuthJWT.JWTKey == "" {
return gConf, errors.New("spidersrv.interceptors.auth_jwt.jwt_key field are not specified")
}
}
if spidersrv.Interceptors.PrometheusMetrics != nil {
if spidersrv.Interceptors.PrometheusMetrics.ListenPort == 0 {
return gConf, errors.New("spidersrv.interceptors.prometheus_metrics.listen_port field are not specified")
}
}
if spidersrv.Interceptors.Opentracing != nil {
if spidersrv.Interceptors.Opentracing.Jaeger != nil {
if spidersrv.Interceptors.Opentracing.Jaeger.Endpoint == "" {
return gConf, errors.New("spidersrv.interceptors.opentracing.jaeger.endpoint field are not specified")
}
}
}
}
return gConf, nil
}