forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
120 lines (101 loc) · 3.07 KB
/
migrate.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
package host
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"github.com/docker/machine/drivers/none"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/version"
)
var (
errConfigFromFuture = errors.New("config version is from the future -- you should upgrade your Docker Machine client")
)
type RawDataDriver struct {
*none.Driver
Data []byte // passed directly back when invoking json.Marshal on this type
}
func (r *RawDataDriver) MarshalJSON() ([]byte, error) {
return r.Data, nil
}
func (r *RawDataDriver) UnmarshalJSON(data []byte) error {
r.Data = data
return nil
}
func getMigratedHostMetadata(data []byte) (*Metadata, error) {
// HostMetadata is for a "first pass" so we can then load the driver
var (
hostMetadata *MetadataV0
)
if err := json.Unmarshal(data, &hostMetadata); err != nil {
return &Metadata{}, err
}
migratedHostMetadata := MigrateHostMetadataV0ToHostMetadataV1(hostMetadata)
return migratedHostMetadata, nil
}
func MigrateHost(h *Host, data []byte) (*Host, bool, error) {
var (
migrationNeeded = false
migrationPerformed = false
hostV1 *V1
hostV2 *V2
)
migratedHostMetadata, err := getMigratedHostMetadata(data)
if err != nil {
return nil, false, err
}
globalStorePath := filepath.Dir(filepath.Dir(migratedHostMetadata.HostOptions.AuthOptions.StorePath))
driver := &RawDataDriver{none.NewDriver(h.Name, globalStorePath), nil}
if migratedHostMetadata.ConfigVersion > version.ConfigVersion {
return nil, false, errConfigFromFuture
}
if migratedHostMetadata.ConfigVersion == version.ConfigVersion {
h.Driver = driver
if err := json.Unmarshal(data, &h); err != nil {
return nil, migrationPerformed, fmt.Errorf("Error unmarshalling most recent host version: %s", err)
}
} else {
migrationNeeded = true
}
if migrationNeeded {
migrationPerformed = true
for h.ConfigVersion = migratedHostMetadata.ConfigVersion; h.ConfigVersion < version.ConfigVersion; h.ConfigVersion++ {
log.Debugf("Migrating to config v%d", h.ConfigVersion)
switch h.ConfigVersion {
case 0:
hostV0 := &V0{
Driver: driver,
}
if err := json.Unmarshal(data, &hostV0); err != nil {
return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 0: %s", err)
}
hostV1 = MigrateHostV0ToHostV1(hostV0)
case 1:
if hostV1 == nil {
hostV1 = &V1{
Driver: driver,
}
if err := json.Unmarshal(data, &hostV1); err != nil {
return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 1: %s", err)
}
}
hostV2 = MigrateHostV1ToHostV2(hostV1)
case 2:
if hostV2 == nil {
hostV2 = &V2{
Driver: driver,
}
if err := json.Unmarshal(data, &hostV2); err != nil {
return nil, migrationPerformed, fmt.Errorf("Error unmarshalling host config version 2: %s", err)
}
}
h = MigrateHostV2ToHostV3(hostV2, data, globalStorePath)
driver.Data = h.RawDriver
h.Driver = driver
case 3:
}
}
}
h.RawDriver = driver.Data
return h, migrationPerformed, nil
}