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 ( |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 20 | "context" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 21 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 22 | corderrors "github.com/opencord/cordctl/error" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | DEFAULT_BACKUP_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}" |
| 28 | ) |
| 29 | |
| 30 | type BackupOutput struct { |
| 31 | Status string `json:"status"` |
| 32 | Checksum string `json:"checksum"` |
| 33 | Chunks int `json:"chunks"` |
| 34 | Bytes int `json:"bytes"` |
| 35 | } |
| 36 | |
| 37 | type BackupCreate struct { |
| 38 | OutputOptions |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 39 | ChunkSize int `short:"h" long:"chunksize" default:"65536" description:"Chunk size for streaming transfer"` |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 40 | Args struct { |
| 41 | LocalFileName string |
| 42 | } `positional-args:"yes" required:"yes"` |
| 43 | } |
| 44 | |
| 45 | type BackupRestore struct { |
| 46 | OutputOptions |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 47 | ChunkSize int `short:"h" long:"chunksize" default:"65536" description:"Chunk size for streaming transfer"` |
| 48 | Args struct { |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 49 | LocalFileName string |
| 50 | } `positional-args:"yes" required:"yes"` |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 51 | CreateURIFunc func() (string, string) // allow override of CreateURIFunc for easy unit testing |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | type BackupOpts struct { |
| 55 | Create BackupCreate `command:"create"` |
| 56 | Restore BackupRestore `command:"restore"` |
| 57 | } |
| 58 | |
| 59 | var backupOpts = BackupOpts{} |
| 60 | |
| 61 | func RegisterBackupCommands(parser *flags.Parser) { |
| 62 | parser.AddCommand("backup", "backup management commands", "Commands to create backups and restore backups", &backupOpts) |
| 63 | } |
| 64 | |
| 65 | func (options *BackupCreate) Execute(args []string) error { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 66 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | defer conn.Close() |
| 71 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 72 | ctx := context.Background() // TODO: Implement a sync timeout |
| 73 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 74 | // We might close and reopen the connection befor we do the DownloadFile, |
| 75 | // so make sure we've downloaded the service descriptor. |
| 76 | _, err = descriptor.FindSymbol("xos.filetransfer") |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | local_name := options.Args.LocalFileName |
| 82 | |
| 83 | // STEP 1: Create backup operation |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 84 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 85 | backupop := make(map[string]interface{}) |
| 86 | backupop["operation"] = "create" |
| 87 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | conditional_printf(!options.Quiet, "Created backup-create operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 92 | conditional_printf(!options.Quiet, "Waiting for sync ") |
| 93 | |
| 94 | // STEP 2: Wait for the operation to complete |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 95 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 96 | flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | Ternary_uint32(options.Quiet, GM_QUIET, 0) |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 97 | conn, completed_backupop, err := GetModelWithRetry(ctx, conn, descriptor, "BackupOperation", backupop["id"].(int32), flags) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | |
| 102 | defer conn.Close() |
| 103 | |
| 104 | status := completed_backupop.GetFieldByName("status").(string) |
| 105 | conditional_printf(!options.Quiet, "\nStatus: %s\n", status) |
| 106 | |
| 107 | // we've failed. leave. |
| 108 | if status != "created" { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 109 | return corderrors.NewInternalError("BackupOp status is %s", status) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // STEP 3: Retrieve URI |
| 113 | backupfile_id := completed_backupop.GetFieldByName("file_id").(int32) |
| 114 | if backupfile_id == 0 { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 115 | return corderrors.NewInternalError("BackupOp.file_id is not set") |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 118 | completed_backupfile, err := GetModel(ctx, conn, descriptor, "BackupFile", backupfile_id) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | |
| 123 | uri := completed_backupfile.GetFieldByName("uri").(string) |
| 124 | conditional_printf(!options.Quiet, "URI %s\n", uri) |
| 125 | |
| 126 | // STEP 4: Download the file |
| 127 | |
| 128 | conditional_printf(!options.Quiet, "Downloading %s\n", local_name) |
| 129 | |
| 130 | h, err := DownloadFile(conn, descriptor, uri, local_name) |
| 131 | if err != nil { |
| 132 | return err |
| 133 | } |
| 134 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 135 | // STEP 5: Verify checksum |
| 136 | |
| 137 | if completed_backupfile.GetFieldByName("checksum").(string) != h.GetChecksum() { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 138 | return corderrors.WithStackTrace(&corderrors.ChecksumMismatchError{ |
| 139 | Actual: h.GetChecksum(), |
| 140 | Expected: completed_backupfile.GetFieldByName("checksum").(string)}) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // STEP 6: Show results |
| 144 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 145 | data := make([]BackupOutput, 1) |
| 146 | data[0].Chunks = h.chunks |
| 147 | data[0].Bytes = h.bytes |
| 148 | data[0].Status = h.status |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 149 | data[0].Checksum = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 150 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 151 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 152 | |
| 153 | return nil |
| 154 | } |
| 155 | |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 156 | // Create a file:/// URI to use for storing the file in the core |
| 157 | func CreateDynamicURI() (string, string) { |
| 158 | remote_name := "cordctl-restore-" + time.Now().Format("20060102T150405Z") |
| 159 | uri := "file:///var/run/xos/backup/local/" + remote_name |
| 160 | return remote_name, uri |
| 161 | } |
| 162 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 163 | func (options *BackupRestore) Execute(args []string) error { |
Scott Baker | 867aa30 | 2019-06-19 13:18:45 -0700 | [diff] [blame] | 164 | conn, descriptor, err := InitClient(INIT_DEFAULT) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 165 | if err != nil { |
| 166 | return err |
| 167 | } |
| 168 | defer conn.Close() |
| 169 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 170 | ctx := context.Background() // TODO: Implement a sync timeout |
| 171 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 172 | local_name := options.Args.LocalFileName |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 173 | |
| 174 | var remote_name, uri string |
| 175 | if options.CreateURIFunc != nil { |
| 176 | remote_name, uri = options.CreateURIFunc() |
| 177 | } else { |
| 178 | remote_name, uri = CreateDynamicURI() |
| 179 | } |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 180 | |
| 181 | // STEP 1: Upload the file |
| 182 | |
Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 183 | h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, options.ChunkSize) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | |
| 188 | upload_status := GetEnumValue(upload_result, "status") |
| 189 | if upload_status != "SUCCESS" { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 190 | return corderrors.NewInternalError("Upload status was %s", upload_status) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 193 | // STEP 2: Verify checksum |
| 194 | |
| 195 | if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() { |
Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame] | 196 | return corderrors.WithStackTrace(&corderrors.ChecksumMismatchError{ |
| 197 | Expected: h.GetChecksum(), |
| 198 | Actual: upload_result.GetFieldByName("checksum").(string)}) |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 201 | // STEP 2: Create a BackupFile object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 202 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 203 | backupfile := make(map[string]interface{}) |
| 204 | backupfile["name"] = remote_name |
| 205 | backupfile["uri"] = uri |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 206 | backupfile["checksum"] = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 207 | err = CreateModel(conn, descriptor, "BackupFile", backupfile) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | conditional_printf(!options.Quiet, "Created backup file %d\n", backupfile["id"]) |
| 212 | |
| 213 | // STEP 3: Create a BackupOperation object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 214 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 215 | backupop := make(map[string]interface{}) |
| 216 | backupop["operation"] = "restore" |
| 217 | backupop["file_id"] = backupfile["id"] |
| 218 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 219 | if err != nil { |
| 220 | return err |
| 221 | } |
| 222 | conditional_printf(!options.Quiet, "Created backup-restore operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 223 | |
| 224 | conditional_printf(!options.Quiet, "Waiting for completion ") |
| 225 | |
| 226 | // STEP 4: Wait for completion |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 227 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 228 | flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | GM_UNTIL_STATUS | Ternary_uint32(options.Quiet, GM_QUIET, 0) |
Scott Baker | 5201c0b | 2019-05-15 15:35:56 -0700 | [diff] [blame] | 229 | queries := map[string]string{"uuid": backupop["uuid"].(string)} |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 230 | conn, completed_backupop, err := FindModelWithRetry(ctx, conn, descriptor, "BackupOperation", queries, flags) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 231 | if err != nil { |
| 232 | return err |
| 233 | } |
| 234 | |
| 235 | defer conn.Close() |
| 236 | |
| 237 | conditional_printf(!options.Quiet, "\n") |
| 238 | |
| 239 | // STEP 5: Show results |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 240 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 241 | data := make([]BackupOutput, 1) |
| 242 | data[0].Checksum = upload_result.GetFieldByName("checksum").(string) |
| 243 | data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32)) |
| 244 | data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32)) |
| 245 | |
| 246 | if completed_backupop.GetFieldByName("status") == "restored" { |
| 247 | data[0].Status = "SUCCESS" |
| 248 | } else { |
| 249 | data[0].Status = "FAILURE" |
| 250 | } |
| 251 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 252 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 253 | |
| 254 | return nil |
| 255 | } |