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 | "errors" |
| 22 | "fmt" |
| 23 | flags "github.com/jessevdk/go-flags" |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 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 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame^] | 71 | ctx := context.Background() // TODO: Implement a sync timeout |
| 72 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 73 | // We might close and reopen the connection befor we do the DownloadFile, |
| 74 | // so make sure we've downloaded the service descriptor. |
| 75 | _, err = descriptor.FindSymbol("xos.filetransfer") |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | local_name := options.Args.LocalFileName |
| 81 | |
| 82 | // STEP 1: Create backup operation |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 83 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 84 | backupop := make(map[string]interface{}) |
| 85 | backupop["operation"] = "create" |
| 86 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | conditional_printf(!options.Quiet, "Created backup-create operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 91 | conditional_printf(!options.Quiet, "Waiting for sync ") |
| 92 | |
| 93 | // STEP 2: Wait for the operation to complete |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 94 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 95 | 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^] | 96 | 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] | 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | defer conn.Close() |
| 102 | |
| 103 | status := completed_backupop.GetFieldByName("status").(string) |
| 104 | conditional_printf(!options.Quiet, "\nStatus: %s\n", status) |
| 105 | |
| 106 | // we've failed. leave. |
| 107 | if status != "created" { |
| 108 | return errors.New("BackupOp status is " + status) |
| 109 | } |
| 110 | |
| 111 | // STEP 3: Retrieve URI |
| 112 | backupfile_id := completed_backupop.GetFieldByName("file_id").(int32) |
| 113 | if backupfile_id == 0 { |
| 114 | return errors.New("BackupOp.file_id is not set") |
| 115 | } |
| 116 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame^] | 117 | completed_backupfile, err := GetModel(ctx, conn, descriptor, "BackupFile", backupfile_id) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | uri := completed_backupfile.GetFieldByName("uri").(string) |
| 123 | conditional_printf(!options.Quiet, "URI %s\n", uri) |
| 124 | |
| 125 | // STEP 4: Download the file |
| 126 | |
| 127 | conditional_printf(!options.Quiet, "Downloading %s\n", local_name) |
| 128 | |
| 129 | h, err := DownloadFile(conn, descriptor, uri, local_name) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 134 | // STEP 5: Verify checksum |
| 135 | |
| 136 | if completed_backupfile.GetFieldByName("checksum").(string) != h.GetChecksum() { |
| 137 | return fmt.Errorf("Checksum mismatch, received=%s, expected=%s", |
| 138 | h.GetChecksum(), |
| 139 | completed_backupfile.GetFieldByName("checksum").(string)) |
| 140 | } |
| 141 | |
| 142 | // STEP 6: Show results |
| 143 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 144 | data := make([]BackupOutput, 1) |
| 145 | data[0].Chunks = h.chunks |
| 146 | data[0].Bytes = h.bytes |
| 147 | data[0].Status = h.status |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 148 | data[0].Checksum = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 149 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 150 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 151 | |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | func (options *BackupRestore) Execute(args []string) error { |
| 156 | conn, descriptor, err := InitReflectionClient() |
| 157 | if err != nil { |
| 158 | return err |
| 159 | } |
| 160 | defer conn.Close() |
| 161 | |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame^] | 162 | ctx := context.Background() // TODO: Implement a sync timeout |
| 163 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 164 | local_name := options.Args.LocalFileName |
| 165 | remote_name := "cordctl-restore-" + time.Now().Format("20060102T150405Z") |
| 166 | uri := "file:///var/run/xos/backup/local/" + remote_name |
| 167 | |
| 168 | // STEP 1: Upload the file |
| 169 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 170 | h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, 65536) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 171 | if err != nil { |
| 172 | return err |
| 173 | } |
| 174 | |
| 175 | upload_status := GetEnumValue(upload_result, "status") |
| 176 | if upload_status != "SUCCESS" { |
| 177 | return errors.New("Upload status was " + upload_status) |
| 178 | } |
| 179 | |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 180 | // STEP 2: Verify checksum |
| 181 | |
| 182 | if upload_result.GetFieldByName("checksum").(string) != h.GetChecksum() { |
| 183 | return fmt.Errorf("Checksum mismatch, expected=%s, received=%s", |
| 184 | h.GetChecksum(), |
| 185 | upload_result.GetFieldByName("checksum").(string)) |
| 186 | } |
| 187 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 188 | // STEP 2: Create a BackupFile object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 189 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 190 | backupfile := make(map[string]interface{}) |
| 191 | backupfile["name"] = remote_name |
| 192 | backupfile["uri"] = uri |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 193 | backupfile["checksum"] = h.GetChecksum() |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 194 | err = CreateModel(conn, descriptor, "BackupFile", backupfile) |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| 198 | conditional_printf(!options.Quiet, "Created backup file %d\n", backupfile["id"]) |
| 199 | |
| 200 | // STEP 3: Create a BackupOperation object |
Scott Baker | 72efd75 | 2019-05-15 13:12:20 -0700 | [diff] [blame] | 201 | |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 202 | backupop := make(map[string]interface{}) |
| 203 | backupop["operation"] = "restore" |
| 204 | backupop["file_id"] = backupfile["id"] |
| 205 | err = CreateModel(conn, descriptor, "BackupOperation", backupop) |
| 206 | if err != nil { |
| 207 | return err |
| 208 | } |
| 209 | conditional_printf(!options.Quiet, "Created backup-restore operation id=%d uuid=%s\n", backupop["id"], backupop["uuid"]) |
| 210 | |
| 211 | conditional_printf(!options.Quiet, "Waiting for completion ") |
| 212 | |
| 213 | // STEP 4: Wait for completion |
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 | 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] | 216 | queries := map[string]string{"uuid": backupop["uuid"].(string)} |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame^] | 217 | conn, completed_backupop, err := FindModelWithRetry(ctx, conn, descriptor, "BackupOperation", queries, flags) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 218 | if err != nil { |
| 219 | return err |
| 220 | } |
| 221 | |
| 222 | defer conn.Close() |
| 223 | |
| 224 | conditional_printf(!options.Quiet, "\n") |
| 225 | |
| 226 | // STEP 5: Show results |
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 | data := make([]BackupOutput, 1) |
| 229 | data[0].Checksum = upload_result.GetFieldByName("checksum").(string) |
| 230 | data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32)) |
| 231 | data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32)) |
| 232 | |
| 233 | if completed_backupop.GetFieldByName("status") == "restored" { |
| 234 | data[0].Status = "SUCCESS" |
| 235 | } else { |
| 236 | data[0].Status = "FAILURE" |
| 237 | } |
| 238 | |
Scott Baker | 5281d00 | 2019-05-16 10:45:26 -0700 | [diff] [blame] | 239 | FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data) |
Scott Baker | 6cf525a | 2019-05-09 12:25:08 -0700 | [diff] [blame] | 240 | |
| 241 | return nil |
| 242 | } |