From ffa029b822fb4651aa220fd139fc769f29e0adac Mon Sep 17 00:00:00 2001 From: Damian ONeill Date: Fri, 15 Nov 2019 16:49:14 +0000 Subject: [PATCH] add command for extracting installation, i.e. a backup --- README.md | 3 +- cmd/extract_installation.go | 100 +++++++++++++++++++++++++++++ cmd/extract_installation_test.go | 16 +++++ docs/h7t.md | 2 +- docs/h7t_completion.md | 2 +- docs/h7t_configure.md | 2 +- docs/h7t_configure_devices.md | 2 +- docs/h7t_docs.md | 2 +- docs/h7t_extract.md | 3 +- docs/h7t_extract_device-groups.md | 2 +- docs/h7t_extract_devices.md | 2 +- docs/h7t_extract_installation.md | 34 ++++++++++ docs/h7t_load.md | 2 +- docs/h7t_load_device-groups.md | 2 +- docs/h7t_load_devices.md | 2 +- docs/h7t_load_helper-files.md | 2 +- docs/h7t_summarise.md | 2 +- docs/h7t_summarise_installation.md | 2 +- docs/h7t_transform.md | 2 +- docs/h7t_transform_devices.md | 2 +- docs/h7t_version.md | 2 +- 21 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 cmd/extract_installation.go create mode 100644 cmd/extract_installation_test.go create mode 100644 docs/h7t_extract_installation.md diff --git a/README.md b/README.md index 6beb206..95ea448 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,8 @@ h7t │   └── devices ├── extract │   ├── device-groups -│   └── devices +│   ├── devices +│   └── installation ├── load │   ├── device-groups │   ├── devices diff --git a/cmd/extract_installation.go b/cmd/extract_installation.go new file mode 100644 index 0000000..06c5d08 --- /dev/null +++ b/cmd/extract_installation.go @@ -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) +} diff --git a/cmd/extract_installation_test.go b/cmd/extract_installation_test.go new file mode 100644 index 0000000..44d5bdc --- /dev/null +++ b/cmd/extract_installation_test.go @@ -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") +} diff --git a/docs/h7t.md b/docs/h7t.md index 664cda2..9ce6c86 100644 --- a/docs/h7t.md +++ b/docs/h7t.md @@ -30,4 +30,4 @@ The intent with this tool is to provide bulk or aggregate functions, that simpli * [h7t transform](h7t_transform.md) - Transform things from proprietary formats into Healthbot dsl format * [h7t version](h7t_version.md) - Output the current build information -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_completion.md b/docs/h7t_completion.md index 46f4522..3d92900 100644 --- a/docs/h7t_completion.md +++ b/docs/h7t_completion.md @@ -54,4 +54,4 @@ h7t completion [flags] * [h7t](h7t.md) - Healthbot Command Line Interface -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_configure.md b/docs/h7t_configure.md index f228599..17571ec 100644 --- a/docs/h7t_configure.md +++ b/docs/h7t_configure.md @@ -31,4 +31,4 @@ h7t configure [flags] * [h7t](h7t.md) - Healthbot Command Line Interface * [h7t configure devices](h7t_configure_devices.md) - Load Devices with configuration -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_configure_devices.md b/docs/h7t_configure_devices.md index 0268e9c..d6f60f9 100644 --- a/docs/h7t_configure_devices.md +++ b/docs/h7t_configure_devices.md @@ -56,4 +56,4 @@ h7t configure devices [flags] * [h7t configure](h7t_configure.md) - Configure information relating to a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_docs.md b/docs/h7t_docs.md index 2b7fead..00d3a12 100644 --- a/docs/h7t_docs.md +++ b/docs/h7t_docs.md @@ -30,4 +30,4 @@ h7t docs [flags] * [h7t](h7t.md) - Healthbot Command Line Interface -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_extract.md b/docs/h7t_extract.md index d9d339d..7e6cce6 100644 --- a/docs/h7t_extract.md +++ b/docs/h7t_extract.md @@ -32,5 +32,6 @@ h7t extract [flags] * [h7t](h7t.md) - Healthbot Command Line Interface * [h7t extract device-groups](h7t_extract_device-groups.md) - Extract Device Groups configuration * [h7t extract devices](h7t_extract_devices.md) - Extract Device configuration +* [h7t extract installation](h7t_extract_installation.md) - Extract installation configuration -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_extract_device-groups.md b/docs/h7t_extract_device-groups.md index 96d13d9..7f88719 100644 --- a/docs/h7t_extract_device-groups.md +++ b/docs/h7t_extract_device-groups.md @@ -31,4 +31,4 @@ h7t extract device-groups [flags] * [h7t extract](h7t_extract.md) - Extract information from a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_extract_devices.md b/docs/h7t_extract_devices.md index 5e00799..e868fda 100644 --- a/docs/h7t_extract_devices.md +++ b/docs/h7t_extract_devices.md @@ -31,4 +31,4 @@ h7t extract devices [flags] * [h7t extract](h7t_extract.md) - Extract information from a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_extract_installation.md b/docs/h7t_extract_installation.md new file mode 100644 index 0000000..9b009fc --- /dev/null +++ b/docs/h7t_extract_installation.md @@ -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 diff --git a/docs/h7t_load.md b/docs/h7t_load.md index 55c85ff..3d7b9fc 100644 --- a/docs/h7t_load.md +++ b/docs/h7t_load.md @@ -37,4 +37,4 @@ h7t load [flags] * [h7t load devices](h7t_load_devices.md) - Load Device configuration * [h7t load helper-files](h7t_load_helper-files.md) - Load Helper Files -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_load_device-groups.md b/docs/h7t_load_device-groups.md index b189f66..63a5767 100644 --- a/docs/h7t_load_device-groups.md +++ b/docs/h7t_load_device-groups.md @@ -32,4 +32,4 @@ h7t load device-groups [flags] * [h7t load](h7t_load.md) - Load information into a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_load_devices.md b/docs/h7t_load_devices.md index 11d7f82..f29b4dd 100644 --- a/docs/h7t_load_devices.md +++ b/docs/h7t_load_devices.md @@ -32,4 +32,4 @@ h7t load devices [flags] * [h7t load](h7t_load.md) - Load information into a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_load_helper-files.md b/docs/h7t_load_helper-files.md index 9d7405b..c2eb7e3 100644 --- a/docs/h7t_load_helper-files.md +++ b/docs/h7t_load_helper-files.md @@ -32,4 +32,4 @@ h7t load helper-files [flags] * [h7t load](h7t_load.md) - Load information into a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_summarise.md b/docs/h7t_summarise.md index cbbf739..f9ee3a3 100644 --- a/docs/h7t_summarise.md +++ b/docs/h7t_summarise.md @@ -31,4 +31,4 @@ h7t summarise [flags] * [h7t](h7t.md) - Healthbot Command Line Interface * [h7t summarise installation](h7t_summarise_installation.md) - Summarise information collected from a Healthbot installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_summarise_installation.md b/docs/h7t_summarise_installation.md index cec6029..e7a045b 100644 --- a/docs/h7t_summarise_installation.md +++ b/docs/h7t_summarise_installation.md @@ -30,4 +30,4 @@ h7t summarise installation [flags] * [h7t summarise](h7t_summarise.md) - Summarise information from a Healthbot Installation -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_transform.md b/docs/h7t_transform.md index 60a0a38..43121da 100644 --- a/docs/h7t_transform.md +++ b/docs/h7t_transform.md @@ -36,4 +36,4 @@ h7t transform [flags] * [h7t](h7t.md) - Healthbot Command Line Interface * [h7t transform devices](h7t_transform_devices.md) - Transform Devices configuration -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_transform_devices.md b/docs/h7t_transform_devices.md index eae2d58..8463bb2 100644 --- a/docs/h7t_transform_devices.md +++ b/docs/h7t_transform_devices.md @@ -33,4 +33,4 @@ h7t transform devices [flags] * [h7t transform](h7t_transform.md) - Transform things from proprietary formats into Healthbot dsl format -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019 diff --git a/docs/h7t_version.md b/docs/h7t_version.md index 51e3463..3b365bb 100644 --- a/docs/h7t_version.md +++ b/docs/h7t_version.md @@ -30,4 +30,4 @@ h7t version [flags] * [h7t](h7t.md) - Healthbot Command Line Interface -###### Auto generated by spf13/cobra on 14-Nov-2019 +###### Auto generated by spf13/cobra on 15-Nov-2019