diff --git a/config_handler/service_config.go b/config_handler/service_config.go index f8fd554..b7db56c 100644 --- a/config_handler/service_config.go +++ b/config_handler/service_config.go @@ -16,7 +16,7 @@ func (sl ServiceList) toServiceEnv() map[string]string { func (sl ServiceList) toDestinationConfig(nameSpace string) (string, error) { retConfig := "" for _, service := range sl { - config, err := service.toDestinationConfig(nameSpace) + config, err := service.toDestinationConfig(nameSpace, sl.toServiceEnv()) if err != nil { return "", err } @@ -25,15 +25,16 @@ func (sl ServiceList) toDestinationConfig(nameSpace string) (string, error) { return retConfig, nil } -func (s Service) toDestinationConfig(nameSpace string) (string, error) { +func (s Service) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { var handler Handler switch s.Type { case go_handler.TypeGolang: handler = go_handler.GoServiceHandler{ - NameSpace: nameSpace, - Name: s.Name, - Root: s.Root, - Port: s.Port, + NameSpace: nameSpace, + Name: s.Name, + Root: s.Root, + Port: s.Port, + ServiceEnv: serviceEnv, } } diff --git a/go_handler/go_service_handler.go b/go_handler/go_service_handler.go index cb7e3c6..0bbe066 100644 --- a/go_handler/go_service_handler.go +++ b/go_handler/go_service_handler.go @@ -1,12 +1,67 @@ package go_handler +import ( + "bytes" + "text/template" +) + +const servicePipeline = ` +kind: pipeline +type: docker +name: 部署{{ .Name }} + +steps: + - name: build + image: plugins/docker + volumes: + - name: docker + path: /var/run/docker.sock + settings: + username: + from_secret: reg_username + password: + from_secret: reg_password + repo: reg.icechen.cn/{{ .NameSpace }}/{{ .Name }} + registry: reg.icechen.cn + tags: ${DRONE_COMMIT:0:8} + dockerfile: ./app/api/{{ .Name }}/Dockerfile + + - name: run + image: docker + volumes: + - name: docker + path: /var/run/docker.sock + commands: + - docker rm -f {{ .NameSpace }}-{{ .Name }} + - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net"{{ range $key, $value := .ServiceEnv }} --env {{ $key }}={{ $value }} {{ end }}reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + +volumes: + - name: docker + host: + path: /var/run/docker.sock +` + type GoServiceHandler struct { - NameSpace string - Name string - Root string - Port string + NameSpace string + Name string + Root string + Port string + ServiceEnv map[string]string } -func (GoServiceHandler) ToDestinationConfig() (string, error) { - return "", nil +func (gsh GoServiceHandler) ToDestinationConfig() (string, error) { + var err error + t := template.New(gsh.Name) + t, err = t.Parse(apiPipeline) + if err != nil { + return "", err + } + + b := bytes.Buffer{} + err = t.Execute(&b, gsh) + if err != nil { + return "", err + } + + return b.String(), nil }