-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from jllucas/backup_restore
QED on-demand backup
- Loading branch information
Showing
30 changed files
with
1,373 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/octago/sflags/gen/gpflag" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/bbva/qed/log" | ||
) | ||
|
||
type BackupConfig struct { | ||
// Endpoint [host:port] to ask for QED management APIs. | ||
Endpoint string `desc:"QED Log service management endpoint http://ip:port"` | ||
|
||
// Log level | ||
Log string `desc:"Set log level to info, error or debug"` | ||
|
||
// ApiKey to query the server endpoint. | ||
APIKey string `desc:"Set API Key to talk to QED Log service"` | ||
} | ||
|
||
func defaultConfig() *BackupConfig { | ||
return &BackupConfig{ | ||
Endpoint: "http://127.0.0.1:8700", | ||
APIKey: "my-key", | ||
} | ||
} | ||
|
||
var backupCmd *cobra.Command = &cobra.Command{ | ||
Use: "backup", | ||
Short: "Manages QED log backups", | ||
TraverseChildren: true, | ||
PersistentPreRunE: runBackup, | ||
} | ||
|
||
var backupCtx context.Context | ||
|
||
func init() { | ||
backupCtx = configBackup() | ||
Root.AddCommand(backupCmd) | ||
} | ||
|
||
func configBackup() context.Context { | ||
|
||
conf := defaultConfig() | ||
|
||
err := gpflag.ParseTo(conf, backupCmd.PersistentFlags()) | ||
if err != nil { | ||
log.Fatalf("err: %v", err) | ||
} | ||
return context.WithValue(Ctx, k("backup.config"), conf) | ||
} | ||
|
||
func runBackup(cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
endpoint, _ := cmd.Flags().GetString("endpoint") | ||
err = urlParse(endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright 2018-2019 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/bbva/qed/log" | ||
) | ||
|
||
var backupCreateCmd *cobra.Command = &cobra.Command{ | ||
Use: "create", | ||
Short: "Create a backup of the QED Log", | ||
RunE: runBackupCreate, | ||
} | ||
|
||
var backupCreateCtx context.Context | ||
|
||
func init() { | ||
backupCmd.AddCommand(backupCreateCmd) | ||
} | ||
|
||
func runBackupCreate(cmd *cobra.Command, args []string) error { | ||
|
||
config := backupCtx.Value(k("backup.config")).(*BackupConfig) | ||
log.SetLogger("backup", config.Log) | ||
|
||
_, err := createBackup(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Backup created!") | ||
|
||
return nil | ||
} | ||
|
||
func createBackup(config *BackupConfig) ([]byte, error) { | ||
|
||
// Build request | ||
req, err := http.NewRequest("POST", config.Endpoint+"/backup", nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Api-Key", config.APIKey) | ||
|
||
// Get response | ||
client := http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Infof("Request error: %v\n", err) | ||
return nil, err | ||
} | ||
|
||
var bodyBytes []byte | ||
if resp.Body != nil { | ||
defer resp.Body.Close() | ||
bodyBytes, err = ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
if resp.StatusCode >= 400 && resp.StatusCode < 500 { | ||
return nil, fmt.Errorf("Invalid request %v", string(bodyBytes)) | ||
} | ||
|
||
return bodyBytes, nil | ||
} |
Oops, something went wrong.