-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add command for extracting installation, i.e. a backup
- Loading branch information
1 parent
8151741
commit ffa029b
Showing
21 changed files
with
170 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/spf13/afero" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/resty.v1" | ||
) | ||
|
||
func appendResourceBody(backup *[]byte, responseBody []byte) (err error) { | ||
// remove all whitespace / carriage returns, etc. | ||
buffer := new(bytes.Buffer) | ||
err = json.Compact(buffer, responseBody) | ||
if err != nil { | ||
return | ||
} | ||
// remove outer brackets | ||
by := bytes.TrimPrefix(buffer.Bytes(), []byte("{")) | ||
by = bytes.TrimSuffix(by, []byte("}")) | ||
// append to main json object and add comma in advance of next object being added | ||
*backup = append(*backup, by...) | ||
*backup = append(*backup, ',') | ||
return | ||
} | ||
|
||
func createBackup(rc *resty.Client, resources []string, filename string) (err error) { | ||
b := []byte{'{'} | ||
for _, resource := range resources { | ||
resp, restErr := rc.R(). | ||
SetBasicAuth(ci.Username, ci.Password). | ||
Get("https://" + ci.Authority + resource) | ||
if restErr != nil { | ||
return restErr | ||
} | ||
// ensure we don't get an empty body | ||
if resp.Body() != nil && resp.StatusCode() == 200 && len(resp.Body()) > 4 { | ||
err = appendResourceBody(&b, resp.Body()) | ||
if err != nil { | ||
return | ||
} | ||
fmt.Fprintf(os.Stdout, "Including in backup: %v \n", resource) | ||
} | ||
} | ||
|
||
// need to remove the last comma we added, to make it valid json or the indenter will barf | ||
b = bytes.TrimSuffix(b, []byte(",")) | ||
b = append(b, '}') | ||
|
||
// pretty print the output | ||
var prettyJSON bytes.Buffer | ||
err = json.Indent(&prettyJSON, b, "", " ") | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = afero.WriteFile(AppFs, filename, prettyJSON.Bytes(), 0777) | ||
return | ||
} | ||
|
||
// extractInstallationCmd represents the extract installation command | ||
var extractInstallationCmd = &cobra.Command{ | ||
Use: "installation", | ||
Short: "Extract installation configuration", | ||
Long: `Collect from a Healthbot installation the complete configuration and generate a backup file.`, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
jsonResources := []string{ | ||
"/api/v1/devices/", | ||
"/api/v1/topics/", | ||
"/api/v1/playbooks/", | ||
"/api/v1/device-groups/", | ||
"/api/v1/network-groups/", | ||
"/api/v1/notifications/", | ||
"/api/v1/retention-policies/", | ||
"/api/v1/system-settings/report-generation/destinations/", | ||
"/api/v1/system-settings/report-generation/reports/", | ||
"/api/v1/system-settings/schedulers", | ||
"/api/v1/data-store/grafana/", | ||
// TODO - extra resources in 2.1? | ||
} | ||
|
||
t := time.Now() | ||
err = createBackup(resty.DefaultClient, jsonResources, cmd.Flag("output_directory").Value.String()+filePathSeperator+"healthbot_backup-"+t.Format("20060102150405")+".json") | ||
if err != nil { | ||
return | ||
} | ||
|
||
// TODO Helper Files | ||
|
||
return | ||
}, | ||
} | ||
|
||
func init() { | ||
extractCmd.AddCommand(extractInstallationCmd) | ||
} |
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,16 @@ | ||
package cmd | ||
|
||
import "testing" | ||
|
||
import "github.com/stretchr/testify/assert" | ||
|
||
func Test_appendResourceBody(t *testing.T) { | ||
// fugly test | ||
backup := []byte("{\"device-group\":[{\"device-group-name\":\"ptp\",\"devices\":[\"mmx960-1\",\"mmx960-3\"]}],") | ||
responseBody := []byte("{\"device\":[{\"device-id\":\"mmx960-1\",\"host\":\"172.30.177.102\"}]}") | ||
expected := []byte("{\"device-group\":[{\"device-group-name\":\"ptp\",\"devices\":[\"mmx960-1\",\"mmx960-3\"]}],\"device\":[{\"device-id\":\"mmx960-1\",\"host\":\"172.30.177.102\"}],") | ||
|
||
err := appendResourceBody(&backup, responseBody) | ||
assert.Nil(t, err, "Should not return an error %v", err) | ||
assert.Equal(t, expected, backup, "should start with { and end with , and separate objects with a comma") | ||
} |
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
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
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,34 @@ | ||
## h7t extract installation | ||
|
||
Extract installation configuration | ||
|
||
### Synopsis | ||
|
||
Collect from a Healthbot installation the complete configuration and generate a backup file. | ||
|
||
``` | ||
h7t extract installation [flags] | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for installation | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-a, --authority string healthbot HTTPS Authority (default "localhost:8080") | ||
--config string config file (default is $HOME/.h7t.yaml) | ||
-o, --output_directory string directory where the configuration will be stored (default ".") | ||
-p, --password string healthbot Password (default "****") | ||
-u, --username string healthbot Username (default "admin") | ||
-v, --verbose cause h7t to be more verbose | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [h7t extract](h7t_extract.md) - Extract information from a Healthbot Installation | ||
|
||
###### Auto generated by spf13/cobra on 15-Nov-2019 |
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
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
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