Skip to content

Commit

Permalink
Add prototype for installing apps
Browse files Browse the repository at this point in the history
Installs OpenFaaS, but not on armhf, optional loadbalancer
flag.

Pre-reqs are not checked i.e. helm/tiller.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Oct 10, 2019
1 parent e7cbae8 commit 0b4997e
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func main() {

cmdJoin := cmd.MakeJoin()

cmdApps := cmd.MakeApps()

printk3supASCIIArt := cmd.PrintK3supASCIIArt

var rootCmd = &cobra.Command{
Expand All @@ -26,6 +28,7 @@ func main() {
rootCmd.AddCommand(cmdInstall)
rootCmd.AddCommand(cmdVersion)
rootCmd.AddCommand(cmdJoin)
rootCmd.AddCommand(cmdApps)

rootCmd.Execute()
}
128 changes: 128 additions & 0 deletions pkg/cmd/apps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package cmd

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"strings"

"github.com/spf13/cobra"
)

func MakeApps() *cobra.Command {
var command = &cobra.Command{
Use: "app",
Short: "Manage Kubernetes apps",
Long: `Manage Kubernetes apps`,
Example: ` k3sup app install openfaas`,
SilenceUsage: false,
}

var install = &cobra.Command{
Use: "install",
Short: "Install a Kubernetes app",
Long: `Install a Kubernetes app`,
Example: ` k3sup app install [APP]`,
SilenceUsage: true,
}

install.Flags().String("kubeconfig", "kubeconfig", "Local path to save the kubeconfig file")

openfaas := makeInstallOpenFaaS()

install.RunE = func(command *cobra.Command, args []string) error {

if len(args) == 0 {
fmt.Printf("You can install: %s\n", strings.TrimRight(strings.Join(getApps(), ", "), ", "))
return nil
}

return nil
}

command.AddCommand(install)
install.AddCommand(openfaas)

return command
}

func getApps() []string {
return []string{"openfaas"}
}

func installOpenFaaS(kubeconfigPath string, loadBalancer bool) error {

res, err := http.Get("https://raw.githubusercontent.com/openfaas/faas-netes/master/install.sh")
if err != nil {
return err
}
defer res.Body.Close()

out, _ := ioutil.ReadAll(res.Body)

val := string(out)
if !loadBalancer {
val = strings.Replace(val, "LoadBalancer", "NodePort", -1)
}

script := path.Join(os.TempDir(), "install.sh")

err = ioutil.WriteFile(script, []byte(val), 0600)
if err != nil {
return err
}

fmt.Printf("Wrote script to: %s\n", script)

cmd1 := exec.Command("/bin/bash", script)
cmd1.Env = append(os.Environ(), "KUBECONFIG="+kubeconfigPath)
cmd1.Stderr = os.Stderr
cmd1.Stdout = os.Stdout

startErr := cmd1.Start()
if startErr != nil {
return startErr
}

cmd1.Wait()

return nil
}

func makeInstallOpenFaaS() *cobra.Command {
var openfaas = &cobra.Command{
Use: "openfaas",
Short: "Install openfaas",
Long: `Install openfaas`,
Example: ` k3sup app install openfaas --loadbalancer`,
SilenceUsage: true,
}

openfaas.Flags().Bool("loadbalancer", false, "add a loadbalancer")

openfaas.RunE = func(command *cobra.Command, args []string) error {
kubeConfigPath := path.Join(os.Getenv("HOME"), ".kube/config")

if val, ok := os.LookupEnv("KUBECONFIG"); ok {
kubeConfigPath = val
}
if command.Flags().Changed("kubeconfig") {
kubeConfigPath, _ = command.Flags().GetString("kubeconfig")
}
fmt.Printf("Using context: %s\n", kubeConfigPath)

lb, _ := command.Flags().GetBool("loadbalancer")
err := installOpenFaaS(kubeConfigPath, lb)

if err != nil {
return err
}

return nil
}

return openfaas
}

0 comments on commit 0b4997e

Please sign in to comment.