blob: db6e51a85287fd4a08db302e0e1bdd9005e0239e [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 (
20 "errors"
21 "fmt"
22 flags "github.com/jessevdk/go-flags"
23 "github.com/opencord/cordctl/format"
24 "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
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 Baker72efd752019-05-15 13:12:20 -070081
Scott Baker6cf525a2019-05-09 12:25:08 -070082 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 Baker72efd752019-05-15 13:12:20 -070092
Scott Baker6cf525a2019-05-09 12:25:08 -070093 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 Baker72efd752019-05-15 13:12:20 -0700132 // 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 Baker6cf525a2019-05-09 12:25:08 -0700142 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 Baker72efd752019-05-15 13:12:20 -0700154 data[0].Checksum = h.GetChecksum()
Scott Baker6cf525a2019-05-09 12:25:08 -0700155
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
167func (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 Baker72efd752019-05-15 13:12:20 -0700180 h, upload_result, err := UploadFile(conn, descriptor, local_name, uri, 65536)
Scott Baker6cf525a2019-05-09 12:25:08 -0700181 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 Baker72efd752019-05-15 13:12:20 -0700190 // 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 Baker6cf525a2019-05-09 12:25:08 -0700198 // STEP 2: Create a BackupFile object
Scott Baker72efd752019-05-15 13:12:20 -0700199
Scott Baker6cf525a2019-05-09 12:25:08 -0700200 backupfile := make(map[string]interface{})
201 backupfile["name"] = remote_name
202 backupfile["uri"] = uri
Scott Baker72efd752019-05-15 13:12:20 -0700203 backupfile["checksum"] = h.GetChecksum()
Scott Baker6cf525a2019-05-09 12:25:08 -0700204 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 Baker72efd752019-05-15 13:12:20 -0700211
Scott Baker6cf525a2019-05-09 12:25:08 -0700212 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 Baker72efd752019-05-15 13:12:20 -0700224
Scott Baker6cf525a2019-05-09 12:25:08 -0700225 flags := GM_UNTIL_ENACTED | GM_UNTIL_FOUND | GM_UNTIL_STATUS | Ternary_uint32(options.Quiet, GM_QUIET, 0)
Scott Baker5201c0b2019-05-15 15:35:56 -0700226 queries := map[string]string{"uuid": backupop["uuid"].(string)}
227 conn, completed_backupop, err := FindModelWithRetry(conn, descriptor, "BackupOperation", queries, flags)
Scott Baker6cf525a2019-05-09 12:25:08 -0700228 if err != nil {
229 return err
230 }
231
232 defer conn.Close()
233
234 conditional_printf(!options.Quiet, "\n")
235
236 // STEP 5: Show results
Scott Baker72efd752019-05-15 13:12:20 -0700237
Scott Baker6cf525a2019-05-09 12:25:08 -0700238 outputFormat := CharReplacer.Replace(options.Format)
239 if outputFormat == "" {
240 outputFormat = DEFAULT_BACKUP_FORMAT
241 }
242 if options.Quiet {
243 outputFormat = "{{.Status}}"
244 }
245
246 data := make([]BackupOutput, 1)
247 data[0].Checksum = upload_result.GetFieldByName("checksum").(string)
248 data[0].Chunks = int(upload_result.GetFieldByName("chunks_received").(int32))
249 data[0].Bytes = int(upload_result.GetFieldByName("bytes_received").(int32))
250
251 if completed_backupop.GetFieldByName("status") == "restored" {
252 data[0].Status = "SUCCESS"
253 } else {
254 data[0].Status = "FAILURE"
255 }
256
257 result := CommandResult{
258 Format: format.Format(outputFormat),
259 OutputAs: toOutputType(options.OutputAs),
260 Data: data,
261 }
262
263 GenerateOutput(&result)
264
265 return nil
266}