Skip to content

Commit

Permalink
Merge pull request #18 from replicatedhq/cli-license
Browse files Browse the repository at this point in the history
UploadFromLicense export
  • Loading branch information
marccampbell authored Aug 26, 2019
2 parents 315a0ba + 30fbdb3 commit 18ab0cb
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 17 deletions.
54 changes: 54 additions & 0 deletions cmd/kots/cli/install-license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cli

import (
"os"
"path/filepath"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/upload"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

/*
This code needs to be worked into an existing CLI method
*/

func InstallLicenseCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "install-license [path-to-license]",
Short: "",
Long: ``,
SilenceUsage: true,
SilenceErrors: false,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

if len(args) == 0 {
cmd.Help()
os.Exit(1)
}

uploadLicenseOptions := upload.UploadLicenseOptions{
Namespace: v.GetString("namespace"),
Kubeconfig: v.GetString("kubeconfig"),
NewAppName: v.GetString("name"),
}

if err := upload.UploadLicense(ExpandDir(args[0]), uploadLicenseOptions); err != nil {
return errors.Cause(err)
}

return nil
},
}

cmd.Flags().String("kubeconfig", filepath.Join(homeDir(), ".kube", "config"), "the kubeconfig to use")
cmd.Flags().String("namespace", "default", "the namespace to upload to")
cmd.Flags().String("name", "", "the name of the kotsadm application to create")

