-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
247 lines (196 loc) · 5.57 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
243
244
245
246
247
// Copyright (c) 2018 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"cloud.google.com/go/storage"
"golang.org/x/oauth2/google"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
"github.com/rcrowley/go-metrics"
"github.com/intel-hpdd/lemur/dmplugin"
"github.com/intel-hpdd/lemur/pkg/fsroot"
"github.com/intel-hpdd/logging/alert"
"github.com/intel-hpdd/logging/audit"
"github.com/intel-hpdd/logging/debug"
)
type (
archiveConfig struct {
Name string `hcl:",key"`
ID int
Bucket string
Prefix string
Credentials *google.Credentials
}
// ArchiveSet is a list of mover configs.
archiveSet []*archiveConfig
gcsConfig struct {
NumThreads int `hcl:"num_threads"`
ServiceAccountKey string `hcl:"service_account_key"`
Archives archiveSet `hcl:"archive"`
}
)
// Should this be configurable?
const updateInterval = 10 * time.Second
var rate metrics.Meter
func (c *gcsConfig) String() string {
return dmplugin.DisplayConfig(c)
}
func (a *archiveConfig) String() string {
return fmt.Sprintf("%d:%s:%s", a.ID, a.Bucket, a.Prefix)
}
func init() {
rate = metrics.NewMeter()
// if debug.Enabled() {
go func() {
var lastCount int64
for {
if lastCount != rate.Count() {
audit.Logf("total %s (1 min/5 min/15 min/inst): %s/%s/%s/%s msg/sec\n",
humanize.Comma(rate.Count()),
humanize.Comma(int64(rate.Rate1())),
humanize.Comma(int64(rate.Rate5())),
humanize.Comma(int64(rate.Rate15())),
humanize.Comma(int64(rate.RateMean())),
)
lastCount = rate.Count()
}
time.Sleep(10 * time.Second)
}
}()
// }
}
// CheckValid determines if the archive configuration is a valid one.
func (a *archiveConfig) checkValid() error {
var errs []string
if a.Bucket == "" {
errs = append(errs, fmt.Sprintf("Archive %s: archive bucket not set", a.Name))
}
if a.ID < 1 {
errs = append(errs, fmt.Sprintf("Archive %s: archive id not set", a.Name))
}
if len(errs) > 0 {
return errors.Errorf("Errors: %s", strings.Join(errs, ", "))
}
return nil
}
func (a *archiveConfig) checkGCSAccess() error {
ctx := context.Background()
// Creates a client.
client, err := storage.NewClient(ctx, option.WithCredentials(a.Credentials))
if err != nil {
return errors.Wrap(err, "Failed to create client")
}
b := client.Bucket(a.Bucket)
it := b.Objects(ctx, nil)
if _, err := it.Next(); err != iterator.Done && err != nil {
return errors.Wrap(err, "Unable to list GCS bucket objects")
}
return nil
}
func (a *archiveConfig) createCredentials(ctx context.Context, cfg *gcsConfig) error {
var errs error
if cfg.ServiceAccountKey != "" {
data, err := ioutil.ReadFile(cfg.ServiceAccountKey)
if err != nil {
return errors.Wrap(err, "Unable to read the service account key file")
}
a.Credentials, errs = google.CredentialsFromJSON(ctx, data, "https://www.googleapis.com/auth/devstorage.read_write")
if errs != nil {
return errors.Wrap(errs, "Failed to get credentials from service account key file")
}
} else {
a.Credentials, errs = google.FindDefaultCredentials(ctx, storage.ScopeReadWrite)
if errs != nil {
return errors.Wrap(errs, "No default credentials found")
}
}
return nil
}
func getMergedConfig(plugin *dmplugin.Plugin) (*gcsConfig, error) {
var cfg *gcsConfig
cfg = new(gcsConfig)
err := dmplugin.LoadConfig(plugin.ConfigFile(), &cfg)
if err != nil {
return nil, fmt.Errorf("Failed to load config: %s", err)
}
return cfg, nil
}
func main() {
plugin, err := dmplugin.New(path.Base(os.Args[0]), func(path string) (fsroot.Client, error) {
return fsroot.New(path)
})
if err != nil {
alert.Abort(errors.Wrap(err, "create plugin failed"))
}
cfg, err := getMergedConfig(plugin)
if err != nil {
alert.Abort(errors.Wrap(err, "Unable to determine plugin configuration"))
}
debug.Printf("GCS Mover configuration:\n%v", cfg)
if len(cfg.Archives) == 0 {
alert.Abort(errors.New("Invalid configuration: No archives defined"))
}
//Create context
ctx := context.Background()
for _, archive := range cfg.Archives {
debug.Print(archive)
if err := archive.createCredentials(ctx, cfg); err != nil {
alert.Abort(errors.Wrap(err, "Unable create credentials"))
}
if err := archive.checkValid(); err != nil {
alert.Abort(errors.Wrap(err, "Invalid configuration"))
}
if err := archive.checkGCSAccess(); err != nil {
alert.Abort(errors.Wrap(err, "GCS access check failed"))
}
}
debug.Printf("GCS configuration:\n%v", cfg)
// All base filesystem operations will be relative to current directory
err = os.Chdir(plugin.Base())
if err != nil {
alert.Abort(errors.Wrap(err, "chdir failed"))
}
interruptHandler(func() {
plugin.Stop()
})
for _, archive := range cfg.Archives {
// Creates a client.
client, err := storage.NewClient(ctx, option.WithCredentials(archive.Credentials))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
plugin.AddMover(&dmplugin.Config{
Mover: GcsMover(archive, ctx, client),
NumThreads: cfg.NumThreads,
ArchiveID: uint32(archive.ID),
})
}
plugin.Run()
}
func interruptHandler(once func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
stopping := false
for sig := range c {
debug.Printf("signal received: %s", sig)
if !stopping {
stopping = true
once()
}
}
}()
}