This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
189 lines (159 loc) · 5.24 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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"time"
"code.cloudfoundry.org/bbs"
"code.cloudfoundry.org/cfhttp"
"code.cloudfoundry.org/clock"
"code.cloudfoundry.org/consuladapter"
"code.cloudfoundry.org/debugserver"
"code.cloudfoundry.org/diego-ssh/keys"
"code.cloudfoundry.org/lager"
"code.cloudfoundry.org/lager/lagerflags"
"code.cloudfoundry.org/runtimeschema/cc_messages/flags"
"github.com/cloudfoundry/dropsonde"
"github.com/nu7hatch/gouuid"
"github.com/tedsuo/ifrit"
"github.com/tedsuo/ifrit/grouper"
"github.com/tedsuo/ifrit/sigmon"
"code.cloudfoundry.org/nsync"
"code.cloudfoundry.org/nsync/bulk"
"code.cloudfoundry.org/nsync/config"
"code.cloudfoundry.org/nsync/recipebuilder"
)
var configPath = flag.String(
"configPath",
"",
"path to config",
)
const (
dropsondeOrigin = "nsync_bulker"
)
func main() {
flag.Parse()
bulkerConfig, err := config.NewBulkerConfig(*configPath)
if err != nil {
panic(err.Error())
}
lifecycles := flags.LifecycleMap{}
for _, value := range bulkerConfig.Lifecycles {
lifecycles.Set(value)
}
logger, reconfigurableSink := lagerflags.NewFromConfig("nsync-bulker", bulkerConfig.LagerConfig)
initializeDropsonde(logger, bulkerConfig)
cfhttp.Initialize(time.Duration(bulkerConfig.CommunicationTimeout))
serviceClient := initializeServiceClient(logger, bulkerConfig)
uuid, err := uuid.NewV4()
if err != nil {
logger.Fatal("Couldn't generate uuid", err)
}
lockMaintainer := serviceClient.NewNsyncBulkerLockRunner(logger, uuid.String(), time.Duration(bulkerConfig.LockRetryInterval), time.Duration(bulkerConfig.LockTTL))
dockerRecipeBuilderConfig := recipebuilder.Config{
Lifecycles: lifecycles,
FileServerURL: bulkerConfig.FileServerUrl,
KeyFactory: keys.RSAKeyPairFactory,
}
buildpackRecipeBuilderConfig := recipebuilder.Config{
Lifecycles: lifecycles,
FileServerURL: bulkerConfig.FileServerUrl,
KeyFactory: keys.RSAKeyPairFactory,
PrivilegedContainers: bulkerConfig.PrivilegedContainers,
}
recipeBuilders := map[string]recipebuilder.RecipeBuilder{
"buildpack": recipebuilder.NewBuildpackRecipeBuilder(logger, buildpackRecipeBuilderConfig),
"docker": recipebuilder.NewDockerRecipeBuilder(logger, dockerRecipeBuilderConfig),
}
lrpRunner := bulk.NewLRPProcessor(
logger,
initializeBBSClient(logger, bulkerConfig),
time.Duration(bulkerConfig.CCPollingInterval),
time.Duration(bulkerConfig.DomainTTL),
bulkerConfig.CCBulkBatchSize,
bulkerConfig.BBSUpdateLRPWorkers,
bulkerConfig.SkipCertVerify,
&bulk.CCFetcher{
BaseURI: bulkerConfig.CCBaseUrl,
BatchSize: int(bulkerConfig.CCBulkBatchSize),
Username: bulkerConfig.CCUsername,
Password: bulkerConfig.CCPassword,
},
recipeBuilders,
clock.NewClock(),
)
taskRunner := bulk.NewTaskProcessor(
logger,
initializeBBSClient(logger, bulkerConfig),
&bulk.CCTaskClient{},
time.Duration(bulkerConfig.CCPollingInterval),
time.Duration(bulkerConfig.DomainTTL),
bulkerConfig.BBSFailTaskPoolSize,
bulkerConfig.BBSCancelTaskPoolSize,
bulkerConfig.SkipCertVerify,
&bulk.CCFetcher{
BaseURI: bulkerConfig.CCBaseUrl,
BatchSize: int(bulkerConfig.CCBulkBatchSize),
Username: bulkerConfig.CCUsername,
Password: bulkerConfig.CCPassword,
},
clock.NewClock(),
)
members := grouper.Members{
{"lock-maintainer", lockMaintainer},
{"lrp-runner", lrpRunner},
{"task-runner", taskRunner},
}
if dbgAddr := bulkerConfig.DebugServerConfig.DebugAddress; dbgAddr != "" {
members = append(grouper.Members{
{"debug-server", debugserver.Runner(dbgAddr, reconfigurableSink)},
}, members...)
}
group := grouper.NewOrdered(os.Interrupt, members)
logger.Info("waiting-for-lock")
monitor := ifrit.Invoke(sigmon.New(group))
logger.Info("started")
err = <-monitor.Wait()
if err != nil {
logger.Error("exited-with-failure", err)
os.Exit(1)
}
logger.Info("exited")
os.Exit(0)
}
func initializeDropsonde(logger lager.Logger, bulkerConfig config.BulkerConfig) {
dropsondeDestination := fmt.Sprint("localhost:", bulkerConfig.DropsondePort)
err := dropsonde.Initialize(dropsondeDestination, dropsondeOrigin)
if err != nil {
logger.Error("failed to initialize dropsonde: %v", err)
}
}
func initializeServiceClient(logger lager.Logger, bulkerConfig config.BulkerConfig) nsync.ServiceClient {
consulClient, err := consuladapter.NewClientFromUrl(bulkerConfig.ConsulCluster)
if err != nil {
logger.Fatal("new-client-failed", err)
}
return nsync.NewServiceClient(consulClient, clock.NewClock())
}
func initializeBBSClient(logger lager.Logger, bulkerConfig config.BulkerConfig) bbs.Client {
bbsURL, err := url.Parse(bulkerConfig.BBSAddress)
if err != nil {
logger.Fatal("Invalid BBS URL", err)
}
if bbsURL.Scheme != "https" {
var cfg bbs.ClientConfig
cfg.URL = bulkerConfig.BBSAddress
cfg.IsTLS = false
bbsClient, err := bbs.NewClientWithConfig(cfg)
if err != nil {
logger.Fatal("Failed to configure insecure BBS client", err)
}
return bbsClient
}
bbsClient, err := bbs.NewClient(bulkerConfig.BBSAddress, bulkerConfig.BBSCACert, bulkerConfig.BBSClientCert, bulkerConfig.BBSClientKey, bulkerConfig.BBSClientSessionCacheSize, bulkerConfig.BBSMaxIdleConnsPerHost)
if err != nil {
logger.Fatal("Failed to configure secure BBS client", err)
}
return bbsClient
}