Skip to content

Commit

Permalink
Add cli integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomascogez committed Jun 23, 2022
1 parent 1a5511b commit e962dbe
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 6 deletions.
46 changes: 46 additions & 0 deletions cmd/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"context"
"fmt"

"github.com/Thomascogez/dump-and-dumper/dumper"
"github.com/Thomascogez/dump-and-dumper/helpers"
upload "github.com/Thomascogez/dump-and-dumper/uploader"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
)

var dumpCmd = &cobra.Command{
Use: "dump",
Short: "Execute dump against all container using required label",
Run: func(cmd *cobra.Command, args []string) {
cli, err := client.NewClientWithOpts(client.FromEnv)
helpers.CheckError(err)

containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{})
helpers.CheckError(err)

s3Uploader := upload.NewS3UploaderFromFlags(cmd.Flags())
dockerDumper := dumper.DockerDumper{Uploader: s3Uploader}

containersToDump := dumper.FindContainersToDump(containers)

fmt.Printf("[dump & dumper] - Start dumping %d container(s) ...\n", len(containersToDump))

dockerDumper.Dump(containersToDump)

fmt.Printf("[dump & dumper] - Done dumping %d container(s)\n", len(containersToDump))
},
}

func init() {
dumpCmd.PersistentFlags().String("s3-endpoint", "", "Define endpoint of s3 bucket")
dumpCmd.PersistentFlags().String("s3-region", "", "Define region of s3 bucket")
dumpCmd.PersistentFlags().String("s3-bucket", "", "Define name of the destination bucket")
dumpCmd.PersistentFlags().String("s3-secretKeyId", "", "Id of the secret in order to access s3 bucket")
dumpCmd.PersistentFlags().String("s3-secretKey", "", "Secret key in order to access s3 bucket")

rootCmd.AddCommand(dumpCmd)
}
23 changes: 23 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "dump-dumper",
Short: "dump-dumper is a simple cli tool to streamline your dockerize database backup process",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Welcome to dump-dumper")
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
4 changes: 2 additions & 2 deletions dumper/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func BuildContainerDumpCommandArgs(containerId string, dumpOptions DumpOptions)
return args
}

func FindContainersByTypes(containers []types.Container, containerType string) []types.Container {
func FindContainersToDump(containers []types.Container) []types.Container {
containerToDump := make([]types.Container, 0)

for _, container := range containers {
containerDumpConfig := ExtractDumpOptionsFromLabels(container.Labels)

if containerDumpConfig.Enabled && containerDumpConfig.Type == containerType {
if containerDumpConfig.Enabled {
containerToDump = append(containerToDump, container)
}
}
Expand Down
12 changes: 8 additions & 4 deletions dumper/dockerDumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dumper
import (
"os"
"os/exec"
"path"
"sync"

"github.com/Thomascogez/dump-and-dumper/helpers"
Expand All @@ -22,19 +23,22 @@ func (dockerDumper DockerDumper) Dump(containers []types.Container) {
wg.Add(1)

go func(container types.Container) {
defer wg.Done()
dumpConfig := ExtractDumpOptionsFromLabels(container.Labels)
dumpCommandArgs := BuildContainerDumpCommandArgs(container.ID, dumpConfig)

tempDumpFile, tempDumpFolderPath, tempDumpFileName := helpers.CreateTempDumpFile()
defer os.RemoveAll(tempDumpFolderPath)
defer tempDumpFile.Close()

dumpCommand := exec.Command("docker", dumpCommandArgs...)
dumpCommand.Stdout = tempDumpFile
dumpCommand.Run()

println(tempDumpFileName)
tempDumpFile.Close()
os.RemoveAll(tempDumpFolderPath)
wg.Done()
dockerDumper.Uploader.Upload(
path.Join(tempDumpFolderPath, tempDumpFileName),
container.Names[0]+"-"+tempDumpFileName,
)
}(container)
}
wg.Wait()
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VM
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/aws/aws-sdk-go v1.44.40 h1:MR0qefjBJrZuXE0VoeKMQFtjS2tUeVpbQNfb7NzQNgI=
github.com/aws/aws-sdk-go v1.44.40/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
Expand All @@ -14,6 +15,8 @@ github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
Expand All @@ -26,9 +29,14 @@ github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zM
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -71,3 +79,4 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package main

import "github.com/Thomascogez/dump-and-dumper/cmd"

func main() {
cmd.Execute()
}
17 changes: 17 additions & 0 deletions uploader/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/spf13/pflag"
)

type S3Uploader struct {
Expand Down Expand Up @@ -47,3 +48,19 @@ func (s3Uploader S3Uploader) Upload(filePath string, fileName string) {

fmt.Printf("[dump & dumper] - file uploaded to, %s\n", result.Location)
}

func NewS3UploaderFromFlags(flags *pflag.FlagSet) S3Uploader {
endPoint, _ := flags.GetString("s3-endpoint")
region, _ := flags.GetString("s3-region")
bucket, _ := flags.GetString("s3-bucket")
secretKeyId, _ := flags.GetString("s3-secretKeyId")
secretKey, _ := flags.GetString("s3-secretKey")

return S3Uploader{
SecretKeyID: secretKeyId,
SecretKey: secretKey,
Bucket: bucket,
Endpoint: endPoint,
Region: region,
}
}

0 comments on commit e962dbe

Please sign in to comment.