Commit e2b07c6d authored by SWE CI's avatar SWE CI

Merge branch 'release/v0.1.0'

parents 56c5e3d2 85fcb9da
Pipeline #29189 passed with stage
in 20 seconds
{
"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
.devtool.config
include:
- project: k8s/gitlab-ci-templates
ref: master
file: eio-swe-golang-flux/.gitlab-ci.yml
variables:
GO_LIB: "true"
# 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)
// 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 (
"errors"
"os"
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
var skipCompile bool
var runTests bool
var openServices bool
// buildCmd represents the build command
var buildCmd = &cobra.Command{
Use: "build",
Short: "build the application",
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).PrintFunc()("Error reading configuration file: %s", configFile)
return
}
err = build(conf)
if err != nil {
color.Red("Failed to build application.")
return
}
err = buildDockerAndDeploy(conf)
if err != nil {
color.Red("Failed to build docker image.")
return
}
},
}
func init() {
rootCmd.AddCommand(buildCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// buildCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// 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")
}
func build(config config.Config) error {
if skipCompile {
color.Yellow("Skipping Build Step.")
return nil
}
color.Blue("Building Application")
switch lang := config.Language; lang {
case "java":
args := []string{"clean", "install", "-Ddependency-check.skip=true"}
if !runTests {
args = append(args, "-DskipTests")
}
environment.RunRequired(true, "mvn", args...)
case "golang":
fallthrough
case "go":
environment.RunRequired(true, "make", "build")
default:
color.New(color.FgRed).PrintFunc()("devtool doesn't support the '%s' language yet.", lang)
return errors.New("Language not supported")
}
return nil
}
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()
color.New(color.FgGreen).PrintFunc()("Building ", dockerTag)
err = environment.Run(true, "docker", "build", "-t", dockerTag, "-f", dockerfile, ".")
if err != nil {
return err
}
color.Blue("Installing Chart")
releaseName := deployable.Name + "-local"
args := []string{"upgrade", releaseName, "--install", deployable.Chart, "--version", deployable.ChartVersion}
if len(config.LocalEnvVars) > 0 {
for _, key := range config.LocalEnvVars {
args = append(args, "--set", "environmentVariables."+key+"="+os.Getenv(key))
}
}
if len(config.LocalSecrets) > 0 {
for _, key := range config.LocalSecrets {
args = append(args, "--set", "secrets."+key+"="+os.Getenv(key))
}
}
args = append(args, "--set", "image.repository="+image)
args = append(args, "--set", "image.tag="+tag.String())
args = append(args, "-f", deployable.LocalConfig)
err = environment.Run(true, "helm", args...)
if err != nil {
color.New(color.FgRed).PrintFunc()("helm error. try running 'helm del --purge %s'", releaseName)
return err
}
if openServices {
environment.Run(true, "minikube", "service", releaseName)
}
}
return nil
}
// 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 (
"github.com/spf13/cobra"
)
var dockerRegistry string
var dockerRegistryNamespace string
var imageTag string
var environmentSuffix string
// configCmd represents the config command
var ciCmd = &cobra.Command{
Use: "ci",
Short: "Build tools for the CI system",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("config called")
// },
}
func init() {
rootCmd.AddCommand(ciCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
ciCmd.PersistentFlags().StringVarP(&dockerRegistry, "registry", "r", "", "url of the docker registry")
ciCmd.PersistentFlags().StringVarP(&dockerRegistryNamespace, "registry-namespace", "n", "", "namespace in the docker registry")
ciCmd.PersistentFlags().StringVarP(&imageTag, "image", "i", "", "image tag for docker")
ciCmd.PersistentFlags().StringVarP(&environmentSuffix, "environment", "e", "", "environment suffix to append to helm release name")
ciCmd.MarkPersistentFlagRequired("registry")
ciCmd.MarkPersistentFlagRequired("registry-namespace")
ciCmd.MarkPersistentFlagRequired("image")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// 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/spf13/cobra"
)
// buildCmd represents the build command
var ciBuildCmd = &cobra.Command{
Use: "build",
Short: "build the application within the CI system",
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).PrintFunc()("Error reading configuration file: %s", configFile)
return err
}
err = buildCiDocker(conf)
if err != nil {
color.Red("Failed to build docker image.")
return err
}
return nil
},
}
func init() {
ciCmd.AddCommand(ciBuildCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// buildCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func buildCiDocker(config config.Config) error {
color.Blue("Building Docker Containers")
for _, deployable := range config.Deployables {
dockerfile := deployable.Dockerfile
image := deployable.Name
dockerImage := dockerRegistry + "/" + dockerRegistryNamespace + "/" + image
dockerTag := dockerImage + ":" + imageTag
color.New(color.FgGreen).PrintFunc()("Building: ", dockerTag)
err := environment.Run(true, "docker", "build", "-t", dockerTag, "-f", dockerfile, ".")
if err != nil {
return err
}
color.New(color.FgGreen).PrintFunc()("Pushing Docker image: ", dockerTag)
err = environment.Run(true, "docker", "push", dockerImage)
if err != nil {
return err
}
}
return nil
}
// 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"
"git.psu.edu/k8s/devtool/config"
"git.psu.edu/k8s/devtool/environment"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// buildCmd represents the build command
var ciDeployCmd = &cobra.Command{
Use: "deploy",
Short: "deploy the application within the CI system",
RunE: func(cmd *cobra.Command, args []string) error {
conf, err := config.New(configFile)
if err != nil {
color.New(color.FgRed).PrintFunc()("Error reading configuration file: %s", configFile)
return err
}
err = fluxDeployCi(conf)
if err != nil {
color.Red("Failed to build docker image.")
return err
}
return nil
},
}
func init() {
ciCmd.AddCommand(ciDeployCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// buildCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// buildCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
func fluxDeployCi(config config.Config) error {
color.Blue("Updating Flux Releases")
for _, deployable := range config.Deployables {
image := deployable.Name
dockerImage := dockerRegistry + "/" + dockerRegistryNamespace + "/" + image
dockerTag := dockerImage + ":" + imageTag
var releaseName string
if environmentSuffix == "" {
releaseName = deployable.Name
} else {
releaseName = deployable.Name + "-" + environmentSuffix
}
color.Blue("Updating Flux Release:", releaseName)
os.Setenv("KUBE_SERVICE_NAME", releaseName)
os.Setenv("DOCKER_IMAGE_TAG", dockerTag)
os.Setenv("CI_PROJECT_NAME", image)
err := environment.Run(true, "fluxhelmrelease")
if err != nil {
color.Red("fluxhelmrelease error.")
return err
}
}
return nil
}
// 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 (
"github.com/spf13/cobra"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage the projects build configuration",
// Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("config called")
// },
}
func init() {
rootCmd.AddCommand(configCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// configCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// configCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// 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 (
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "adds a deployable unit to the project configuration",
Run: func(cmd *cobra.Command, args []string) {
color.Red("Not implemented yet!")
},
}
func init() {
configCmd.AddCommand(addCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// addCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// addCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// 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 (
"github.com/fatih/color"
"github.com/spf13/cobra"
)
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "removes a deployable unit to the project configuration",
Run: func(cmd *cobra.Command, args []string) {
color.Red("Not implemented yet!")
},
}
func init() {
configCmd.AddCommand(deleteCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// deleteCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// 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 (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"git.psu.edu/k8s/devtool/config"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
var force bool
// initCmd represents the init command
var initCmd = &cobra.Command{
Use: "init",
Short: "initialize the project configuration",
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New(configFile)
if err == nil && !force {
color.Red("Configuration already exists.")
prompt := promptui.Prompt{
Label: "Are you sure you want to overwrite",
IsConfirm: true,
}
_, err := prompt.Run()
if err != nil {
return
}
}
conf.Deployables = []config.Deployable{}
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)
}
}
prompt := promptui.Prompt{
Label: "Project Name",
Default: defaultName,
}
response, err := prompt.Run()
if err != nil {
return
}
conf.Name = response
selectPrompt := promptui.Select{
Label: "Language",
Items: []string{"java", "golang", "angular"},
}
_, response, err = selectPrompt.Run()
if err != nil {
return
}
conf.Language = response
dockerfiles := findDockerfiles()
for _, file := range dockerfiles {
color.Yellow("Found Dockerfile: %s", file)
prompt := promptui.Prompt{
Label: "Include in config",
Default: "Yes",