-
Notifications
You must be signed in to change notification settings - Fork 491
/
metadata.go
410 lines (349 loc) · 10.5 KB
/
metadata.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package backups
import (
"bytes"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"io"
"math"
"os"
"time"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/utils/v3/filestorage"
"github.com/juju/version/v2"
"github.com/juju/juju/controller"
jujuversion "github.com/juju/juju/version"
)
// checksumFormat identifies how to interpret the checksum for a backup
// generated with this version of juju.
const checksumFormat = "SHA-1, base64 encoded"
// Origin identifies where a backup archive came from. While it is
// more about where and Metadata about what and when, that distinction
// does not merit special consideration. Instead, Origin exists
// separately from Metadata due to its use as an argument when
// requesting the creation of a new backup.
type Origin struct {
Model string
Machine string
Hostname string
Version version.Number
Base string
}
// UnknownString is a marker value for string fields with unknown values.
const UnknownString = "<unknown>"
// UnknownVersion is a marker value for version fields with unknown values.
var UnknownVersion = version.MustParse("9999.9999.9999")
// UnknownInt64 is a marker value for int64 fields with unknown values.
var UnknownInt64 = int64(math.MaxInt64)
// UnknownOrigin returns a new backups origin with unknown values.
func UnknownOrigin() Origin {
return Origin{
Model: UnknownString,
Machine: UnknownString,
Hostname: UnknownString,
Version: UnknownVersion,
}
}
// UnknownController returns a new backups origin with unknown values.
func UnknownController() ControllerMetadata {
return ControllerMetadata{
UUID: UnknownString,
MachineID: UnknownString,
MachineInstanceID: UnknownString,
HANodes: UnknownInt64,
}
}
// Metadata contains the metadata for a single state backup archive.
type Metadata struct {
*filestorage.FileMetadata
// Started records when the backup was started.
Started time.Time
// Finished records when the backup was complete.
Finished *time.Time
// Origin identifies where the backup was created.
Origin Origin
// Notes is an optional user-supplied annotation.
Notes string
// FormatVersion stores format version of these metadata.
FormatVersion int64
// Controller contains metadata about the controller where the backup was taken.
Controller ControllerMetadata
}
// ControllerMetadata contains controller specific metadata.
type ControllerMetadata struct {
// UUID contains the controller UUID.
UUID string
// MachineID contains controller machine id from which this backup is taken.
MachineID string
// MachineInstanceID contains controller machine's instance id from which this backup is taken.
MachineInstanceID string
// HANodes contains the number of nodes in this controller's HA configuration.
HANodes int64
}
// All un-versioned metadata is considered to be version 0,
// so the versions start with 1.
const currentFormatVersion = 1
// NewMetadata returns a new Metadata for a state backup archive,
// in the most current format.
func NewMetadata() *Metadata {
return &Metadata{
FileMetadata: filestorage.NewMetadata(),
// TODO(fwereade): 2016-03-17 lp:1558657
Started: time.Now().UTC(),
Origin: Origin{
Version: jujuversion.Current,
},
FormatVersion: currentFormatVersion,
Controller: ControllerMetadata{},
}
}
type backend interface {
ModelTag() names.ModelTag
ControllerConfig() (controller.Config, error)
StateServingInfo() (controller.StateServingInfo, error)
}
// NewMetadataState composes a new backup metadata based on the current Juju state.
func NewMetadataState(db backend, machine, base string) (*Metadata, error) {
hostname, err := os.Hostname()
if err != nil {
// If os.Hostname() is not working, something is woefully wrong.
return nil, errors.Annotate(err, "could not get hostname (system unstable?)")
}
meta := NewMetadata()
meta.Origin.Model = db.ModelTag().Id()
meta.Origin.Machine = machine
meta.Origin.Hostname = hostname
meta.Origin.Base = base
controllerCfg, err := db.ControllerConfig()
if err != nil {
return nil, errors.Annotate(err, "could not get controller config")
}
meta.Controller.UUID = controllerCfg.ControllerUUID()
return meta, nil
}
// MarkComplete populates the remaining metadata values. The default
// checksum format is used.
func (m *Metadata) MarkComplete(size int64, checksum string) error {
if size == 0 {
return errors.New("missing size")
}
if checksum == "" {
return errors.New("missing checksum")
}
format := checksumFormat
// TODO(fwereade): 2016-03-17 lp:1558657
finished := time.Now().UTC()
if err := m.SetFileInfo(size, checksum, format); err != nil {
return errors.Annotate(err, "unexpected failure")
}
m.Finished = &finished
return nil
}
// flatMetadataV0 contains old, un-versioned format of backup, aka v0.
type flatMetadataV0 struct {
ID string
// file storage
Checksum string
ChecksumFormat string
Size int64
Stored time.Time
// backup
Started time.Time
Finished time.Time
Notes string
Environment string
Machine string
Hostname string
Version version.Number
Base string
CACert string
CAPrivateKey string
}
func (flat *flatMetadataV0) inflate() (*Metadata, error) {
meta := NewMetadata()
meta.SetID(flat.ID)
meta.FormatVersion = 0
err := meta.SetFileInfo(flat.Size, flat.Checksum, flat.ChecksumFormat)
if err != nil {
return nil, errors.Trace(err)
}
if !flat.Stored.IsZero() {
meta.SetStored(&flat.Stored)
}
meta.Started = flat.Started
if !flat.Finished.IsZero() {
meta.Finished = &flat.Finished
}
meta.Notes = flat.Notes
meta.Origin = Origin{
Model: flat.Environment,
Machine: flat.Machine,
Hostname: flat.Hostname,
Version: flat.Version,
Base: flat.Base,
}
return meta, nil
}
// flatMetadata contains the latest format of the backup.
// NOTE If any changes need to be made here, rename this struct to
// reflect version 1, for example flatMetadataV1 and construct
// new flatMetadata with desired modifications.
type flatMetadata struct {
ID string
FormatVersion int64
// file storage
Checksum string
ChecksumFormat string
Size int64
Stored time.Time
// backup
Started time.Time
Finished time.Time
Notes string
ModelUUID string
Machine string
Hostname string
Version version.Number
Base string
ControllerUUID string
HANodes int64
ControllerMachineID string
ControllerMachineInstanceID string
}
func (m *Metadata) flat() flatMetadata {
flat := flatMetadata{
ID: m.ID(),
Checksum: m.Checksum(),
ChecksumFormat: m.ChecksumFormat(),
Size: m.Size(),
Started: m.Started,
Notes: m.Notes,
ModelUUID: m.Origin.Model,
Machine: m.Origin.Machine,
Hostname: m.Origin.Hostname,
Version: m.Origin.Version,
Base: m.Origin.Base,
FormatVersion: m.FormatVersion,
ControllerUUID: m.Controller.UUID,
ControllerMachineID: m.Controller.MachineID,
ControllerMachineInstanceID: m.Controller.MachineInstanceID,
HANodes: m.Controller.HANodes,
}
stored := m.Stored()
if stored != nil {
flat.Stored = *stored
}
if m.Finished != nil {
flat.Finished = *m.Finished
}
return flat
}
func (flat *flatMetadata) inflate() (*Metadata, error) {
meta := NewMetadata()
meta.SetID(flat.ID)
meta.FormatVersion = flat.FormatVersion
err := meta.SetFileInfo(flat.Size, flat.Checksum, flat.ChecksumFormat)
if err != nil {
return nil, errors.Trace(err)
}
if !flat.Stored.IsZero() {
meta.SetStored(&flat.Stored)
}
meta.Started = flat.Started
if !flat.Finished.IsZero() {
meta.Finished = &flat.Finished
}
meta.Notes = flat.Notes
meta.Origin = Origin{
Model: flat.ModelUUID,
Machine: flat.Machine,
Hostname: flat.Hostname,
Version: flat.Version,
Base: flat.Base,
}
meta.Controller = ControllerMetadata{
UUID: flat.ControllerUUID,
MachineID: flat.ControllerMachineID,
MachineInstanceID: flat.ControllerMachineInstanceID,
HANodes: flat.HANodes,
}
return meta, nil
}
// AsJSONBuffer returns a bytes.Buffer containing the JSON-ified metadata.
// This will always produce latest known format.
func (m *Metadata) AsJSONBuffer() (io.Reader, error) {
var outfile bytes.Buffer
if err := json.NewEncoder(&outfile).Encode(m.flat()); err != nil {
return nil, errors.Trace(err)
}
return &outfile, nil
}
// NewMetadataJSONReader extracts a new metadata from the JSON file.
func NewMetadataJSONReader(in io.Reader) (*Metadata, error) {
data, err := io.ReadAll(in)
if err != nil {
return nil, errors.Trace(err)
}
// We always want to decode into the most recent format version.
var flat flatMetadata
if err := json.Unmarshal(data, &flat); err != nil {
return nil, errors.Trace(err)
}
// Cater for old backup files, taken as version 0 or with no version.
switch flat.FormatVersion {
case 0:
{
var v0 flatMetadataV0
if err := json.Unmarshal(data, &v0); err != nil {
return nil, errors.Trace(err)
}
return v0.inflate()
}
case 1:
return flat.inflate()
default:
return nil, errors.NotSupportedf("backup format %d", flat.FormatVersion)
}
}
func fileTimestamp(fi os.FileInfo) time.Time {
timestamp := creationTime(fi)
if !timestamp.IsZero() {
return timestamp
}
// Fall back to modification time.
return fi.ModTime()
}
// BuildMetadata generates the metadata for a backup archive file.
func BuildMetadata(file *os.File) (*Metadata, error) {
// Extract the file size.
fi, err := file.Stat()
if err != nil {
return nil, errors.Trace(err)
}
size := fi.Size()
// Extract the timestamp.
timestamp := fileTimestamp(fi)
// Get the checksum.
hasher := sha1.New()
_, err = io.Copy(hasher, file)
if err != nil {
return nil, errors.Trace(err)
}
rawsum := hasher.Sum(nil)
checksum := base64.StdEncoding.EncodeToString(rawsum)
// Build the metadata.
meta := NewMetadata()
meta.Started = time.Time{}
meta.Origin = UnknownOrigin()
meta.FormatVersion = UnknownInt64
meta.Controller = UnknownController()
err = meta.MarkComplete(size, checksum)
if err != nil {
return nil, errors.Trace(err)
}
meta.Finished = ×tamp
return meta, nil
}