blob: 287236f11e4094b0ff87b0e7306aea7f5e93da26 [file] [log] [blame]
Scott Baker6cf525a2019-05-09 12:25:08 -07001/*
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 */
17package commands
18
19import (
Scott Bakerc328cf12019-05-28 16:03:12 -070020 "context"
Scott Baker6cf525a2019-05-09 12:25:08 -070021 "errors"
22 "fmt"
23 flags "github.com/jessevdk/go-flags"
Scott Baker6cf525a2019-05-09 12:25:08 -070024 "time"
25)
26
27const (
28 DEFAULT_BACKUP_FORMAT = "table{{ .Status }}\t{{ .Checksum }}\t{{ .Chunks }}\t{{ .Bytes }}"
29)
30
31type BackupOutput struct {
32 Status string `json:"status"`
33 Checksum string `json:"checksum"`
34 Chunks int `json:"chunks"`
35 Bytes int `json:"bytes"`
36}
37
38type 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
46type BackupRestore struct {
47 OutputOptions
48 Args struct {
49 LocalFileName string
50 } `positional-args:"yes" required:"yes"`
51}
52
53type BackupOpts struct {
54 Create BackupCreate `command:"create"`
55 Restore BackupRestore `command:"restore"`
56}
57
58var backupOpts = BackupOpts{}
59
60func RegisterBackupCommands(parser *flags.Parser) {
61 parser.AddCommand("backup", "backup management commands", "Commands to create backups and restore backups", &backupOpts)
62}
63
64func (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 Bakerc328cf12019-05-28 16:03:12 -070071 ctx := context.Background() // TODO: Implement a sync timeout
72
Scott Baker6cf525a2019-05-09 12:25:08 -070073 // 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 Baker72efd752019-05-15 13:12:20 -070083
Scott Baker6cf525a2019-05-09 12:25:08 -070084 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 Baker72efd752019-05-15 13:12:20 -070094
Scott Baker6cf525a2019-05-09 12:25:08 -070095 flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | Ternary_uint32(options.Quiet, GM_QUIET, 0)
Scott Bakerc328cf12019-05-28 16:03:12 -070096 conn, completed_backupop, err := GetModelWithRetry(ctx, conn, descriptor, "BackupOperation", backupop["id"].(int32), flags)
Scott Baker6cf525a2019-05-09 12:25:08 -070097 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 Bakerc328cf12019-05-28 16:03:12 -0700117 completed_backupfile, err := GetModel(ctx, conn, descriptor, "BackupFile", backupfile_id)
Scott Baker6cf525a2019-05-09 12:25:08 -0700118 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 Baker72efd752019-05-15 13:12:20 -0700134 // 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 Baker6cf525a2019-05-09 12:25:08 -0700144 data := make([]BackupOutput, 1)
145 data[0].Chunks = h.chunks
146 data[0].Bytes = h.bytes
147 data[0].Status = h.status
Scott Baker72efd752019-05-15 13:12:20 -0700148 data[0].Checksum = h.GetChecksum()
Scott Baker6cf525a2019-05-09 12:25:08 -0700149
Scott Baker5281d002019-05-16 10:45:26 -0700150 FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data)
Scott Baker6cf525a2019-05-09 12:25:08 -0700151
152 return nil
153}
154
155func (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 Bakerc328cf12019-05-28 16:03:12 -0700162 ctx := context.Background() // TODO: Implement a sync timeout
163
Scott Baker6cf525a2019-05-09 12:25:08 -0700164 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 Baker72efd752019-05-15 13:12:20 -0700170 h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, 65536)
Scott Baker6cf525a2019-05-09 12:25:08 -0700171 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 Baker72efd752019-05-15 13:12:20 -0700180 // 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 Baker6cf525a2019-05-09 12:25:08 -0700188 // STEP 2: Create a BackupFile object
Scott Baker72efd752019-05-15 13:12:20 -0700189
Scott Baker6cf525a2019-05-09 12:25:08 -0700190 backupfile := make(map[string]interface{})
191 backupfile["name"] = remote_name
192 backupfile["uri"] = uri
Scott Baker72efd752019-05-15 13:12:20 -0700193 backupfile["checksum"] = h.GetChecksum()
Scott Baker6cf525a2019-05-09 12:25:08 -0700194 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 Baker72efd752019-05-15 13:12:20 -0700201
Scott Baker6cf525a2019-05-09 12:25:08 -0700202 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 Baker72efd752019-05-15 13:12:20 -0700214
Scott Baker6cf525a2019-05-09 12:25:08 -0700215 flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | GM_UNTIL_STATUS | Ternary_uint32(options.Quiet, GM_QUIET, 0)
Scott Baker5201c0b2019-05-15 15:35:56 -0700216 queries := map[string]string{"uuid": backupop["uuid"].(string)}
Scott Bakerc328cf12019-05-28 16:03:12 -0700217 conn, completed_backupop, err := FindModelWithRetry(ctx, conn, descriptor, "BackupOperation", queries, flags)
Scott Baker6cf525a2019-05-09 12:25:08 -0700218 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 Baker72efd752019-05-15 13:12:20 -0700227
Scott Baker6cf525a2019-05-09 12:25:08 -0700228 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 Baker5281d002019-05-16 10:45:26 -0700239 FormatAndGenerateOutput(&options.OutputOptions, DEFAULT_BACKUP_FORMAT, "{{.Status}}", data)
Scott Baker6cf525a2019-05-09 12:25:08 -0700240
241 return nil
242}