[ADD] add depend_on for docker-compose

This commit is contained in:
huangrh 2019-03-26 15:13:05 +08:00
parent df131acae6
commit 9d47fae53e
3 changed files with 53 additions and 1 deletions

View File

@ -498,6 +498,7 @@ type Service struct {
Volumes []string `yaml:"volumes,omitempty"`
Command string `yaml:"command,omitempty"`
Environment map[string]string `yaml:"environment,omitempty"`
DependsOn []string `yaml:"depends_on,omitempty"`
Loggin struct {
Driver string `yaml:"driver,omitempty"`
Options struct {
@ -564,6 +565,7 @@ func (i *ExportApp) buildDockerComposeYaml() error {
envs[key] = value
}
var depServices []string
// 如果该app依赖了另了个app-b则把app-b中所有公开环境变量注入到该app
for _, item := range app.Get("dep_service_map_list").Array() {
serviceKey := item.Get("dep_service_key").String()
@ -571,6 +573,10 @@ func (i *ExportApp) buildDockerComposeYaml() error {
for k, v := range depEnvs {
envs[k] = v
}
if svc := i.getDependedService(serviceKey, &apps); svc != "" {
depServices = append(depServices, svc)
}
}
service := &Service{
@ -585,6 +591,9 @@ func (i *ExportApp) buildDockerComposeYaml() error {
service.Loggin.Driver = "json-file"
service.Loggin.Options.MaxSize = "5m"
service.Loggin.Options.MaxFile = "2"
if depServices != nil && len(depServices) > 0 {
service.DependsOn = depServices
}
y.Services[appName] = service
}
@ -609,7 +618,7 @@ func (i *ExportApp) buildDockerComposeYaml() error {
func (i *ExportApp) getPublicEnvByKey(serviceKey string, apps *[]gjson.Result) map[string]string {
envs := make(map[string]string, 5)
for _, app := range *apps {
appKey := app.Get("service_key").String()
appKey := app.Get("service_share_uuid").String()
if appKey == serviceKey {
for _, item := range app.Get("service_connect_info_map_list").Array() {
key := item.Get("attr_name").String()
@ -623,6 +632,15 @@ func (i *ExportApp) getPublicEnvByKey(serviceKey string, apps *[]gjson.Result) m
return envs
}
func (i *ExportApp) getDependedService(key string, apps *[]gjson.Result) string {
for _, app := range *apps {
if key == app.Get("service_share_uuid").String() {
return app.Get("service_cname").String()
}
}
return ""
}
func (i *ExportApp) buildStartScript() error {
if err := exec.Command("cp", "/src/export-app/run.sh", i.SourceDir).Run(); err != nil {
err = errors.New("Failed to generate start script to: " + i.SourceDir)

View File

@ -0,0 +1,33 @@
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package exector
import (
"github.com/goodrain/rainbond/event"
"testing"
)
func TestExportApp_parseApps(t *testing.T) {
e := &ExportApp{
SourceDir: "./",
Logger: event.GetTestLogger(),
}
err := e.buildDockerComposeYaml()
t.Error(err)
}

File diff suppressed because one or more lines are too long