From c12435eb7f5e87851cb4c3319946874b0c7c12a3 Mon Sep 17 00:00:00 2001 From: icechen Date: Wed, 29 Dec 2021 23:23:35 +0800 Subject: [PATCH] update --- config_handler/api_config.go | 31 ++++++++++++++ config_handler/config_handler.go | 6 ++- config_handler/destination_config.go | 61 ---------------------------- config_handler/service_config.go | 41 +++++++++++++++++++ go_handler/go_api_handler.go | 11 ++--- 5 files changed, 83 insertions(+), 67 deletions(-) create mode 100644 config_handler/api_config.go delete mode 100644 config_handler/destination_config.go create mode 100644 config_handler/service_config.go diff --git a/config_handler/api_config.go b/config_handler/api_config.go new file mode 100644 index 0000000..9ba9344 --- /dev/null +++ b/config_handler/api_config.go @@ -0,0 +1,31 @@ +package config_handler + +import "git.icechen.cn/pkg/drone_plugin/go_handler" + +func (al ApiList) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { + retConfig := "" + for _, api := range al { + config, err := api.toDestinationConfig(nameSpace, serviceEnv) + if err != nil { + return "", err + } + retConfig += config + "\n\n---\n\n" + } + return retConfig, nil +} + +func (a Api) toDestinationConfig(nameSpace string, serviceEnv map[string]string) (string, error) { + var handler Handler + switch a.Type { + case go_handler.TypeGolang: + handler = go_handler.GoApiHandler{ + NameSpace: nameSpace, + Name: a.Name, + Root: a.Root, + Port: a.Port, + ServiceEnv: serviceEnv, + } + } + + return handler.ToDestinationConfig() +} diff --git a/config_handler/config_handler.go b/config_handler/config_handler.go index 5f09ad5..19736c1 100644 --- a/config_handler/config_handler.go +++ b/config_handler/config_handler.go @@ -19,6 +19,10 @@ func New() config.Plugin { type plugin struct{} +type Handler interface { + ToDestinationConfig() (string, error) +} + func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, error) { resp, err := json.Marshal(req) if err != nil { @@ -50,7 +54,7 @@ func (p *plugin) Find(ctx context.Context, req *config.Request) (*drone.Config, // 4. 根据文件树以及原始配置文件的信息,组装需要构建的服务的ci信息 // 4.1 api modifiedApiList := getModifiedApi(cfg.Api, modifiedFileList) - destinationApi, err := modifiedApiList.toDestinationConfig(cfg.Name) + destinationApi, err := modifiedApiList.toDestinationConfig(cfg.Name, cfg.Service.toServiceEnv()) if err != nil { logrus.Error(err) return nil, err diff --git a/config_handler/destination_config.go b/config_handler/destination_config.go deleted file mode 100644 index 1d0e29c..0000000 --- a/config_handler/destination_config.go +++ /dev/null @@ -1,61 +0,0 @@ -package config_handler - -import "git.icechen.cn/pkg/drone_plugin/go_handler" - -type Handler interface { - ToDestinationConfig() (string, error) -} - -func (al ApiList) toDestinationConfig(nameSpace string) (string, error) { - retConfig := "" - for _, api := range al { - config, err := api.toDestinationConfig(nameSpace) - if err != nil { - return "", err - } - retConfig += config + "\n\n---\n\n" - } - return retConfig, nil -} - -func (a Api) toDestinationConfig(nameSpace string) (string, error) { - var handler Handler - switch a.Type { - case go_handler.TypeGolang: - handler = go_handler.GoApiHandler{ - NameSpace: nameSpace, - Name: a.Name, - Root: a.Root, - Port: a.Port, - } - } - - return handler.ToDestinationConfig() -} - -func (sl ServiceList) toDestinationConfig(nameSpace string) (string, error) { - retConfig := "" - for _, service := range sl { - config, err := service.toDestinationConfig(nameSpace) - if err != nil { - return "", err - } - retConfig += config + "\n\n---\n\n" - } - return retConfig, nil -} - -func (s Service) toDestinationConfig(nameSpace 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, - } - } - - return handler.ToDestinationConfig() -} diff --git a/config_handler/service_config.go b/config_handler/service_config.go new file mode 100644 index 0000000..f8fd554 --- /dev/null +++ b/config_handler/service_config.go @@ -0,0 +1,41 @@ +package config_handler + +import ( + "git.icechen.cn/pkg/drone_plugin/go_handler" + "strings" +) + +func (sl ServiceList) toServiceEnv() map[string]string { + retMap := make(map[string]string) + for _, service := range sl { + retMap[strings.ToUpper("SERVICE_"+service.Name)] = service.Name + ":" + service.Port + } + return retMap +} + +func (sl ServiceList) toDestinationConfig(nameSpace string) (string, error) { + retConfig := "" + for _, service := range sl { + config, err := service.toDestinationConfig(nameSpace) + if err != nil { + return "", err + } + retConfig += config + "\n\n---\n\n" + } + return retConfig, nil +} + +func (s Service) toDestinationConfig(nameSpace 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, + } + } + + return handler.ToDestinationConfig() +} diff --git a/go_handler/go_api_handler.go b/go_handler/go_api_handler.go index c02a321..46f012a 100644 --- a/go_handler/go_api_handler.go +++ b/go_handler/go_api_handler.go @@ -35,7 +35,7 @@ steps: path: /var/run/docker.sock commands: - docker rm -f {{ .NameSpace }}-{{ .Name }} - - docker run -d --name="{{ .NameSpace }}-{{ .Name }}" --network="nginx-net" reg.icechen.cn/{{ .NameSpace }}/{{ .Name }}:${DRONE_COMMIT:0:8} + - 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 @@ -44,10 +44,11 @@ volumes: ` type GoApiHandler struct { - NameSpace string - Name string - Root string - Port string + NameSpace string + Name string + Root string + Port string + ServiceEnv map[string]string } func (gah GoApiHandler) ToDestinationConfig() (string, error) {