forked from goodrain/rainbond
/
build_from_image_run.go
141 lines (125 loc) · 5.07 KB
/
build_from_image_run.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
// 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 exector
import (
"fmt"
"time"
"github.com/goodrain/rainbond/builder"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/event"
"github.com/tidwall/gjson"
//"github.com/docker/docker/api/types"
//"github.com/docker/docker/client"
"github.com/docker/docker/client"
"github.com/goodrain/rainbond/builder/sources"
"github.com/goodrain/rainbond/db"
)
//ImageBuildItem ImageBuildItem
type ImageBuildItem struct {
Namespace string `json:"namespace"`
TenantName string `json:"tenant_name"`
ServiceAlias string `json:"service_alias"`
Image string `json:"image"`
DestImage string `json:"dest_image"`
Logger event.Logger `json:"logger"`
EventID string `json:"event_id"`
DockerClient *client.Client
TenantID string
ServiceID string
DeployVersion string
HubUser string
HubPassword string
Action string
}
//NewImageBuildItem 创建实体
func NewImageBuildItem(in []byte) *ImageBuildItem {
eventID := gjson.GetBytes(in, "event_id").String()
logger := event.GetManager().GetLogger(eventID)
return &ImageBuildItem{
Namespace: gjson.GetBytes(in, "namespace").String(),
TenantName: gjson.GetBytes(in, "tenant_name").String(),
ServiceAlias: gjson.GetBytes(in, "service_alias").String(),
ServiceID: gjson.GetBytes(in, "service_id").String(),
Image: gjson.GetBytes(in, "image").String(),
DeployVersion: gjson.GetBytes(in, "deploy_version").String(),
Action: gjson.GetBytes(in, "action").String(),
HubUser: gjson.GetBytes(in, "user").String(),
HubPassword: gjson.GetBytes(in, "password").String(),
Logger: logger,
EventID: eventID,
}
}
//Run Run
func (i *ImageBuildItem) Run(timeout time.Duration) error {
_, err := sources.ImagePull(i.DockerClient, i.Image, i.HubUser, i.HubPassword, i.Logger, 10)
if err != nil {
logrus.Errorf("pull image %s error: %s", i.Image, err.Error())
i.Logger.Error(fmt.Sprintf("获取指定镜像: %s失败", i.Image), map[string]string{"step": "builder-exector", "status": "failure"})
return err
}
localImageURL := i.ImageNameHandler(i.Image)
if err := sources.ImageTag(i.DockerClient, i.Image, localImageURL, i.Logger, 1); err != nil {
logrus.Errorf("change image tag error: %s", err.Error())
i.Logger.Error(fmt.Sprintf("修改镜像tag: %s -> %s 失败", i.Image, localImageURL), map[string]string{"step": "builder-exector", "status": "failure"})
return err
}
err = sources.ImagePush(i.DockerClient, localImageURL, builder.REGISTRYUSER, builder.REGISTRYPASS, i.Logger, 30)
if err != nil {
logrus.Errorf("push image into registry error: %s", err.Error())
i.Logger.Error("推送镜像至镜像仓库失败", map[string]string{"step": "builder-exector", "status": "failure"})
return err
}
if err := i.StorageVersionInfo(localImageURL); err != nil {
logrus.Errorf("storage version info error, ignor it: %s", err.Error())
i.Logger.Error("更新应用版本信息失败", map[string]string{"step": "builder-exector", "status": "failure"})
return err
}
return nil
}
//ImageNameHandler 根据平台配置处理镜像名称
func (i *ImageBuildItem) ImageNameHandler(source string) string {
imageModel := sources.ImageNameHandle(source)
localImageURL := fmt.Sprintf("%s/%s:%s", builder.REGISTRYDOMAIN, imageModel.Name, i.DeployVersion)
return localImageURL
}
//StorageVersionInfo 存储version信息
func (i *ImageBuildItem) StorageVersionInfo(imageURL string) error {
version, err := db.GetManager().VersionInfoDao().GetVersionByDeployVersion(i.DeployVersion, i.ServiceID)
if err != nil {
return err
}
version.DeliveredType = "image"
version.DeliveredPath = imageURL
version.ImageName = imageURL
version.RepoURL = i.Image
version.FinalStatus = "success"
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
return err
}
return nil
}
//UpdateVersionInfo 更新任务执行结果
func (i *ImageBuildItem) UpdateVersionInfo(status string) error {
version, err := db.GetManager().VersionInfoDao().GetVersionByEventID(i.EventID)
if err != nil {
return err
}
version.FinalStatus = status
version.RepoURL = i.Image
if err := db.GetManager().VersionInfoDao().UpdateModel(version); err != nil {
return err
}
return nil
}