forked from slntopp/nocloud-driver-ione
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
161 lines (130 loc) · 4.17 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
/*
Copyright © 2021-2022 Nikita Ivanovski [email protected]
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
import (
"context"
"fmt"
"github.com/slntopp/nocloud-driver-ione/pkg/actions"
epb "github.com/slntopp/nocloud-proto/events"
"net"
"github.com/go-redis/redis/v8"
"github.com/slntopp/nocloud/pkg/nocloud"
"github.com/spf13/viper"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/protobuf/proto"
"github.com/slntopp/nocloud-driver-ione/pkg/datas"
"github.com/slntopp/nocloud-driver-ione/pkg/server"
amqp "github.com/rabbitmq/amqp091-go"
billingpb "github.com/slntopp/nocloud-proto/billing"
pb "github.com/slntopp/nocloud-proto/drivers/instance/vanilla"
)
var (
port string
type_key string
log *zap.Logger
statesHost string
RabbitMQConn string
SIGNING_KEY []byte
redisHost string
)
func init() {
viper.AutomaticEnv()
log = nocloud.NewLogger()
viper.SetDefault("PORT", "8080")
port = viper.GetString("PORT")
viper.SetDefault("DRIVER_TYPE_KEY", "ione")
type_key = viper.GetString("DRIVER_TYPE_KEY")
viper.SetDefault("STATES_HOST", "states:8080")
statesHost = viper.GetString("STATES_HOST")
viper.SetDefault("RABBITMQ_CONN", "amqp://nocloud:secret@rabbitmq:5672/")
RabbitMQConn = viper.GetString("RABBITMQ_CONN")
viper.SetDefault("SIGNING_KEY", "seeeecreet")
SIGNING_KEY = []byte(viper.GetString("SIGNING_KEY"))
viper.SetDefault("REDIS_HOST", "redis:6379")
redisHost = viper.GetString("REDIS_HOST")
}
func main() {
defer func() {
_ = log.Sync()
}()
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
log.Fatal("Failed to listen", zap.String("address", port), zap.Error(err))
}
log.Info("Dialing RabbitMQ", zap.String("url", RabbitMQConn))
rbmq, err := amqp.Dial(RabbitMQConn)
if err != nil {
log.Fatal("Failed to connect to RabbitMQ", zap.Error(err))
}
defer rbmq.Close()
datas.Configure(log, rbmq)
actions.ConfigureStatusesClient(log)
s := grpc.NewServer()
server.SetDriverType(type_key)
rdb := redis.NewClient(&redis.Options{
Addr: redisHost,
DB: 0, // use default DB
})
log.Info("RedisDB connection established")
srv := server.NewDriverServiceServer(log.Named("IONe Driver"), SIGNING_KEY, rdb)
srv.HandlePublishRecords = SetupRecordsPublisher(rbmq)
srv.HandlePublishEvents = SetupEventPublisher(rbmq)
pb.RegisterDriverServiceServer(s, srv)
log.Info(fmt.Sprintf("Serving gRPC on 0.0.0.0:%v", port))
log.Fatal("Failed to serve gRPC", zap.Error(s.Serve(lis)))
}
func SetupRecordsPublisher(rbmq *amqp.Connection) server.RecordsPublisherFunc {
return func(ctx context.Context, payload []*billingpb.Record) {
ch, err := rbmq.Channel()
if err != nil {
log.Fatal("Failed to open a channel", zap.Error(err))
}
defer ch.Close()
queue, _ := ch.QueueDeclare(
"records",
true, false, false, true, nil,
)
for _, record := range payload {
body, err := proto.Marshal(record)
if err != nil {
log.Error("Error while marshalling record", zap.Error(err))
continue
}
ch.PublishWithContext(ctx, "", queue.Name, false, false, amqp.Publishing{
ContentType: "text/plain", Body: body,
})
}
}
}
func SetupEventPublisher(rbmq *amqp.Connection) server.EventsPublisherFunc {
return func(ctx context.Context, event *epb.Event) {
ch, err := rbmq.Channel()
if err != nil {
log.Fatal("Failed to open a channel", zap.Error(err))
}
defer ch.Close()
queue, _ := ch.QueueDeclare(
"events",
true, false, false, true, nil,
)
body, err := proto.Marshal(event)
if err != nil {
log.Error("Error while marshalling record", zap.Error(err))
return
}
ch.PublishWithContext(ctx, "", queue.Name, false, false, amqp.Publishing{
ContentType: "text/plain", Body: body,
})
}
}