return cmd
}
1 change: 1 addition & 0 deletions cmd/kots/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func RootCmd() *cobra.Command {

cmd.AddCommand(PullCmd())
cmd.AddCommand(InstallCmd())
cmd.AddCommand(InstallLicenseCmd())
cmd.AddCommand(UploadCmd())
cmd.AddCommand(DownloadCmd())
cmd.AddCommand(AdminConsoleCmd())
Expand Down
14 changes: 13 additions & 1 deletion deploy/.goreleaser.snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ release:
name: kots
builds:
- id: kots
- goos:
goos:
- linux
- windows
- darwin
Expand All @@ -24,6 +24,18 @@ builds:
flags: -tags netgo -installsuffix netgo
binary: kots
hooks: {}
- id: so
goos:
- linux
goarch:
- amd64
env:
- CGO_ENABLED=1
- GO111MODULE=on
main: ffi/main.go
flags: -buildmode=c-shared
binary: kots.so
hooks: {}

snapshot:
name_template: SNAPSHOT-{{ .Commit }}
13 changes: 13 additions & 0 deletions deploy/.goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ builds:
flags: -tags netgo -installsuffix netgo
binary: kots
hooks: {}
- id: so
goos:
- linux
goarch:
- amd64
env:
- CGO_ENABLED=1
- GO111MODULE=on
main: ffi/main.go
flags: -buildmode=c-shared
binary: kots.so
hooks: {}

archives:
- id: kots
builds:
- kots
- so
format: tar.gz
format_overrides:
- goos: windows
Expand Down
90 changes: 84 additions & 6 deletions ffi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,97 @@ package main
import "C"

import (
"os"
"fmt"
"path"
"io/ioutil"

"github.com/replicatedhq/kots/pkg/pull"
"github.com/mholt/archiver"

)


//export Pull
func Pull(upstreamURI string) error {
pullOptions := pull.PullOptions{}
//export PullFromLicense
func PullFromLicense(licenseData string, downstream string, outputFile string) int {
licenseFile, err := ioutil.TempFile("", "kots")
if err != nil {
fmt.Printf("failed to create temp file: %#v\n", err)
return 1
}
defer os.Remove(licenseFile.Name())

if err := ioutil.WriteFile(licenseFile.Name(), []byte(licenseData), 0644); err != nil {
fmt.Printf("failed to write license to temp file: %#v\n", err)
return 1
}

// pull to a tmp dir
tmpRoot, err := ioutil.TempDir("", "kots")
if err != nil {
fmt.Printf("failed to create temp root path: %#v\n", err)
return 1
}
defer os.RemoveAll(tmpRoot)

pullOptions := pull.PullOptions{
Overwrite: true,
Downstreams: []string{downstream},
LicenseFile: licenseFile.Name(),
ExcludeKotsKinds: true,
RootDir: tmpRoot,
}

if err := pull.Pull("replicated://sentry-enterprise", pullOptions); err != nil {
fmt.Printf("failed to pull upstream: %#v\n", err)
return 1
}

// make an archive
tarGz := archiver.TarGz{
Tar: &archiver.Tar{
ImplicitTopLevelFolder: true,
},
}

// the tmp dir has the app name in it now
subdirs, err := ioutil.ReadDir(tmpRoot)
if err != nil {
fmt.Printf("unable to read dir: %#v\n", err)
return 1
}

uploadRootDir := ""
for _, subdir := range subdirs {
if subdir.IsDir() {
if subdir.Name() == "." {
continue
}
if subdir.Name() == ".." {
continue
}

uploadRootDir = path.Join(tmpRoot, subdir.Name())
break
}
}
if uploadRootDir == "" {
fmt.Println("failed to find upload root dir")
return 1
}

paths := []string{
path.Join(uploadRootDir, "upstream"),
path.Join(uploadRootDir, "base"),
path.Join(uploadRootDir, "overlays"),
}

if err := pull.Pull(upstreamURI, pullOptions); err != nil {
return err
if err := tarGz.Archive(paths, outputFile); err != nil {
fmt.Printf("failed to write archive: %#v\n", err)
return 1
}

return nil
return 0
}

func main() {}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
10 changes: 6 additions & 4 deletions pkg/base/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ func renderReplicated(u *upstream.Upstream, renderOptions *RenderOptions) (*Base
builder := template.Builder{}
builder.AddCtx(template.StaticCtx{})

configCtx, err := builder.NewConfigContext(config.Spec.Groups, templateContext)
if err != nil {
return nil, errors.Wrap(err, "failed to create config context")
if config != nil {
configCtx, err := builder.NewConfigContext(config.Spec.Groups, templateContext)
if err != nil {
return nil, errors.Wrap(err, "failed to create config context")
}
builder.AddCtx(configCtx)
}
builder.AddCtx(configCtx)

for _, upstreamFile := range u.Files {
rendered, err := builder.RenderTemplate(upstreamFile.Path, string(upstreamFile.Content))
Expand Down
2 changes: 1 addition & 1 deletion pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Download(path string, downloadOptions DownloadOptions) error {
podName, err := findKotsadm(downloadOptions)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "faioled to find kotsadm pod")
return errors.Wrap(err, "failed to find kotsadm pod")
}

// set up port forwarding to get to it
Expand Down
3 changes: 1 addition & 2 deletions pkg/upload/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func findUpdateCursor(rootPath string) (string, error) {
if os.IsNotExist(err) {
return "", nil
}

if err != nil {
return "", errors.Wrap(err, "failed to open file with update cursor")
return "", errors.Wrap(err, "failed to open file")
}

b, err := ioutil.ReadFile(cursorFilePath)
Expand Down
6 changes: 3 additions & 3 deletions pkg/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func Upload(path string, uploadOptions UploadOptions) error {
log := logger.NewLogger()
log.ActionWithSpinner("Uploading local application to Admin Console")

podName, err := findKotsadm(uploadOptions)
podName, err := findKotsadm(uploadOptions.Namespace)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed to find kotsadm pod")
Expand Down Expand Up @@ -147,7 +147,7 @@ func Upload(path string, uploadOptions UploadOptions) error {
return nil
}

func findKotsadm(uploadOptions UploadOptions) (string, error) {
func findKotsadm(namespace string) (string, error) {
cfg, err := config.GetConfig()
if err != nil {
return "", errors.Wrap(err, "failed to get cluster config")
Expand All @@ -158,7 +158,7 @@ func findKotsadm(uploadOptions UploadOptions) (string, error) {
return "", errors.Wrap(err, "failed to create kubernetes clientset")
}

pods, err := clientset.CoreV1().Pods(uploadOptions.Namespace).List(metav1.ListOptions{LabelSelector: "app=kotsadm-api"})
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: "app=kotsadm-api"})
if err != nil {
return "", errors.Wrap(err, "failed to list pods")
}
Expand Down
Loading

0 comments on commit 18ab0cb

Please sign in to comment.