Commit 641990a8 authored by SWE CI's avatar SWE CI

Merge branch 'release/v0.3.0'

parents cc210235 f56ec983
Pipeline #32043 passed with stage
in 40 seconds
.go-project.mk
devtool
.devtool.config
.vscode
......@@ -33,7 +33,11 @@ var buildCmd = &cobra.Command{
Use: "build",
Short: "build the application",
Run: func(cmd *cobra.Command, args []string) {
checkLocalSetup()
prereqsInstalled := checkLocalSetup()
if !prereqsInstalled {
color.Red("Please install the required applications and try again.\n")
return
}
err := environment.Run(false, "minikube", "status")
if err != nil {
......@@ -108,6 +112,13 @@ func init() {
panic("invalid arg")
}
buildCmd.Flags().Bool("dry-run", false, "Use the helm template command to output k8s objects")
viper.SetDefault("build.helm.dry.run", false)
err = viper.BindPFlag("build.helm.dry.run", buildCmd.Flags().Lookup("dry-run"))
if err != nil {
panic("invalid arg")
}
}
func build(config config.Config) error {
......@@ -129,6 +140,8 @@ func build(config config.Config) error {
fallthrough
case "go":
environment.RunRequired(true, "make", "build")
case "angular":
environment.RunRequired(true, "yarn", "build:prod")
default:
color.New(color.FgRed).Printf("devtool doesn't support the '%s' language yet.\n", lang)
return errors.New("Language not supported")
......@@ -155,32 +168,35 @@ func buildDockerAndDeploy(config config.Config) error {
color.Blue("Building Docker Containers")
for _, deployable := range config.Deployables {
dockerfile := deployable.Dockerfile
image := deployable.Name
tag, err := uuid.NewRandom()
if err != nil {
color.Red("Failed to generate UUID for docker tag")
return err
}
dockerTag := image + ":" + tag.String()
dockerRootDir := "."
if idx := strings.LastIndex(dockerfile, "/"); idx != -1 {
dockerRootDir = dockerfile[:idx]
fmt.Println(dockerRootDir)
}
color.New(color.FgGreen).Printf("Building: %s\n", dockerTag)
err = environment.Run(true, "docker", "build", "-t", dockerTag, "-f", dockerfile, dockerRootDir)
if err != nil {
return err
var image string
var tag string
var err error
if deployable.Dockerfile != "" {
image, tag, err = buildDockerImage(deployable)
if err != nil {
color.New(color.FgRed).Printf("Failed to build docker image: %s", err.Error())
return err
}
} else if deployable.Image != "" {
image = deployable.Image
tag = deployable.ImageTag
if tag == "" {
tag = "latest"
}
} else {
color.New(color.FgYellow).Printf("Deployable: %s does not have a valid Docker configuration", deployable.Name)
continue
}
color.Blue("Installing Chart")
releaseName := deployable.Name + "-local"
args := []string{"upgrade", releaseName, "--install", deployable.Chart, "--version", deployable.ChartVersion}
if viper.GetBool("build.helm.dry.run") {
args = append(args, "--dry-run", "--debug")
}
if len(config.LocalEnvVars) > 0 {
for _, key := range config.LocalEnvVars {
args = append(args, "--set", "environmentVariables."+key+"="+os.Getenv(key))
......@@ -197,7 +213,7 @@ func buildDockerAndDeploy(config config.Config) error {
}
args = append(args, "--set", "image.repository="+image)
args = append(args, "--set", "image.tag="+tag.String())
args = append(args, "--set", "image.tag="+tag)
args = append(args, "-f", deployable.LocalConfig)
err = environment.Run(true, "helm", args...)
......@@ -215,3 +231,28 @@ func buildDockerAndDeploy(config config.Config) error {
}
return nil
}
func buildDockerImage(deployable config.Deployable) (string, string, error) {
dockerfile := deployable.Dockerfile
image := deployable.Name
tag, err := uuid.NewRandom()
if err != nil {
color.Red("Failed to generate UUID for docker tag")
return "", "", err
}
dockerTag := image + ":" + tag.String()
dockerRootDir := "."
if idx := strings.LastIndex(dockerfile, "/"); idx != -1 {
dockerRootDir = dockerfile[:idx]
fmt.Println(dockerRootDir)
}
color.New(color.FgGreen).Printf("Building: %s\n", dockerTag)
err = environment.Run(true, "docker", "build", "-t", dockerTag, "-f", dockerfile, dockerRootDir)
if err != nil {
return "", "", err
}
return image, tag.String(), nil
}
......@@ -18,6 +18,7 @@ import (
"os"
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
......@@ -30,9 +31,9 @@ var environmentSuffix string
var ciCmd = &cobra.Command{
Use: "ci",
Short: "Build tools for the CI system",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("config called")
// },
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
func init() {
......@@ -69,3 +70,8 @@ func getCiConfiguration() config.Config {
}
return conf
}
func helmUpdate() {
environment.RunRequired(false, "helm", "repo", "update")
}
......@@ -83,6 +83,11 @@ func buildCiDocker(config config.Config) error {
dockerfile := deployable.Dockerfile
image := deployable.Name
if dockerfile == "" {
color.New(color.FgYellow).Printf("Deployable %s has no Dockerfile, nothing to build.", deployable.Name)
continue
}
dockerImage := dockerRegistry + "/" + dockerRegistryNamespace + "/" + image
dockerTag := dockerImage + ":" + imageTag
......
......@@ -59,6 +59,11 @@ func fluxDeployCi(config config.Config) error {
for _, deployable := range config.Deployables {
image := deployable.Name
if deployable.Dockerfile == "" {
color.New(color.FgYellow).Printf("Deployable %s has no Dockerfile, nothing to deploy.", deployable.Name)
continue
}
var releaseName string
if environmentSuffix == "" {
releaseName = deployable.Name
......
// 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 (
"os"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generates bash completion scripts",
Long: `To load completion run
source <(devtool completion)
To configure your bash shell to load completions for each session add to your bashrc
# ~/.bashrc or ~/.profile
source <(devtool completion)
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Do Nothing
},
Run: func(cmd *cobra.Command, args []string) {
err := rootCmd.GenBashCompletion(os.Stdout)
if err != nil {
color.Red("Failed to generate bash completion script")
}
},
}
func init() {
rootCmd.AddCommand(completionCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// completionCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
......@@ -22,9 +22,9 @@ import (
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the projects build configuration",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("config called")
// },
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
func init() {
......
......@@ -15,7 +15,6 @@
package cmd
import (
"fmt"
"strings"
"git.psu.edu/k8s/devtool/config"
......@@ -29,18 +28,19 @@ var addCmd = &cobra.Command{
Use: "add",
Short: "adds a deployable unit to the project configuration",
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New(configFile)
if err != nil {
color.Red("Configuration doesn't exist, please run 'devtool config init' instead.")
return
}
helmUpdate()
deployables := conf.Deployables
dockerfiles := findDockerfiles()
for _, file := range dockerfiles {
fmt.Printf("Found Dockerfile: %s\n", file)
foundAny := false
for _, file := range dockerfiles {
found := false
for _, deployable := range deployables {
if deployable.Dockerfile == file {
......@@ -52,6 +52,8 @@ var addCmd = &cobra.Command{
if !found {
color.New(color.FgYellow).Printf("Found Dockerfile: %s\n", file)
foundAny = true
prompt := promptui.Prompt{
Label: "Include in config",
Default: "Yes",
......@@ -85,6 +87,9 @@ var addCmd = &cobra.Command{
}
}
}
if !foundAny {
color.Red("Failed to find any new Dockerfiles")
}
},
}
......
......@@ -15,7 +15,6 @@
package cmd
import (
"fmt"
"io/ioutil"
"log"
"os"
......@@ -52,15 +51,14 @@ var initCmd = &cobra.Command{
}
conf.Deployables = []config.Deployable{}
helmUpdate()
defaultName := conf.Name
if defaultName == "" {
wd, _ := os.Getwd()
fmt.Println(wd)
if idx := strings.LastIndex(wd, "/"); idx != -1 {
fmt.Println(idx)
idx++
defaultName = wd[idx:]
fmt.Println(defaultName)
}
}
......
......@@ -38,7 +38,12 @@ var rootCmd = &cobra.Command{
Long: `A tool used by software engineering to build applications.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
PrintLogo()
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
......@@ -86,11 +91,11 @@ func initConfig() {
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())
}
// if err := viper.ReadInConfig(); err == nil {
// fmt.Println("Using config file:", viper.ConfigFileUsed())
// } else {
// color.Red(err.Error())
// }
}
func PrintLogo() {
......
......@@ -26,6 +26,7 @@ import (
)
var command string
var envFile string
// runCmd represents the run command
var runCmd = &cobra.Command{
......@@ -39,13 +40,21 @@ var runCmd = &cobra.Command{
color.New(color.FgYellow).Printf("WARNING: no project configuration exists: %s\n", configFile)
}
checkEnvVars(conf)
localConfig := selectLocalConfig(conf.Deployables)
if _, err := os.Stat(localConfig); localConfig != "" && os.IsNotExist(err) {
color.New(color.FgYellow).Printf("WARN: Configuration file: '%s' doesn't exist\n", localConfig)
return
}
if localConfig != "" {
var helmValues config.HelmValues
helmValues, err = config.ReadHelmValues(localConfig)
if err != nil {
color.New(color.FgRed).Printf("Error reading local config file: %s\n", localConfig)
return
}
for k, v := range helmValues.EnvironmentVariables {
os.Setenv(k, v)
......@@ -74,17 +83,25 @@ func init() {
if err != nil {
panic("invalid arg")
}
runCmd.Flags().StringVarP(&envFile, "env", "e", "", "Config YAML file with environmentVariables")
}
func selectLocalConfig(deployables []config.Deployable) string {
if envFile != "" {
return envFile
}
if len(deployables) == 0 {
color.Yellow("WARN: This project has no deployables, please specify one with the --env param.")
return ""
}
//if len(deployables) == 1 {
return deployables[0].LocalConfig
//}
//TODO add support for multiple deployables (prompt user and add arg)
if len(deployables) == 1 {
return deployables[0].LocalConfig
}
color.Yellow("WARN: This project has multiple deployables, please specify one with the --env param.")
return ""
}
......@@ -18,6 +18,8 @@ type (
Deployable struct {
Name string `json:"name"`
Dockerfile string `json:"dockerfile"`
Image string `json:"image"`
ImageTag string `json:"imageTag"`
Chart string `json:"chart"`
ChartVersion string `json:"chartVersion"`
LocalConfig string `json:"localConfig"`
......
......@@ -3,6 +3,5 @@ package main
import "git.psu.edu/k8s/devtool/cmd"
func main() {
cmd.PrintLogo()
cmd.Execute()
}
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