|
| 1 | +package mg5 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "io/ioutil" |
| 8 | + "os" |
| 9 | + "reflect" |
| 10 | +) |
| 11 | + |
| 12 | +// convFunc does an inplace conversion of the "datastore" |
| 13 | +// configuration (represted as a map[string]interface{}) from one |
| 14 | +// version to another |
| 15 | +type convFunc func(ds ciConfig) error |
| 16 | + |
| 17 | +// convertFile converts a config file from one version to another, the |
| 18 | +// converted config is stored in |
| 19 | +func convertFile(orig string, new string, convFunc convFunc) (ciConfig, error) { |
| 20 | + in, err := os.Open(orig) |
| 21 | + if err != nil { |
| 22 | + return ciConfig{}, err |
| 23 | + } |
| 24 | + out, err := os.Create(new) |
| 25 | + if err != nil { |
| 26 | + return ciConfig{}, err |
| 27 | + } |
| 28 | + return convert(in, out, convFunc) |
| 29 | +} |
| 30 | + |
| 31 | +// convert converts the config from one version to another, returns |
| 32 | +// the converted config as a map[string]interface{} |
| 33 | +func convert(in io.Reader, out io.Writer, convFunc convFunc) (ciConfig, error) { |
| 34 | + data, err := ioutil.ReadAll(in) |
| 35 | + if err != nil { |
| 36 | + return ciConfig{}, err |
| 37 | + } |
| 38 | + confMap := make(map[string]interface{}) |
| 39 | + err = json.Unmarshal(data, &confMap) |
| 40 | + if err != nil { |
| 41 | + return ciConfig{}, err |
| 42 | + } |
| 43 | + conf := newCiConfig(confMap) |
| 44 | + ds, _ := conf.get("datastore").(map[string]interface{}) |
| 45 | + if ds == nil { |
| 46 | + return ciConfig{}, fmt.Errorf("Datastore field missing or of the wrong type") |
| 47 | + } |
| 48 | + err = convFunc(newCiConfig(ds)) |
| 49 | + if err != nil { |
| 50 | + return ciConfig{}, err |
| 51 | + } |
| 52 | + fixed, err := json.MarshalIndent(confMap, "", " ") |
| 53 | + if err != nil { |
| 54 | + return ciConfig{}, err |
| 55 | + } |
| 56 | + out.Write(fixed) |
| 57 | + out.Write([]byte("\n")) |
| 58 | + return conf, nil |
| 59 | +} |
| 60 | + |
| 61 | +func ver5to6(ds ciConfig) error { |
| 62 | + noSyncVal := ds.get("nosync") |
| 63 | + if noSyncVal == nil { |
| 64 | + noSyncVal = interface{}(false) |
| 65 | + } |
| 66 | + noSync, ok := noSyncVal.(bool) |
| 67 | + if !ok { |
| 68 | + return fmt.Errorf("unsupported value for Datastore.NoSync fields: %v", noSyncVal) |
| 69 | + } |
| 70 | + ds.del("nosync") |
| 71 | + |
| 72 | + dsTypeVal := ds.get("type") |
| 73 | + if dsTypeVal == nil { |
| 74 | + dsTypeVal = interface{}("") |
| 75 | + } |
| 76 | + dsType, ok := dsTypeVal.(string) |
| 77 | + if !ok || (dsType != "default" && dsType != "leveldb" && dsType != "") { |
| 78 | + return fmt.Errorf("unsupported value for Datastore.Type fields: %s", dsType) |
| 79 | + } |
| 80 | + ds.del("type") |
| 81 | + |
| 82 | + // Path and Params never appear to have been used so just delete them |
| 83 | + ds.del("path") |
| 84 | + ds.del("params") |
| 85 | + |
| 86 | + ds.set("Spec", datastoreSpec(!noSync)) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func ver6to5(ds ciConfig) (err error) { |
| 91 | + defer func() { |
| 92 | + if r := recover(); r != nil { |
| 93 | + err = fmt.Errorf("incompatible config detected, downgrade not possible: %v", r.(error)) |
| 94 | + } |
| 95 | + }() |
| 96 | + spec := ds.get("Spec").(map[string]interface{}) |
| 97 | + if spec == nil { |
| 98 | + return fmt.Errorf("Datastore.Spec field missing or not a json object, can't downgrade") |
| 99 | + } |
| 100 | + mounts, _ := spec["mounts"].([]interface{}) |
| 101 | + if mounts == nil { |
| 102 | + return fmt.Errorf("Datastore.Spec.mounts field is missing or not an array") |
| 103 | + } |
| 104 | + var root, blocks interface{} |
| 105 | + sync := true |
| 106 | + for _, mount := range mounts { |
| 107 | + switch mountpoint := mount.(map[string]interface{})["mountpoint"].(string); mountpoint { |
| 108 | + case "/blocks": |
| 109 | + sync = mount.(map[string]interface{})["child"].(map[string]interface{})["sync"].(bool) |
| 110 | + blocks = mount |
| 111 | + case "/": |
| 112 | + root = mount |
| 113 | + default: |
| 114 | + return fmt.Errorf("unknown mountpoint: %s", mountpoint) |
| 115 | + } |
| 116 | + } |
| 117 | + // normalize spec |
| 118 | + spec["mounts"] = []interface{}{blocks, root} |
| 119 | + expected := datastoreSpec(sync) |
| 120 | + if !reflect.DeepEqual(spec, expected) { |
| 121 | + return fmt.Errorf("Datastore.Spec field not of a supported value, can't downgrade") |
| 122 | + } |
| 123 | + ds.del("Spec") |
| 124 | + ds.set("Type", "leveldb") |
| 125 | + ds.set("Params", nil) |
| 126 | + ds.set("NoSync", !sync) |
| 127 | + println("ver6to5 done!") |
| 128 | + return nil |
| 129 | +} |
| 130 | + |
| 131 | +func datastoreSpec(sync bool) map[string]interface{} { |
| 132 | + return map[string]interface{}{ |
| 133 | + "type": "mount", |
| 134 | + "mounts": []interface{}{ |
| 135 | + map[string]interface{}{ |
| 136 | + "mountpoint": "/blocks", |
| 137 | + "type": "measure", |
| 138 | + "prefix": "flatfs.datastore", |
| 139 | + "child": map[string]interface{}{ |
| 140 | + "type": "flatfs", |
| 141 | + "path": "blocks", |
| 142 | + "sync": sync, |
| 143 | + "shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", |
| 144 | + }, |
| 145 | + }, |
| 146 | + map[string]interface{}{ |
| 147 | + "mountpoint": "/", |
| 148 | + "type": "measure", |
| 149 | + "prefix": "leveldb.datastore", |
| 150 | + "child": map[string]interface{}{ |
| 151 | + "type": "levelds", |
| 152 | + "path": "datastore", |
| 153 | + "compression": "none", |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | +} |
0 commit comments