Skip to content

Commit

Permalink
added minio push cmd (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-hermann-sva authored Feb 6, 2024
1 parent cf23fce commit 7229f03
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 59 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ machineShop version

## USAGE EXAMPLES

### PUSH

<details><summary><b>GIT</b></summary>

```bash
export MINIO_ACCESS_KEY=sthings
export MINIO_SECRET_KEY=<PASSWORD>
export MINIO_ADDR=artifacts.automation.sthings-vsphere.labul.sva.de
export MINIO_SECURE=true

machineShop push \
--target minio \
--source pod.yaml \
--destination manifests:pod-example.yaml # <BUCKET>:<OBECTNAME>
```

### RENDER

<details><summary><b>GIT</b></summary>
Expand Down Expand Up @@ -62,6 +78,7 @@ machineShop render \
<details><summary><b>GET</b></summary>

### REQUIREMENT: VAULT APPROLE EXPORTS

```bash
export VAULT_NAMESPACE=root
export VAULT_ROLE_ID=1d42d7e7-8c14-e5f9-801d-b3ecef416616
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ tasks:
echo "ENTER COMMIT MESSAGE"
read COMMIT_MESSAGE;
git add cmd/*
git add internal/*
git add .github/workflows/*
git add modules/*
git commit -am "$(echo ${COMMIT_MESSAGE})"
git push origin -u {{ .BRANCH }}
Expand Down
82 changes: 48 additions & 34 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Copyright © 2023 Patrick Hermann [email protected]
package cmd

import (
"fmt"
"os"
"strings"

"github.com/stuttgart-things/machineShop/internal"
sthingsCli "github.com/stuttgart-things/sthingsCli"
Expand All @@ -18,6 +18,7 @@ import (

var (
commitMessage = "pushed w/ machineShop CLI"
minioLocation = "us-east-1"
)

// pushCmd represents the push command
Expand All @@ -33,51 +34,64 @@ var pushCmd = &cobra.Command{
destinationPath, _ := cmd.LocalFlags().GetString("destination")

// VERIFY IF SOURCE FILE IS EXISTING
if sourceFile != "" {
sourceExists, _ := sthingsBase.VerifyIfFileOrDirExists(sourceFile, "file")
if sourceExists {
log.Info("SOURCE FOUND : ", sourceFile)
} else {
log.Error("SOURCE NOT FOUND : ", sourceFile)
os.Exit(3)
}
} else {
log.Error("SOURCE UNDEFINED")
os.Exit(3)
}
internal.ValidateSourceFile(sourceFile)

if destinationPath != "" {

switch target {

case "minio":

log.Info("PUSHING TO MINIO S3")
log.Info("MINIO URL: ", os.Getenv("MINIO_ADDR"))
log.Info("SOURCE: ", sourceFile)
log.Info("TARGET: ", destinationPath)

switch target {
clientCreated, minioClient := sthingsCli.CreateMinioClient()

case "s3":
fmt.Println("s3")
// VERIFY S3 ENV VARS
// MINIO_URL
// ACCESS_KEY_ID
// SECRET_ACCESS_KEY
// SECURE
if !clientCreated {
log.Error("MINIO CLIENT CAN NOT BE CREATED")
os.Exit(3)
} else {
log.Info("MINIO CLIENT CREATED")

// sourceFile
// destinationPath e.g. bucket:filepath/objectname
destination := strings.Split(destinationPath, ":")
bucket := destination[0]
objectName := destination[1]

case "git":
log.Info("BUCKET: ", bucket)
log.Info("OBJECT: ", objectName)

fileContent := sthingsBase.ReadFileToVariable(sourceFile)
sthingsCli.CreateMinioBucket(minioClient, bucket, minioLocation)
uploaded, fileSize := sthingsCli.UploadObjectToMinioBucket(minioClient, bucket, sourceFile, objectName)

gitUser = internal.ValidateGetVaultSecretValue(gitUser, log)
gitToken = internal.ValidateGetVaultSecretValue(gitToken, log)
if uploaded {
log.Info("SUCCESSFULLY UPLOADED OF SIZE: ", fileSize)
}
}

// GET SECRET VALUE
gitAuth := sthingsCli.CreateGitAuth(gitUser, gitToken)
case "git":

if sthingsCli.AddCommitFileToGitRepository(gitRepository, gitBranch, gitAuth, []byte(fileContent), destinationPath, commitMessage) {
log.Info("PUSH OF FILE ", sourceFile+" SUCCESSFUL")
fileContent := sthingsBase.ReadFileToVariable(sourceFile)

gitUser = internal.ValidateGetVaultSecretValue(gitUser, log)
gitToken = internal.ValidateGetVaultSecretValue(gitToken, log)

// GET SECRET VALUE
gitAuth := sthingsCli.CreateGitAuth(gitUser, gitToken)

if sthingsCli.AddCommitFileToGitRepository(gitRepository, gitBranch, gitAuth, []byte(fileContent), destinationPath, commitMessage) {
log.Info("PUSH OF FILE ", sourceFile+" SUCCESSFUL")

} else {
log.Error("PUSH OF FILE ", sourceFile+" NOT SUCCESSFUL")
}

} else {
log.Error("PUSH OF FILE ", sourceFile+" NOT SUCCESSFUL")
}

} else {
log.Error("DESTINATION PATH SEEMS SO BE EMPTY")
}

},
}

Expand Down
26 changes: 18 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/pterm/pterm v0.12.62
github.com/spf13/cobra v1.7.0
github.com/stuttgart-things/sthingsBase v0.1.31
github.com/stuttgart-things/sthingsCli v0.1.78
github.com/stuttgart-things/sthingsCli v0.1.85
go.hein.dev/go-version v0.1.0
)

Expand All @@ -34,6 +34,7 @@ require (
github.com/coreos/go-semver v0.3.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
Expand All @@ -44,7 +45,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/gomodule/redigo v1.8.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -61,8 +62,11 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/klauspost/compress v1.17.4 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kyokomi/emoji/v2 v2.2.11 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -71,23 +75,29 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mattn/goveralls v0.0.12 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/minio-go/v7 v7.0.66 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nitishm/go-rejson/v4 v4.1.1-0.20230331060235-d2aa875760e4 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/schollz/progressbar/v3 v3.13.1 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.0 // indirect
github.com/snowzach/rotatefilehook v0.0.0-20220211133110-53752135082d // indirect
github.com/spf13/afero v1.9.5 // indirect
Expand All @@ -103,13 +113,13 @@ require (
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20230315142452-642cacee5cc0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.8.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading

0 comments on commit 7229f03

Please sign in to comment.