Skip to content

Commit

Permalink
feat: add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarisW committed Aug 27, 2024
1 parent 3489990 commit 432b6c4
Show file tree
Hide file tree
Showing 14 changed files with 641 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cmd/static/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import (
"github.com/cloudwego/cwgo/pkg/client"
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/pkg/curd/doc"
"github.com/cloudwego/cwgo/pkg/docker"
"github.com/cloudwego/cwgo/pkg/fallback"
"github.com/cloudwego/cwgo/pkg/job"
"github.com/cloudwego/cwgo/pkg/kube"
"github.com/cloudwego/cwgo/pkg/model"
"github.com/cloudwego/cwgo/pkg/server"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -118,6 +120,28 @@ func Init() *cli.App {
return api_list.Api(globalArgs.ApiArgument)
},
},
{
Name: DockerName,
Usage: DockerUsage,
Flags: dockerFlags(),
Action: func(c *cli.Context) error {
if err := globalArgs.DockerArgument.ParseCli(c); err != nil {
return err
}
return docker.Docker(globalArgs.DockerArgument)
},
},
{
Name: KubeName,
Usage: KubeUsage,
Flags: kubeFlags(),
Action: func(c *cli.Context) error {
if err := globalArgs.KubeArgument.ParseCli(c); err != nil {
return err
}
return kube.Kube(globalArgs.KubeArgument)
},
},
{
Name: FallbackName,
Usage: FallbackUsage,
Expand Down Expand Up @@ -209,6 +233,21 @@ Examples:
Examples:
cwgo api --project_path ./
`

DockerName = "docker"
DockerUsage = `generate docker file
Examples:
cwgo docker [args]
`

KubeName = "kube"
KubeUsage = `generate kube file
Examples:
cwgo kube [args]
`

JobName = "job"
JobUsage = `generate job code
Expand Down
67 changes: 67 additions & 0 deletions cmd/static/docker_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package static

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/cloudwego/cwgo/tpl"
"github.com/urfave/cli/v2"
)

func dockerFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: consts.GoVersion,
Usage: "Specify the go version.",
Aliases: []string{"go"},
Required: true,
},
&cli.BoolFlag{
Name: consts.EnableGoProxy,
Usage: "Enable go proxy.",
DefaultText: "false",
},
&cli.StringFlag{
Name: consts.GoProxy,
Usage: "Specify the go proxy.",
DefaultText: "https://goproxy.cn,direct",
},
&cli.StringFlag{
Name: consts.Timezone,
Usage: "Specify the timezone.",
DefaultText: "Asia/Shanghai",
},
&cli.StringFlag{
Name: consts.BaseImage,
Usage: "Specify the base image.",
DefaultText: "scratch",
},
&cli.IntFlag{
Name: consts.Port,
Usage: "Specify the port.",
DefaultText: "0",
},
&cli.StringFlag{
Name: consts.GoFileName,
Usage: "Specify the go file name.",
Aliases: []string{"f"},
Required: true,
},
&cli.StringFlag{
Name: consts.ExeFileName,
Usage: "Specify the exe file name.",
},
&cli.StringFlag{
Name: consts.Template,
Usage: "Specify the template path. Currently cwgo supports git templates, such as `--template https://github.com/***/cwgo_template.git`",
Aliases: []string{"t"},
DefaultText: tpl.DockerDir,
},
&cli.StringFlag{
Name: consts.Branch,
Usage: "Specify the git template's branch, default is main branch.",
},
&cli.StringSliceFlag{
Name: consts.RunArgs,
Usage: "Specify the run args.",
},
}
}
69 changes: 69 additions & 0 deletions cmd/static/kube_flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package static

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

func kubeFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: consts.Name,
Usage: "Specify the name of the service.",
},
&cli.StringFlag{
Name: consts.Namespace,
Usage: "Specify the namespace.",
},
&cli.StringFlag{
Name: consts.Image,
Usage: "Specify the image.",
},
&cli.StringFlag{
Name: consts.Secret,
Usage: "Specify the secret.",
},
&cli.IntFlag{
Name: consts.RequestCpu,
Usage: "Specify the request cpu.",
DefaultText: "500",
},
&cli.StringFlag{
Name: consts.RequestMem,
Usage: "Specify the request memory.",
DefaultText: "512",
},
&cli.StringFlag{
Name: consts.LimitCpu,
Usage: "Specify the limit cpu.",
DefaultText: "1000",
},
&cli.StringFlag{
Name: consts.LimitMem,
Usage: "Specify the limit memory.",
DefaultText: "1024",
},
&cli.StringFlag{
Name: consts.Port,
Usage: "Specify the port.",
},
&cli.StringFlag{
Name: consts.MinReplicas,
Usage: "Specify the min replicas.",
DefaultText: "3",
},
&cli.StringFlag{
Name: consts.MaxReplicas,
Usage: "Specify the max replicas.",
DefaultText: "10",
},
&cli.StringFlag{
Name: consts.ImagePullPolicy,
Usage: "Specify the image pull policy. (Always or IfNotPresent or Never)",
},
&cli.StringFlag{
Name: consts.ServiceAccount,
Usage: "Specify the service account.",
},
}
}
2 changes: 2 additions & 0 deletions config/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Argument struct {
*DocArgument
*JobArgument
*ApiArgument
*DockerArgument
*KubeArgument
*FallbackArgument
}

Expand Down
44 changes: 44 additions & 0 deletions config/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
"strings"
)

type DockerArgument struct {
GoVersion string // compile image go version
EnableGoProxy bool // enable go proxy
GoProxy string // go proxy url
Timezone string // image timezone
BaseImage string // service run image
Port int // docker image expose port
GoFileName string // go project main file name
ExeFileName string // build exe file name
Template string // specify local or remote template path
Branch string // remote template branch
RunArgs string // ext run args
}

func NewDockerArgument() *DockerArgument {
return &DockerArgument{}
}

func (c *DockerArgument) ParseCli(ctx *cli.Context) error {
c.GoVersion = ctx.String(consts.GoVersion)
c.EnableGoProxy = ctx.Bool(consts.EnableGoProxy)
c.GoProxy = ctx.String(consts.GoProxy)
c.Timezone = ctx.String(consts.Timezone)
c.BaseImage = ctx.String(consts.BaseImage)
c.Port = ctx.Int(consts.Port)
c.GoFileName = ctx.String(consts.GoFileName)
c.ExeFileName = ctx.String(consts.ExeFileName)
c.Template = ctx.String(consts.Template)
var builder strings.Builder
for _, arg := range ctx.StringSlice(consts.RunArgs) {
builder.WriteString(`, "` + arg + `"`)
}
c.RunArgs = builder.String()

return nil
}
41 changes: 41 additions & 0 deletions config/kube.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package config

import (
"github.com/cloudwego/cwgo/pkg/consts"
"github.com/urfave/cli/v2"
)

type KubeArgument struct {
Name string
Namespace string
Image string
Secret string
RequestCpu int
RequestMem string
LimitCpu string
LimitMem string
MinReplicas int
MaxReplicas int
ImagePullPolicy string
ServiceAccount string
}

func NewKubeArgument() *KubeArgument {
return &KubeArgument{}
}

func (c *KubeArgument) ParseCli(ctx *cli.Context) error {
c.Name = ctx.String(consts.Name)
c.Namespace = ctx.String(consts.Namespace)
c.Image = ctx.String(consts.Image)
c.Secret = ctx.String(consts.Secret)
c.RequestCpu = ctx.Int(consts.RequestCpu)
c.RequestMem = ctx.String(consts.RequestMem)
c.LimitCpu = ctx.String(consts.LimitCpu)
c.LimitMem = ctx.String(consts.LimitMem)
c.MinReplicas = ctx.Int(consts.MinReplicas)
c.MaxReplicas = ctx.Int(consts.MaxReplicas)
c.ImagePullPolicy = ctx.String(consts.ImagePullPolicy)
c.ServiceAccount = ctx.String(consts.ServiceAccount)
return nil
}
10 changes: 10 additions & 0 deletions pkg/common/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ func findRootPathRecursive(currentDirPath, relativeFilePath string) string {

return findRootPathRecursive(parentPath, relativeFilePath)
}

// CreateIfNotExist creates a file if it is not exists.
func CreateIfNotExist(file string) (*os.File, error) {
_, err := os.Stat(file)
if !os.IsNotExist(err) {
return nil, fmt.Errorf("%s already exist", file)
}

return os.Create(file)
}
30 changes: 30 additions & 0 deletions pkg/consts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import "runtime"
const (
Kitex = "kitex"
Hertz = "hertz"

Docker = "docker"
Kube = "kube"
)

const (
Expand Down Expand Up @@ -159,6 +162,33 @@ const (
TypeTag = "type_tag"
HexTag = "hex"
SQLDir = "sql_dir"

GoVersion = "go_version"
EnableGoProxy = "enable_go_proxy"
GoProxy = "go_proxy"
Timezone = "timezone"
BaseImage = "base_image"
Port = "port"
GoFileName = "go_file"
ExeFileName = "exe_file"
RunArgs = "run_args"

Namespace = "namespace"
Image = "image"
Secret = "secret"
RequestCpu = "requestCpu"
RequestMem = "requestMem"
LimitCpu = "limitCpu"
LimitMem = "limitMem"
O = "o"
Replicas = "replicas"
Revisions = "revisions"
NodePort = "nodePort"
TargetPort = "targetPort"
MinReplicas = "minReplicas"
MaxReplicas = "maxReplicas"
ImagePullPolicy = "imagePullPolicy"
ServiceAccount = "serviceAccount"
)

const (
Expand Down
Loading

0 comments on commit 432b6c4

Please sign in to comment.