Commit 905f703a authored by SWE CI's avatar SWE CI

Merge branch 'release/v0.2.0'

parents bf02850d 40787037
Pipeline #30101 passed with stage
in 20 seconds
```
__________ _____________ ___ .___ __ .__
\______ \/ _____/ | \ __| _/_______ ___/ |_ ____ ____ | |
| ___/\_____ \| | / / __ |/ __ \ \/ /\ __\/ _ \ / _ \| |
| | / \ | / / /_/ \ ___/\ / | | ( <_> | <_> ) |__
|____| /_______ /______/ \____ |\___ >\_/ |__| \____/ \____/|____/
\/ \/ \/
```
### Help
```
A tool used by software engineering to build applications.
Usage:
devtool [command]
Available Commands:
build build the application
ci Build tools for the CI system
clean delete the helm release(s)
config Manage the projects build configuration
help Help about any command
run run the application
start start the minikube environment
version displays the software version
Flags:
--config string config file (default is $HOME/.devtool.yaml)
-h, --help help for devtool
Use "devtool [command] --help" for more information about a command.
```
### Configuration
The devtool commands can be configured through CLI arguments, through ENV variables, or a global configuration file (~/.devtool.yaml)
```yaml
minikube:
memory: 8192
cpu: 6
build:
skipCompile: false
runTests: true
openServices: true
```
\ No newline at end of file
......@@ -25,12 +25,9 @@ import (
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var skipCompile bool
var runTests bool
var openServices bool
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
......@@ -77,13 +74,30 @@ func init() {
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
buildCmd.Flags().BoolVar(&skipCompile, "skip-compile", false, "Skip the compile step")
buildCmd.Flags().BoolVar(&runTests, "tests", false, "Run unit tests during the build phase")
buildCmd.Flags().BoolVar(&openServices, "open", false, "Open browser to the services after deploying")
buildCmd.Flags().Bool("skip-compile", false, "Skip the compile step")
viper.SetDefault("build.skipCompile", false)
err := viper.BindPFlag("build.skipCompile", buildCmd.Flags().Lookup("skip-compile"))
if err != nil {
panic("invalid arg")
}
buildCmd.Flags().Bool("tests", false, "Run unit tests during the build phase")
viper.SetDefault("build.runTests", false)
err = viper.BindPFlag("build.runTests", buildCmd.Flags().Lookup("tests"))
if err != nil {
panic("invalid arg")
}
buildCmd.Flags().Bool("open", false, "Open browser to the services after deploying")
viper.SetDefault("build.openServices", false)
err = viper.BindPFlag("build.openServices", buildCmd.Flags().Lookup("open"))
if err != nil {
panic("invalid arg")
}
}
func build(config config.Config) error {
if skipCompile {
if viper.GetBool("build.skipCompile") {
color.Yellow("Skipping Build Step.")
return nil
}
......@@ -93,7 +107,7 @@ func build(config config.Config) error {
switch lang := config.Language; lang {
case "java":
args := []string{"clean", "install", "-Ddependency-check.skip=true"}
if !runTests {
if !viper.GetBool("build.runTests") {
args = append(args, "-DskipTests")
}
environment.RunRequired(true, "mvn", args...)
......@@ -158,7 +172,7 @@ func buildDockerAndDeploy(config config.Config) error {
return err
}
if openServices {
if viper.GetBool("build.openServices") {
err = environment.Run(true, "minikube", "service", releaseName)
if err != nil {
return err
......
// Copyright © 2019 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
var cleanAll bool
var cleanForce bool
// cleanCmd represents the clean command
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "delete the helm release(s)",
Run: func(cmd *cobra.Command, args []string) {
environment.EvalDockerEnv()
err := environment.Run(true, "minikube", "status")
if err != nil {
color.Red("Minikube is not started... run `devtool start` first.")
return
}
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).Printf("Error reading configuration file: %s\n", configFile)
return
}
if cleanAll {
err = cleanAllHelm()
} else {
err = cleanHelm(conf)
}
if err != nil {
color.Red("Failed to clean helm releases.")
return
}
},
}
func init() {
rootCmd.AddCommand(cleanCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// cleanCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
cleanCmd.Flags().BoolVarP(&cleanAll, "all", "a", false, "remove all helm releases")
cleanCmd.Flags().BoolVarP(&cleanForce, "force", "f", false, "don't prompt user to confirm removal")
}
func cleanHelm(config config.Config) error {
color.Blue("Removing Helm Releases")
for _, deployable := range config.Deployables {
releaseName := deployable.Name + "-local"
err := removeHelmRelease(releaseName)
if err != nil {
return err
}
}
return nil
}
func cleanAllHelm() error {
color.Blue("Removing ALL Helm Releases")
helmReleases, err := environment.RunAndGetOutput("helm", "ls", "--all", "--short")
if err != nil {
return err
}
for _, releaseName := range helmReleases {
if releaseName == "" {
continue
}
err := removeHelmRelease(releaseName)
if err != nil {
return err
}
}
return nil
}
func removeHelmRelease(releaseName string) error {
color.New(color.FgGreen).Printf("Removing: %s\n", releaseName)
if !cleanForce {
prompt := promptui.Prompt{
Label: "Are you sure",
IsConfirm: true,
}
_, err := prompt.Run()
if err != nil {
return err
}
}
args := []string{"delete", "--purge", releaseName}
err := environment.Run(true, "helm", args...)
if err != nil {
color.New(color.FgRed).Printf("helm error: %s\n", err.Error())
return err
}
return nil
}
......@@ -17,6 +17,7 @@ package cmd
import (
"fmt"
"os"
"strings"
"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
......@@ -81,10 +82,14 @@ func initConfig() {
}
viper.AutomaticEnv() // read in environment variables that match
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
color.Red(err.Error())
}
}
......
......@@ -17,25 +17,29 @@ package cmd
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/spf13/viper"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var Memory string
var Cpu string
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "start the minikube environment",
Run: func(command *cobra.Command, args []string) {
fmt.Println("start called")
start(Memory, Cpu)
color.Red(viper.GetString("example"))
memory := strconv.Itoa(viper.GetInt("minikube.memory"))
cpu := strconv.Itoa(viper.GetInt("minikube.cpu"))
start(memory, cpu)
},
}
......@@ -51,9 +55,19 @@ func init() {
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
startCmd.PersistentFlags().StringVarP(&Memory, "memory", "m", "8192", "memory for minikube (default is 8192)")
startCmd.PersistentFlags().StringVarP(&Cpu, "cpr", "c", "6", "cpr for minikube (default is 6)")
startCmd.PersistentFlags().IntP("memory", "m", 0, "memory for minikube (default is 8192)")
viper.SetDefault("minikube.memory", 8192)
err := viper.BindPFlag("minikube.memory", startCmd.PersistentFlags().Lookup("memory"))
if err != nil {
panic("invalid arg")
}
startCmd.PersistentFlags().IntP("cpu", "c", 0, "cpu for minikube (default is 6)")
viper.SetDefault("minikube.cpu", 6)
err = viper.BindPFlag("minikube.cpu", startCmd.PersistentFlags().Lookup("cpu"))
if err != nil {
panic("invalid arg")
}
}
func start(memory, cpu string) {
......@@ -70,7 +84,7 @@ func start(memory, cpu string) {
color.Blue("Setting up Minikube environment.")
fmt.Printf("Using memory: %s and cpr: %s\n", memory, cpu)
fmt.Printf("Using memory: %s and cpu: %s\n", memory, cpu)
err := environment.Run(true, "minikube", "status")
if err != nil {
......@@ -81,6 +95,8 @@ func start(memory, cpu string) {
checkInstallTiller()
checkIfSwaggerUiInstalled()
}
func checkLocalSetup() bool {
......@@ -141,12 +157,25 @@ func checkInstallTiller() {
color.Yellow("Installing Tiller into the cluster...")
environment.RunRequired(true, "helm", "init")
time.Sleep(10 * time.Second)
serverVersionOutput, err = environment.RunAndGetOutput("helm", "version", "--server", "--short")
yellow := color.New(color.FgYellow)
yellow.Print("Waiting for Tiller to start.")
for i := 0; i < 30; i++ {
time.Sleep(2 * time.Second)
serverVersionOutput, err = environment.RunAndGetOutput("helm", "version", "--server", "--short")
if err != nil {
yellow.Print(".")
} else {
fmt.Println()
break
}
}
if err != nil {
color.Red("Cannot determin helm server version")
color.Red("Could not install Tiller into the cluster.")
}
}
clientVersion := strings.TrimPrefix(clientVersionOutput[0], "Client: ")
......@@ -159,3 +188,22 @@ func checkInstallTiller() {
warn(" Server Version: %s\n", serverVersion)
}
}
func checkIfSwaggerUiInstalled() {
color.Blue("Checking if Swagger UI is installed.")
helmReleases := environment.RunAndGetOutputRequired("helm", "ls", "--all", "--short")
swaggerInstalled := false
for _, releaseName := range helmReleases {
if releaseName == "swagger-ui" {
swaggerInstalled = true
break
}
}
if !swaggerInstalled {
environment.RunRequired(true, "helm", "install", "--name", "swagger-ui", "cm/swagger-ui")
}
}
......@@ -58,7 +58,7 @@ func RunAndGetOutput(name string, args ...string) ([]string, error) {
out, err := cmd.CombinedOutput()
if err != nil {
color.Red("Command Failed with '%s'\n", err)
return []string{}, err
}
lines := strings.Split(string(out), "\n")
......
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