fonchain-artistinfo/main.go
2023-02-02 14:10:24 +08:00

244 lines
6.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
)
type App struct {
DevImage string
Image string
Path string
CmdFile string
Doc string
Env string
}
var serviceMap = map[string]App{
"account": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/account:3.1",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/account:2.1",
Path: "D:\\project\\fonchain-account\\",
CmdFile: "D:\\project\\fonchain-account\\Dockerfile",
Doc: "D:\\project",
},
"seller": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/seller:3.1",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/seller:2.1",
Path: "D:\\project\\seller-main\\",
CmdFile: "D:\\project\\seller-main\\Dockerfile",
Doc: "D:\\project",
},
"task": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/task:3.1",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/task:2.1",
Path: "D:\\project\\fonchain-task_microservice\\",
CmdFile: "D:\\project\\fonchain-task_microservice\\Dockerfile",
Doc: "D:\\project",
},
"auth": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/auth:3.2",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/auth:2.2",
Path: "D:\\project\\fonchain-auth\\",
CmdFile: "D:\\project\\fonchain-auth\\Dockerfile",
},
"chain": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/chain:3.2",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/chain:2.2",
Path: "D:\\project\\fontree-chain_microservice\\",
CmdFile: "D:\\project\\fontree-chain_microservice\\Dockerfile",
Doc: "D:\\project",
},
"mall": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/malltest:3.0",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/malltest:2.0",
Path: "D:\\project\\fonchain-main_mall\\",
CmdFile: "D:\\project\\fonchain-main_mall\\Dockerfile",
Doc: "D:\\project",
},
"approval": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/approval:3.3",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/approval:2.3",
Path: "D:\\project\\fonchain-approval\\",
CmdFile: "D:\\project\\fonchain-approval\\Dockerfile",
Doc: "D:\\project",
},
"order": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/order:3.1",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/order:2.1",
Path: "D:\\project\\fonchain-order\\",
CmdFile: "D:\\project\\fonchain-order\\Dockerfile",
Doc: "D:\\project",
},
"client": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/client:3.0",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/client:2.0",
Path: "C:\\Users\\user054\\Documents\\fonchain-main\\",
CmdFile: "C:\\Users\\user054\\Documents\\fonchain-main\\Dockerfile",
},
"server": {
Image: "121.229.45.214:9006/main-server",
DevImage: "121.229.45.214:9006/main-server",
Path: "D:\\project\\fonchain-main\\",
CmdFile: "D:\\project\\fonchain-main\\Dockerfile",
Doc: "D:\\project",
},
"shop_mall": {
Image: "121.229.45.214:9006/shop-main-server",
DevImage: "121.229.45.214:9006/shop-main-server",
Path: "D:\\project\\shop-main\\",
CmdFile: "D:\\project\\shop-main\\Dockerfile",
Doc: "D:\\project",
},
"shop": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/shop:3.0",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/shop:2.0",
Path: "D:\\project\\shop-main\\",
CmdFile: "D:\\project\\shop-main\\Dockerfile",
Doc: "D:\\project",
},
"artist": {
Image: "registry.cn-hangzhou.aliyuncs.com/fontree/artistinfo:3.0",
DevImage: "registry.cn-hangzhou.aliyuncs.com/fontree/artistinfo:2.0",
Path: "D:\\code\\go\\src\\github.com\\fonchain\\fonchain-artistinfo\\",
CmdFile: "D:\\code\\go\\src\\github.com\\fonchain\\fonchain-artistinfo\\Dockerfile",
Doc: "D:\\code\\go\\src\\github.com\\fonchain",
},
}
func main() {
//查询命令所在目录,可用于判断操作系统中是否能用该命令
//docker build -f ./Dockerfile -t xxx .
//cmd中执行的命令ffmpeg -i psg.flv test.mp4
var name string
var image string
var env string
var envDir string
var appMap App
fmt.Println("1--2-")
flag.StringVar(&name, "n", "", "没有服务名称 auth approval account client")
flag.StringVar(&envDir, "e", "", "没有使用的配置文件信息 auth approval account client")
flag.Parse()
fmt.Printf("---------------执行项目%s,使用配置文件%s\n", name, envDir)
_, ok := serviceMap[name]
if ok == false {
fmt.Println("没有找对应项目项目")
return
}
appMap = serviceMap[name]
if envDir == "test" {
env = "conf\\" + envDir + "\\"
image = appMap.DevImage
} else if envDir == "prod" {
env = "conf\\" + envDir + "\\"
image = appMap.Image
} else {
fmt.Println("没有配置配置文件")
return
}
s, _ := GetAllFile(appMap.Path+env, []string{})
fmt.Println(s)
for _, t := range s {
fmt.Println(t, path.Base(t))
copyFile(t, appMap.Path+"conf\\"+path.Base(t))
}
fmt.Println("----------------------------0 配置文件拷贝成功")
//构建
var cmd *exec.Cmd
if appMap.Doc == "" {
cmd = exec.Command("docker", "build", "-f", appMap.CmdFile, "-t", image, ".")
} else {
cmd = exec.Command("docker", "build", "-f", appMap.CmdFile, appMap.Doc, "-t", image)
}
cmd.Dir = appMap.Path
/*
docker build -t registry.cn-hangzhou.aliyuncs.com/fontree/auth:2.2 .
docker push registry.cn-hangzhou.aliyuncs.com/fontree/account:2.1
docker push registry.cn-hangzhou.aliyuncs.com/fontree/account:2.1
*/
//阻塞至等待命令执行完成
fmt.Println(cmd.String())
fmt.Println("1---------------", appMap.Path)
out, err1 := cmd.CombinedOutput()
fmt.Printf("%s\n", out)
if err1 != nil {
fmt.Println(err1)
fmt.Println("结束")
return
}
fmt.Println("------------------1 Success------------------")
//成功则去执行docker push操作
cmd2 := exec.Command("docker", "push", image)
out2, err2 := cmd2.CombinedOutput()
fmt.Printf("%s\n", out2)
if err2 != nil {
fmt.Println(err1)
fmt.Println("结束")
return
}
fmt.Println("------------------2 Success------------------")
//开始重新构建xxx
}
func copyFile(old string, new string) bool {
op, err1 := os.Open(old)
of, err2 := os.Create(new)
if err1 != nil || err2 != nil {
fmt.Println("文件拷贝失败")
return false
}
defer op.Close()
defer of.Close()
_, err := io.Copy(of, op)
if err != nil {
fmt.Println(err)
return false
}
return true
}
func GetAllFile(pathname string, s []string) ([]string, error) {
rd, err := ioutil.ReadDir(pathname)
if err != nil {
fmt.Println("read dir fail:", err)
return s, err
}
for _, fi := range rd {
if !fi.IsDir() {
fullName := pathname + "/" + fi.Name()
s = append(s, fullName)
} else { //是文件夹
//s = append(s, )
}
}
return s, nil
}