Commit c5d371b7 authored by Christopher Harm's avatar Christopher Harm

CLI with start and environment commands

parent 56c5e3d2
{
"name": "example",
"language": "java",
"deployables": [
{
"name": "wildfly",
"dockerfile": "",
"chart": "cm/eio-swe-service",
"chartVersion": "v0.3.13",
"localConfig": "config/local.yml"
}
],
"localEnvVars": [
"OAUTH_CLIENT_ID"
],
"localSecrets": [
"OAUTH_CLIENT_SECRET",
"EXT_DB_PASSWORD"
]
}
\ No newline at end of file
.go-project.mk
devtool
# Define where your included makefile lives
GITURL = git.psu.edu/k8s/gitlab-ci-templates.git
GITPROJECT = eio-swe-golang-flux
GITFILE = .go-project.mk
# Override your docker tag here, otherwise it'll use your PWD
# DOCKER_TAG = everything-is-awesome
-include $(shell test -f .go-project.mk || git archive --remote=ssh://git@$(GITURL) HEAD:$(GITPROJECT) $(GITFILE) | tar -x ; echo .go-project.mk)
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"git.psu.edu/swe-golang/logenv"
"github.com/codegangsta/cli"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
)
var printWarn = color.New(color.FgYellow).PrintfFunc()
var printError = color.New(color.FgRed).PrintfFunc()
type (
Config struct {
Name string `json:"name"`
Language string `json:"language"`
Deployables []Deployable `json:"deployables"`
LocalEnvVars []string `json:"localEnvVars"`
LocalSecrets []string `json:"localSecrets"`
}
Deployable struct {
Name string `json:"name"`
Dockerfile string `json:"dockerfile"`
Chart string `json:"chart"`
ChartVersion string `json:"chartVersion"`
LocalConfig string `json:"localConfig"`
}
)
func main() {
app := cli.NewApp()
app.Name = "Penn State devtool"
app.Usage = "to build projects"
app.Version = "1.0"
app.Author = "EIT - SWE"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose",
Usage: "Show more output",
},
}
app.Commands = []cli.Command{
{
Name: "start",
Usage: "start the environment",
Action: CmdStart,
Flags: []cli.Flag{
cli.StringFlag{
Name: "memory",
Usage: "memory for the minikube environment",
},
cli.StringFlag{
Name: "cpu",
Usage: "cpus for the minikube environment",
},
},
},
{
Name: "environment",
Usage: "displays the current environment variables",
Action: CmdEnvironment,
Flags: []cli.Flag{
cli.StringFlag{
Name: "memory",
Usage: "memory for the minikube environment",
},
cli.StringFlag{
Name: "cpu",
Usage: "cpus for the minikube environment",
},
},
},
{
Name: "build",
Usage: "builds the current project",
Action: CmdBuild,
Flags: []cli.Flag{
cli.StringFlag{
Name: "memory",
Usage: "memory for the minikube environment",
},
cli.StringFlag{
Name: "cpu",
Usage: "cpus for the minikube environment",
},
},
},
}
color.Blue("Penn State - devtool...")
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
app.Run(os.Args)
}
func CmdStart(context *cli.Context) {
color.Blue("Checking Prerequisites.")
kubeMem := context.String("memory")
kubeCpu := context.String("cpu")
prereqsInstalled := checkLocalSetup()
if !prereqsInstalled {
printError("Please install the required applications and try again.\n")
return
}
runRequired(true, "helm", "repo", "add", "qa", "https://charts.qa.k8s.psu.edu")
runRequired(true, "helm", "repo", "add", "cm", "https://cm.qa.k8s.psu.edu")
color.Blue("Setting up Minikube environment.")
if kubeMem == "" {
kubeMem = os.Getenv("MINIKUBE_MEMORY_MB")
if kubeMem == "" {
kubeMem = "8192"
}
}
if kubeCpu == "" {
kubeCpu = os.Getenv("MINIKUBE_CPUS")
if kubeCpu == "" {
kubeCpu = "6"
}
}
err := run(true, "minikube", "status")
if err != nil {
runRequired(true, "minikube", "start", "--memory", kubeMem, "--cpus", kubeCpu)
}
evalDockerEnv()
checkInstallTiller()
}
func init() {
err := logenv.ConfigureFromEnvironment()
if err != nil {
log.Fatal("Unable to configure the logger from the environment", err)
}
}
func checkLocalSetup() bool {
var prereqsInstalled = true
err := checkInstalled("kubectl")
if err != nil {
prereqsInstalled = false
}
err = checkInstalled("helm")
if err != nil {
prereqsInstalled = false
}
err = checkInstalled("minikube")
if err != nil {
prereqsInstalled = false
}
return prereqsInstalled
}
func checkInstalled(app string) error {
path, err := exec.LookPath(app)
if err != nil {
printWarn("%s is not installed\n", app)
} else {
fmt.Printf("%s: %s\n", app, path)
}
return err
}
func evalDockerEnv() {
lines := runAndGetOutput("minikube", "docker-env")
r := regexp.MustCompile(`export (?P<Name>.*)="(?P<Value>.*)"`)
for _, line := range lines {
if line != "" && !strings.HasPrefix(line, "#") {
parts := r.FindStringSubmatch(line)
os.Setenv(parts[1], parts[2])
}
}
}
func checkInstallTiller() {
clientVersionOutput := runAndGetOutput("helm", "version", "--client", "--short")
serverVersionOutput := runAndGetOutput("helm", "version", "--server", "--short")
clientVersion := strings.TrimPrefix(clientVersionOutput[0], "Client: ")
serverVersion := strings.TrimPrefix(serverVersionOutput[0], "Server: ")
if clientVersion != serverVersion {
printWarn("Helm version mismatch between client and server")
printWarn("Client Version: %s\n", clientVersion)
printWarn("Server Version: %s\n", serverVersion)
}
}
func CmdEnvironment(context *cli.Context) {
CmdStart(context)
color.Blue("Environment Variables:")
for _, e := range os.Environ() {
fmt.Println(e)
}
}
func CmdBuild(context *cli.Context) {
CmdStart(context)
color.Blue("Building Application")
}
func run(viewOutput bool, name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Env = os.Environ()
if viewOutput {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err := cmd.Run()
return err
}
func runRequired(viewOutput bool, name string, args ...string) {
err := run(viewOutput, name, args...)
if err != nil {
color.Red("Command Failed with '%s'\n", err)
panic("exiting.")
}
}
func runAndGetOutput(name string, args ...string) []string {
cmd := exec.Command(name, args...)
cmd.Env = os.Environ()
out, err := cmd.CombinedOutput()
if err != nil {
color.Red("Command Failed with '%s'\n", err)
}
lines := strings.Split(string(out), "\n")
return lines
}
git.psu.edu/swe-golang/logenv v0.1.1 h1:V0m60SPDgSReGfFKUEiv1+/p37bl3BK6BgAHnTN7onY=
git.psu.edu/swe-golang/logenv v0.1.1/go.mod h1:HNbPmgmnKC0uyZ7pg0qz9y+sriBQQ6fDl3CbB+YV4yY=
github.com/codegangsta/cli v1.20.0 h1:iX1FXEgwzd5+XN6wk5cVHOGQj6Q3Dcp20lUeS4lHNTw=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/joonix/log v0.0.0-20190213172830-51a6cca1fed3 h1:tqmwKErGTTFFdDDeLuOKK2OJZzxaw3xmnlg4y8qj4qY=
github.com/joonix/log v0.0.0-20190213172830-51a6cca1fed3/go.mod h1:9alna084PKap49x3Dl7QTGUXiS37acLi8ryAexT1SJc=
github.com/kelseyhightower/envconfig v1.3.0 h1:IvRS4f2VcIQy6j4ORGIf9145T/AsUB+oY8LyvN8BXNM=
github.com/kelseyhightower/envconfig v1.3.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/mattn/go-colorable v0.1.1 h1:G1f5SKeVxmagw/IyvzvtZE4Gybcc4Tr1tf7I8z0XgOg=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.0 h1:yKenngtzGh+cUSSh6GWbxW2abRqhYUSR/t/6+2QqNvE=
github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment