wujian_develop_tool/template/api/deploy/deoloy.go

126 lines
2.5 KiB
Go
Raw Normal View History

2022-01-06 20:27:18 +08:00
package deploy
import (
"embed"
_ "embed"
2022-01-06 23:34:37 +08:00
"fmt"
2022-01-06 20:27:18 +08:00
"os"
2022-01-10 20:26:14 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/config"
2022-01-06 20:27:18 +08:00
"github.com/fatih/color"
2022-01-06 23:04:25 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/util"
2022-01-06 20:27:18 +08:00
)
//go:embed "*"
var deployDir embed.FS
2022-01-10 20:26:14 +08:00
func GenDeploy(api config.Api) error {
2022-01-06 20:27:18 +08:00
color.Green("正在生成deploy...")
// deploy 文件夹
2022-01-06 23:34:37 +08:00
deployDirPath := api.Root + "/deploy"
2022-01-06 20:27:18 +08:00
err := os.MkdirAll(deployDirPath, os.ModePerm)
if err != nil {
return err
}
2022-01-06 23:34:37 +08:00
host := api.Host
if host == "" {
host = "api.seamlesser.com"
}
path := api.Path
if path == "" {
2022-01-11 00:03:14 +08:00
path = fmt.Sprintf("/%s/%s/?(.*)", api.Namespace, api.Name)
2022-01-06 23:34:37 +08:00
}
data := map[string]string{
2022-01-11 00:03:14 +08:00
"NameSpace": api.Namespace,
2022-01-06 23:34:37 +08:00
"AppName": api.Name,
"AliasName": api.AliasName,
"Port": api.Port,
"Host": host,
2022-01-10 20:26:14 +08:00
"Path": path,
}
2022-01-06 23:34:37 +08:00
2022-01-06 20:27:18 +08:00
// value.yaml
valuesTemplate, err := deployDir.ReadFile("values.tpl")
if err != nil {
return err
}
2022-01-06 23:34:37 +08:00
err = util.TemplateToFile(deployDirPath+"/values.yaml", string(valuesTemplate), data)
2022-01-06 20:27:18 +08:00
if err != nil {
return err
}
// Chart.yaml
chartTemplate, err := deployDir.ReadFile("Chart.tpl")
if err != nil {
return err
}
2022-01-06 23:34:37 +08:00
err = util.TemplateToFile(deployDirPath+"/Chart.yaml", string(chartTemplate), data)
2022-01-06 20:27:18 +08:00
if err != nil {
return err
}
// templates 文件夹
2022-01-06 23:34:37 +08:00
templatesDirPath := api.Root + "/deploy/templates"
2022-01-06 20:27:18 +08:00
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
}
2022-01-09 22:23:28 +08:00
// application.yaml 文件
err = copyTo("templates/application.yaml", templatesDirPath+"/application.yaml")
if err != nil {
return err
}
2022-01-06 20:27:18 +08:00
// deployment.yaml 文件
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
if err != nil {
return err
}
// ingress.yaml 文件
err = copyTo("templates/ingress.yaml", templatesDirPath+"/ingress.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
}
2022-01-10 20:26:14 +08:00
return util.WriteFile(toPath, fileContent)
2022-01-06 20:27:18 +08:00
}