Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
icechen | d984d78b03 | |
icechen | 0540fcd17f | |
icechen | 7f8bb65be3 | |
icechen | 5826eef7bf | |
icechen | 9137abdabf | |
icechen | 540e84ef09 | |
icechen | b86b8baa56 | |
icechen | b682605762 | |
icechen | 79629d1263 |
35
api.go
35
api.go
|
@ -2,9 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
"os"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/template/api/docker/golang"
|
||||
|
@ -17,12 +19,12 @@ import (
|
|||
|
||||
// ActionListApi API应用列表
|
||||
func ActionListApi(c *cli.Context) error {
|
||||
if err := Init(c); err != nil {
|
||||
if err := config.Init(c); err != nil {
|
||||
return err
|
||||
}
|
||||
color.Green("API应用列表:")
|
||||
for _, api := range config.HasApi {
|
||||
color.Green("\t%s", api.Name)
|
||||
for _, api := range config.Config.HasApi {
|
||||
color.Green("\t%s.%s", api.Name, api.Namespace)
|
||||
color.Green("\t\t路径:\t%s", api.Root)
|
||||
color.Green("\t\t类型:\t%s", api.Type)
|
||||
color.Green("\t\t端口:\t%s", api.Port)
|
||||
|
@ -40,7 +42,7 @@ func ActionListApi(c *cli.Context) error {
|
|||
|
||||
// ActionNewApi 创建API应用
|
||||
func ActionNewApi(c *cli.Context) error {
|
||||
if err := Init(c); err != nil {
|
||||
if err := config.Init(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -58,11 +60,21 @@ func ActionNewApi(c *cli.Context) error {
|
|||
appList = append(appList, appName)
|
||||
}
|
||||
for _, newApp := range appList {
|
||||
namespace := ""
|
||||
for {
|
||||
namespace = util.ReadLineWithMessage(fmt.Sprintf("请输入%s所属的项目(namespace): ", newApp))
|
||||
if namespace != "" {
|
||||
break
|
||||
} else {
|
||||
color.Red("项目名称不能为空!")
|
||||
}
|
||||
}
|
||||
color.Green("正在创建[%s]API应用...", newApp)
|
||||
app := config.Api.GetApp(newApp)
|
||||
app = Api{
|
||||
app := config.Config.Api.GetApp(namespace, newApp)
|
||||
app = config.Api{
|
||||
Namespace: namespace,
|
||||
Name: newApp,
|
||||
Root: "app/api/" + newApp,
|
||||
Root: fmt.Sprintf("app/%s/api/%s", namespace, newApp),
|
||||
}
|
||||
|
||||
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
|
||||
|
@ -95,17 +107,17 @@ func ActionNewApi(c *cli.Context) error {
|
|||
app.Host = util.ReadLineWithMessage("请输入API应用请求的域名(host,如:api.seamlesser.com,建议为空): ")
|
||||
app.Path = util.ReadLineWithMessage("请输入API应用请求的路由(path,如:/uwe/core/?(.*),建议为空): ")
|
||||
|
||||
err = deploy.GenDeploy(config.Name, newApp, app.AliasName)
|
||||
err = deploy.GenDeploy(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = golang.GenDockerfile(newApp)
|
||||
err = golang.GenDockerfile(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.AppendAPI(app)
|
||||
err = config.Config.AppendAPI(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -117,5 +129,6 @@ func ActionNewApi(c *cli.Context) error {
|
|||
|
||||
// ActionFixApi 修复API应用
|
||||
func ActionFixApi(c *cli.Context) error {
|
||||
color.Red("开发中...")
|
||||
return nil
|
||||
}
|
||||
|
|
12
ci.go
12
ci.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
@ -14,13 +15,12 @@ import (
|
|||
|
||||
var (
|
||||
ciHost = "https://ci.icechen.cn"
|
||||
// ciToken = "b3e73c2e2f405e65de484d37ea48bb26"
|
||||
ciToken = "XRFPl4YgUk8ZT3q5D2dLzqNDUNhPuwCf"
|
||||
ciToken = "b3e73c2e2f405e65de484d37ea48bb26"
|
||||
)
|
||||
|
||||
// ActionBuildCI ci创建新构建
|
||||
func ActionBuildCI(c *cli.Context) error {
|
||||
if err := Init(c); err != nil {
|
||||
if err := config.Init(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -43,15 +43,15 @@ func ActionBuildCI(c *cli.Context) error {
|
|||
temp := strings.Split(fInfo, string(os.PathSeparator))
|
||||
name := temp[len(temp)-1]
|
||||
|
||||
color.Red("创建构建的仓库: %s/%s@master", config.Name, name)
|
||||
color.Red("创建构建的仓库: %s/%s@master", config.Config.Name, name)
|
||||
if len(app) > 0 {
|
||||
color.Red("强制构建的应用(除了最后一次提交或指定的commit提交本应构建的应用): %+v\n\n", app)
|
||||
}
|
||||
b, err := client.BuildCreate(config.Name, name, commit, "master", map[string]string{"app": app})
|
||||
b, err := client.BuildCreate(config.Config.Name, name, commit, "master", map[string]string{"app": app})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
color.Green("构建成功!\n构建ID: %d\n去看看: %s/%s/%s/%d", b.Number, ciHost, config.Name, name, b.Number)
|
||||
color.Green("构建成功!\n构建ID: %d\n去看看: %s/%s/%s/%d", b.Number, ciHost, config.Config.Name, name, b.Number)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
package main
|
||||
package config
|
||||
|
||||
import (
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
"github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
var DefaultConfigFile = ".drone.yml"
|
||||
|
@ -22,13 +21,13 @@ func Init(c *cli.Context) error {
|
|||
}
|
||||
|
||||
appList := c.Args().Slice()
|
||||
config.HasApi = config.Api.HasApp(appList)
|
||||
config.HasService = config.Service.HasApp(appList)
|
||||
Config.HasApi = Config.Api.HasApp(appList)
|
||||
Config.HasService = Config.Service.HasApp(appList)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
type ConfigFile struct {
|
||||
Kind string `json:"kind" yaml:"kind"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
|
@ -42,8 +41,9 @@ type Config struct {
|
|||
type (
|
||||
ApiList []Api
|
||||
Api struct {
|
||||
Namespace string `json:"namespace" yaml:"namespace"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
AliasName string `json:"alias_name" yaml:"aliasName"`
|
||||
AliasName string `json:"alias_name" yaml:"alias_name"`
|
||||
Root string `json:"root" yaml:"root"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Port string `json:"port" yaml:"port"`
|
||||
|
@ -55,32 +55,33 @@ type (
|
|||
type (
|
||||
ServiceList []Service
|
||||
Service struct {
|
||||
Namespace string `json:"namespace" yaml:"namespace"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
AliasName string `json:"alias_name" yaml:"aliasName"`
|
||||
AliasName string `json:"alias_name" yaml:"alias_name"`
|
||||
Root string `json:"root" yaml:"root"`
|
||||
Type string `json:"type" yaml:"type"`
|
||||
Port string `json:"port" yaml:"port"`
|
||||
}
|
||||
)
|
||||
|
||||
var config Config
|
||||
var Config ConfigFile
|
||||
|
||||
func ReadConfig() error {
|
||||
configFile, err := ioutil.ReadFile(DefaultConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = yaml.Unmarshal(configFile, &config)
|
||||
err = yaml.Unmarshal(configFile, &Config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) AppendAPI(api Api) error {
|
||||
func (c *ConfigFile) AppendAPI(api Api) error {
|
||||
has := false
|
||||
for i, a := range c.Api {
|
||||
if a.Name == api.Name {
|
||||
if a.Name == api.Name && a.Namespace == api.Namespace {
|
||||
c.Api[i] = api
|
||||
has = true
|
||||
break
|
||||
|
@ -99,10 +100,10 @@ func (c *Config) AppendAPI(api Api) error {
|
|||
return util.WriteFileWithNotEdit(DefaultConfigFile, out)
|
||||
}
|
||||
|
||||
func (c *Config) AppendService(service Service) error {
|
||||
func (c *ConfigFile) AppendService(service Service) error {
|
||||
has := false
|
||||
for i, s := range c.Service {
|
||||
if s.Name == service.Name {
|
||||
if s.Name == service.Name && s.Namespace == service.Namespace {
|
||||
c.Service[i] = service
|
||||
has = true
|
||||
break
|
||||
|
@ -140,9 +141,9 @@ al:
|
|||
return hasApp
|
||||
}
|
||||
|
||||
func (sl ApiList) GetApp(appName string) Api {
|
||||
func (sl ApiList) GetApp(namespace, appName string) Api {
|
||||
for _, api := range sl {
|
||||
if api.Name == appName {
|
||||
if api.Name == appName && api.Namespace == namespace {
|
||||
return api
|
||||
}
|
||||
}
|
||||
|
@ -168,9 +169,9 @@ sl:
|
|||
return hasApp
|
||||
}
|
||||
|
||||
func (sl ServiceList) GetApp(appName string) Service {
|
||||
func (sl ServiceList) GetApp(namespace, appName string) Service {
|
||||
for _, service := range sl {
|
||||
if service.Name == appName {
|
||||
if service.Name == appName && service.Namespace == namespace {
|
||||
return service
|
||||
}
|
||||
}
|
4
go.mod
4
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
|||
require (
|
||||
github.com/drone/drone-go v1.7.1
|
||||
github.com/fatih/color v1.13.0
|
||||
github.com/sergi/go-diff v1.2.0
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
||||
|
@ -16,8 +17,9 @@ require (
|
|||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/russross/blackfriday/v2 v2.1.0 // indirect
|
||||
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 // indirect
|
||||
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d // indirect
|
||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/protobuf v1.27.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
|
16
go.sum
16
go.sum
|
@ -44,6 +44,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
|
|||
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=
|
||||
github.com/drone/drone-go v1.7.1/go.mod h1:fxCf9jAnXDZV1yDr0ckTuWd1intvcQwfJmTRpTZ1mXg=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
|
@ -122,14 +124,18 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
|
|||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
|
||||
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||
|
@ -202,8 +208,8 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
|
|||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 h1:+6WJMRLHlD7X7frgp7TUZ36RnQzSf9wVVTNakEp+nqY=
|
||||
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d h1:62NvYBuaanGXR2ZOfwDFkhhl6X1DUgf8qg3GuQvxZsE=
|
||||
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
|
@ -386,11 +392,15 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
|
|||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
|
28
health.go
28
health.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
|
@ -33,7 +34,7 @@ func (hr healthResp) String() string {
|
|||
return b.String()
|
||||
}
|
||||
|
||||
func healthApi(api Api) healthResp {
|
||||
func healthApi(api config.Api) healthResp {
|
||||
ret := healthResp{
|
||||
Dir: false,
|
||||
Deploy: false,
|
||||
|
@ -57,3 +58,28 @@ func healthApi(api Api) healthResp {
|
|||
|
||||
return ret
|
||||
}
|
||||
|
||||
func healthService(service config.Service) healthResp {
|
||||
ret := healthResp{
|
||||
Dir: false,
|
||||
Deploy: false,
|
||||
Dockerfile: false,
|
||||
}
|
||||
|
||||
// Dir 健康度
|
||||
if util.ExistDir(service.Root) {
|
||||
ret.Dir = true
|
||||
}
|
||||
|
||||
// Deploy 健康度
|
||||
if util.ExistDir(service.Root + "/deploy") {
|
||||
ret.Deploy = true
|
||||
}
|
||||
|
||||
// Dockerfile 健康度
|
||||
if util.ExistFile(service.Root + "/Dockerfile") {
|
||||
ret.Dockerfile = true
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
8
main.go
8
main.go
|
@ -30,7 +30,7 @@ func main() {
|
|||
app = &cli.App{
|
||||
Name: "wdt",
|
||||
Usage: "无间开发者工具箱",
|
||||
Version: "v0.0.1",
|
||||
Version: "v1.0.0",
|
||||
EnableBashCompletion: true,
|
||||
Flags: flags,
|
||||
Commands: cli.Commands{
|
||||
|
@ -67,17 +67,17 @@ func main() {
|
|||
{
|
||||
Name: "new",
|
||||
Usage: "创建一个service应用",
|
||||
Action: ActionNewApi,
|
||||
Action: ActionNewService,
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Usage: "service应用列表",
|
||||
Action: ActionListApi,
|
||||
Action: ActionListService,
|
||||
},
|
||||
{
|
||||
Name: "fix",
|
||||
Usage: "service应用修复",
|
||||
Action: ActionFixApi,
|
||||
Action: ActionFixService,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/template/service/deploy"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/template/service/docker/golang"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
_ "embed"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// ActionListService Service应用列表
|
||||
func ActionListService(c *cli.Context) error {
|
||||
if err := config.Init(c); err != nil {
|
||||
return err
|
||||
}
|
||||
color.Green("Service应用列表:")
|
||||
for _, service := range config.Config.HasService {
|
||||
color.Green("\t%s.%s", service.Name, service.Namespace)
|
||||
color.Green("\t\t路径:\t%s", service.Root)
|
||||
color.Green("\t\t类型:\t%s", service.Type)
|
||||
color.Green("\t\t端口:\t%s", service.Port)
|
||||
c := color.New(color.Bold, color.FgRed)
|
||||
c.EnableColor()
|
||||
_, err := c.Println(healthService(service))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Print("\n")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActionNewService 创建Service应用
|
||||
func ActionNewService(c *cli.Context) error {
|
||||
if err := config.Init(c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
appList := c.Args().Slice()
|
||||
if len(appList) == 0 {
|
||||
appName := ""
|
||||
for {
|
||||
appName = util.ReadLineWithMessage("请输入要创建的service名称: ")
|
||||
if appName != "" {
|
||||
break
|
||||
} else {
|
||||
color.Red("应用名称不能为空!")
|
||||
}
|
||||
}
|
||||
appList = append(appList, appName)
|
||||
}
|
||||
for _, newApp := range appList {
|
||||
namespace := ""
|
||||
for {
|
||||
namespace = util.ReadLineWithMessage(fmt.Sprintf("请输入%s所属的项目(namespace): ", newApp))
|
||||
if namespace != "" {
|
||||
break
|
||||
} else {
|
||||
color.Red("项目名称不能为空!")
|
||||
}
|
||||
}
|
||||
color.Green("正在创建[%s]Service应用...", newApp)
|
||||
app := config.Config.Service.GetApp(namespace, newApp)
|
||||
app = config.Service{
|
||||
Namespace: namespace,
|
||||
Name: newApp,
|
||||
Root: fmt.Sprintf("app/%s/service/%s", namespace, newApp),
|
||||
}
|
||||
|
||||
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := os.MkdirAll(app.Root, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
app.AliasName = util.ReadLineWithMessage("请输入应用的简短描述(别名): ")
|
||||
for {
|
||||
app.Type = util.ReadLineWithMessage("请输入应用的类型(默认golang): ")
|
||||
if app.Type == "golang" || app.Type == "" {
|
||||
app.Type = "golang"
|
||||
break
|
||||
} else {
|
||||
color.Red("暂不支持: [%s]类型", app.Type)
|
||||
}
|
||||
}
|
||||
for {
|
||||
app.Port = util.ReadLineWithMessage("请输入应用暴露的端口: ")
|
||||
if app.Port != "" {
|
||||
break
|
||||
} else {
|
||||
color.Red("端口号不能为空!")
|
||||
}
|
||||
}
|
||||
|
||||
err = deploy.GenDeploy(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = golang.GenDockerfile(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = config.Config.AppendService(app)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
color.Green("[%s]Service应用创建成功!!!\n\n", newApp)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActionFixService 修复Service应用
|
||||
func ActionFixService(c *cli.Context) error {
|
||||
color.Red("开发中...")
|
||||
return nil
|
||||
}
|
|
@ -3,8 +3,10 @@ package deploy
|
|||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
|
@ -13,24 +15,41 @@ import (
|
|||
//go:embed "*"
|
||||
var deployDir embed.FS
|
||||
|
||||
func GenDeploy(namespace string, name string, aliasName string) error {
|
||||
func GenDeploy(api config.Api) error {
|
||||
color.Green("正在生成deploy...")
|
||||
|
||||
// deploy 文件夹
|
||||
deployDirPath := "app/api/" + name + "/deploy"
|
||||
deployDirPath := api.Root + "/deploy"
|
||||
err := os.MkdirAll(deployDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
host := api.Host
|
||||
if host == "" {
|
||||
host = "api.seamlesser.com"
|
||||
}
|
||||
|
||||
path := api.Path
|
||||
if path == "" {
|
||||
path = fmt.Sprintf("/%s/%s/?(.*)", api.Namespace, api.Name)
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
"NameSpace": api.Namespace,
|
||||
"AppName": api.Name,
|
||||
"AliasName": api.AliasName,
|
||||
"Port": api.Port,
|
||||
"Host": host,
|
||||
"Path": path,
|
||||
}
|
||||
|
||||
// value.yaml
|
||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml",
|
||||
string(valuesTemplate),
|
||||
map[string]string{"NameSpace": namespace, "AppName": name, "AliasName": aliasName})
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml", string(valuesTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -40,15 +59,13 @@ func GenDeploy(namespace string, name string, aliasName string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml",
|
||||
string(chartTemplate),
|
||||
map[string]string{"NameSpace": namespace, "AppName": name, "AliasName": aliasName})
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml", string(chartTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// templates 文件夹
|
||||
templatesDirPath := "app/api/" + name + "/deploy/templates"
|
||||
templatesDirPath := api.Root + "/deploy/templates"
|
||||
err = os.MkdirAll(templatesDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -66,6 +83,12 @@ func GenDeploy(namespace string, name string, aliasName string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// application.yaml 文件
|
||||
err = copyTo("templates/application.yaml", templatesDirPath+"/application.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// deployment.yaml 文件
|
||||
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
|
||||
if err != nil {
|
||||
|
@ -98,13 +121,5 @@ func copyTo(fileName string, toPath string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
toFile, err := os.OpenFile(toPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer toFile.Close()
|
||||
|
||||
_, err = toFile.Write(fileContent)
|
||||
return err
|
||||
return util.WriteFile(toPath, fileContent)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
apiVersion: app.k8s.io/v1beta1
|
||||
kind: Application
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
annotations:
|
||||
servicemesh.kubesphere.io/enabled: 'true'
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
addOwnerRef: true
|
|
@ -5,18 +5,32 @@ metadata:
|
|||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
annotations:
|
||||
kubesphere.io/alias-name: {{ .Values.aliasName }}
|
||||
kubesphere.io/creator: drone
|
||||
meta.helm.sh/release-name: {{ .Release.Name }}
|
||||
meta.helm.sh/release-namespace: {{ .Values.nameSpace }}
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
spec:
|
||||
volumes:
|
||||
- name: host-time
|
||||
|
@ -30,8 +44,14 @@ spec:
|
|||
- containerPort: {{ .Values.port }}
|
||||
protocol: TCP
|
||||
env:
|
||||
- name: endpoints
|
||||
- name: ENDPOINTS
|
||||
value: 'etcd:2379'
|
||||
- name: APP_NAME
|
||||
value: {{ .Values.appName }}
|
||||
- name: NAMESPACE
|
||||
value: {{ .Values.nameSpace }}
|
||||
- name: APP_TYPE
|
||||
value: api
|
||||
resources:
|
||||
limits:
|
||||
cpu: 200m
|
||||
|
|
|
@ -4,6 +4,12 @@ apiVersion: networking.k8s.io/v1
|
|||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
annotations:
|
||||
kubesphere.io/creator: drone
|
||||
nginx.ingress.kubernetes.io/enable-cors: 'true'
|
||||
|
|
|
@ -7,6 +7,10 @@ metadata:
|
|||
kubesphere.io/creator: drone
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
spec:
|
||||
ports:
|
||||
- name: http-{{ .Release.Name }}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
nameSpace: {{ .NameSpace }}
|
||||
appName: {{ .AppName }}
|
||||
aliasName: {{ .AliasName }}
|
||||
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
||||
imageTag: latest
|
||||
port: 8080
|
||||
host: api.seamlesser.com
|
||||
path: /{{ .NameSpace }}/{{ .AppName }}/?(.*)
|
||||
port: {{ .Port }}
|
||||
host: {{ .Host }}
|
||||
path: {{ .Path }}
|
|
@ -4,11 +4,11 @@ ENV GOPROXY https://goproxy.io,direct
|
|||
WORKDIR /go/cache
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download
|
||||
RUN go mod download -x
|
||||
WORKDIR /go/src
|
||||
ADD . .
|
||||
RUN go mod tidy
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_{{ .Name }} ./app/api/{{ .Name }}
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_{{ .Name }} ./{{ .Root }}
|
||||
|
||||
FROM reg.icechen.cn/alpine as {{ .Name }}
|
||||
WORKDIR /go/src
|
||||
|
|
|
@ -3,6 +3,8 @@ package golang
|
|||
import (
|
||||
_ "embed"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
|
@ -14,11 +16,11 @@ var dockerfileTemplate string
|
|||
//go:embed "main.tpl"
|
||||
var mainGoTemplate string
|
||||
|
||||
func GenDockerfile(name string) error {
|
||||
func GenDockerfile(api config.Api) error {
|
||||
color.Green("正在生成dockerfile...")
|
||||
err := util.TemplateToFile("app/api/"+name+"/Dockerfile", dockerfileTemplate, map[string]string{"Name": name})
|
||||
err := util.TemplateToFile(api.Root+"/Dockerfile", dockerfileTemplate, api)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.TemplateToFile("app/api/"+name+"/main.go", mainGoTemplate, nil)
|
||||
return util.TemplateToFile(api.Root+"/main.go", mainGoTemplate, nil)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
# Patterns to ignore when building packages.
|
||||
# This supports shell glob matching, relative path matching, and
|
||||
# negation (prefixed with !). Only one pattern per line.
|
||||
.DS_Store
|
||||
# Common VCS dirs
|
||||
.git/
|
||||
.gitignore
|
||||
.bzr/
|
||||
.bzrignore
|
||||
.hg/
|
||||
.hgignore
|
||||
.svn/
|
||||
# Common backup files
|
||||
*.swp
|
||||
*.bak
|
||||
*.tmp
|
||||
*.orig
|
||||
*~
|
||||
# Various IDEs
|
||||
.project
|
||||
.idea/
|
||||
*.tmproj
|
||||
.vscode/
|
|
@ -0,0 +1,24 @@
|
|||
apiVersion: v2
|
||||
name: {{ .AppName }}
|
||||
description: {{ .AliasName }}
|
||||
|
||||
# A chart can be either an 'application' or a 'library' chart.
|
||||
#
|
||||
# Application charts are a collection of templates that can be packaged into versioned archives
|
||||
# to be deployed.
|
||||
#
|
||||
# Library charts provide useful utilities or functions for the chart developer. They're included as
|
||||
# a dependency of application charts to inject those utilities and functions into the rendering
|
||||
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
|
||||
type: application
|
||||
|
||||
# This is the chart version. This version number should be incremented each time you make changes
|
||||
# to the chart and its templates, including the app version.
|
||||
# Versions are expected to follow Semantic Versioning (https://semver.org/)
|
||||
version: 0.0.1
|
||||
|
||||
# This is the version number of the application being deployed. This version number should be
|
||||
# incremented each time you make changes to the application. Versions are not expected to
|
||||
# follow Semantic Versioning. They should reflect the version the application is using.
|
||||
# It is recommended to use it with quotes.
|
||||
appVersion: "0.0.1"
|
|
@ -0,0 +1,107 @@
|
|||
package deploy
|
||||
|
||||
import (
|
||||
"embed"
|
||||
_ "embed"
|
||||
"os"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
//go:embed "*"
|
||||
var deployDir embed.FS
|
||||
|
||||
func GenDeploy(service config.Service) error {
|
||||
color.Green("正在生成deploy...")
|
||||
|
||||
// deploy 文件夹
|
||||
deployDirPath := service.Root + "/deploy"
|
||||
err := os.MkdirAll(deployDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
"NameSpace": service.Namespace,
|
||||
"AppName": service.Name,
|
||||
"AliasName": service.AliasName,
|
||||
"Port": service.Port,
|
||||
}
|
||||
|
||||
// value.yaml
|
||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/values.yaml", string(valuesTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Chart.yaml
|
||||
chartTemplate, err := deployDir.ReadFile("Chart.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = util.TemplateToFile(deployDirPath+"/Chart.yaml", string(chartTemplate), data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// templates 文件夹
|
||||
templatesDirPath := service.Root + "/deploy/templates"
|
||||
err = os.MkdirAll(templatesDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// .helmignore 文件
|
||||
err = copyTo(".helmignore", deployDirPath+"/.helmignore")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// _helpers.tpl 文件
|
||||
err = copyTo("templates/helpers.tpl", templatesDirPath+"/_helpers.tpl")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// application.yaml 文件
|
||||
err = copyTo("templates/application.yaml", templatesDirPath+"/application.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// deployment.yaml 文件
|
||||
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// NOTES.txt 文件
|
||||
err = copyTo("templates/NOTES.txt", templatesDirPath+"/NOTES.txt")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// service.yaml 文件
|
||||
err = copyTo("templates/service.yaml", templatesDirPath+"/service.yaml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyTo(fileName string, toPath string) error {
|
||||
fileContent, err := deployDir.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.WriteFile(toPath, fileContent)
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
notes
|
|
@ -0,0 +1,20 @@
|
|||
apiVersion: app.k8s.io/v1beta1
|
||||
kind: Application
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
annotations:
|
||||
servicemesh.kubesphere.io/enabled: 'true'
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
addOwnerRef: true
|
|
@ -0,0 +1,82 @@
|
|||
kind: Deployment
|
||||
apiVersion: apps/v1
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
annotations:
|
||||
kubesphere.io/alias-name: {{ .Values.aliasName }}
|
||||
kubesphere.io/creator: drone
|
||||
meta.helm.sh/release-name: {{ .Release.Name }}
|
||||
meta.helm.sh/release-namespace: {{ .Values.nameSpace }}
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
spec:
|
||||
volumes:
|
||||
- name: host-time
|
||||
hostPath:
|
||||
path: /etc/localtime
|
||||
type: ''
|
||||
containers:
|
||||
- name: {{ .Release.Name }}
|
||||
image: {{ .Values.image }}:{{ .Values.imageTag }}
|
||||
ports:
|
||||
- containerPort: {{ .Values.port }}
|
||||
protocol: TCP
|
||||
env:
|
||||
- name: ENDPOINTS
|
||||
value: 'etcd:2379'
|
||||
- name: APP_NAME
|
||||
value: {{ .Values.appName }}
|
||||
- name: NAMESPACE
|
||||
value: {{ .Values.nameSpace }}
|
||||
- name: APP_TYPE
|
||||
value: service
|
||||
resources:
|
||||
limits:
|
||||
cpu: 200m
|
||||
memory: 1000Mi
|
||||
requests:
|
||||
cpu: 10m
|
||||
memory: 200Mi
|
||||
volumeMounts:
|
||||
- name: host-time
|
||||
readOnly: true
|
||||
mountPath: /etc/localtime
|
||||
terminationMessagePath: /dev/termination-log
|
||||
terminationMessagePolicy: File
|
||||
imagePullPolicy: IfNotPresent
|
||||
restartPolicy: Always
|
||||
terminationGracePeriodSeconds: 30
|
||||
dnsPolicy: ClusterFirst
|
||||
securityContext: {}
|
||||
imagePullSecrets:
|
||||
- name: registry-secret
|
||||
schedulerName: default-scheduler
|
||||
strategy:
|
||||
type: RollingUpdate
|
||||
rollingUpdate:
|
||||
maxUnavailable: 25%
|
||||
maxSurge: 25%
|
||||
revisionHistoryLimit: 10
|
||||
progressDeadlineSeconds: 600
|
|
@ -0,0 +1,26 @@
|
|||
kind: Service
|
||||
apiVersion: v1
|
||||
metadata:
|
||||
name: {{ .Release.Name }}
|
||||
namespace: {{ .Values.nameSpace }}
|
||||
annotations:
|
||||
kubesphere.io/creator: drone
|
||||
labels:
|
||||
app: {{ .Release.Name }}
|
||||
app.kubernetes.io/version: v1
|
||||
app.kubernetes.io/name: {{ .Release.Name }}
|
||||
version: v1
|
||||
app.kubernetes.io/instance: {{ .Release.Name }}
|
||||
spec:
|
||||
ports:
|
||||
- name: http-{{ .Release.Name }}
|
||||
protocol: TCP
|
||||
port: 80
|
||||
targetPort: {{ .Values.port }}
|
||||
selector:
|
||||
app: {{ .Release.Name }}
|
||||
type: ClusterIP
|
||||
sessionAffinity: None
|
||||
ipFamilies:
|
||||
- IPv4
|
||||
ipFamilyPolicy: SingleStack
|
|
@ -0,0 +1,6 @@
|
|||
nameSpace: {{ .NameSpace }}
|
||||
appName: {{ .AppName }}
|
||||
aliasName: {{ .AliasName }}
|
||||
image: reg.icechen.cn/{{ .NameSpace }}/service-{{ .AppName }}
|
||||
imageTag: latest
|
||||
port: {{ .Port }}
|
|
@ -0,0 +1,20 @@
|
|||
FROM golang:1.17 as builder
|
||||
ENV GO111MODULE on
|
||||
ENV GOPROXY https://goproxy.io,direct
|
||||
WORKDIR /go/cache
|
||||
ADD go.mod .
|
||||
ADD go.sum .
|
||||
RUN go mod download -x
|
||||
WORKDIR /go/src
|
||||
ADD . .
|
||||
RUN go mod tidy
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o service_{{ .Name }} ./{{ .Root }}
|
||||
|
||||
FROM reg.icechen.cn/alpine as {{ .Name }}
|
||||
WORKDIR /go/src
|
||||
COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=builder /go/src/service_{{ .Name }} ./
|
||||
ENV TZ=Asia/Shanghai
|
||||
RUN chmod +x ./service_{{ .Name }}
|
||||
EXPOSE 8080
|
||||
CMD ["./service_{{ .Name }}"]
|
|
@ -0,0 +1,26 @@
|
|||
package golang
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||
)
|
||||
|
||||
//go:embed "Dockerfile.tpl"
|
||||
var dockerfileTemplate string
|
||||
|
||||
//go:embed "main.tpl"
|
||||
var mainGoTemplate string
|
||||
|
||||
func GenDockerfile(service config.Service) error {
|
||||
color.Green("正在生成dockerfile...")
|
||||
err := util.TemplateToFile(service.Root+"/Dockerfile", dockerfileTemplate, service)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return util.TemplateToFile(service.Root+"/main.go", mainGoTemplate, nil)
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("我是个示例")
|
||||
}
|
|
@ -3,12 +3,14 @@ package util
|
|||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
func ReadBool() bool {
|
||||
retry:
|
||||
read := bufio.NewReader(os.Stdin)
|
||||
ret, _ := read.ReadByte()
|
||||
switch ret {
|
||||
|
@ -20,8 +22,10 @@ func ReadBool() bool {
|
|||
fallthrough
|
||||
case 'N':
|
||||
return false
|
||||
default:
|
||||
NoNewLinePrint(color.HiRedString, "请输入(y,Y,n,N)其中之一: ")
|
||||
goto retry
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ReadBoolWithMessage(message string) bool {
|
||||
|
|
68
util/file.go
68
util/file.go
|
@ -2,19 +2,18 @@ package util
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
)
|
||||
|
||||
func TemplateToFile(desFile string, templateContent string, data interface{}) error {
|
||||
file, err := os.OpenFile(desFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
tpl := template.New("template")
|
||||
tpl, err = tpl.Parse(templateContent)
|
||||
tpl, err := tpl.Parse(templateContent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -25,14 +24,66 @@ func TemplateToFile(desFile string, templateContent string, data interface{}) er
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = file.Write(b.Bytes())
|
||||
err = WriteFile(desFile, b.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var defaultPrintDiff = color.WhiteString
|
||||
|
||||
func IsChange(diff []diffmatchpatch.Diff) bool {
|
||||
for _, d := range diff {
|
||||
if d.Type != diffmatchpatch.DiffEqual {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddLine 增加行号 TODO: 颜色不完美
|
||||
func AddLine(s string) string {
|
||||
lines := strings.Split(s, "\n")
|
||||
for i, line := range lines {
|
||||
// lines[i] = fmt.Sprintf("%s %s", defaultPrintDiff("%4d.", i+1), line)
|
||||
lines[i] = fmt.Sprintf("%4d. %s", i+1, line)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func WriteFile(filePath string, data []byte) error {
|
||||
color.Green("写入[%s]文件...", filePath)
|
||||
toFileContent, err := os.ReadFile(filePath)
|
||||
if err == nil {
|
||||
dmp := diffmatchpatch.New()
|
||||
diffContent := dmp.DiffMain(string(toFileContent), string(data), true)
|
||||
|
||||
if IsChange(diffContent) {
|
||||
color.Red("文件[%s]已存在:", filePath)
|
||||
b := bytes.Buffer{}
|
||||
diffContent = dmp.DiffCleanupSemanticLossless(diffContent)
|
||||
for _, diff := range diffContent {
|
||||
switch diff.Type {
|
||||
case diffmatchpatch.DiffDelete:
|
||||
b.WriteString(color.RedString(diff.Text))
|
||||
case diffmatchpatch.DiffInsert:
|
||||
b.WriteString(color.GreenString(diff.Text))
|
||||
case diffmatchpatch.DiffEqual:
|
||||
b.WriteString(diff.Text)
|
||||
}
|
||||
}
|
||||
fmt.Println(AddLine(b.String()))
|
||||
if !ReadBoolWithMessage("是否确认此次修改(y修改/n跳过): ") {
|
||||
color.Red("跳过文件: %s\n\n", filePath)
|
||||
return nil
|
||||
}
|
||||
} else {
|
||||
color.Red("文件已存在且无变动,跳过文件: %s\n\n", filePath)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -40,6 +91,7 @@ func WriteFile(filePath string, data []byte) error {
|
|||
defer file.Close()
|
||||
|
||||
_, err = file.Write(data)
|
||||
color.Red("写入文件完成: %s\n\n", filePath)
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue