|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 NetEase Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: CurveAdm |
| 19 | + * Created Date: 2023-02-24 |
| 20 | + * Author: Lijin Xiong ([email protected]) |
| 21 | + */ |
| 22 | + |
| 23 | +package disks |
| 24 | + |
| 25 | +import ( |
| 26 | + "strings" |
| 27 | + |
| 28 | + "github.com/fatih/color" |
| 29 | + "github.com/opencurve/curveadm/cli/cli" |
| 30 | + "github.com/opencurve/curveadm/internal/common" |
| 31 | + comm "github.com/opencurve/curveadm/internal/common" |
| 32 | + "github.com/opencurve/curveadm/internal/configure/disks" |
| 33 | + "github.com/opencurve/curveadm/internal/configure/topology" |
| 34 | + "github.com/opencurve/curveadm/internal/errno" |
| 35 | + "github.com/opencurve/curveadm/internal/storage" |
| 36 | + "github.com/opencurve/curveadm/internal/tui" |
| 37 | + tuicomm "github.com/opencurve/curveadm/internal/tui/common" |
| 38 | + "github.com/opencurve/curveadm/internal/utils" |
| 39 | + "github.com/spf13/cobra" |
| 40 | +) |
| 41 | + |
| 42 | +const ( |
| 43 | + COMMIT_EXAMPLE = `Examples: |
| 44 | + $ curveadm disks commit /path/to/disks.yaml # Commit disks` |
| 45 | +) |
| 46 | + |
| 47 | +type commitOptions struct { |
| 48 | + filename string |
| 49 | + slient bool |
| 50 | +} |
| 51 | + |
| 52 | +func NewCommitCommand(curveadm *cli.CurveAdm) *cobra.Command { |
| 53 | + var options commitOptions |
| 54 | + cmd := &cobra.Command{ |
| 55 | + Use: "commit DISKS [OPTIONS]", |
| 56 | + Short: "Commit disks", |
| 57 | + Args: utils.ExactArgs(1), |
| 58 | + Example: COMMIT_EXAMPLE, |
| 59 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 60 | + options.filename = args[0] |
| 61 | + return runCommit(curveadm, options) |
| 62 | + }, |
| 63 | + DisableFlagsInUseLine: true, |
| 64 | + } |
| 65 | + |
| 66 | + flags := cmd.Flags() |
| 67 | + flags.BoolVarP(&options.slient, "slient", "s", false, "Slient output for disks commit") |
| 68 | + |
| 69 | + return cmd |
| 70 | +} |
| 71 | + |
| 72 | +func readAndCheckDisks(curveadm *cli.CurveAdm, options commitOptions) (string, []*disks.DiskConfig, error) { |
| 73 | + var dcs []*disks.DiskConfig |
| 74 | + // 1) read disks from file |
| 75 | + if !utils.PathExist(options.filename) { |
| 76 | + return "", dcs, errno.ERR_DISKS_FILE_NOT_FOUND. |
| 77 | + F("%s: no such file", utils.AbsPath(options.filename)) |
| 78 | + } |
| 79 | + data, err := utils.ReadFile(options.filename) |
| 80 | + if err != nil { |
| 81 | + return data, dcs, errno.ERR_READ_DISKS_FILE_FAILED.E(err) |
| 82 | + } |
| 83 | + |
| 84 | + // 2) display disks difference |
| 85 | + oldData := curveadm.Disks() |
| 86 | + if !options.slient { |
| 87 | + diff := utils.Diff(oldData, data) |
| 88 | + curveadm.WriteOutln(diff) |
| 89 | + } |
| 90 | + |
| 91 | + // 3) check disks data |
| 92 | + dcs, err = disks.ParseDisks(data, curveadm) |
| 93 | + return data, dcs, err |
| 94 | +} |
| 95 | + |
| 96 | +func assambleNewDiskRecords(dcs []*disks.DiskConfig, |
| 97 | + oldDiskRecords []storage.Disk) ([]storage.Disk, []storage.Disk) { |
| 98 | + keySep := ":" |
| 99 | + newDiskMap := make(map[string]bool) |
| 100 | + |
| 101 | + var newDiskRecords, diskRecordDeleteList []storage.Disk |
| 102 | + for _, dc := range dcs { |
| 103 | + for _, host := range dc.GetHost() { |
| 104 | + key := strings.Join([]string{host, dc.GetDevice()}, keySep) |
| 105 | + newDiskMap[key] = true |
| 106 | + newDiskRecords = append( |
| 107 | + newDiskRecords, storage.Disk{ |
| 108 | + Host: host, |
| 109 | + Device: dc.GetDevice(), |
| 110 | + Size: comm.DISK_DEFAULT_NULL_SIZE, |
| 111 | + URI: comm.DISK_DEFAULT_NULL_URI, |
| 112 | + MountPoint: dc.GetMountPoint(), |
| 113 | + FormatPercent: dc.GetFormatPercent(), |
| 114 | + ChunkServerID: comm.DISK_DEFAULT_NULL_CHUNKSERVER_ID, |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + for _, dr := range oldDiskRecords { |
| 120 | + key := strings.Join([]string{dr.Host, dr.Device}, keySep) |
| 121 | + if _, ok := newDiskMap[key]; !ok { |
| 122 | + diskRecordDeleteList = append(diskRecordDeleteList, dr) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return newDiskRecords, diskRecordDeleteList |
| 127 | +} |
| 128 | + |
| 129 | +func writeDiskRecord(dr storage.Disk, curveadm *cli.CurveAdm) error { |
| 130 | + if diskRecords, err := curveadm.Storage().GetDisk( |
| 131 | + common.DISK_FILTER_DEVICE, dr.Host, dr.Device); err != nil { |
| 132 | + return err |
| 133 | + } else if len(diskRecords) == 0 { |
| 134 | + if err := curveadm.Storage().SetDisk( |
| 135 | + dr.Host, |
| 136 | + dr.Device, |
| 137 | + dr.MountPoint, |
| 138 | + dr.ContainerImage, |
| 139 | + dr.FormatPercent); err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func syncDiskRecords(data string, dcs []*disks.DiskConfig, |
| 147 | + curveadm *cli.CurveAdm, options commitOptions) error { |
| 148 | + oldDiskRecords := curveadm.DiskRecords() |
| 149 | + tui.SortDiskRecords(oldDiskRecords) |
| 150 | + |
| 151 | + newDiskRecords, diskRecordDeleteList := assambleNewDiskRecords(dcs, oldDiskRecords) |
| 152 | + tui.SortDiskRecords(newDiskRecords) |
| 153 | + oldDiskRecordsString := tui.FormatDisks(oldDiskRecords) |
| 154 | + newDiskRecordsString := tui.FormatDisks(newDiskRecords) |
| 155 | + |
| 156 | + if !options.slient { |
| 157 | + diff := utils.Diff(oldDiskRecordsString, newDiskRecordsString) |
| 158 | + curveadm.WriteOutln(diff) |
| 159 | + } |
| 160 | + |
| 161 | + pass := tuicomm.ConfirmYes("Disk changes are showing above. Do you want to continue?") |
| 162 | + if !pass { |
| 163 | + curveadm.WriteOut(tuicomm.PromptCancelOpetation("commit disk table")) |
| 164 | + return errno.ERR_CANCEL_OPERATION |
| 165 | + } |
| 166 | + |
| 167 | + // write new disk records |
| 168 | + for _, dr := range newDiskRecords { |
| 169 | + if err := writeDiskRecord(dr, curveadm); err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // delete obsolete disk records |
| 175 | + for _, dr := range diskRecordDeleteList { |
| 176 | + if dr.ChunkServerID != comm.DISK_DEFAULT_NULL_CHUNKSERVER_ID { |
| 177 | + return errno.ERR_DELETE_SERVICE_BINDING_DISK. |
| 178 | + F("The disk[%s:%s] is used by service[%s:%s]", |
| 179 | + dr.Host, dr.Device, topology.ROLE_CHUNKSERVER, dr.ChunkServerID) |
| 180 | + } |
| 181 | + |
| 182 | + if err := curveadm.Storage().DeleteDisk(dr.Host, dr.Device); err != nil { |
| 183 | + return errno.ERR_UPDATE_DISK_FAILED.E(err) |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
| 189 | + |
| 190 | +func runCommit(curveadm *cli.CurveAdm, options commitOptions) error { |
| 191 | + // 1) read and check disks |
| 192 | + data, dcs, err := readAndCheckDisks(curveadm, options) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + |
| 197 | + // 2) confirm by user |
| 198 | + pass := tuicomm.ConfirmYes("Do you want to continue?") |
| 199 | + if !pass { |
| 200 | + curveadm.WriteOut(tuicomm.PromptCancelOpetation("commit disks")) |
| 201 | + return errno.ERR_CANCEL_OPERATION |
| 202 | + } |
| 203 | + |
| 204 | + // 3) add disk records |
| 205 | + err = syncDiskRecords(data, dcs, curveadm, options) |
| 206 | + if err != nil { |
| 207 | + return err |
| 208 | + } |
| 209 | + |
| 210 | + // 4) add disks data |
| 211 | + err = curveadm.Storage().SetDisks(data) |
| 212 | + if err != nil { |
| 213 | + return errno.ERR_UPDATE_DISKS_FAILED. |
| 214 | + F("commit disks failed") |
| 215 | + } |
| 216 | + |
| 217 | + // 5) print success prompt |
| 218 | + curveadm.WriteOutln(color.GreenString("Disks updated")) |
| 219 | + return nil |
| 220 | +} |
0 commit comments