-
Notifications
You must be signed in to change notification settings - Fork 765
/
parser.go
222 lines (194 loc) · 4.97 KB
/
parser.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// 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 parser
import (
"fmt"
"strconv"
"strings"
"github.com/goodrain/rainbond/builder/parser/code"
)
//Port 端口
type Port struct {
ContainerPort int `json:"container_port"`
Protocol string `json:"protocol"`
}
//Volume 存储地址
type Volume struct {
VolumePath string `json:"volume_path"`
VolumeType string `json:"volume_type"`
}
//Env env desc
type Env struct {
Name string `json:"name"`
Value string `json:"value"`
}
//ParseError 错误信息
type ParseError struct {
ErrorType ParseErrorType `json:"error_type"`
ErrorInfo string `json:"error_info"`
SolveAdvice string `json:"solve_advice"`
}
//ParseErrorList 错误列表
type ParseErrorList []ParseError
//ParseErrorType 错误类型
type ParseErrorType string
//FatalError 致命错误
var FatalError ParseErrorType = "FatalError"
//NegligibleError 可忽略错误
var NegligibleError ParseErrorType = "NegligibleError"
//Errorf error create
func Errorf(errtype ParseErrorType, format string, a ...interface{}) ParseError {
parseError := ParseError{
ErrorType: errtype,
ErrorInfo: fmt.Sprintf(format, a...),
}
return parseError
}
//ErrorAndSolve error create
func ErrorAndSolve(errtype ParseErrorType, errorInfo, SolveAdvice string) ParseError {
parseError := ParseError{
ErrorType: errtype,
ErrorInfo: errorInfo,
SolveAdvice: SolveAdvice,
}
return parseError
}
//SolveAdvice 构建a标签建议
func SolveAdvice(actionType, message string) string {
return fmt.Sprintf("<a action_type=\"%s\">%s</a>", actionType, message)
}
func (p ParseError) Error() string {
return fmt.Sprintf("Type:%s Message:%s", p.ErrorType, p.ErrorInfo)
}
func (ps ParseErrorList) Error() string {
var re string
for _, p := range ps {
re += fmt.Sprintf("Type:%s Message:%s\n", p.ErrorType, p.ErrorInfo)
}
return re
}
//IsFatalError 是否具有致命错误
func (ps ParseErrorList) IsFatalError() bool {
for _, p := range ps {
if p.ErrorType == FatalError {
return true
}
}
return false
}
//Image 镜像
type Image struct {
Name string `json:"name"`
Tag string `json:"tag"`
}
func (i Image) String() string {
return fmt.Sprintf("%s:%s", i.Name, i.Tag)
}
//Parser 解析器
type Parser interface {
Parse() ParseErrorList
GetServiceInfo() []ServiceInfo
GetImage() Image
}
//Lang 语言类型
type Lang string
//ServiceInfo 智能获取的应用信息
type ServiceInfo struct {
Ports []Port `json:"ports"`
Envs []Env `json:"envs"`
Volumes []Volume `json:"volumes"`
Image Image `json:"image"`
Args []string `json:"args"`
DependServices []string `json:"depends,omitempty"`
ServiceDeployType string `json:"deploy_type,omitempty"`
Branchs []string `json:"branchs,omitempty"`
Memory int `json:"memory"`
Lang code.Lang `json:"language"`
Runtime bool `json:"runtime"`
Dependencies bool `json:"dependencies"`
Procfile bool `json:"procfile"`
ImageAlias string `json:"image_alias"`
}
//GetServiceInfo GetServiceInfo
type GetServiceInfo struct {
UUID string `json:"uuid"`
Source string `json:"source"`
}
//GetPortProtocol 获取端口协议
func GetPortProtocol(port int) string {
if port == 80 {
return "http"
}
if port == 8080 {
return "http"
}
if port == 22 {
return "tcp"
}
if port == 3306 {
return "mysql"
}
if port == 443 {
return "https"
}
if port == 3128 {
return "http"
}
if port == 1080 {
return "udp"
}
if port == 6379 {
return "tcp"
}
if port > 1 && port < 5000 {
return "tcp"
}
return "http"
}
//readmemory
//10m 10
//10g 10*1024
//10k 128
//10b 128
func readmemory(s string) int {
if strings.HasSuffix(s, "m") {
s, err := strconv.Atoi(s[0 : len(s)-1])
if err != nil {
return 128
}
return s
}
if strings.HasSuffix(s, "g") {
s, err := strconv.Atoi(s[0 : len(s)-1])
if err != nil {
return 128
}
return s * 1024
}
return 128
}
func parseImageName(s string) Image {
index := strings.Index(s, ":")
if index > -1 {
return Image{
Name: s[0:index],
Tag: s[index+1:],
}
}
return Image{
Name: s,
Tag: "latest",
}
}