Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | flags "github.com/jessevdk/go-flags" |
| 23 | "github.com/opencord/cordctl/format" |
| 24 | "time" |
| 25 | ) |
| 26 | |
| 27 | const ( |
| 28 | DEFAULT_BACKUP_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}" |
| 29 | ) |
| 30 | |
| 31 | type BackupOutput struct { |
| 32 | Status string `json:"status"` |
| 33 | Checksum string `json:"checksum"` |
| 34 | Chunks int `json:"chunks"` |
| 35 | Bytes int `json:"bytes"` |
| 36 | } |
| 37 | |
| 38 | type BackupCreate struct { |
| 39 | OutputOptions |
| 40 | ChunkSize int `short:"h" long:"chunksize" default:"65536" description:"Host and port"` |
| 41 | Args struct { |
| 42 | LocalFileName string |
| 43 | } `positional-args:"yes" required:"yes"` |
| 44 | } |
| 45 | |
| 46 | type BackupRestore struct { |
| 47 | OutputOptions |
| 48 | Args struct { |
| 49 | LocalFileName string |
| 50 | } `positional-args:"yes" required:"yes"` |
| 51 | } |
| 52 | |
| 53 | type BackupOpts struct { |
| 54 | Create BackupCreate `command:"create"` |
| 55 | Restore BackupRestore `command:"restore"` |
| 56 | } |
| 57 | |
| 58 | var backupOpts = BackupOpts{} |
| 59 | |
| 60 | func RegisterBackupCommands(parser *flags.Parser) { |
| 61 | parser.AddCommand("backup", "backup management commands", "Commands to create backups and restore backups", &backupOpts) |
| 62 | } |
| 63 | |
| 64 | func (options *BackupCreate) Execute(args []string) error { |
| 65 | conn, descriptor, err := InitReflectionClient() |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | defer conn.Close() |
| 70 | |
| 71 | // We might close and reopen the connection befor we do the DownloadFile, |
| 72 | // so make sure we've downloaded the service descriptor. |
| 73 | _, err = descriptor.FindSymbol("xos.filetransfer") |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | |
| 78 | local_name := options.Args.LocalFileName |
| 79 | |
| 80 | // STEP 1: Create backup operation |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 81 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 82 | backupop := make(map[string]interface{}) |
| 83 | backupop["operation"] = "create" |
| 84 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | conditional_printf(!options.Quiet, "Created backup-create operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 89 | conditional_printf(!options.Quiet, "Waiting for sync ") |
| 90 | |
| 91 | // STEP 2: Wait for the operation to complete |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 92 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 93 | flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | Ternary_uint32(options.Quiet, GM_QUIET, 0) |
| 94 | conn, completed_backupop, err := GetModelWithRetry(conn, descriptor, "BackupOperation", backupop["id"].(int32), flags) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | defer conn.Close() |
| 100 | |
| 101 | status := completed_backupop.GetFieldByName("status").(string) |
| 102 | conditional_printf(!options.Quiet, "\nStatus: %s\n", status) |
| 103 | |
| 104 | // we've failed. leave. |
| 105 | if status != "created" { |
| 106 | return errors.New("BackupOp status is " + status) |
| 107 | } |
| 108 | |
| 109 | // STEP 3: Retrieve URI |
| 110 | backupfile_id := completed_backupop.GetFieldByName("file_id").(int32) |
| 111 | if backupfile_id == 0 { |
| 112 | return errors.New("BackupOp.file_id is not set") |
| 113 | } |
| 114 | |
| 115 | completed_backupfile, err := GetModel(conn, descriptor, "BackupFile", backupfile_id) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
| 120 | uri := completed_backupfile.GetFieldByName("uri").(string) |
| 121 | conditional_printf(!options.Quiet, "URI %s\n", uri) |
| 122 | |
| 123 | // STEP 4: Download the file |
| 124 | |
| 125 | conditional_printf(!options.Quiet, "Downloading %s\n", local_name) |
| 126 | |
| 127 | h, err := DownloadFile(conn, descriptor, uri, local_name) |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 132 | // STEP 5: Verify checksum |
| 133 | |
| 134 | if completed_backupfile.GetFieldByName("checksum").(string) != h.GetChecksum() { |
| 135 | return fmt.Errorf("Checksum mismatch, received=%s, expected=%s", |
| 136 | h.GetChecksum(), |
| 137 | completed_backupfile.GetFieldByName("checksum").(string)) |
| 138 | } |
| 139 | |
| 140 | // STEP 6: Show results |
| 141 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 142 | outputFormat := CharReplacer.Replace(options.Format) |
| 143 | if outputFormat == "" { |
| 144 | outputFormat = DEFAULT_BACKUP_FORMAT |
| 145 | } |
| 146 | if options.Quiet { |
| 147 | outputFormat = "{{.Status}}" |
| 148 | } |
| 149 | |
| 150 | data := make([]BackupOutput, 1) |
| 151 | data[0].Chunks = h.chunks |
| 152 | data[0].Bytes = h.bytes |
| 153 | data[0].Status = h.status |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 154 | data[0].Checksum = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 155 | |
| 156 | result := CommandResult{ |
| 157 | Format: format.Format(outputFormat), |
| 158 | OutputAs: toOutputType(options.OutputAs), |
| 159 | Data: data, |
| 160 | } |
| 161 | |
| 162 | GenerateOutput(&result) |
| 163 | |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func (options *BackupRestore) Execute(args []string) error { |
| 168 | conn, descriptor, err := InitReflectionClient() |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | defer conn.Close() |
| 173 | |
| 174 | local_name := options.Args.LocalFileName |
| 175 | remote_name := "cordctl-restore-" + time.Now().Format("20060102T150405Z") |
| 176 | uri := "file:///var/run/xos/backup/local/" + remote_name |
| 177 | |
| 178 | // STEP 1: Upload the file |
| 179 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 180 | h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, 65536) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | |
| 185 | upload_status := GetEnumValue(upload_result, "status") |
| 186 | if upload_status != "SUCCESS" { |
| 187 | return errors.New("Upload status was " + upload_status) |
| 188 | } |
| 189 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 190 | // STEP 2: Verify checksum |
| 191 | |
| 192 | if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() { |
| 193 | return fmt.Errorf("Checksum mismatch, expected=%s, received=%s", |
| 194 | h.GetChecksum(), |
| 195 | upload_result.GetFieldByName("checksum").(string)) |
| 196 | } |
| 197 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 198 | // STEP 2: Create a BackupFile object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 199 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 200 | backupfile := make(map[string]interface{}) |
| 201 | backupfile["name"] = remote_name |
| 202 | backupfile["uri"] = uri |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 203 | backupfile["checksum"] = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 204 | err = CreateModel(conn, descriptor, "BackupFile", backupfile) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | conditional_printf(!options.Quiet, "Created backup file %d\n", backupfile["id"]) |
| 209 | |
| 210 | // STEP 3: Create a BackupOperation object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 211 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 212 | backupop := make(map[string]interface{}) |
| 213 | backupop["operation"] = "restore" |
| 214 | backupop["file_id"] = backupfile["id"] |
| 215 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 216 | if err != nil { |
| 217 | return err |
| 218 | } |
| 219 | conditional_printf(!options.Quiet, "Created backup-restore operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 220 | |
| 221 | conditional_printf(!options.Quiet, "Waiting for completion ") |
| 222 | |
| 223 | // STEP 4: Wait for completion |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 224 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 225 | flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | GM_UNTIL_STATUS | Ternary_uint32(options.Quiet, GM_QUIET, 0) |
| 226 | conn, completed_backupop, err := FindModelWithRetry(conn, descriptor, "BackupOperation", "uuid", backupop["uuid"].(string), flags) |
| 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | |
| 231 | defer conn.Close() |
| 232 | |
| 233 | conditional_printf(!options.Quiet, "\n") |
| 234 | |
| 235 | // STEP 5: Show results |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame^] | 236 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 237 | outputFormat := CharReplacer.Replace(options.Format) |
| 238 | if outputFormat == "" { |
| 239 | outputFormat = DEFAULT_BACKUP_FORMAT |
| 240 | } |
| 241 | if options.Quiet { |
| 242 | outputFormat = "{{.Status}}" |
| 243 | } |
| 244 | |
| 245 | data := make([]BackupOutput, 1) |
| 246 | data[0].Checksum = upload_result.GetFieldByName("checksum").(string) |
| 247 | data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32)) |
| 248 | data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32)) |
| 249 | |
| 250 | if completed_backupop.GetFieldByName("status") == "restored" { |
| 251 | data[0].Status = "SUCCESS" |
| 252 | } else { |
| 253 | data[0].Status = "FAILURE" |
| 254 | } |
| 255 | |
| 256 | result := CommandResult{ |
| 257 | Format: format.Format(outputFormat), |
| 258 | OutputAs: toOutputType(options.OutputAs), |
| 259 | Data: data, |
| 260 | } |
| 261 | |
| 262 | GenerateOutput(&result) |
| 263 | |
| 264 | return nil |
| 265 | } |