forked from goodrain/rainbond
/
build.go
134 lines (114 loc) · 3.74 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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 build
import (
"context"
"fmt"
"regexp"
"strings"
"github.com/goodrain/rainbond/builder"
"github.com/goodrain/rainbond/builder/parser/code"
"github.com/goodrain/rainbond/event"
"k8s.io/client-go/kubernetes"
"github.com/docker/docker/client"
)
func init() {
buildcreaters = make(map[code.Lang]CreaterBuild)
buildcreaters[code.Dockerfile] = dockerfileBuilder
buildcreaters[code.Docker] = dockerfileBuilder
buildcreaters[code.NetCore] = netcoreBuilder
buildcreaters[code.JavaJar] = slugBuilder
buildcreaters[code.JavaMaven] = slugBuilder
buildcreaters[code.JaveWar] = slugBuilder
buildcreaters[code.PHP] = slugBuilder
buildcreaters[code.Python] = slugBuilder
buildcreaters[code.Nodejs] = slugBuilder
buildcreaters[code.Golang] = slugBuilder
}
var buildcreaters map[code.Lang]CreaterBuild
//Build app build pack
type Build interface {
Build(*Request) (*Response, error)
}
//CreaterBuild CreaterBuild
type CreaterBuild func() (Build, error)
//MediumType Build output medium type
type MediumType string
//ImageMediumType image type
var ImageMediumType MediumType = "image"
//SlugMediumType slug type
var SlugMediumType MediumType = "slug"
//Response build result
type Response struct {
MediumPath string
MediumType MediumType
}
//Request build input
type Request struct {
TenantID string
SourceDir string
CacheDir string
TGZDir string
RepositoryURL string
Branch string
ServiceAlias string
ServiceID string
DeployVersion string
Runtime string
ServerType string
Commit Commit
Lang code.Lang
BuildEnvs map[string]string
Logger event.Logger
DockerClient *client.Client
KubeClient kubernetes.Interface
ExtraHosts []string
HostAlias []HostAlias
Ctx context.Context
}
// HostAlias holds the mapping between IP and hostnames that will be injected as an entry in the
// pod's hosts file.
type HostAlias struct {
// IP address of the host file entry.
IP string `json:"ip,omitempty" protobuf:"bytes,1,opt,name=ip"`
// Hostnames for the above IP address.
Hostnames []string `json:"hostnames,omitempty" protobuf:"bytes,2,rep,name=hostnames"`
}
//Commit Commit
type Commit struct {
User string
Message string
Hash string
}
//GetBuild GetBuild
func GetBuild(lang code.Lang) (Build, error) {
if fun, ok := buildcreaters[lang]; ok {
return fun()
}
return slugBuilder()
}
//CreateImageName create image name
func CreateImageName(repoURL, serviceAlias, deployversion string) string {
reg := regexp.MustCompile(`.*(?:\:|\/)([\w\-\.]+)/([\w\-\.]+)\.git`)
rc := reg.FindSubmatch([]byte(repoURL))
var name string
if len(rc) == 3 {
name = fmt.Sprintf("%s_%s_%s", serviceAlias, string(rc[1]), string(rc[2]))
} else {
name = fmt.Sprintf("%s_%s", serviceAlias, "rainbondbuild")
}
buildImageName := strings.ToLower(fmt.Sprintf("%s/%s:%s", builder.REGISTRYDOMAIN, name, deployversion))
return buildImageName
}