Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dkapanidis committed May 27, 2016
1 parent 2a86e57 commit 618a356
Show file tree
Hide file tree
Showing 12 changed files with 780 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
go build github.com/harbur/tide/cmd/tide

cross:
mkdir -p build
gox --os windows --os linux --os darwin --arch 386 --arch amd64 github.com/harbur/tide/cmd/tide
mv tide_{darwin,linux,windows}_* build

45 changes: 45 additions & 0 deletions cmd/tide/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"io/ioutil"
"log"

"github.com/spf13/cobra"
)

const applyDesc = `
This command applys a chart archive.
The apply argument must be either a relative
path to a chart directory or the name of a
chart in the current working directory.
`

var applyCmd = &cobra.Command{
Use: "apply [CHART]",
Short: "apply a chart archive.",
Long: applyDesc,
RunE: runApply,
}

func runApply(cmd *cobra.Command, args []string) error {
log.SetOutput(ioutil.Discard)
setupInstallEnv(args)
manifest, _ := readManifest(installArg)
execute("apply", manifest)
return nil
}

func setupApplyEnv(args []string) {
if len(args) > 0 {
installArg = args[0]
} else {
fatalf("This command needs at least one argument, the name of the chart.")
}

}

func init() {
applyCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose apply")
RootCommand.AddCommand(applyCmd)
}
54 changes: 54 additions & 0 deletions cmd/tide/engine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"bytes"

"github.com/kubernetes/helm/cmd/tiller/environment"
chartutil "github.com/kubernetes/helm/pkg/chart"

"github.com/kubernetes/helm/pkg/helm"
)

var env = environment.New()

func readManifest(installArg string) (string, error) {
chfi, err := chartutil.LoadChart(installArg)
if err != nil {
return "", err
}

chpb, err := helm.ChartToProto(chfi)
if err != nil {
return "", err
}

vals, err := helm.ValuesToProto(chfi)
if err != nil {
return "", err
}

overrides := map[string]interface{}{
"Release": map[string]interface{}{
"Name": "name",
"Time": "ts",
"Namespace": "s.env.Namespace",
"Service": "Tide",
},
"Chart": "req.Chart.Metadata",
}

files, err := env.EngineYard.Default().Render(chpb, vals, overrides)

b := bytes.NewBuffer(nil)
for name, file := range files {
// Ignore empty documents because the Kubernetes library can't handle
// them.
if len(file) > 0 {
b.WriteString("\n---\n# Source: " + name + "\n")
b.WriteString(file)
}
}

manifest := b.String()
return manifest, nil
}
30 changes: 30 additions & 0 deletions cmd/tide/execute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
// "bytes"
"fmt"
"io"
"os"
"os/exec"
)

func execute(command string, manifest string) {
cmd := exec.Command("kubectl", command, "-f", "-")

stdin, err := cmd.StdinPipe()

if err != nil {
fmt.Println(err)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err = cmd.Start(); err != nil {
fmt.Println("An error occured: ", err)
}

io.WriteString(stdin, manifest)
stdin.Close()
cmd.Wait()
}
59 changes: 59 additions & 0 deletions cmd/tide/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

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

"github.com/spf13/cobra"
)

const installDesc = `
This command installs a chart archive.
The install argument must be either a relative
path to a chart directory or the name of a
chart in the current working directory.
`

// install flags & args
var (
// installArg is the name or relative path of the chart to install
installArg string
// verbose enables verbose output
verbose bool
)

var installCmd = &cobra.Command{
Use: "install [CHART]",
Short: "install a chart archive.",
Long: installDesc,
RunE: runInstall,
}

func runInstall(cmd *cobra.Command, args []string) error {
log.SetOutput(ioutil.Discard)
setupInstallEnv(args)
manifest, _ := readManifest(installArg)
execute("create", manifest)
return nil
}

func setupInstallEnv(args []string) {
if len(args) > 0 {
installArg = args[0]
} else {
fatalf("This command needs at least one argument, the name of the chart.")
}
}

func fatalf(format string, args ...interface{}) {
fmt.Printf("fatal: %s\n", fmt.Sprintf(format, args...))
os.Exit(0)
}

func init() {
installCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose install")
RootCommand.AddCommand(installCmd)
}
45 changes: 45 additions & 0 deletions cmd/tide/render.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"
"io/ioutil"
"log"

"github.com/spf13/cobra"
)

const renderDesc = `
This command renders a chart archive.
The render argument must be either a relative
path to a chart directory or the name of a
chart in the current working directory.
`

var renderCmd = &cobra.Command{
Use: "render [CHART]",
Short: "render a chart archive.",
Long: renderDesc,
RunE: runRender,
}

func runRender(cmd *cobra.Command, args []string) error {
log.SetOutput(ioutil.Discard)
setupInstallEnv(args)
manifest, _ := readManifest(installArg)
fmt.Printf("%s\n", manifest)
return nil
}

func setupRenderEnv(args []string) {
if len(args) > 0 {
installArg = args[0]
} else {
fatalf("This command needs at least one argument, the name of the chart.")
}
}

func init() {
renderCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose render")
RootCommand.AddCommand(renderCmd)
}
57 changes: 57 additions & 0 deletions cmd/tide/tide.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
)

var stdout = os.Stdout
var helmHome string

// flagVerbose is a signal that the user wants additional output.
var flagVerbose bool

var globalUsage = `The Kubernetes package renderer
Tide is a standalone CLI tool to render Kubernetes packages with the Helm format.
It is a stateless tool, which uses the current directory to render the templates
and either outputs them for further usage (e.g. pipeline with kubectl or other
scripting methods) or installs them directly to Kubernetes.
Common actions from this point on include:
- tide search: search for charts
- tide fetch: download a chart to your local directory to view
- tide install: upload the chart to Kubernetes
- tide list: list releases of charts
`

// RootCommand is the top-level command for Helm.
var RootCommand = &cobra.Command{
Use: "tide",
Short: "The Kubernetes package renderer.",
Long: globalUsage,
}

func init() {
RootCommand.PersistentFlags().BoolVarP(&flagVerbose, "verbose", "v", false, "enable verbose output")
}

func main() {
RootCommand.Execute()
}

func checkArgsLength(expectedNum, actualNum int, requiredArgs ...string) error {
if actualNum != expectedNum {
arg := "arguments"
if expectedNum == 1 {
arg = "argument"
}
return fmt.Errorf("This command needs %v %s: %s", expectedNum, arg, strings.Join(requiredArgs, ", "))
}
return nil
}
45 changes: 45 additions & 0 deletions cmd/tide/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"io/ioutil"
"log"

"github.com/spf13/cobra"
)

const uninstallDesc = `
This command uninstalls a chart archive.
The uninstall argument must be either a relative
path to a chart directory or the name of a
chart in the current working directory.
`

var uninstallCmd = &cobra.Command{
Use: "uninstall [CHART]",
Short: "uninstall a chart archive.",
Long: uninstallDesc,
RunE: runUninstall,
}

func runUninstall(cmd *cobra.Command, args []string) error {
log.SetOutput(ioutil.Discard)
setupInstallEnv(args)
manifest, _ := readManifest(installArg)
execute("delete", manifest)
return nil
}

func setupUninstallEnv(args []string) {
if len(args) > 0 {
installArg = args[0]
} else {
fatalf("This command needs at least one argument, the name of the chart.")
}

}

func init() {
uninstallCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose render")
RootCommand.AddCommand(uninstallCmd)
}
Loading

0 comments on commit 618a356

Please sign in to comment.