Skip to content

Commit

Permalink
add command for extracting installation, i.e. a backup
Browse files Browse the repository at this point in the history
  • Loading branch information
damianoneill committed Nov 15, 2019
1 parent 8151741 commit ffa029b
Show file tree
Hide file tree
Showing 21 changed files with 170 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ h7t
│   └── devices
├── extract
│   ├── device-groups
│   └── devices
│   ├── devices
│   └── installation
├── load
│   ├── device-groups
│   ├── devices
Expand Down
100 changes: 100 additions & 0 deletions cmd/extract_installation.go
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)
}
16 changes: 16 additions & 0 deletions cmd/extract_installation_test.go
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")
}
2 changes: 1 addition & 1 deletion docs/h7t.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_configure_devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion docs/h7t_extract.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_extract_device-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_extract_devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions docs/h7t_extract_installation.md
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
2 changes: 1 addition & 1 deletion docs/h7t_load.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_load_device-groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_load_devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_load_helper-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_summarise.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_summarise_installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_transform_devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion docs/h7t_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ffa029b

Please sign in to comment.