-
Notifications
You must be signed in to change notification settings - Fork 1
/
pkg-logic.go
190 lines (165 loc) · 5.79 KB
/
pkg-logic.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
// Copyright 2019 - now The https://github.com/fuhsicloud/fuhsi-next Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package logic
import (
"errors"
"fmt"
"github.com/fuhsicloud/fuhsi-next/fuhsi-agent/clients/oss-client"
"github.com/fuhsicloud/fuhsi-next/fuhsi-agent/libs"
"github.com/fuhsicloud/fuhsi-next/fuhsi-agent/libs/logger"
"github.com/fuhsicloud/fuhsi-next/fuhsi-agent/option"
"io/ioutil"
"os"
"strings"
)
func NewPkgLogic() *PkgLogic {
return new(PkgLogic)
}
type PkgLogic struct {
opt *option.Option
}
func (t *PkgLogic) Option(opt *option.Option) *PkgLogic {
t.opt = opt
return t
}
func (t *PkgLogic) Pull() error {
fileKey := t.FormatKey(t.opt.App, t.opt.Pull.VersionPkg)
pkgPath := t.FormatPkgPath(t.opt.App, t.opt.Pull.VersionPkg)
appPkgDir := t.FormatPkgDir(t.opt.App)
err := os.MkdirAll(appPkgDir, 0755)
if err != nil {
info := fmt.Sprintf("Failed to mkdir %s, err=%s", appPkgDir, err.Error())
logger.Errorf(info)
return errors.New(fmt.Sprintf("%s %s", info, err.Error()))
}
logger.Debugf("%s created.", appPkgDir)
data := make([]byte, 0)
switch t.opt.Cfg.StorageType {
case option.STORAGE_TYPE_LOCAL:
logger.Infof("package is stored in fuhsi-server host, do nothing, package transport by ssh")
case option.STORAGE_TYPE_OSS:
// pull file content
data, err = oss_client.C().Config(&oss_client.OssClientConfig{
Endpoint: t.opt.Cfg.Oss.Endpoint,
AccessKey: t.opt.Cfg.Oss.AccessKey,
AccessSecret: t.opt.Cfg.Oss.AccessSecret,
Bucket: t.opt.Cfg.Oss.Bucket,
}).Get(fileKey)
if err != nil {
logger.Errorf("Failed to pull version package from oss, err=%s", err.Error())
return err
}
// write to local file
err = ioutil.WriteFile(pkgPath, data, 0644)
if err != nil {
logger.Errorf("Failed to save package bytes to file %s, err=%s", pkgPath, err.Error())
return err
}
case option.STORAGE_TYPE_COS:
// @TODO store in tencent cos
case option.STORAGE_TYPE_AWS_S3:
// @TODO store in aws s3
default:
return errors.New(fmt.Sprintf("Invalid storage type: %s", t.opt.Cfg.StorageType))
}
logger.Debugf("%s saved.", pkgPath)
return nil
}
func (t *PkgLogic) Push() error {
localFile := fmt.Sprintf("%s/%s/%s", strings.TrimRight(t.opt.Cfg.JenkinsPkgWorkspace, "/"),
strings.Trim(t.opt.App, "/"),
strings.Trim(t.opt.Push.VersionPkg, "/"))
fileKey := t.FormatKey(t.opt.App, t.opt.Push.VersionPkg)
switch t.opt.Cfg.StorageType {
case option.STORAGE_TYPE_LOCAL:
err := NewApiLogic().Option(t.opt).UploadPackage(t.opt.Push.BuildId, t.opt.Push.VersionPkg, localFile)
if err != nil {
logger.Errorf("Failed to UploadPackage, err=%s", err.Error())
return err
}
return nil
case option.STORAGE_TYPE_OSS:
err := oss_client.C().Config(&oss_client.OssClientConfig{
Endpoint: t.opt.Cfg.Oss.Endpoint,
AccessKey: t.opt.Cfg.Oss.AccessKey,
AccessSecret: t.opt.Cfg.Oss.AccessSecret,
Bucket: t.opt.Cfg.Oss.Bucket,
}).Put(localFile, fileKey)
if err != nil {
logger.Errorf("Failed to put version package to oss, err=%s", err.Error())
return err
}
return nil
case option.STORAGE_TYPE_COS:
// @TODO store in tencent cos
return nil
case option.STORAGE_TYPE_AWS_S3:
// @TODO store in aws s3
return nil
default:
return errors.New(fmt.Sprintf("Invalid storage type: %s", t.opt.Cfg.StorageType))
}
}
func (t *PkgLogic) Tar(buildId int64, files []string, excludes []string) (string, error) {
// format jenkins packing package name and path
pkgName := t.FormatVersionPackageName()
appPkgWorkspace := fmt.Sprintf("%s/%s", strings.TrimRight(t.opt.Cfg.JenkinsPkgWorkspace, "/"), t.opt.App)
pkgPath := fmt.Sprintf("%s/%s", appPkgWorkspace, pkgName)
logger.Infof("version package path: %s", pkgPath)
// create jenkins package workspace
err := os.MkdirAll(appPkgWorkspace, 0644)
if err != nil {
info := fmt.Sprintf("Failed to mkdir -p %s, err=%s", appPkgWorkspace, err.Error())
logger.Errorf(info)
return "", errors.New(info)
}
// pack version package using linux tar command
output, cmd, err := libs.TarPackage(t.opt.JenkinsEnv.Workspace, pkgPath, files, excludes)
if err != nil {
info := fmt.Sprintf("Failed to pack version package, err=%s, cmd=%s, output=%s", err.Error(), cmd, string(output))
logger.Errorf(info)
return "", errors.New(info)
}
return pkgName, nil
}
// format tar version package name
func (t *PkgLogic) FormatVersionPackageName() string {
// substr short commit
shortCommit := "-"
if len(t.opt.JenkinsEnv.GitCommit) > 8 {
shortCommit = t.opt.JenkinsEnv.GitCommit[0:8]
} else if len(t.opt.JenkinsEnv.GitCommit) > 0 {
shortCommit = t.opt.JenkinsEnv.GitCommit
} else {
shortCommit = "unknown"
}
// appName.buildId.branch.commit.datetime.tar.gz
return fmt.Sprintf("%s.%d.%s.%s.%s.tar.gz",
t.opt.App,
t.opt.Tar.BuildId,
t.opt.JenkinsEnv.GitBranch,
shortCommit,
libs.Date("YmdHis"))
}
// format package file name
func (t *PkgLogic) FormatKey(app, pkgName string) string {
return fmt.Sprintf("packages/%s/%s", app, pkgName)
}
// format target host's package path
func (t *PkgLogic) FormatPkgPath(app, pkgName string) string {
return fmt.Sprintf("%s/%s/%s", strings.TrimRight(t.opt.Cfg.PkgRootPath, "/"), app, pkgName)
}
// format target host's package dir
func (t *PkgLogic) FormatPkgDir(app string) string {
return fmt.Sprintf("%s/%s", strings.TrimRight(t.opt.Cfg.PkgRootPath, "/"), app)
}