Rainbond/builder/parser/source_code.go

635 lines
20 KiB
Go
Raw Normal View History

2018-03-14 14:12:26 +08:00
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
2018-01-17 22:06:58 +08:00
// RAINBOND, Application Management Platform
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// 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.
2018-03-14 14:33:31 +08:00
2018-01-17 22:06:58 +08:00
// 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 (
2021-07-28 21:31:58 +08:00
"context"
"encoding/base64"
2018-01-17 22:06:58 +08:00
"fmt"
2021-07-28 21:31:58 +08:00
"os"
2018-01-17 22:06:58 +08:00
"path"
2020-02-18 13:55:17 +08:00
"runtime"
2018-01-17 22:06:58 +08:00
"strconv"
2018-03-02 19:50:20 +08:00
"strings"
2018-01-17 22:06:58 +08:00
"github.com/goodrain/rainbond/builder"
"github.com/goodrain/rainbond/builder/parser/code"
2019-04-12 21:25:44 +08:00
multi "github.com/goodrain/rainbond/builder/parser/code/multisvc"
"github.com/goodrain/rainbond/builder/parser/types"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/util"
2021-07-28 21:31:58 +08:00
"github.com/melbahja/got"
"github.com/pquerna/ffjson/ffjson"
2020-11-25 16:39:38 +08:00
"github.com/sirupsen/logrus"
2018-01-17 22:06:58 +08:00
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/transport" //"github.com/docker/docker/client"
2018-01-17 22:06:58 +08:00
)
//SourceCodeParse docker run 命令解析或直接镜像名解析
type SourceCodeParse struct {
2021-07-28 21:31:58 +08:00
ports map[int]*types.Port
volumes map[string]*types.Volume
envs map[string]*types.Env
source string
memory int
image Image
args []string
branchs []string
errors []ParseError
logger event.Logger
Lang code.Lang
2019-04-11 23:46:21 +08:00
Runtime bool `json:"runtime"`
2018-04-24 18:18:34 +08:00
Dependencies bool `json:"dependencies"`
Procfile bool `json:"procfile"`
2019-04-11 23:46:21 +08:00
isMulti bool
services []*types.Service
2018-01-17 22:06:58 +08:00
}
//CreateSourceCodeParse create parser
func CreateSourceCodeParse(source string, logger event.Logger) Parser {
return &SourceCodeParse{
source: source,
ports: make(map[int]*types.Port),
volumes: make(map[string]*types.Volume),
envs: make(map[string]*types.Env),
2018-01-17 22:06:58 +08:00
logger: logger,
image: ParseImageName(builder.RUNNERIMAGENAME),
2018-01-17 22:06:58 +08:00
args: []string{"start", "web"},
}
}
//Parse 获取代码 解析代码 检验代码
func (d *SourceCodeParse) Parse() ParseErrorList {
if d.source == "" {
d.logger.Error("源码检查输入参数错误", map[string]string{"step": "parse"})
d.errappend(Errorf(FatalError, "source can not be empty"))
return d.errors
}
2021-07-28 21:31:58 +08:00
logrus.Debugf("component source check info: %s", d.source)
2018-01-17 22:06:58 +08:00
var csi sources.CodeSourceInfo
err := ffjson.Unmarshal([]byte(d.source), &csi)
if err != nil {
d.logger.Error("源码检查输入参数错误", map[string]string{"step": "parse"})
d.errappend(Errorf(FatalError, "source data can not be read"))
return d.errors
}
if csi.Branch == "" {
csi.Branch = "master"
}
2018-02-27 22:31:44 +08:00
if csi.RepositoryURL == "" {
d.logger.Error("Git项目仓库地址不能为空", map[string]string{"step": "parse"})
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库地址格式错误", SolveAdvice("modify_url", "请确认并修改仓库地址")))
return d.errors
}
2018-03-02 10:11:57 +08:00
//验证仓库地址
2018-07-23 17:39:01 +08:00
buildInfo, err := sources.CreateRepostoryBuildInfo(csi.RepositoryURL, csi.ServerType, csi.Branch, csi.TenantID, csi.ServiceID)
2018-03-02 10:11:57 +08:00
if err != nil {
d.logger.Error("Git项目仓库地址格式错误", map[string]string{"step": "parse"})
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库地址格式错误", SolveAdvice("modify_url", "请确认并修改仓库地址")))
return d.errors
}
2021-07-28 21:31:58 +08:00
// The source code is useless after the test is completed, and needs to be deleted.
defer func() {
if sources.CheckFileExist(buildInfo.GetCodeHome()) {
if err := sources.RemoveDir(buildInfo.GetCodeHome()); err != nil {
logrus.Warningf("remove source code: %v", err)
}
}
}()
2018-01-24 17:20:06 +08:00
gitFunc := func() ParseErrorList {
//get code
if !util.DirIsEmpty(buildInfo.GetCodeHome()) {
2018-03-02 10:11:57 +08:00
if err := sources.RemoveDir(buildInfo.GetCodeHome()); err != nil {
logrus.Errorf("remove code dir failure %s", err.Error())
2018-02-06 14:06:23 +08:00
return d.errors
}
}
2018-03-02 10:11:57 +08:00
csi.RepositoryURL = buildInfo.RepostoryURL
rs, err := sources.GitClone(csi, buildInfo.GetCodeHome(), d.logger, 5)
2018-01-22 15:20:49 +08:00
if err != nil {
2018-03-02 10:11:57 +08:00
if err == transport.ErrAuthenticationRequired || err == transport.ErrAuthorizationFailed {
if buildInfo.GetProtocol() == "ssh" {
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库需要安全验证", SolveAdvice("get_publickey", "请获取授权Key配置到你的仓库项目中")))
2018-01-22 15:20:49 +08:00
} else {
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库需要安全验证", SolveAdvice("modify_userpass", "请提供正确的账号密码")))
2018-01-22 15:20:49 +08:00
}
return d.errors
}
if err == plumbing.ErrReferenceNotFound {
solve := "请到代码仓库查看正确的分支情况"
d.errappend(ErrorAndSolve(FatalError, fmt.Sprintf("Git项目仓库指定分支 %s 不存在", csi.Branch), solve))
return d.errors
}
2018-03-02 10:11:57 +08:00
if err == transport.ErrRepositoryNotFound {
solve := SolveAdvice("modify_repo", "请确认仓库地址是否正确")
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库不存在", solve))
2018-03-02 10:11:57 +08:00
return d.errors
}
if err == transport.ErrEmptyRemoteRepository {
solve := SolveAdvice("open_repo", "请确认已提交代码")
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "Git项目仓库无有效文件", solve))
2018-03-02 10:11:57 +08:00
return d.errors
}
2018-03-02 19:50:20 +08:00
if strings.Contains(err.Error(), "ssh: unable to authenticate") {
solve := SolveAdvice("get_publickey", "请获取授权Key配置到你的仓库项目试试")
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "远程仓库SSH验证错误", solve))
2018-03-02 19:50:20 +08:00
return d.errors
}
if strings.Contains(err.Error(), "context deadline exceeded") {
solve := "请确认源码仓库能否正常访问"
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "获取代码超时", solve))
return d.errors
}
2018-02-27 22:31:44 +08:00
logrus.Errorf("git clone error,%s", err.Error())
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "获取代码失败"+err.Error(), "请确认仓库能否正常访问。"))
2018-01-22 15:20:49 +08:00
return d.errors
}
//获取分支
branch, err := rs.Branches()
if err == nil {
branch.ForEach(func(re *plumbing.Reference) error {
name := re.Name()
if name.IsBranch() {
d.branchs = append(d.branchs, name.Short())
}
return nil
})
} else {
d.branchs = append(d.branchs, csi.Branch)
}
2018-01-24 17:20:06 +08:00
return nil
}
2018-07-23 17:39:01 +08:00
svnFunc := func() ParseErrorList {
if sources.CheckFileExist(buildInfo.GetCodeHome()) {
if err := sources.RemoveDir(buildInfo.GetCodeHome()); err != nil {
return d.errors
}
}
csi.RepositoryURL = buildInfo.RepostoryURL
svnclient := sources.NewClient(csi, buildInfo.GetCodeHome(), d.logger)
2018-10-25 11:41:02 +08:00
rs, err := svnclient.UpdateOrCheckout(buildInfo.BuildPath)
2018-07-23 17:39:01 +08:00
if err != nil {
if strings.Contains(err.Error(), "svn:E170000") {
solve := "请到代码仓库查看正确的分支情况"
d.errappend(ErrorAndSolve(FatalError, fmt.Sprintf("Svn项目仓库指定分支 %s 不存在", csi.Branch), solve))
return d.errors
}
2018-10-25 11:41:02 +08:00
logrus.Errorf("svn checkout or update error,%s", err.Error())
2021-07-28 21:31:58 +08:00
d.errappend(ErrorAndSolve(FatalError, "获取代码失败"+err.Error(), "请确认仓库能否正常访问,或查看社区文档"))
2018-07-23 17:39:01 +08:00
return d.errors
}
//get branchs
d.branchs = rs.Branchs
return nil
}
2021-07-28 21:31:58 +08:00
ossFunc := func() ParseErrorList {
g := got.NewWithContext(context.Background())
util.CheckAndCreateDir(buildInfo.GetCodeHome())
fileName := path.Join(buildInfo.GetCodeHome(), path.Base(csi.RepositoryURL))
if err := g.Do(&got.Download{
URL: csi.RepositoryURL,
Dest: fileName,
Header: []got.GotHeader{
{Key: "Authorization", Value: "Basic " + basicAuth(csi.User, csi.Password)},
},
}); err != nil {
logrus.Errorf("download package file from oss failure %s", err.Error())
d.errappend(ErrorAndSolve(FatalError, "文件下载失败:"+err.Error(), "请确认该文件可以被正常下载"))
return d.errors
}
fi, err := os.Stat(fileName)
if err != nil {
d.errappend(ErrorAndSolve(FatalError, "文件下载失败:"+err.Error(), "请确认该文件可以被正常下载"))
return d.errors
}
logrus.Infof("download package file success, size %d MB", fi.Size()/1024/1024)
ext := path.Ext(csi.RepositoryURL)
switch ext {
case ".tar":
if err := util.UnTar(fileName, buildInfo.GetCodeHome(), false); err != nil {
logrus.Errorf("untar package file failure %s", err.Error())
d.errappend(ErrorAndSolve(FatalError, "文件解压失败", "请确认该文件是否为tar规范文件"))
}
case ".tgz", ".tar.gz":
if err := util.UnTar(fileName, buildInfo.GetCodeHome(), true); err != nil {
logrus.Errorf("untar package file failure %s", err.Error())
d.errappend(ErrorAndSolve(FatalError, "文件解压失败", "请确认该文件是否为tgz规范文件"))
}
case ".zip":
if err := util.Unzip(fileName, buildInfo.GetCodeHome()); err != nil {
logrus.Errorf("untar package file failure %s", err.Error())
d.errappend(ErrorAndSolve(FatalError, "文件解压失败", "请确认该文件是否为zip规范文件"))
}
}
logrus.Infof("unpack package file success")
return d.errors
}
logrus.Debugf("start get service %s code by %s server type", csi.ServiceID, csi.ServerType)
2018-01-24 17:20:06 +08:00
//获取代码仓库
switch csi.ServerType {
case "git":
2018-02-27 22:31:44 +08:00
if err := gitFunc(); err != nil && err.IsFatalError() {
return err
}
2018-01-17 22:06:58 +08:00
case "svn":
2018-07-23 17:39:01 +08:00
if err := svnFunc(); err != nil && err.IsFatalError() {
return err
}
2021-07-28 21:31:58 +08:00
case "oss":
if err := ossFunc(); err != nil && err.IsFatalError() {
return err
}
default:
2018-07-23 17:39:01 +08:00
//default git
logrus.Warningf("do not get void server type,default use git")
2018-02-27 22:31:44 +08:00
if err := gitFunc(); err != nil && err.IsFatalError() {
return err
}
2018-01-17 22:06:58 +08:00
}
2018-01-24 17:20:06 +08:00
//read rainbondfile
rbdfileConfig, err := code.ReadRainbondFile(buildInfo.GetCodeBuildAbsPath())
2018-01-17 22:06:58 +08:00
if err != nil {
if err != code.ErrRainbondFileNotFound {
d.errappend(ErrorAndSolve(NegligibleError, "rainbondfile定义格式有误", "可以参考文档说明配置此文件定义应用属性"))
2018-01-17 22:06:58 +08:00
}
}
//判断对象目录
2018-03-02 10:11:57 +08:00
var buildPath = buildInfo.GetCodeBuildAbsPath()
2018-01-17 22:06:58 +08:00
//解析代码类型
var lang code.Lang
if rbdfileConfig != nil && rbdfileConfig.Language != "" {
lang = code.Lang(rbdfileConfig.Language)
} else {
lang, err = code.GetLangType(buildPath)
if err != nil {
if err == code.ErrCodeDirNotExist {
d.errappend(ErrorAndSolve(FatalError, "源码目录不存在", "获取代码任务失败,请联系客服"))
} else if err == code.ErrCodeNotExist {
d.errappend(ErrorAndSolve(FatalError, "仓库中代码不存在", "请提交代码到仓库"))
} else {
2018-02-27 22:31:44 +08:00
d.errappend(ErrorAndSolve(FatalError, "代码无法识别语言类型", "请参考文档查看平台语言支持规范"))
2018-01-17 22:06:58 +08:00
}
return d.errors
}
}
2018-01-24 17:20:06 +08:00
d.Lang = lang
2018-01-17 22:06:58 +08:00
if lang == code.NO {
d.errappend(ErrorAndSolve(FatalError, "代码无法识别语言类型", "请参考文档查看平台语言支持规范"))
2018-01-17 22:06:58 +08:00
return d.errors
}
2018-04-24 18:02:00 +08:00
//check code Specification
2021-08-03 15:44:45 +08:00
spec := code.CheckCodeSpecification(buildPath, lang, csi.ServerType)
2018-01-17 22:06:58 +08:00
if spec.Advice != nil {
for k, v := range spec.Advice {
d.errappend(ErrorAndSolve(NegligibleError, k, v))
}
}
if spec.Noconform != nil {
for k, v := range spec.Noconform {
d.errappend(ErrorAndSolve(FatalError, k, v))
}
}
if !spec.Conform {
return d.errors
}
//如果是dockerfile 解析dockerfile文件
if lang == code.Dockerfile {
if ok := d.parseDockerfileInfo(path.Join(buildPath, "Dockerfile")); !ok {
return d.errors
}
}
runtimeInfo, err := code.CheckRuntime(buildPath, lang)
if err != nil && err == code.ErrRuntimeNotSupport {
d.errappend(ErrorAndSolve(FatalError, "代码选择的运行时版本不支持", "请参考文档查看平台各语言支持的Runtime版本"))
return d.errors
}
for k, v := range runtimeInfo {
d.envs["BUILD_"+k] = &types.Env{
Name: "BUILD_" + k,
Value: v,
}
}
d.memory = getRecommendedMemory(lang)
var ProcfileLine string
d.Procfile, ProcfileLine = code.CheckProcfile(buildPath, lang)
if ProcfileLine != "" {
2019-04-17 15:50:38 +08:00
d.envs["BUILD_PROCFILE"] = &types.Env{
Name: "BUILD_PROCFILE",
Value: ProcfileLine,
}
}
// multi services
m := multi.NewMultiServiceI(lang.String())
if m != nil {
logrus.Infof("Lang: %s; start listing multi modules", lang.String())
services, err := m.ListModules(buildInfo.GetCodeBuildAbsPath())
if err != nil {
d.logger.Error("解析多模块项目失败", map[string]string{"step": "parse"})
2019-09-17 16:31:21 +08:00
d.errappend(ErrorAndSolve(FatalError, fmt.Sprintf("error listing modules: %v", err), "check source code for multi-modules"))
return d.errors
}
2021-07-28 21:31:58 +08:00
if len(services) > 1 {
d.isMulti = true
d.services = services
}
if rbdfileConfig != nil && rbdfileConfig.Services != nil && len(rbdfileConfig.Services) > 0 {
mm := make(map[string]*types.Service)
for i := range services {
mm[services[i].Name] = services[i]
}
for _, svc := range rbdfileConfig.Services {
if item := mm[svc.Name]; item != nil {
for k, v := range svc.Envs {
if item.Envs == nil {
item.Envs = make(map[string]*types.Env, len(rbdfileConfig.Envs))
}
item.Envs[k] = &types.Env{Name: k, Value: v}
}
for i := range svc.Ports {
if item.Ports == nil {
item.Ports = make(map[int]*types.Port, len(rbdfileConfig.Ports))
}
item.Ports[i] = &types.Port{
ContainerPort: svc.Ports[i].Port, Protocol: svc.Ports[i].Protocol,
}
}
for k, v := range rbdfileConfig.Envs {
if item.Envs == nil {
item.Envs = make(map[string]*types.Env, len(rbdfileConfig.Envs))
}
if item.Envs[k] == nil {
item.Envs[k] = &types.Env{Name: k, Value: fmt.Sprintf("%v", v)}
}
}
for _, port := range rbdfileConfig.Ports {
if item.Ports == nil {
item.Ports = make(map[int]*types.Port, len(rbdfileConfig.Ports))
}
if item.Ports[port.Port] == nil {
item.Ports[port.Port] = &types.Port{
ContainerPort: port.Port,
Protocol: port.Protocol,
}
}
}
}
}
}
2019-05-22 10:55:52 +08:00
if rbdfileConfig != nil && d.isMulti {
rbdfileConfig.Envs = nil
rbdfileConfig.Ports = nil
}
}
2018-06-26 15:18:52 +08:00
if rbdfileConfig != nil {
//handle profile env
for k, v := range rbdfileConfig.Envs {
d.envs[k] = &types.Env{Name: k, Value: fmt.Sprintf("%v", v)}
2018-06-26 15:18:52 +08:00
}
//handle profile port
for _, port := range rbdfileConfig.Ports {
2019-10-12 14:02:10 +08:00
if port.Port == 0 {
continue
}
if port.Protocol == "" {
port.Protocol = GetPortProtocol(port.Port)
}
d.ports[port.Port] = &types.Port{ContainerPort: port.Port, Protocol: port.Protocol}
2018-06-26 15:18:52 +08:00
}
2018-07-11 14:27:02 +08:00
if rbdfileConfig.Cmd != "" {
d.args = strings.Split(rbdfileConfig.Cmd, " ")
}
}
2018-01-17 22:06:58 +08:00
return d.errors
}
2018-07-23 17:39:01 +08:00
//ReadRbdConfigAndLang read rainbondfile and lang
func ReadRbdConfigAndLang(buildInfo *sources.RepostoryBuildInfo) (*code.RainbondFileConfig, code.Lang, error) {
rbdfileConfig, err := code.ReadRainbondFile(buildInfo.GetCodeBuildAbsPath())
2018-07-23 17:39:01 +08:00
if err != nil {
return nil, code.NO, err
}
var lang code.Lang
if rbdfileConfig != nil && rbdfileConfig.Language != "" {
lang = code.Lang(rbdfileConfig.Language)
} else {
lang, err = code.GetLangType(buildInfo.GetCodeBuildAbsPath())
if err != nil {
return rbdfileConfig, code.NO, err
}
}
return rbdfileConfig, lang, nil
}
func getRecommendedMemory(lang code.Lang) int {
//java recommended 1024
if lang == code.JavaJar || lang == code.JavaMaven || lang == code.JaveWar || lang == code.Gradle {
return 1024
}
if lang == code.Python {
return 512
}
if lang == code.Nodejs {
return 512
}
if lang == code.PHP {
return 512
}
2019-06-14 15:19:53 +08:00
return 512
}
2018-01-17 22:06:58 +08:00
func (d *SourceCodeParse) errappend(pe ParseError) {
d.errors = append(d.errors, pe)
}
//GetBranchs 获取分支列表
func (d *SourceCodeParse) GetBranchs() []string {
return d.branchs
}
//GetPorts 获取端口列表
func (d *SourceCodeParse) GetPorts() (ports []types.Port) {
2018-01-17 22:06:58 +08:00
for _, cv := range d.ports {
ports = append(ports, *cv)
}
return ports
}
//GetVolumes 获取存储列表
func (d *SourceCodeParse) GetVolumes() (volumes []types.Volume) {
2018-01-17 22:06:58 +08:00
for _, cv := range d.volumes {
volumes = append(volumes, *cv)
}
return
}
//GetValid 获取源是否合法
func (d *SourceCodeParse) GetValid() bool {
return false
}
//GetEnvs 环境变量
func (d *SourceCodeParse) GetEnvs() (envs []types.Env) {
2018-01-17 22:06:58 +08:00
for _, cv := range d.envs {
envs = append(envs, *cv)
}
return
}
//GetImage 获取镜像
func (d *SourceCodeParse) GetImage() Image {
return d.image
}
//GetArgs 启动参数
func (d *SourceCodeParse) GetArgs() []string {
return d.args
}
//GetMemory 获取内存
func (d *SourceCodeParse) GetMemory() int {
return d.memory
}
2018-01-24 17:20:06 +08:00
//GetLang 获取识别语言
func (d *SourceCodeParse) GetLang() code.Lang {
return d.Lang
}
2018-01-17 22:06:58 +08:00
//GetServiceInfo 获取service info
func (d *SourceCodeParse) GetServiceInfo() []ServiceInfo {
serviceInfo := ServiceInfo{
Ports: d.GetPorts(),
Envs: d.GetEnvs(),
Volumes: d.GetVolumes(),
Image: d.GetImage(),
Args: d.GetArgs(),
Branchs: d.GetBranchs(),
Memory: d.memory,
Lang: d.GetLang(),
2020-02-19 23:02:25 +08:00
ServiceType: model.ServiceTypeStatelessMultiple.String(),
2020-02-24 10:31:08 +08:00
OS: runtime.GOOS,
2018-01-17 22:06:58 +08:00
}
var res []ServiceInfo
if d.isMulti && d.services != nil && len(d.services) > 0 {
for idx := range d.services {
svc := d.services[idx]
info := serviceInfo
info.ID = util.NewUUID()
info.Name = svc.Name
info.Cname = svc.Cname
info.Packaging = svc.Packaging
for i := range svc.Envs {
info.Envs = append(info.Envs, *svc.Envs[i])
}
for i := range svc.Ports {
info.Ports = append(info.Ports, *svc.Ports[i])
}
res = append(res, info)
}
} else {
serviceInfo.Envs = d.GetEnvs()
serviceInfo.Ports = d.GetPorts()
res = []ServiceInfo{serviceInfo}
}
return res
2018-01-17 22:06:58 +08:00
}
2020-09-15 11:46:12 +08:00
func removeQuotes(value string) string {
if len(value) > 0 && (value[0] == '"' || value[0] == '\'') {
value = value[1:]
}
if len(value) > 0 && (value[len(value)-1] == '"' || value[0] == '\'') {
2020-09-15 12:08:24 +08:00
value = value[:len(value)-1]
2020-09-15 11:46:12 +08:00
}
return value
}
2018-01-17 22:06:58 +08:00
func (d *SourceCodeParse) parseDockerfileInfo(dockerfile string) bool {
commands, err := sources.ParseFile(dockerfile)
if err != nil {
d.errappend(ErrorAndSolve(FatalError, err.Error(), "请确认Dockerfile格式是否符合规范"))
return false
}
for _, cm := range commands {
switch cm.Cmd {
case "arg":
length := len(cm.Value)
for i := 0; i < length; i++ {
if kv := strings.Split(cm.Value[i], "="); len(kv) > 1 {
key := "BUILD_ARG_" + kv[0]
2020-09-15 11:46:12 +08:00
d.envs[key] = &types.Env{Name: key, Value: removeQuotes(kv[1])}
} else {
if i+1 >= length {
logrus.Error("Parse ARG format error at ", cm.Value[i])
continue
}
key := "BUILD_ARG_" + cm.Value[i]
2020-09-15 11:46:12 +08:00
d.envs[key] = &types.Env{Name: key, Value: removeQuotes(cm.Value[i+1])}
i++
}
}
2018-01-17 22:06:58 +08:00
case "env":
length := len(cm.Value)
for i := 0; i < len(cm.Value); i++ {
if kv := strings.Split(cm.Value[i], "="); len(kv) > 1 {
d.envs[kv[0]] = &types.Env{Name: kv[0], Value: kv[1]}
} else {
if i+1 >= length {
logrus.Error("Parse ENV format error at ", cm.Value[1])
continue
}
d.envs[cm.Value[i]] = &types.Env{Name: cm.Value[i], Value: cm.Value[i+1]}
i++
}
2018-01-17 22:06:58 +08:00
}
case "expose":
for _, v := range cm.Value {
port, _ := strconv.Atoi(v)
2018-01-17 22:06:58 +08:00
if port != 0 {
d.ports[port] = &types.Port{ContainerPort: port, Protocol: GetPortProtocol(port)}
2018-01-17 22:06:58 +08:00
}
}
case "volume":
for _, v := range cm.Value {
d.volumes[v] = &types.Volume{VolumePath: v, VolumeType: model.ShareFileVolumeType.String()}
2018-01-17 22:06:58 +08:00
}
}
}
// dockerfile empty args
d.args = []string{}
2018-01-17 22:06:58 +08:00
return true
}
2021-07-28 21:31:58 +08:00
func basicAuth(username, password string) string {
auth := username + ":" + password
return base64.StdEncoding.EncodeToString([]byte(auth))
}