Skip to content

Commit

Permalink
Install actually installs, added get and create commands
Browse files Browse the repository at this point in the history
  • Loading branch information
micahhausler committed Jan 17, 2017
1 parent c4e2c5d commit d31fc0e
Show file tree
Hide file tree
Showing 140 changed files with 11,387 additions and 43,706 deletions.
36 changes: 36 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Development

Always, always, always run `go fmt ./...` before committing!

### Running the tests

```bash
go get golang.org/x/tools/cmd/cover

make test
```

See the html output of the coverage information

```bash
make test-cover
```

### Updating dependencies

```bash
go get -u github.com/kardianos/govendor

govendor add +external
```

### Linting

Perfect linting is not required, but it is helpful for new people coming to the code.

```
go get -u github.com/golang/lint/golint
golint ./
golint ./render
```
59 changes: 10 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ ad01e6d4-05ec-4f18-ba6a-87cd49e6be25 alertmanager default skuid/a
fa718433-d76e-4edd-b263-9c50246c2f80 prom1 default skuid/prometheus 0.1.2 map[environment:test region:us-west-2] 0
```

Install multiple releases:
Install a release:

```
Install Releases
$ helm value-store install --selector environment=test --selector region=us-west-2
Installing releases:
helm install --name alertmanager --namespace default --version 0.1.0 alertmanager
helm install --name prom1 --namespace default --version 0.1.2 prometheus
helm install --name exporter --namespace default --version 0.1.0 prom-node-exporter
$ ./helm-value-store install --uuid 6fad4903-58ec-446f-bda4-bd39c4ff96aa
Fetched chart skuid/alertmanager to /var/folders/pr/79r611f576jczk_79lfndzgc0000gn/T/370122778/alertmanager-0.1.0.tgz
Installing Release 6fad4903-58ec-446f-bda4-bd39c4ff96aa alertmanager skuid/alertmanager 0.1.0
Successfully installed release alertmanager!
```


## Installation

### Prerequisite
Expand Down Expand Up @@ -98,57 +94,22 @@ EOF

```
$ helm-value-store
A tool loading/backing up AWS Dynamo demo data
A tool working with Helm Release data
Usage:
helm-value-store [command]
Available Commands:
install install a release
create create a release in the relase store
get-values get the values of a release
install install or upgrade a release
list list the releases
load load a json file of releases
version Print the version number
version print the version number
Use "helm-value-store [command] --help" for more information about a command.
```

## Development

Always, always, always run `go fmt ./...` before committing!

### Running the tests

```bash
go get golang.org/x/tools/cmd/cover

make test
```

See the html output of the coverage information

```bash
make test-cover
```

### Updating dependencies

```bash
go get -u github.com/kardianos/govendor

govendor add +external
```

### Linting

Perfect linting is not required, but it is helpful for new people coming to the code.

```
go get -u github.com/golang/lint/golint
golint ./
golint ./render
```

## License

MIT License (see [LICENSE](/LICENSE))
71 changes: 71 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package cmd

import (
"errors"
"fmt"
"io/ioutil"

"github.com/google/uuid"
"github.com/skuid/helm-value-store/dynamo"
"github.com/skuid/helm-value-store/store"
"github.com/spf13/cobra"
)

type createCmdArgs struct {
table string
file string
labels selectorSet
name string
chart string
namespace string
version string
}

var createArgs = &createCmdArgs{}

var createCmd = &cobra.Command{
Use: "create",
Short: "create a release in the relase store",
Run: create,
}

func init() {
RootCmd.AddCommand(createCmd)
createCmd.Flags().StringVar(&createArgs.table, "table", "helm-charts", "Name of table")
createCmd.Flags().StringVarP(&createArgs.file, "file", "f", "", "Name of values file")
createCmd.Flags().VarP(&createArgs.labels, "labels", "l", `The labels to apply. Each label should have the format "k=v".
Can be specified multiple times, or a comma-separated list.`)
createCmd.Flags().StringVar(&createArgs.name, "name", "", "Name of the release")
createCmd.Flags().StringVar(&createArgs.chart, "chart", "", "Chart of the release")
createCmd.Flags().StringVar(&createArgs.namespace, "namespace", "default", "Namespace of the release")
createCmd.Flags().StringVar(&createArgs.version, "version", "", "Version of the release")
}

func create(cmd *cobra.Command, args []string) {
r := store.Release{
UniqueID: uuid.New().String(),
Labels: createArgs.labels.ToMap(),
Name: createArgs.name,
Chart: createArgs.chart,
Namespace: createArgs.namespace,
Version: createArgs.version,
}
fmt.Printf("%#v\n", r)
fmt.Println(r)

if len(createArgs.file) > 0 {
values, err := ioutil.ReadFile(createArgs.file)
exitOnErr(err)
r.Values = string(values)
}
if len(createArgs.chart) == 0 {
exitOnErr(errors.New("No chart provided!"))
}

rs, err := dynamo.NewReleaseStore(createArgs.table)
exitOnErr(err)

err = rs.Put(r)
exitOnErr(err)
fmt.Println("Created release in dynamo!")
}
46 changes: 46 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"errors"
"fmt"

"github.com/skuid/helm-value-store/dynamo"
"github.com/spf13/cobra"
)

type getCmdArgs struct {
timeout int64
dryRun bool
table string
selector selectorSet

uuid string
name string
}

var getArgs = getCmdArgs{}

var getCmd = &cobra.Command{
Use: "get-values",
Short: "get the values of a release",
Run: get,
}

func init() {
RootCmd.AddCommand(getCmd)
getCmd.Flags().StringVar(&getArgs.table, "table", "helm-charts", "Name of table")
getCmd.Flags().StringVar(&getArgs.uuid, "uuid", "", "The UUID to get.")
}

func get(cmd *cobra.Command, args []string) {
rs, err := dynamo.NewReleaseStore(getArgs.table)
exitOnErr(err)

if len(getArgs.uuid) == 0 {
exitOnErr(errors.New("Must supply a UUID!"))
}
release, err := rs.Get(getArgs.uuid)
exitOnErr(err)

fmt.Print(release.Values)
}
78 changes: 59 additions & 19 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package cmd

import (
"fmt"
"os"
"text/template"

"github.com/skuid/helm-value-store/dynamo"
"github.com/skuid/helm-value-store/store"
"github.com/spf13/cobra"
)

Expand All @@ -14,47 +13,88 @@ type installCmdArgs struct {
dryRun bool
table string
selector selectorSet

uuid string
name string
}

var installArgs = installCmdArgs{}

var installCmd = &cobra.Command{
Use: "install",
Short: "install a release",
Short: "install or upgrade a release",
Long: "Install a release in the cluster and use the values from the value store",
Run: install,
}

var installTmpl *template.Template

func init() {
RootCmd.AddCommand(installCmd)
installCmd.Flags().StringVar(&installArgs.table, "table", "helm-charts", "Name of table")
installCmd.Flags().Int64Var(&installArgs.timeout, "timeout", 300, "time in seconds to wait for any individual kubernetes operation (like Jobs for hooks)")
installCmd.Flags().BoolVar(&installArgs.dryRun, "dry-run", false, "simulate an upgrade")
installCmd.Flags().BoolVar(&installArgs.dryRun, "dry-run", false, "simulate an install/upgrade")
installCmd.Flags().VarP(&installArgs.selector, "selector", "s", `The selectors to use. Each selector should have the format "k=v".
Can be specified multiple times, or a comma-separated list.`)
installCmd.Flags().StringVar(&installArgs.uuid, "uuid", "", "The UUID to install. Takes precedence over --name")
installCmd.Flags().StringVar(&installArgs.name, "name", "", `The name of the release to install. If multiple releases of the same name are found,
the install will fail. Use selectors to pair down releases`)
}

var err error
installTmpl, err = template.New("InstallCmd").Parse(
"REGION={{.Labels.region}} ENV={{.Labels.environment}} helm install{{ if .Name }} --name {{.Name}}{{ end }}{{if .Namespace}} --namespace {{.Namespace}}{{end}}{{if .Version}} --version {{.Version}}{{end}} {{.Chart}} \n",
)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
func releasesByName(name string, releases store.Releases) (release store.Releases) {
response := store.Releases{}
for _, r := range releases {
if r.Name == name {
response = append(response, r)
}
}
return response
}

func install(cmd *cobra.Command, args []string) {
fmt.Println("Installing releases:")
fmt.Println("")

rs, err := dynamo.NewReleaseStore(installArgs.table)
exitOnErr(err)

releases, err := rs.List(installArgs.selector.ToMap())
release := &store.Release{}

if len(installArgs.uuid) > 0 {
release, err = rs.Get(installArgs.uuid)
exitOnErr(err)
} else if len(installArgs.name) > 0 {
releases, err := rs.List(installArgs.selector.ToMap())
exitOnErr(err)

matches := releasesByName(installArgs.name, releases)
if len(matches) > 1 {
exitOnErr(fmt.Errorf("Too many releases by the name: %s", installArgs.name))
} else if len(matches) < 1 {
exitOnErr(fmt.Errorf("No releases by the name: %s", installArgs.name))
}
release = &matches[0]
} else {
exitOnErr(fmt.Errorf("No release specified! Use %s or %s", "--name", "--uuid"))
}
_, getErr := release.Get()

if getErr != nil && getErr.Error() != "rpc error: code = 2 desc = release: not found" {
exitOnErr(err)
}

dlLocation, err := release.Download()
exitOnErr(err)
fmt.Printf("Fetched chart %s to %s\n", release.Chart, dlLocation)

for _, r := range releases {
installTmpl.Execute(os.Stdout, &r)
if getErr != nil && getErr.Error() == "rpc error: code = 2 desc = release: not found" {
// Install
fmt.Printf("Installing Release %s\n", release)

response, err := release.Install(dlLocation, installArgs.dryRun, installArgs.timeout)
exitOnErr(err)
fmt.Printf("Successfully installed release %s!\n", response.Release.Name)
} else if getErr == nil {
// Update
fmt.Printf("Updating Release %s\n", release)
response, err := release.Upgrade(dlLocation, installArgs.dryRun, installArgs.timeout)
exitOnErr(err)
fmt.Printf("Successfully upgraded release %s!\n", response.Release.Name)
}

}
2 changes: 1 addition & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"strings"
"text/tabwriter"

"github.com/cloudfoundry/bytefmt"
"github.com/skuid/helm-value-store/dynamo"
"github.com/spf13/cobra"
"github.com/cloudfoundry/bytefmt"
)

type listCmdArgs struct {
Expand Down
5 changes: 0 additions & 5 deletions cmd/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ var loadCmd = &cobra.Command{
}

func init() {
err := os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
RootCmd.AddCommand(loadCmd)
loadCmd.Flags().StringVar(&loadArgs.file, "file", "dynamoReleases.json", "Name of file to ingest")
loadCmd.Flags().StringVar(&loadArgs.table, "table", "helm-charts", "Name of table")
Expand Down
Loading

0 comments on commit d31fc0e

Please sign in to comment.